Skip to content

fix: invalid cwd when running bytecode #19148

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions src/fd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ pub const FD = packed struct(backing_int) {
}

pub fn fromStdFile(file: std.fs.File) FD {
return .fromNative(file.handle);
return FD.fromNative(file.handle);
}

pub fn fromStdDir(dir: std.fs.Dir) FD {
return .fromNative(dir.fd);
return FD.fromNative(dir.fd);
}

pub fn stdFile(fd: FD) std.fs.File {
Expand Down Expand Up @@ -120,14 +120,15 @@ pub const FD = packed struct(backing_int) {
},
};
}

pub fn unwrapValid(fd: FD) ?FD {
return if (fd.isValid()) fd else null;
}

/// When calling fd function, you may not be able to close the returned fd.
/// To close the fd, you have to call `.close()` on the `bun.FD`.
pub fn native(fd: FD) fd_t {
if (Environment.isDebug and !@inComptime()) bun.assert(fd.isValid());
if (!@inComptime()) bun.assertf(fd.isValid(), "Cannot convert invalid fd to native", .{});
return switch (os) {
else => fd.value.as_system,
.windows => switch (fd.decodeWindows()) {
Expand All @@ -149,7 +150,7 @@ pub const FD = packed struct(backing_int) {
if (isStdioHandle(std.os.windows.STD_INPUT_HANDLE, handle)) return 0;
if (isStdioHandle(std.os.windows.STD_OUTPUT_HANDLE, handle)) return 1;
if (isStdioHandle(std.os.windows.STD_ERROR_HANDLE, handle)) return 2;
std.debug.panic(
bun.Output.panic(
\\Cast bun.FD.uv({}) makes closing impossible!
\\
\\The supplier of fd FD should call 'FD.makeLibUVOwned',
Expand Down
9 changes: 7 additions & 2 deletions src/transpiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1207,11 +1207,16 @@ pub const Transpiler = struct {
var path_buf2: bun.PathBuffer = undefined;
@memcpy(path_buf2[0..path.text.len], path.text);
path_buf2[path.text.len..][0..bun.bytecode_extension.len].* = bun.bytecode_extension.*;
const bytecode = bun.sys.File.toSourceAt(dirname_fd, path_buf2[0 .. path.text.len + bun.bytecode_extension.len], bun.default_allocator).asValue() orelse break :brk default_value;
const bytecode_path = path_buf2[0 .. path.text.len + bun.bytecode_extension.len];
const dir_fd = dirname_fd.unwrapValid() orelse bun.FD.cwd();
const bytecode = bun.sys.File.toSourceAt(dir_fd, bytecode_path, bun.default_allocator).asValue() orelse break :brk default_value;
if (bytecode.contents.len == 0) {
break :brk default_value;
}
break :brk if (already_bundled == .bytecode_cjs) .{ .bytecode_cjs = @constCast(bytecode.contents) } else .{ .bytecode = @constCast(bytecode.contents) };
break :brk if (already_bundled == .bytecode_cjs)
.{ .bytecode_cjs = @constCast(bytecode.contents) }
else
.{ .bytecode = @constCast(bytecode.contents) };
}
break :brk default_value;
},
Expand Down