Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1834,10 +1834,18 @@ 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();
Expand Down Expand Up @@ -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);
};

// Add .exe extension for Windows targets if not already present
if (compile_options.compile_target.os == .windows and !strings.hasSuffixComptime(full_outfile_path, ".exe")) {
Expand All @@ -1932,12 +1947,15 @@ pub const BundleV2 = struct {
};
}
Comment on lines +1948 to 1958

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 openDirAbsolute; makeOpenPath assumes relative. Otherwise absolute paths may fail or open relative to CWD unintentionally.

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) })));
                 };
             }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/bundler/bundle_v2.zig around lines 1948 to 1958, the POSIX branch uses
makeOpenPath which assumes a relative path; if dirname is absolute this will
open relative to CWD incorrectly. Change the logic to detect absolute paths
(e.g. path starts with '/') and when absolute call the appropriate absolute-open
API (openDirAbsolute or equivalent on root_dir) with the same error handling,
otherwise keep using makeOpenPath for relative paths; preserve the existing
error messages and OOM handling on failure.


// 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(),
module_prefix,
basename,
this.env,
this.config.format,
Expand Down