Skip to content
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

Alternate fix for bun --bun folder #16492

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 0 additions & 7 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,6 @@ pub const Command = struct {
break :brk null;
};

const force_using_bun = ctx.debug.run_in_bun;
var did_check = false;
if (default_loader) |loader| {
if (loader.canBeRunByBun()) {
Expand All @@ -2283,12 +2282,6 @@ pub const Command = struct {
}
}

if (force_using_bun and !did_check) {
if (maybeOpenWithBunJS(ctx)) {
return;
}
}

if (ctx.positionals.len > 0 and extension.len == 0) {
if (ctx.filters.len > 0) {
Output.prettyln("<r><yellow>warn<r>: Filters are ignored for auto command", .{});
Expand Down
2 changes: 1 addition & 1 deletion src/cli/exec_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub const ExecCommand = struct {
};
const script_path = bun.path.join(parts, .auto);

const code = bun.shell.Interpreter.initAndRunFromSource(ctx, mini, script_path, script) catch |err| {
const code = bun.shell.Interpreter.initAndRunFromSource(ctx, mini, script_path, script, null) catch |err| {
Output.err(err, "failed to run script <b>{s}<r>", .{script_path});
Global.exit(1);
};
Expand Down
18 changes: 11 additions & 7 deletions src/cli/run_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub const RunCommand = struct {

if (!use_system_shell) {
const mini = bun.JSC.MiniEventLoop.initGlobal(env);
const code = bun.shell.Interpreter.initAndRunFromSource(ctx, mini, name, copy_script.items) catch |err| {
const code = bun.shell.Interpreter.initAndRunFromSource(ctx, mini, name, copy_script.items, cwd) catch |err| {
if (!silent) {
Output.prettyErrorln("<r><red>error<r>: Failed to run script <b>{s}<r> due to error <b>{s}<r>", .{ name, @errorName(err) });
}
Expand Down Expand Up @@ -1301,12 +1301,12 @@ pub const RunCommand = struct {
return true;
}

if (!did_try_open_with_bun_js and (log_errors or force_using_bun)) {
if (!did_try_open_with_bun_js and log_errors) {
if (script_name_to_search.len > 0) {
possibly_open_with_bun_js: {
const ext = std.fs.path.extension(script_name_to_search);
var has_loader = false;
if (!force_using_bun) {
{
if (options.defaultLoaders.get(ext)) |load| {
has_loader = true;
if (!load.canBeRunByBun())
Expand Down Expand Up @@ -1345,7 +1345,7 @@ pub const RunCommand = struct {

const file = file_ catch break :possibly_open_with_bun_js;

if (!force_using_bun) {
{
// Due to preload, we don't know if they intend to run
// this as a script or as a regular file
// once we know it's a file, check if they have any preloads
Expand Down Expand Up @@ -1424,6 +1424,10 @@ pub const RunCommand = struct {
if (root_dir_info.enclosing_package_json) |package_json| {
if (package_json.scripts) |scripts| {
if (scripts.get(script_name_to_search)) |script_content| {
const package_json_path = root_dir_info.enclosing_package_json.?.source.path.text;
const package_json_dir = strings.withoutTrailingSlash(strings.withoutSuffixComptime(package_json_path, "package.json"));
log("Running in dir `{s}`", .{package_json_dir});

// allocate enough to hold "post${scriptname}"
var temp_script_buffer = try std.fmt.allocPrint(ctx.allocator, "ppre{s}", .{script_name_to_search});
defer ctx.allocator.free(temp_script_buffer);
Expand All @@ -1434,7 +1438,7 @@ pub const RunCommand = struct {
ctx.allocator,
prescript,
temp_script_buffer[1..],
this_transpiler.fs.top_level_dir,
package_json_dir,
this_transpiler.env,
&.{},
ctx.debug.silent,
Expand All @@ -1448,7 +1452,7 @@ pub const RunCommand = struct {
ctx.allocator,
script_content,
script_name_to_search,
this_transpiler.fs.top_level_dir,
package_json_dir,
this_transpiler.env,
passthrough,
ctx.debug.silent,
Expand All @@ -1464,7 +1468,7 @@ pub const RunCommand = struct {
ctx.allocator,
postscript,
temp_script_buffer,
this_transpiler.fs.top_level_dir,
package_json_dir,
this_transpiler.env,
&.{},
ctx.debug.silent,
Expand Down
4 changes: 2 additions & 2 deletions src/shell/interpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ pub const Interpreter = struct {
return code;
}

pub fn initAndRunFromSource(ctx: bun.CLI.Command.Context, mini: *JSC.MiniEventLoop, path_for_errors: []const u8, src: []const u8) !ExitCode {
pub fn initAndRunFromSource(ctx: bun.CLI.Command.Context, mini: *JSC.MiniEventLoop, path_for_errors: []const u8, src: []const u8, cwd: ?[]const u8) !ExitCode {
bun.Analytics.Features.standalone_shell += 1;
var shargs = ShellArgs.init();
defer shargs.deinit();
Expand Down Expand Up @@ -1505,7 +1505,7 @@ pub const Interpreter = struct {
shargs,
jsobjs,
null,
null,
cwd,
)) {
.err => |*e| {
e.throwMini();
Expand Down
93 changes: 93 additions & 0 deletions test/cli/install/bun-run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,96 @@ describe("should handle run case", () => {
});
}
});

describe("should run scripts from the project root (#16169)", async () => {
const dir = tempDirWithFiles("test", {
"run_here": {
"myscript.ts": "console.log('successful run')",
"package.json": JSON.stringify({
scripts: { "sample": "pwd", "runscript": "bun myscript.ts" },
}),
"dont_run_in_here": {
"runme.ts": "console.log('do run this script')",
},
},
});

it("outside", () => {
const run_outside = spawnSync({
cmd: [bunExe(), "run", "sample"],
cwd: dir + "/run_here",
env: bunEnv,
});
expect(run_outside.stdout.toString()).toContain("run_here");
expect(run_outside.stdout.toString()).not.toContain("dont_run_in_here");
expect(run_outside.exitCode).toBe(0);
});

it("inside", () => {
const run_inside = spawnSync({
cmd: [bunExe(), "run", "sample"],
cwd: dir + "/run_here/dont_run_in_here",
env: bunEnv,
});
expect(run_inside.stdout.toString()).toContain("run_here");
expect(run_inside.stdout.toString()).not.toContain("dont_run_in_here");
expect(run_inside.exitCode).toBe(0);
});

it("inside --shell=bun", () => {
const run_inside = spawnSync({
cmd: [bunExe(), "--shell=bun", "run", "sample"],
cwd: dir + "/run_here/dont_run_in_here",
env: bunEnv,
});
expect(run_inside.stdout.toString()).toContain("run_here");
expect(run_inside.stdout.toString()).not.toContain("dont_run_in_here");
expect(run_inside.exitCode).toBe(0);
});

it("inside script", () => {
const run_inside = spawnSync({
cmd: [bunExe(), "run", "runme.ts"],
cwd: dir + "/run_here/dont_run_in_here",
env: bunEnv,
});
expect(run_inside.stdout.toString()).toContain("do run this script");
expect(run_inside.exitCode).toBe(0);
});

it("inside wrong script", () => {
const run_inside = spawnSync({
cmd: [bunExe(), "run", "myscript.ts"],
cwd: dir + "/run_here/dont_run_in_here",
env: bunEnv,
});
const stderr = run_inside.stderr.toString();
if (stderr.includes("myscript.ts") && stderr.includes("EACCES")) {
// for some reason on musl, the run_here folder is in $PATH
// 'error: Failed to run "myscript.ts" due to:\nEACCES: run_here/myscript.ts: Permission denied (posix_spawn())'
} else {
expect(stderr).toBe('error: Script not found "myscript.ts"\n');
}
expect(run_inside.exitCode).toBe(1);
});

it("outside 2", () => {
const run_outside_script = spawnSync({
cmd: [bunExe(), "runscript"],
cwd: dir + "/run_here",
env: bunEnv,
});
expect(run_outside_script.stdout.toString()).toBe("successful run\n");
expect(run_outside_script.exitCode).toBe(0);
});

it("inside 2", () => {
const run_inside_script = spawnSync({
cmd: [bunExe(), "runscript"],
cwd: dir + "/run_here/dont_run_in_here",
env: bunEnv,
});
expect(run_inside_script.stdout.toString()).toBe("successful run\n");
expect(run_inside_script.exitCode).toBe(0);
});
});
4 changes: 2 additions & 2 deletions test/regression/issue/10132.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ echo My name is bun-hello2
}
});

test("issue #10132, bun run sets cwd", async () => {
test("issue #10132, bun run sets cwd for script, matching npm", async () => {
$.cwd(dir);
const currentPwd = (await $`${bunExe()} run get-pwd`.text()).trim();
expect(currentPwd).toBe(dir);

const currentPwd2 = join(currentPwd, "subdir", "one");
$.cwd(currentPwd2);
expect((await $`${bunExe()} run get-pwd`.text()).trim()).toBe(currentPwd2);
expect((await $`${bunExe()} run get-pwd`.text()).trim()).toBe(dir);

$.cwd(process.cwd());
});
Expand Down
Loading