Commit 424dd17b6c

Ian Johnson <ian@ianjohnson.dev>
2024-03-24 04:46:17
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 parent 9dac8db
Changed files (1)
src/Compilation.zig
@@ -3693,6 +3693,9 @@ fn workerDocsCopy(comp: *Compilation, wg: *WaitGroup) void {
 }
 
 fn docsCopyFallible(comp: *Compilation) anyerror!void {
+    const zcu = comp.module orelse
+        return comp.lockAndSetMiscFailure(.docs_copy, "no Zig code to document", .{});
+
     const emit = comp.docs_emit.?;
     var out_dir = emit.directory.handle.makeOpenPath(emit.sub_path, .{}) catch |err| {
         return comp.lockAndSetMiscFailure(
@@ -3723,7 +3726,25 @@ fn docsCopyFallible(comp: *Compilation) anyerror!void {
     };
     defer tar_file.close();
 
-    const root = comp.root_mod.root;
+    var seen_table: std.AutoArrayHashMapUnmanaged(*Package.Module, void) = .{};
+    defer seen_table.deinit(comp.gpa);
+
+    try seen_table.put(comp.gpa, zcu.main_mod, {});
+    try seen_table.put(comp.gpa, zcu.std_mod, {});
+
+    var i: usize = 0;
+    while (i < seen_table.count()) : (i += 1) {
+        const mod = seen_table.keys()[i];
+        try comp.docsCopyModule(mod, tar_file);
+
+        const deps = mod.deps.values();
+        try seen_table.ensureUnusedCapacity(comp.gpa, deps.len);
+        for (deps) |dep| seen_table.putAssumeCapacity(dep, {});
+    }
+}
+
+fn docsCopyModule(comp: *Compilation, module: *Package.Module, tar_file: std.fs.File) !void {
+    const root = module.root;
     const sub_path = if (root.sub_path.len == 0) "." else root.sub_path;
     var mod_dir = root.root_dir.handle.openDir(sub_path, .{ .iterate = true }) catch |err| {
         return comp.lockAndSetMiscFailure(.docs_copy, "unable to open directory '{}': {s}", .{
@@ -3762,7 +3783,7 @@ fn docsCopyFallible(comp: *Compilation) anyerror!void {
 
         var file_header = std.tar.output.Header.init();
         file_header.typeflag = .regular;
-        try file_header.setPath(comp.root_name, entry.path);
+        try file_header.setPath(module.fully_qualified_name, entry.path);
         try file_header.setSize(stat.size);
         try file_header.updateChecksum();