From b321908b2b592e40d5b21bcda93e4d04d35c51e7 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Tue, 17 Dec 2024 22:31:07 -0800 Subject: [PATCH] explain the error --- build.zig | 21 +++++++++++++++++---- src/codegen/bindgen.ts | 10 +++++++--- src/noop.zig | 7 +++++++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/noop.zig diff --git a/build.zig b/build.zig index 6861c5cfe5..7f080dd86d 100644 --- a/build.zig +++ b/build.zig @@ -295,9 +295,9 @@ pub fn build(b: *Build) !void { } // zig build check + var bun_check_obj = addBunObject(b, &build_options); { var step = b.step("check", "Check for semantic analysis errors"); - var bun_check_obj = addBunObject(b, &build_options); bun_check_obj.generated_bin = null; step.dependOn(&bun_check_obj.step); @@ -349,10 +349,23 @@ pub fn build(b: *Build) !void { .target = b.graph.host, .optimize = .Debug, }); - // This executable intentionally does not add all of Bun's includes, as they - // are never needed, and any compile error referencing an external module is - // something that should be gated behind `bun.Environment.export_cpp_apis` + // This executable intentionally does not add all of Bun's proper + // modules, as they are theoretically not needed, and any compile error + // referencing an external module is something that should be gated + // behind `bun.Environment.export_cpp_apis`. If these modules were + // required, then that indicates that random code is being analyzed + // when it should not be, and is a mistake. exe.root_module.addImport("build_options", build_options.buildOptionsModule(b)); + const noop = b.createModule(.{ + .root_source_file = b.path("src/noop.zig"), + .target = b.graph.host, + .optimize = .Debug, + }); + var it = bun_check_obj.root_module.import_table.iterator(); + while (it.next()) |entry| { + if(!std.mem.eql(u8, entry.key_ptr.*, "build_options")) + exe.root_module.addImport(entry.key_ptr.*, noop); + } const run = b.addRunArtifact(exe); step.dependOn(&run.step); diff --git a/src/codegen/bindgen.ts b/src/codegen/bindgen.ts index 59b741a6a0..89173ba506 100644 --- a/src/codegen/bindgen.ts +++ b/src/codegen/bindgen.ts @@ -1536,9 +1536,13 @@ pub fn main() !void { }); if (!result.success) { console.error("Failed to extract enums, see above for the error."); - console.error("If you just added a new t.zigEnum, check the file for top-level comptime blocks, they may"); - console.error("need to add new checks for `if (bun.Environment.export_cpp_apis)` to not export code"); - console.error("when being compiled from the code generator."); + console.error(""); + console.error("If you just added a new t.zigEnum, check the file for top-level comptime blocks,"); + console.error("they may need to add new checks for `if (bun.Environment.export_cpp_apis)` to"); + console.error("avoid exporting and referencing code when being compiled from the code generator."); + console.error(""); + console.error("If that does not work, then move the desired Zig enum to a new file, or consider"); + console.error("cutting down on namespace indirection."); process.exit(1); } const out = JSON.parse(result.stdout.toString("utf-8")); diff --git a/src/noop.zig b/src/noop.zig new file mode 100644 index 0000000000..997fa2562d --- /dev/null +++ b/src/noop.zig @@ -0,0 +1,7 @@ +// This file is used as a module entry point for modules that do not exist, +// while the `bindgen.ts` code generator is in use. *Some* module has to be +// defined because Zig will eagerly resolve all source files and their Zir. +// +// If this source file appears in an error message, walk up its reference trace +// and see what is referencing it. It is highly likely that reference can be +// removed by gating it behind `Environment.export_cpp_apis`.