Commit 150515f44d

Andrew Kelley <andrew@ziglang.org>
2021-06-23 01:11:02
stage2: slightly improve error reporting for missing imports
There is now a distinction between `@import` with a .zig extension and without. Without a .zig extension it assumes it is a package name, and returns error.PackageNotFound if not mapped into the package table.
1 parent 6fb4580
Changed files (3)
src/codegen/x86_64.zig
@@ -4,7 +4,6 @@ const mem = std.mem;
 const assert = std.debug.assert;
 const ArrayList = std.ArrayList;
 const Allocator = std.mem.Allocator;
-const Type = @import("../Type.zig");
 const DW = std.dwarf;
 
 // zig fmt: off
src/Compilation.zig
@@ -2319,6 +2319,9 @@ fn workerAstGenFile(
                 break :blk mod.importFile(file, import_path) catch continue;
             };
             if (import_result.is_new) {
+                log.debug("AstGen of {s} has import '{s}'; queuing AstGen of {s}", .{
+                    file.sub_file_path, import_path, import_result.file.sub_file_path,
+                });
                 wg.start();
                 comp.thread_pool.spawn(workerAstGenFile, .{
                     comp, import_result.file, prog_node, wg,
@@ -2540,13 +2543,23 @@ fn reportRetryableAstGenError(
 
     file.status = .retryable_failure;
 
-    const err_msg = try Module.ErrorMsg.create(gpa, .{
+    const src_loc: Module.SrcLoc = .{
         .file_scope = file,
         .parent_decl_node = 0,
         .lazy = .entire_file,
-    }, "unable to load {s}: {s}", .{
-        file.sub_file_path, @errorName(err),
-    });
+    };
+
+    const err_msg = if (file.pkg.root_src_directory.path) |dir_path|
+        try Module.ErrorMsg.create(
+            gpa,
+            src_loc,
+            "unable to load {s}" ++ std.fs.path.sep_str ++ "{s}: {s}",
+            .{ dir_path, file.sub_file_path, @errorName(err) },
+        )
+    else
+        try Module.ErrorMsg.create(gpa, src_loc, "unable to load {s}: {s}", .{
+            file.sub_file_path, @errorName(err),
+        });
     errdefer err_msg.destroy(gpa);
 
     {
src/Module.zig
@@ -3185,6 +3185,9 @@ pub fn importFile(
     if (cur_file.pkg.table.get(import_string)) |pkg| {
         return mod.importPkg(pkg);
     }
+    if (!mem.endsWith(u8, import_string, ".zig")) {
+        return error.PackageNotFound;
+    }
     const gpa = mod.gpa;
 
     // The resolved path is used as the key in the import table, to detect if