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

fix shebangExpr: ignore -S argument for /usr/bin/env #42

Merged
merged 2 commits into from Dec 13, 2022

Conversation

milahu
Copy link

@milahu milahu commented Nov 24, 2022

fix pnpm/pnpm#5575

upstream issue npm#54
upstream patch npm#55

side note: upstream's shebangExpr is more complex, as it can parse env vars

@zkochan
Copy link
Member

zkochan commented Nov 24, 2022

Although I myself am not too familiar with env.
I've ready the linked conversations and it looks like it should be fine to merge this. But let's add a test for it.

@milahu
Copy link
Author

milahu commented Aug 12, 2023

nitpicks:

require space after /usr/bin/env but not after /usr/bin/env -S
the current version would parse #!/usr/bin/envzzz as zzz but it should be /usr/bin/envzzz

> "#!/usr/bin/envzzz".match(/^#!\s*(?:\/usr\/bin\/env(?:\s+-S\s*)?)?\s*([^ \t]+)(.*)$/)[1]
'zzz'

> "#!/usr/bin/envzzz".match(/^#!\s*(?:\/usr\/bin\/env\s+(?:-S)?)?\s*(\S+)(.*)$/)[1]
'/usr/bin/envzzz'
-const shebangExpr = /^#!\s*(?:\/usr\/bin\/env(?:\s+-S\s*)?)?\s*([^ \t]+)(.*)$/
+const shebangExpr = /^#!\s*(?:\/usr\/bin\/env\s+(?:-S)?)?\s*(\S+)(.*)$/

dont split all lines
keep leading space in firstLine
use trimRight to remove \r

-    const firstLine = data.trim().split(/\r*\n/)[0]
+    let firstLineEnd = data.indexOf('\n')
+    if (firstLineEnd == -1) firstLineEnd = data.length
+    const firstLine = data.slice(0, firstLineEnd).trimRight()

also in the generated binary wrapper scripts, the NODE_PATH should be relative to $basedir
so it still works after moving the project folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

exec: -S: invalid option. pnpm fails to parse shebang: #!/usr/bin/env -S node
2 participants