authorgravatar for ian@ianjohnson.devIan Johnson <ian@ianjohnson.dev> 2024-03-23 23:46:17-04:00
committergravatar for ian@ianjohnson.devIan Johnson <ian@ianjohnson.dev> 2024-03-28 00:30:29-04:00
log424dd17b6cc3db868c837f6b7a8fa36dac2166c1
tree91d247d8250024fd7e6161c9ca11a4553f5854ea
parent9dac8db2df2332d6dd4aa52a0b524e5c88a9349b

Autodoc: add all modules to sources.tar

Closes #19403 This commit adds all modules in the compilation to the generated `sources.tar` when using `-femit-docs` (including `std` and `builtin`). Additionally, it considers the main module when doing so, rather than the root module, so the behavior when running `zig test -femit-docs test.zig` is now correct.

1 files changed, 23 insertions(+), 2 deletions(-)

src/Compilation.zig+23-2
......@@ -3693,6 +3693,9 @@ fn workerDocsCopy(comp: *Compilation, wg: *WaitGroup) void {
36933693}
36943694
36953695fn docsCopyFallible(comp: *Compilation) anyerror!void {
3696 const zcu = comp.module orelse
3697 return comp.lockAndSetMiscFailure(.docs_copy, "no Zig code to document", .{});
3698
36963699 const emit = comp.docs_emit.?;
36973700 var out_dir = emit.directory.handle.makeOpenPath(emit.sub_path, .{}) catch |err| {
36983701 return comp.lockAndSetMiscFailure(
......@@ -3723,7 +3726,25 @@ fn docsCopyFallible(comp: *Compilation) anyerror!void {
37233726 };
37243727 defer tar_file.close();
37253728
3726 const root = comp.root_mod.root;
3729 var seen_table: std.AutoArrayHashMapUnmanaged(*Package.Module, void) = .{};
3730 defer seen_table.deinit(comp.gpa);
3731
3732 try seen_table.put(comp.gpa, zcu.main_mod, {});
3733 try seen_table.put(comp.gpa, zcu.std_mod, {});
3734
3735 var i: usize = 0;
3736 while (i < seen_table.count()) : (i += 1) {
3737 const mod = seen_table.keys()[i];
3738 try comp.docsCopyModule(mod, tar_file);
3739
3740 const deps = mod.deps.values();
3741 try seen_table.ensureUnusedCapacity(comp.gpa, deps.len);
3742 for (deps) |dep| seen_table.putAssumeCapacity(dep, {});
3743 }
3744}
3745
3746fn docsCopyModule(comp: *Compilation, module: *Package.Module, tar_file: std.fs.File) !void {
3747 const root = module.root;
37273748 const sub_path = if (root.sub_path.len == 0) "." else root.sub_path;
37283749 var mod_dir = root.root_dir.handle.openDir(sub_path, .{ .iterate = true }) catch |err| {
37293750 return comp.lockAndSetMiscFailure(.docs_copy, "unable to open directory '{}': {s}", .{
......@@ -3762,7 +3783,7 @@ fn docsCopyFallible(comp: *Compilation) anyerror!void {
37623783
37633784 var file_header = std.tar.output.Header.init();
37643785 file_header.typeflag = .regular;
3765 try file_header.setPath(comp.root_name, entry.path);
3786 try file_header.setPath(module.fully_qualified_name, entry.path);
37663787 try file_header.setSize(stat.size);
37673788 try file_header.updateChecksum();
37683789