Skip to content

bun run script.js fails when script has node shebang #4850

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

Closed
jakeboone02 opened this issue Sep 10, 2023 · 8 comments
Closed

bun run script.js fails when script has node shebang #4850

jakeboone02 opened this issue Sep 10, 2023 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@jakeboone02
Copy link
Contributor

jakeboone02 commented Sep 10, 2023

What version of Bun is running?

1.0.0

What platform is your computer?

Darwin 22.6.0 x86_64 i386

What steps can reproduce the bug?

Example repo: https://github.com/jakeboone02/bun-run-script

$ cat test.js
#!/usr/bin/env node
console.log(process.argv);

$ bun --bun run test.js
[ "/Users/jake/.bun/bin/bun", "/Users/jake/git/test.js" ]

$ bun run test.js
error: missing script "test.js"

$ bun test.js
[ "/Users/jake/.bun/bin/bun", "/Users/jake/git/test.js" ]

What is the expected behavior?

bun run script.js should run as if the user ran node script.js when the node shebang is present.

What do you see instead?

  • bun script.js, bun --bun script.js, and bun --bun run script.js run whether or not the node shebang (#!/usr/bin/env node) is present in script.js.
  • bun run script.js only runs if script.js does not have the node shebang.

It appears that bun run script.js is passing off the execution to node as if "script.js" is a package.json script (i.e. npm run script.js) rather than a standalone script (i.e. node script.js).

Additional information

No response

@jakeboone02 jakeboone02 added the bug Something isn't working label Sep 10, 2023
@nektro nektro self-assigned this May 24, 2024
@nektro
Copy link
Member

nektro commented May 24, 2024

does this still reproduce for you? it appears to be working as of Bun 1.1.9. you may need to run bun upgrade.

@jakeboone02
Copy link
Contributor Author

jakeboone02 commented May 24, 2024

@nektro yes, still reproducible in v1.1.9:

image

image

@nektro
Copy link
Member

nektro commented May 24, 2024

ah I see. I was using bun index.js instead of bun run index.js. apologies

@nektro
Copy link
Member

nektro commented May 24, 2024

talking with the team it seems this is somewhat intended behavior with an unfortunate error message given how many use cases are intersecting at this CLI. bun run ... is primarily meant for running package.json scripts and so they take priority here. however you can signal to it that the argument is a file path with ./ or absolute paths. and we see this with bun-debug run ./index.js producing the expected output.

I will look into making the original call work but landing it may be low priority. hope this helps and will let u know about any developments! :)

@jakeboone02
Copy link
Contributor Author

Thanks for looking into it! No rush, really.

I probably wouldn't have filed the issue except for the fact that bun --bun run test.js works. Seemed like that and bun run test.js should have the same behavior with regard to their arguments.

@shoogle
Copy link

shoogle commented Nov 13, 2024

I get the same error even when the shebang specifies Bun rather than Node.

Also, relative paths that don't start with ./ give the error. E.g. src/test.js (all platforms) and .\test.js on Windows with \ as separator.

Details

I'm using Bun version 1.1.34 on Windows 11. I tested in Git Bash and CMD, both running inside Windows Terminal.

test.js

#!/usr/bin/env -S bun run --bun
console.log(Bun.version); // will throw ReferenceError if run in Node rather than Bun

Spoiler: I never saw the ReferenceError (so the script was never run in Node) but I did see the Script not found error in some cases.

Tested in Git Bash

bun run test.js              # error: Script not found "test.js"
bun run ./test.js            # ok

bun run src/test.js          # error: Script not found "src/test.js"
bun run ./src/test.js        # ok
More
./test.js                    # ok
bun test.js                  # ok
bun ./test.js                # ok
bun run test.js              # error: Script not found "test.js"
bun run ./test.js            # ok
bun run --bun test.js        # ok
bun run --bun ./test.js      # ok

mkdir -p src
cp test.js src/test.js

src/test.js                  # ok
./src/test.js                # ok
bun src/test.js              # ok
bun ./src/test.js            # ok
bun run src/test.js          # error: Script not found "src/test.js"
bun run ./src/test.js        # ok
bun run --bun src/test.js    # ok
bun run --bun ./src/test.js  # ok

${PWD}/test.js               # ok
bun ${PWD}/test.js           # ok
bun run ${PWD}/test.js       # ok
bun run --bun ${PWD}/test.js # ok

node test.js                 # ReferenceError  (intended behavior)

Tested in CMD

bun run test.js              &: error: Script not found "test.js"
bun run ./test.js            &: ok
bun run .\test.js            &: error: Script not found ".\test.js"

bun run src/test.js          &: error: Script not found "src/test.js"
bun run src\test.js          &: error: Script not found "src\test.js"
bun run ./src/test.js        &: ok
bun run .\src\test.js        &: error: Script not found ".\src\test.js"
More
bun test.js                  &: ok
bun ./test.js                &: ok
bun .\test.js                &: ok
bun run test.js              &: error: Script not found "test.js"
bun run ./test.js            &: ok
bun run .\test.js            &: error: Script not found ".\test.js"
bun run --bun test.js        &: ok
bun run --bun ./test.js      &: ok
bun run --bun .\test.js      &: ok

mkdir src
copy test.js src\test.js

bun src/test.js              &: ok
bun src\test.js              &: ok
bun ./src/test.js            &: ok
bun .\src\test.js            &: ok
bun run src/test.js          &: error: Script not found "src/test.js"
bun run src\test.js          &: error: Script not found "src\test.js"
bun run ./src/test.js        &: ok
bun run .\src\test.js        &: error: Script not found ".\src\test.js"
bun run --bun src/test.js    &: ok
bun run --bun src\test.js    &: ok
bun run --bun ./src/test.js  &: ok
bun run --bun .\src\test.js  &: ok

bun %CD%/test.js             &: ok
bun %CD%\test.js             &: ok
bun run %CD%/test.js         &: ok
bun run %CD%\test.js         &: ok
bun run --bun %CD%/test.js   &: ok
bun run --bun %CD%\test.js   &: ok

node test.js                 &: ReferenceError  (intended behavior)

Changing the shebang to #!/usr/bin/env bun in test.js gives the exact same results as I've shown above.

As @jakeboone02 noted, removing the shebang line entirely from test.js makes it work with bun run always.

@RiskyMH
Copy link
Member

RiskyMH commented Feb 8, 2025

This now works in 1.2.2, I'm pretty sure #15117 fixed it!

@RiskyMH RiskyMH closed this as completed Feb 8, 2025
@jakeboone02
Copy link
Contributor Author

This now works in 1.2.2, I'm pretty sure #15117 fixed it!

Confirmed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants