zkat / npx

execute npm package binaries (moved)
https://github.com/npm/npx
Other
2.63k stars 105 forks source link

detecting a bin by shebang is overly specific #54

Closed junosuarez closed 7 years ago

junosuarez commented 7 years ago

npx's heuristic for detecting which bin file to use fails to detect a valid shebang, and results in not finding any bin for some packages.

npx is peeking at the first few bytes of a potential bin file and comparing it exactly to '#!/usr/bin/env node\n' ( https://github.com/zkat/npx/blob/8cb75a295ce7b2cb21b34d72807a2ba9626270d2/index.js#L262 )

however, a valid shebang can have more whitespace, such as `'#! /usr/bin/env node \n'

One potential approach would be to read more bytes from the candidate file, and then use a looser match. This is what node does to strip shebangs when loading a file: https://github.com/nodejs/node/blob/7e3a3c962f09233c53cee7ebe381341d7c8b7162/lib/internal/module.js#L48

reproduction: gist with space in shebang (breaks): https://gist.github.com/js-n/a192c339b652dc29c90d8be397ee3cf7 gist without space (works): https://gist.github.com/js-n/eaab62ef315754e478791ebf63c1bf67

zkat commented 7 years ago

Note: this is an optimization. Code should still work even if a shebang is not detected. npx should really be finding that binary either way and executing it correctly and I'm not sure why it's failing to do so rn.