oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.25k stars 2.77k forks source link

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

Open jakeboone02 opened 1 year ago

jakeboone02 commented 1 year ago

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?

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

nektro commented 5 months ago

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 commented 5 months ago

@nektro yes, still reproducible in v1.1.9:

image

image

nektro commented 5 months ago

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

nektro commented 5 months ago

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 commented 5 months ago

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 commented 2 days ago

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`__ ```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 ```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 ```Bash ./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 ```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 ```CMD 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.