fix invalidexe because we should not be running javascript files as if they were exes

This commit is contained in:
dave caruso
2024-03-08 17:14:49 -08:00
parent c75304edf0
commit da47cf8247
3 changed files with 12 additions and 9 deletions

View File

@@ -1740,7 +1740,7 @@ pub const Command = struct {
if (extension.len > 0) {
if (strings.endsWithComptime(ctx.args.entry_points[0], ".sh")) {
break :brk options.Loader.bunsh;
break :brk .bunsh;
}
if (!ctx.debug.loaded_bunfig) {
@@ -1748,7 +1748,7 @@ pub const Command = struct {
}
if (ctx.preloads.len > 0)
break :brk options.Loader.js;
break :brk .js;
}
break :brk null;
@@ -1812,6 +1812,7 @@ pub const Command = struct {
}
fn maybeOpenWithBunJS(ctx: *Command.Context) bool {
const debug = bun.Output.scoped(.maybeOpenWithBunJS, false);
if (ctx.args.entry_points.len == 0)
return false;
@@ -1829,6 +1830,7 @@ pub const Command = struct {
if (comptime Environment.isWindows) {
resolved = resolve_path.normalizeString(resolved, true, .windows);
}
debug("absolute path = {s}", .{resolved});
break :brk bun.openFile(
resolved,
.{ .mode = .read_only },
@@ -1839,7 +1841,7 @@ pub const Command = struct {
script_name_buf[file_path.len] = 0;
break :brk2 script_name_buf[0..file_path.len :0];
};
debug("relative with dots file_path = {s}", .{file_pathZ});
break :brk bun.openFileZ(file_pathZ, .{ .mode = .read_only });
} else {
var path_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
@@ -1852,6 +1854,7 @@ pub const Command = struct {
&parts,
.auto,
);
debug("relative file_path = {s}", .{file_path});
if (file_path.len == 0) return false;
script_name_buf[file_path.len] = 0;
const file_pathZ = script_name_buf[0..file_path.len :0];
@@ -1891,11 +1894,7 @@ pub const Command = struct {
ctx.*,
absolute_script_path.?,
) catch |err| {
if (Output.enable_ansi_colors) {
ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
} else {
ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
}
ctx.log.printForLogLevel(Output.errorWriter()) catch {};
Output.prettyErrorln("<r><red>error<r>: Failed to run <b>{s}<r> due to error <b>{s}<r>", .{
std.fs.path.basename(file_path),

View File

@@ -1483,6 +1483,7 @@ pub const RunCommand = struct {
}
if (path_for_which.len > 0) {
// TODO(@paperdave): double-check the PATH on windows is correct. there may be something incorrect here
if (which(&path_buf, path_for_which, this_bundler.fs.top_level_dir, script_name_to_search)) |destination| {
const out = bun.asByteSlice(destination);
return try runBinaryWithoutBunxPath(

View File

@@ -15,6 +15,10 @@ fn isValid(buf: *bun.PathBuffer, segment: []const u8, bin: []const u8) ?u16 {
// Like /usr/bin/which but without needing to exec a child process
// Remember to resolve the symlink if necessary
pub fn which(buf: *bun.PathBuffer, path: []const u8, cwd: []const u8, bin: []const u8) ?[:0]const u8 {
if (bin.len == 0) return null;
if (bun.strings.indexOfChar(bin, '/') != null) return null; // invalid exe. TODO: should be assertion?
if (bun.Environment.os == .windows and bun.strings.indexOfChar(bin, '\\') != null) return null; // invalid exe. TODO: should be assertion?
if (bun.Environment.os == .windows) {
var convert_buf: bun.WPathBuffer = undefined;
const result = whichWin(&convert_buf, path, cwd, bin) orelse return null;
@@ -23,7 +27,6 @@ pub fn which(buf: *bun.PathBuffer, path: []const u8, cwd: []const u8, bin: []con
std.debug.assert(result_converted.ptr == buf.ptr);
return buf[0..result_converted.len :0];
}
if (bin.len == 0) return null;
// handle absolute paths
if (std.fs.path.isAbsolute(bin)) {