authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-23 14:25:42+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-23 14:25:42+02:00
log220020599cc11764eb9ed32025dd506f2affedda
tree4467fe3ff1f919eec89bafac7435fe58cf9a876a
parentd395127552ab6f04b7b044b2a309e0d1e8977775
parent5f9186d0ce78ce1eb7db9634849b38d2e90d071e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #13670 from mlugg/fix/astgen-ambiguous-package

AstGen: detect and error on files included in multiple packages

6 files changed, 172 insertions(+), 56 deletions(-)

src/Compilation.zig+71-14
......@@ -639,14 +639,6 @@ pub const AllErrors = struct {
639639 note_i += 1;
640640 }
641641 }
642 if (module_err_msg.src_loc.lazy == .entire_file) {
643 try errors.append(.{
644 .plain = .{
645 .msg = try allocator.dupe(u8, module_err_msg.msg),
646 },
647 });
648 return;
649 }
650642
651643 const reference_trace = try allocator.alloc(Message, module_err_msg.reference_trace.len);
652644 for (reference_trace) |*reference, i| {
......@@ -683,7 +675,7 @@ pub const AllErrors = struct {
683675 .column = @intCast(u32, err_loc.column),
684676 .notes = notes_buf[0..note_i],
685677 .reference_trace = reference_trace,
686 .source_line = try allocator.dupe(u8, err_loc.source_line),
678 .source_line = if (module_err_msg.src_loc.lazy == .entire_file) null else try allocator.dupe(u8, err_loc.source_line),
687679 },
688680 });
689681 }
......@@ -1610,6 +1602,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16101602
16111603 const builtin_pkg = try Package.createWithDir(
16121604 gpa,
1605 "builtin",
16131606 zig_cache_artifact_directory,
16141607 null,
16151608 "builtin.zig",
......@@ -1618,6 +1611,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16181611
16191612 const std_pkg = try Package.createWithDir(
16201613 gpa,
1614 "std",
16211615 options.zig_lib_directory,
16221616 "std",
16231617 "std.zig",
......@@ -1625,11 +1619,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16251619 errdefer std_pkg.destroy(gpa);
16261620
16271621 const root_pkg = if (options.is_test) root_pkg: {
1622 // TODO: we currently have two packages named 'root' here, which is weird. This
1623 // should be changed as part of the resolution of #12201
16281624 const test_pkg = if (options.test_runner_path) |test_runner|
1629 try Package.create(gpa, null, test_runner)
1625 try Package.create(gpa, "root", null, test_runner)
16301626 else
16311627 try Package.createWithDir(
16321628 gpa,
1629 "root",
16331630 options.zig_lib_directory,
16341631 null,
16351632 "test_runner.zig",
......@@ -1640,9 +1637,9 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16401637 } else main_pkg;
16411638 errdefer if (options.is_test) root_pkg.destroy(gpa);
16421639
1643 try main_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);
1644 try main_pkg.add(gpa, "root", root_pkg);
1645 try main_pkg.addAndAdopt(gpa, "std", std_pkg);
1640 try main_pkg.addAndAdopt(gpa, builtin_pkg);
1641 try main_pkg.add(gpa, root_pkg);
1642 try main_pkg.addAndAdopt(gpa, std_pkg);
16461643
16471644 const main_pkg_is_std = m: {
16481645 const std_path = try std.fs.path.resolve(arena, &[_][]const u8{
......@@ -3075,6 +3072,57 @@ pub fn performAllTheWork(
30753072 }
30763073 }
30773074
3075 if (comp.bin_file.options.module) |mod| {
3076 for (mod.import_table.values()) |file| {
3077 if (!file.multi_pkg) continue;
3078 const err = err_blk: {
3079 const notes = try mod.gpa.alloc(Module.ErrorMsg, file.references.items.len);
3080 errdefer mod.gpa.free(notes);
3081
3082 for (notes) |*note, i| {
3083 errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);
3084 note.* = switch (file.references.items[i]) {
3085 .import => |loc| try Module.ErrorMsg.init(
3086 mod.gpa,
3087 loc,
3088 "imported from package {s}",
3089 .{loc.file_scope.pkg.name},
3090 ),
3091 .root => |pkg| try Module.ErrorMsg.init(
3092 mod.gpa,
3093 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
3094 "root of package {s}",
3095 .{pkg.name},
3096 ),
3097 };
3098 }
3099 errdefer for (notes) |*n| n.deinit(mod.gpa);
3100
3101 const err = try Module.ErrorMsg.create(
3102 mod.gpa,
3103 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
3104 "file exists in multiple packages",
3105 .{},
3106 );
3107 err.notes = notes;
3108 break :err_blk err;
3109 };
3110 errdefer err.destroy(mod.gpa);
3111 try mod.failed_files.putNoClobber(mod.gpa, file, err);
3112 }
3113
3114 // Now that we've reported the errors, we need to deal with
3115 // dependencies. Any file referenced by a multi_pkg file should also be
3116 // marked multi_pkg and have its status set to astgen_failure, as it's
3117 // ambiguous which package they should be analyzed as a part of. We need
3118 // to add this flag after reporting the errors however, as otherwise
3119 // we'd get an error for every single downstream file, which wouldn't be
3120 // very useful.
3121 for (mod.import_table.values()) |file| {
3122 if (file.multi_pkg) file.recursiveMarkMultiPkg(mod);
3123 }
3124 }
3125
30783126 {
30793127 const outdated_and_deleted_decls_frame = tracy.namedFrame("outdated_and_deleted_decls");
30803128 defer outdated_and_deleted_decls_frame.end();
......@@ -3499,7 +3547,15 @@ fn workerAstGenFile(
34993547 comp.mutex.lock();
35003548 defer comp.mutex.unlock();
35013549
3502 break :blk mod.importFile(file, import_path) catch continue;
3550 const res = mod.importFile(file, import_path) catch continue;
3551 if (!res.is_pkg) {
3552 res.file.addReference(mod.*, .{ .import = .{
3553 .file_scope = file,
3554 .parent_decl_node = 0,
3555 .lazy = .{ .token_abs = item.data.token },
3556 } }) catch continue;
3557 }
3558 break :blk res;
35033559 };
35043560 if (import_result.is_new) {
35053561 log.debug("AstGen of {s} has import '{s}'; queuing AstGen of {s}", .{
......@@ -5327,6 +5383,7 @@ fn buildOutputFromZig(
53275383 var main_pkg: Package = .{
53285384 .root_src_directory = comp.zig_lib_directory,
53295385 .root_src_path = src_basename,
5386 .name = "root",
53305387 };
53315388 defer main_pkg.deinitTable(comp.gpa);
53325389 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
src/Module.zig+57-5
......@@ -1943,6 +1943,10 @@ pub const File = struct {
19431943 zir: Zir,
19441944 /// Package that this file is a part of, managed externally.
19451945 pkg: *Package,
1946 /// Whether this file is a part of multiple packages. This is an error condition which will be reported after AstGen.
1947 multi_pkg: bool = false,
1948 /// List of references to this file, used for multi-package errors.
1949 references: std.ArrayListUnmanaged(Reference) = .{},
19461950
19471951 /// Used by change detection algorithm, after astgen, contains the
19481952 /// set of decls that existed in the previous ZIR but not in the new one.
......@@ -1958,6 +1962,14 @@ pub const File = struct {
19581962 /// successful, this field is unloaded.
19591963 prev_zir: ?*Zir = null,
19601964
1965 /// A single reference to a file.
1966 const Reference = union(enum) {
1967 /// The file is imported directly (i.e. not as a package) with @import.
1968 import: SrcLoc,
1969 /// The file is the root of a package.
1970 root: *Package,
1971 };
1972
19611973 pub fn unload(file: *File, gpa: Allocator) void {
19621974 file.unloadTree(gpa);
19631975 file.unloadSource(gpa);
......@@ -1990,6 +2002,7 @@ pub const File = struct {
19902002 log.debug("deinit File {s}", .{file.sub_file_path});
19912003 file.deleted_decls.deinit(gpa);
19922004 file.outdated_decls.deinit(gpa);
2005 file.references.deinit(gpa);
19932006 if (file.root_decl.unwrap()) |root_decl| {
19942007 mod.destroyDecl(root_decl);
19952008 }
......@@ -2110,6 +2123,44 @@ pub const File = struct {
21102123 else => true,
21112124 };
21122125 }
2126
2127 /// Add a reference to this file during AstGen.
2128 pub fn addReference(file: *File, mod: Module, ref: Reference) !void {
2129 try file.references.append(mod.gpa, ref);
2130
2131 const pkg = switch (ref) {
2132 .import => |loc| loc.file_scope.pkg,
2133 .root => |pkg| pkg,
2134 };
2135 if (pkg != file.pkg) file.multi_pkg = true;
2136 }
2137
2138 /// Mark this file and every file referenced by it as multi_pkg and report an
2139 /// astgen_failure error for them. AstGen must have completed in its entirety.
2140 pub fn recursiveMarkMultiPkg(file: *File, mod: *Module) void {
2141 file.multi_pkg = true;
2142 file.status = .astgen_failure;
2143
2144 std.debug.assert(file.zir_loaded);
2145 const imports_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.imports)];
2146 if (imports_index == 0) return;
2147 const extra = file.zir.extraData(Zir.Inst.Imports, imports_index);
2148
2149 var import_i: u32 = 0;
2150 var extra_index = extra.end;
2151 while (import_i < extra.data.imports_len) : (import_i += 1) {
2152 const item = file.zir.extraData(Zir.Inst.Imports.Item, extra_index);
2153 extra_index = item.end;
2154
2155 const import_path = file.zir.nullTerminatedString(item.data.name);
2156 if (mem.eql(u8, import_path, "builtin")) continue;
2157
2158 const res = mod.importFile(file, import_path) catch continue;
2159 if (!res.is_pkg and !res.file.multi_pkg) {
2160 res.file.recursiveMarkMultiPkg(mod);
2161 }
2162 }
2163 }
21132164};
21142165
21152166/// Represents the contents of a file loaded with `@embedFile`.
......@@ -3220,16 +3271,11 @@ pub fn deinit(mod: *Module) void {
32203271 // The callsite of `Compilation.create` owns the `main_pkg`, however
32213272 // Module owns the builtin and std packages that it adds.
32223273 if (mod.main_pkg.table.fetchRemove("builtin")) |kv| {
3223 gpa.free(kv.key);
32243274 kv.value.destroy(gpa);
32253275 }
32263276 if (mod.main_pkg.table.fetchRemove("std")) |kv| {
3227 gpa.free(kv.key);
32283277 kv.value.destroy(gpa);
32293278 }
3230 if (mod.main_pkg.table.fetchRemove("root")) |kv| {
3231 gpa.free(kv.key);
3232 }
32333279 if (mod.root_pkg != mod.main_pkg) {
32343280 mod.root_pkg.destroy(gpa);
32353281 }
......@@ -4695,6 +4741,7 @@ pub fn declareDeclDependency(mod: *Module, depender_index: Decl.Index, dependee_
46954741pub const ImportFileResult = struct {
46964742 file: *File,
46974743 is_new: bool,
4744 is_pkg: bool,
46984745};
46994746
47004747pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
......@@ -4714,6 +4761,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
47144761 if (gop.found_existing) return ImportFileResult{
47154762 .file = gop.value_ptr.*,
47164763 .is_new = false,
4764 .is_pkg = true,
47174765 };
47184766
47194767 const sub_file_path = try gpa.dupe(u8, pkg.root_src_path);
......@@ -4737,9 +4785,11 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
47374785 .pkg = pkg,
47384786 .root_decl = .none,
47394787 };
4788 try new_file.addReference(mod.*, .{ .root = pkg });
47404789 return ImportFileResult{
47414790 .file = new_file,
47424791 .is_new = true,
4792 .is_pkg = true,
47434793 };
47444794}
47454795
......@@ -4780,6 +4830,7 @@ pub fn importFile(
47804830 if (gop.found_existing) return ImportFileResult{
47814831 .file = gop.value_ptr.*,
47824832 .is_new = false,
4833 .is_pkg = false,
47834834 };
47844835
47854836 const new_file = try gpa.create(File);
......@@ -4825,6 +4876,7 @@ pub fn importFile(
48254876 return ImportFileResult{
48264877 .file = new_file,
48274878 .is_new = true,
4879 .is_pkg = false,
48284880 };
48294881}
48304882
src/Package.zig+26-13
......@@ -24,10 +24,13 @@ table: Table = .{},
2424parent: ?*Package = null,
2525/// Whether to free `root_src_directory` on `destroy`.
2626root_src_directory_owned: bool = false,
27/// This information can be recovered from 'table', but it's more convenient to store on the package.
28name: []const u8,
2729
2830/// Allocate a Package. No references to the slices passed are kept.
2931pub fn create(
3032 gpa: Allocator,
33 name: []const u8,
3134 /// Null indicates the current working directory
3235 root_src_dir_path: ?[]const u8,
3336 /// Relative to root_src_dir_path
......@@ -42,6 +45,9 @@ pub fn create(
4245 const owned_src_path = try gpa.dupe(u8, root_src_path);
4346 errdefer gpa.free(owned_src_path);
4447
48 const owned_name = try gpa.dupe(u8, name);
49 errdefer gpa.free(owned_name);
50
4551 ptr.* = .{
4652 .root_src_directory = .{
4753 .path = owned_dir_path,
......@@ -49,6 +55,7 @@ pub fn create(
4955 },
5056 .root_src_path = owned_src_path,
5157 .root_src_directory_owned = true,
58 .name = owned_name,
5259 };
5360
5461 return ptr;
......@@ -56,6 +63,7 @@ pub fn create(
5663
5764pub fn createWithDir(
5865 gpa: Allocator,
66 name: []const u8,
5967 directory: Compilation.Directory,
6068 /// Relative to `directory`. If null, means `directory` is the root src dir
6169 /// and is owned externally.
......@@ -69,6 +77,9 @@ pub fn createWithDir(
6977 const owned_src_path = try gpa.dupe(u8, root_src_path);
7078 errdefer gpa.free(owned_src_path);
7179
80 const owned_name = try gpa.dupe(u8, name);
81 errdefer gpa.free(owned_name);
82
7283 if (root_src_dir_path) |p| {
7384 const owned_dir_path = try directory.join(gpa, &[1][]const u8{p});
7485 errdefer gpa.free(owned_dir_path);
......@@ -80,12 +91,14 @@ pub fn createWithDir(
8091 },
8192 .root_src_directory_owned = true,
8293 .root_src_path = owned_src_path,
94 .name = owned_name,
8395 };
8496 } else {
8597 ptr.* = .{
8698 .root_src_directory = directory,
8799 .root_src_directory_owned = false,
88100 .root_src_path = owned_src_path,
101 .name = owned_name,
89102 };
90103 }
91104 return ptr;
......@@ -95,6 +108,7 @@ pub fn createWithDir(
95108/// inside its table; the caller is responsible for calling destroy() on them.
96109pub fn destroy(pkg: *Package, gpa: Allocator) void {
97110 gpa.free(pkg.root_src_path);
111 gpa.free(pkg.name);
98112
99113 if (pkg.root_src_directory_owned) {
100114 // If root_src_directory.path is null then the handle is the cwd()
......@@ -111,24 +125,18 @@ pub fn destroy(pkg: *Package, gpa: Allocator) void {
111125
112126/// Only frees memory associated with the table.
113127pub fn deinitTable(pkg: *Package, gpa: Allocator) void {
114 var it = pkg.table.keyIterator();
115 while (it.next()) |key| {
116 gpa.free(key.*);
117 }
118
119128 pkg.table.deinit(gpa);
120129}
121130
122pub fn add(pkg: *Package, gpa: Allocator, name: []const u8, package: *Package) !void {
131pub fn add(pkg: *Package, gpa: Allocator, package: *Package) !void {
123132 try pkg.table.ensureUnusedCapacity(gpa, 1);
124 const name_dupe = try gpa.dupe(u8, name);
125 pkg.table.putAssumeCapacityNoClobber(name_dupe, package);
133 pkg.table.putAssumeCapacityNoClobber(package.name, package);
126134}
127135
128pub fn addAndAdopt(parent: *Package, gpa: Allocator, name: []const u8, child: *Package) !void {
136pub fn addAndAdopt(parent: *Package, gpa: Allocator, child: *Package) !void {
129137 assert(child.parent == null); // make up your mind, who is the parent??
130138 child.parent = parent;
131 return parent.add(gpa, name, child);
139 return parent.add(gpa, child);
132140}
133141
134142pub const build_zig_basename = "build.zig";
......@@ -237,7 +245,7 @@ pub fn fetchAndAddDependencies(
237245 sub_prefix,
238246 );
239247
240 try addAndAdopt(pkg, gpa, fqn, sub_pkg);
248 try addAndAdopt(pkg, gpa, sub_pkg);
241249
242250 try dependencies_source.writer().print(" pub const {s} = @import(\"{}\");\n", .{
243251 std.zig.fmtId(fqn), std.zig.fmtEscapes(fqn),
......@@ -249,6 +257,7 @@ pub fn fetchAndAddDependencies(
249257
250258pub fn createFilePkg(
251259 gpa: Allocator,
260 name: []const u8,
252261 cache_directory: Compilation.Directory,
253262 basename: []const u8,
254263 contents: []const u8,
......@@ -269,7 +278,7 @@ pub fn createFilePkg(
269278 const o_dir_sub_path = "o" ++ fs.path.sep_str ++ hex_digest;
270279 try renameTmpIntoCache(cache_directory.handle, tmp_dir_sub_path, o_dir_sub_path);
271280
272 return createWithDir(gpa, cache_directory, o_dir_sub_path, basename);
281 return createWithDir(gpa, name, cache_directory, o_dir_sub_path, basename);
273282}
274283
275284fn fetchAndUnpack(
......@@ -312,6 +321,9 @@ fn fetchAndUnpack(
312321 const owned_src_path = try gpa.dupe(u8, build_zig_basename);
313322 errdefer gpa.free(owned_src_path);
314323
324 const owned_name = try gpa.dupe(u8, fqn);
325 errdefer gpa.free(owned_name);
326
315327 const build_root = try global_cache_directory.join(gpa, &.{pkg_dir_sub_path});
316328 errdefer gpa.free(build_root);
317329
......@@ -326,6 +338,7 @@ fn fetchAndUnpack(
326338 },
327339 .root_src_directory_owned = true,
328340 .root_src_path = owned_src_path,
341 .name = owned_name,
329342 };
330343
331344 return ptr;
......@@ -414,7 +427,7 @@ fn fetchAndUnpack(
414427 std.zig.fmtId(fqn), std.zig.fmtEscapes(build_root),
415428 });
416429
417 return createWithDir(gpa, global_cache_directory, pkg_dir_sub_path, build_zig_basename);
430 return createWithDir(gpa, fqn, global_cache_directory, pkg_dir_sub_path, build_zig_basename);
418431}
419432
420433fn reportError(
src/Sema.zig+2-14
......@@ -5211,6 +5211,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
52115211 }
52125212 const c_import_pkg = Package.create(
52135213 sema.gpa,
5214 "c_import", // TODO: should we make this unique?
52145215 null,
52155216 c_import_res.out_zig_path,
52165217 ) catch |err| switch (err) {
......@@ -11663,20 +11664,7 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1166311664 },
1166411665 error.PackageNotFound => {
1166511666 const cur_pkg = block.getFileScope().pkg;
11666 const parent = if (cur_pkg == sema.mod.main_pkg or cur_pkg == sema.mod.root_pkg)
11667 "root"
11668 else if (cur_pkg.parent) |parent| blk: {
11669 var it = parent.table.iterator();
11670 while (it.next()) |pkg| {
11671 if (pkg.value_ptr.* == cur_pkg) {
11672 break :blk pkg.key_ptr.*;
11673 }
11674 }
11675 unreachable;
11676 } else {
11677 return sema.fail(block, operand_src, "no package named '{s}' available", .{operand});
11678 };
11679 return sema.fail(block, operand_src, "no package named '{s}' available within package '{s}'", .{ operand, parent });
11667 return sema.fail(block, operand_src, "no package named '{s}' available within package '{s}'", .{ operand, cur_pkg.name });
1168011668 },
1168111669 else => {
1168211670 // TODO: these errors are file system errors; make sure an update() will
src/main.zig+15-10
......@@ -857,6 +857,7 @@ fn buildOutputType(
857857 var pkg_tree_root: Package = .{
858858 .root_src_directory = .{ .path = null, .handle = fs.cwd() },
859859 .root_src_path = &[0]u8{},
860 .name = &[0]u8{},
860861 };
861862 defer freePkgTree(gpa, &pkg_tree_root, false);
862863 var cur_pkg: *Package = &pkg_tree_root;
......@@ -947,6 +948,7 @@ fn buildOutputType(
947948
948949 const new_cur_pkg = Package.create(
949950 gpa,
951 pkg_name,
950952 fs.path.dirname(pkg_path),
951953 fs.path.basename(pkg_path),
952954 ) catch |err| {
......@@ -958,7 +960,7 @@ fn buildOutputType(
958960 } else if (cur_pkg.table.get(pkg_name)) |prev| {
959961 fatal("unable to add package '{s}' -> '{s}': already exists as '{s}", .{ pkg_name, pkg_path, prev.root_src_path });
960962 }
961 try cur_pkg.addAndAdopt(gpa, pkg_name, new_cur_pkg);
963 try cur_pkg.addAndAdopt(gpa, new_cur_pkg);
962964 cur_pkg = new_cur_pkg;
963965 } else if (mem.eql(u8, arg, "--pkg-end")) {
964966 cur_pkg = cur_pkg.parent orelse
......@@ -2841,14 +2843,14 @@ fn buildOutputType(
28412843 if (main_pkg_path) |unresolved_main_pkg_path| {
28422844 const p = try introspect.resolvePath(arena, unresolved_main_pkg_path);
28432845 if (p.len == 0) {
2844 break :blk try Package.create(gpa, null, src_path);
2846 break :blk try Package.create(gpa, "root", null, src_path);
28452847 } else {
28462848 const rel_src_path = try fs.path.relative(arena, p, src_path);
2847 break :blk try Package.create(gpa, p, rel_src_path);
2849 break :blk try Package.create(gpa, "root", p, rel_src_path);
28482850 }
28492851 } else {
28502852 const root_src_dir_path = fs.path.dirname(src_path);
2851 break :blk Package.create(gpa, root_src_dir_path, fs.path.basename(src_path)) catch |err| {
2853 break :blk Package.create(gpa, "root", root_src_dir_path, fs.path.basename(src_path)) catch |err| {
28522854 if (root_src_dir_path) |p| {
28532855 fatal("unable to open '{s}': {s}", .{ p, @errorName(err) });
28542856 } else {
......@@ -4093,6 +4095,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
40934095 var main_pkg: Package = .{
40944096 .root_src_directory = zig_lib_directory,
40954097 .root_src_path = "build_runner.zig",
4098 .name = "root",
40964099 };
40974100
40984101 if (!build_options.omit_pkg_fetching_code) {
......@@ -4133,20 +4136,22 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
41334136
41344137 const deps_pkg = try Package.createFilePkg(
41354138 gpa,
4139 "@dependencies",
41364140 local_cache_directory,
41374141 "dependencies.zig",
41384142 dependencies_source.items,
41394143 );
41404144
41414145 mem.swap(Package.Table, &main_pkg.table, &deps_pkg.table);
4142 try main_pkg.addAndAdopt(gpa, "@dependencies", deps_pkg);
4146 try main_pkg.addAndAdopt(gpa, deps_pkg);
41434147 }
41444148
41454149 var build_pkg: Package = .{
41464150 .root_src_directory = build_directory,
41474151 .root_src_path = build_zig_basename,
4152 .name = "@build",
41484153 };
4149 try main_pkg.addAndAdopt(gpa, "@build", &build_pkg);
4154 try main_pkg.addAndAdopt(gpa, &build_pkg);
41504155
41514156 const comp = Compilation.create(gpa, .{
41524157 .zig_lib_directory = zig_lib_directory,
......@@ -4381,7 +4386,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
43814386 .root_decl = .none,
43824387 };
43834388
4384 file.pkg = try Package.create(gpa, null, file.sub_file_path);
4389 file.pkg = try Package.create(gpa, "root", null, file.sub_file_path);
43854390 defer file.pkg.destroy(gpa);
43864391
43874392 file.zir = try AstGen.generate(gpa, file.tree);
......@@ -4592,7 +4597,7 @@ fn fmtPathFile(
45924597 .root_decl = .none,
45934598 };
45944599
4595 file.pkg = try Package.create(fmt.gpa, null, file.sub_file_path);
4600 file.pkg = try Package.create(fmt.gpa, "root", null, file.sub_file_path);
45964601 defer file.pkg.destroy(fmt.gpa);
45974602
45984603 if (stat.size > max_src_size)
......@@ -5304,7 +5309,7 @@ pub fn cmdAstCheck(
53045309 file.stat.size = source.len;
53055310 }
53065311
5307 file.pkg = try Package.create(gpa, null, file.sub_file_path);
5312 file.pkg = try Package.create(gpa, "root", null, file.sub_file_path);
53085313 defer file.pkg.destroy(gpa);
53095314
53105315 file.tree = try std.zig.parse(gpa, file.source);
......@@ -5423,7 +5428,7 @@ pub fn cmdChangelist(
54235428 .root_decl = .none,
54245429 };
54255430
5426 file.pkg = try Package.create(gpa, null, file.sub_file_path);
5431 file.pkg = try Package.create(gpa, "root", null, file.sub_file_path);
54275432 defer file.pkg.destroy(gpa);
54285433
54295434 const source = try arena.allocSentinel(u8, @intCast(usize, stat.size), 0);
src/test.zig+1
......@@ -1497,6 +1497,7 @@ pub const TestContext = struct {
14971497 var main_pkg: Package = .{
14981498 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },
14991499 .root_src_path = tmp_src_path,
1500 .name = "root",
15001501 };
15011502 defer main_pkg.table.deinit(allocator);
15021503