Repository navigation
Fix Windows compilation issues with embedded resources and relative paths #22365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
9a51976
d3ff041
80e7317
1046b46
341bc0e
ab3a31d
0752484
fb9b15e
f07ec90
ad6388e
4c30c21
135cc13
8379eab
6182300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1834,11 +1834,19 @@ pub const BundleV2 = struct { | |||||||||||||||
| transpiler.options.chunk_naming = config.names.chunk.data; | ||||||||||||||||
| transpiler.options.asset_naming = config.names.asset.data; | ||||||||||||||||
|
|
||||||||||||||||
| transpiler.options.public_path = config.public_path.list.items; | ||||||||||||||||
| transpiler.options.output_format = config.format; | ||||||||||||||||
| transpiler.options.bytecode = config.bytecode; | ||||||||||||||||
| transpiler.options.compile = config.compile != null; | ||||||||||||||||
|
|
||||||||||||||||
| // For compile mode, set the public_path to the target-specific base path | ||||||||||||||||
| // This ensures embedded resources like yoga.wasm are correctly found | ||||||||||||||||
| if (config.compile) |compile_opts| { | ||||||||||||||||
| const base_public_path = bun.StandaloneModuleGraph.targetBasePublicPath(compile_opts.compile_target.os, "root/"); | ||||||||||||||||
| transpiler.options.public_path = base_public_path; | ||||||||||||||||
| } else { | ||||||||||||||||
| transpiler.options.public_path = config.public_path.list.items; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| transpiler.options.output_dir = config.outdir.slice(); | ||||||||||||||||
| transpiler.options.root_dir = config.rootdir.slice(); | ||||||||||||||||
| transpiler.options.minify_syntax = config.minify.syntax; | ||||||||||||||||
|
|
@@ -1903,11 +1911,18 @@ pub const BundleV2 = struct { | |||||||||||||||
| const outbuf = bun.path_buffer_pool.get(); | ||||||||||||||||
| defer bun.path_buffer_pool.put(outbuf); | ||||||||||||||||
|
|
||||||||||||||||
| // Always get an absolute path for the outfile to ensure it works correctly with PE metadata operations | ||||||||||||||||
| var full_outfile_path = if (this.config.outdir.slice().len > 0) brk: { | ||||||||||||||||
| const outdir_slice = this.config.outdir.slice(); | ||||||||||||||||
| const top_level_dir = bun.fs.FileSystem.instance.top_level_dir; | ||||||||||||||||
| break :brk bun.path.joinAbsStringBuf(top_level_dir, outbuf, &[_][]const u8{ outdir_slice, compile_options.outfile.slice() }, .auto); | ||||||||||||||||
| } else compile_options.outfile.slice(); | ||||||||||||||||
| } else if (std.fs.path.isAbsolute(compile_options.outfile.slice())) | ||||||||||||||||
| compile_options.outfile.slice() | ||||||||||||||||
| else brk: { | ||||||||||||||||
| // For relative paths, ensure we make them absolute relative to the current working directory | ||||||||||||||||
| const top_level_dir = bun.fs.FileSystem.instance.top_level_dir; | ||||||||||||||||
| break :brk bun.path.joinAbsStringBuf(top_level_dir, outbuf, &[_][]const u8{compile_options.outfile.slice()}, .auto); | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||
| // Add .exe extension for Windows targets if not already present | ||||||||||||||||
| if (compile_options.compile_target.os == .windows and !strings.hasSuffixComptime(full_outfile_path, ".exe")) { | ||||||||||||||||
|
|
@@ -1926,19 +1941,32 @@ pub const BundleV2 = struct { | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if (!(dirname.len == 0 or strings.eqlComptime(dirname, "."))) { | ||||||||||||||||
| // On Windows, don't change root_dir, just pass the full relative path | ||||||||||||||||
| // On POSIX, change root_dir to the target directory and pass basename | ||||||||||||||||
| const outfile_for_executable = if (Environment.isWindows) full_outfile_path else basename; | ||||||||||||||||
|
|
||||||||||||||||
|
Comment on lines
+1944
to
+1947
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use target OS, not host OS, for outfile wiring.
Apply: - const outfile_for_executable = if (Environment.isWindows) full_outfile_path else basename;
+ const target_is_windows = compile_options.compile_target.os == .windows;
+ const outfile_for_executable = if (target_is_windows) full_outfile_path else basename;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| if (Environment.isPosix and !(dirname.len == 0 or strings.eqlComptime(dirname, "."))) { | ||||||||||||||||
| // On POSIX, makeOpenPath and change root_dir | ||||||||||||||||
| root_dir = root_dir.makeOpenPath(dirname, .{}) catch |err| { | ||||||||||||||||
| return bun.StandaloneModuleGraph.CompileResult.fail(bun.handleOom(std.fmt.allocPrint(bun.default_allocator, "Failed to open output directory {s}: {s}", .{ dirname, @errorName(err) }))); | ||||||||||||||||
| }; | ||||||||||||||||
| } else if (Environment.isWindows and !(dirname.len == 0 or strings.eqlComptime(dirname, "."))) { | ||||||||||||||||
| // On Windows, ensure directories exist but don't change root_dir | ||||||||||||||||
| _ = bun.makePath(root_dir, dirname) catch |err| { | ||||||||||||||||
| return bun.StandaloneModuleGraph.CompileResult.fail(bun.handleOom(std.fmt.allocPrint(bun.default_allocator, "Failed to create output directory {s}: {s}", .{ dirname, @errorName(err) }))); | ||||||||||||||||
| }; | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+1948
to
1958
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Handle absolute vs relative output dirs (POSIX). For POSIX, opening an absolute dir should use Apply: - if (Environment.isPosix and !(dirname.len == 0 or strings.eqlComptime(dirname, "."))) {
- // On POSIX, makeOpenPath and change root_dir
- root_dir = root_dir.makeOpenPath(dirname, .{}) catch |err| {
+ if (compile_options.compile_target.os != .windows and !(dirname.len == 0 or strings.eqlComptime(dirname, "."))) {
+ // POSIX: open absolute dirs directly; otherwise open relative to CWD
+ root_dir = (if (std.fs.path.isAbsolute(dirname))
+ bun.FD.openDirAbsolute(dirname, .{})
+ else
+ root_dir.makeOpenPath(dirname, .{})) catch |err| {
return bun.StandaloneModuleGraph.CompileResult.fail(bun.handleOom(std.fmt.allocPrint(bun.default_allocator, "Failed to open output directory {s}: {s}", .{ dirname, @errorName(err) })));
};
- } else if (Environment.isWindows and !(dirname.len == 0 or strings.eqlComptime(dirname, "."))) {
+ } else if (compile_options.compile_target.os == .windows and !(dirname.len == 0 or strings.eqlComptime(dirname, "."))) {
// On Windows, ensure directories exist but don't change root_dir
_ = bun.makePath(root_dir, dirname) catch |err| {
return bun.StandaloneModuleGraph.CompileResult.fail(bun.handleOom(std.fmt.allocPrint(bun.default_allocator, "Failed to create output directory {s}: {s}", .{ dirname, @errorName(err) })));
};
}
🤖 Prompt for AI Agents |
||||||||||||||||
|
|
||||||||||||||||
| // Use the target-specific base path for compile mode, not the user-configured public_path | ||||||||||||||||
| const module_prefix = bun.StandaloneModuleGraph.targetBasePublicPath(compile_options.compile_target.os, "root/"); | ||||||||||||||||
|
|
||||||||||||||||
| const result = bun.StandaloneModuleGraph.toExecutable( | ||||||||||||||||
| &compile_options.compile_target, | ||||||||||||||||
| bun.default_allocator, | ||||||||||||||||
| output_files.items, | ||||||||||||||||
| root_dir, | ||||||||||||||||
| this.config.public_path.slice(), | ||||||||||||||||
| basename, | ||||||||||||||||
| module_prefix, | ||||||||||||||||
| outfile_for_executable, | ||||||||||||||||
| this.env, | ||||||||||||||||
| this.config.format, | ||||||||||||||||
| .{ | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, tempDirWithFiles } from "harness"; | ||
| import { join } from "path"; | ||
|
|
||
| describe("Bun.build compile with wasm", () => { | ||
| test("compile with wasm module imports", async () => { | ||
| // This test ensures that embedded wasm modules compile and run correctly | ||
| // The regression was that the module prefix wasn't being set correctly | ||
|
|
||
| const dir = tempDirWithFiles("build-compile-wasm", { | ||
| "app.js": ` | ||
| // Import a wasm module and properly instantiate it | ||
| import wasmPath from "./test.wasm"; | ||
|
|
||
| async function main() { | ||
| try { | ||
| // Read the wasm file as ArrayBuffer | ||
| const wasmBuffer = await Bun.file(wasmPath).arrayBuffer(); | ||
| const { instance } = await WebAssembly.instantiate(wasmBuffer); | ||
|
|
||
| // Call the add function from wasm | ||
| const result = instance.exports.add(2, 3); | ||
| console.log("WASM result:", result); | ||
|
|
||
| if (result === 5) { | ||
| console.log("WASM module loaded successfully"); | ||
| process.exit(0); | ||
| } else { | ||
| console.error("WASM module returned unexpected result:", result); | ||
| process.exit(1); | ||
| } | ||
| } catch (error) { | ||
| console.error("Failed to load WASM module:", error.message); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
| `, | ||
| // A real WebAssembly module that exports an 'add' function | ||
| // (module | ||
| // (func $add (param i32 i32) (result i32) | ||
| // local.get 0 | ||
| // local.get 1 | ||
| // i32.add) | ||
| // (export "add" (func $add))) | ||
| "test.wasm": Buffer.from([ | ||
| 0x00, | ||
| 0x61, | ||
| 0x73, | ||
| 0x6d, // WASM magic number | ||
| 0x01, | ||
| 0x00, | ||
| 0x00, | ||
| 0x00, // WASM version 1 | ||
| // Type section | ||
| 0x01, | ||
| 0x07, | ||
| 0x01, | ||
| 0x60, | ||
| 0x02, | ||
| 0x7f, | ||
| 0x7f, | ||
| 0x01, | ||
| 0x7f, | ||
| // Function section | ||
| 0x03, | ||
| 0x02, | ||
| 0x01, | ||
| 0x00, | ||
| // Export section | ||
| 0x07, | ||
| 0x07, | ||
| 0x01, | ||
| 0x03, | ||
| 0x61, | ||
| 0x64, | ||
| 0x64, | ||
| 0x00, | ||
| 0x00, | ||
| // Code section | ||
| 0x0a, | ||
| 0x09, | ||
| 0x01, | ||
| 0x07, | ||
| 0x00, | ||
| 0x20, | ||
| 0x00, | ||
| 0x20, | ||
| 0x01, | ||
| 0x6a, | ||
| 0x0b, | ||
| ]), | ||
| }); | ||
|
|
||
| // Test compilation with default target (current platform) | ||
| const result = await Bun.build({ | ||
| entrypoints: [join(dir, "app.js")], | ||
| compile: { | ||
| outfile: "app-wasm", | ||
| }, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| expect(result.outputs.length).toBe(1); | ||
|
|
||
| // Run the compiled version to verify it works | ||
| const proc = Bun.spawn({ | ||
| cmd: [result.outputs[0].path], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([ | ||
| new Response(proc.stdout).text(), | ||
| new Response(proc.stderr).text(), | ||
| proc.exited, | ||
| ]); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("WASM result: 5"); | ||
| expect(stdout).toContain("WASM module loaded successfully"); | ||
| expect(stderr).toBe(""); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug:
bun.getFdPathdoes not exist (should beSyscall.getFdPath)Call the sys variant to compile.
Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not the compiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 34316
@coderabbitai You're absolutely right, and I apologize for the incorrect analysis.
Looking at the search results, I can clearly see that
bun.getFdPathis indeed a valid function that exists in the codebase:src/bun.zig:1310, there'spub fn getFdPath(fd: FileDescriptor, buf: *bun.PathBuffer) ![]u8bun.getFdPathis used extensively throughout the codebase (25+ occurrences)bun.getFdPathandSyscall.getFdPathin different contexts, indicating they serve different purposesThe code in the diff is using the correct API. My suggestion to change it to
Syscall.getFdPathwas incorrect -bun.getFdPathis the proper high-level wrapper function that should be used in this context.Thank you for the correction. The code as written is correct and doesn't need any changes.
🧠 Learnings used