Merge pull request #8341 from FabHof/file_errors

roc check: Add report for file open errors
This commit is contained in:
Anton-4 2025-10-28 17:11:24 +01:00 committed by GitHub
commit 70432d9dab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -751,7 +751,33 @@ pub const BuildEnv = struct {
// Read source
const file_abs = try std.fs.path.resolve(self.gpa, &.{file_path});
defer self.gpa.free(file_abs);
const src = try std.fs.cwd().readFileAlloc(self.gpa, file_abs, std.math.maxInt(usize));
const src = std.fs.cwd().readFileAlloc(self.gpa, file_abs, std.math.maxInt(usize)) catch |err| {
const report = blk: switch (err) {
error.FileNotFound => {
var report = Report.init(self.gpa, "FILE NOT FOUND", .fatal);
try report.document.addText("I could not find the file ");
try report.document.addAnnotated(file_abs, .path);
try report.document.addLineBreak();
try report.document.addText("Make sure the file exists and you do not have any typos in its name or path.");
break :blk report;
},
else => {
var report = Report.init(self.gpa, "COULD NOT READ FILE", .fatal);
try report.document.addText("I could not read the file ");
try report.document.addAnnotated(file_abs, .path);
try report.document.addLineBreak();
try report.document.addText("I did get the following error: ");
try report.addErrorMessage(@errorName(err));
try report.document.addText("Make sure the file can be read.");
break :blk report;
},
};
self.sink.emitReport("main", file_abs, report);
try self.sink.buildOrder(&[_][]const u8{"main"}, &[_][]const u8{file_abs}, &[_]u32{0});
self.sink.tryEmit();
return err;
};
defer self.gpa.free(src);
var env = try ModuleEnv.init(self.gpa, src);