pnpm / cmd-shim

The cmd-shim used in pnpm
Other
14 stars 11 forks source link

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

Closed milahu closed 1 year ago

milahu commented 1 year ago

fix https://github.com/pnpm/pnpm/issues/5575

upstream issue https://github.com/npm/cmd-shim/issues/54 upstream patch https://github.com/npm/cmd-shim/pull/55

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

zkochan commented 1 year ago

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 commented 1 year ago

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