[FEATURE] drop env's -S flag
What / Why
When writing shims for node.js application entry-points it happens that we want to pass additional flags to node when starting. env supports this via the -S flag. For example, one of my projects has this shebang in its entry-point:
#!/usr/bin/env -S node --loader ts-node/esm --experimental-specifier-resolution=node --no-warnings
The flag -S tells env to split the following text on whitespace and treat the segments as args to the executable in the first segment after the -S (i.e. node), instead of interpreting the rest of the line as one long command.
This works without issue on *nix based systems (tested on manjaro, ubuntu, macOS).
When
When using this module to create the shim for a package with an entry-point whose shebang uses the -S flag, the -S flag is interpreted as the name of the executable to run. This makes the resulting shim unusable.
Where
This is the absolute opposite of a minimal example, but the issue occurs in an ongoing PR here[1]. If needed I can build a small reproduction repository.
How
Current Behavior
The -S flag in the shebang is interpreted as the name of the executable that the shim should run.
Expected Behavior
I expect the -S flag to be ignored, since skipping it and using only the remaining shebang works on windows (according to my experiments).
Who
- n/a
References
- [1] https://github.com/thenativeweb/roboter/pull/632
Just leaving a comment for visibility. Any chance this is going to be merged soon?
I just tripped over this too, thanks for describing it here! Until this is merged which workarounds do you use?
I currently consider writing some additional scripts which then run the actual scripts. That is a pain but the best I currently imagine.
I'd argue that this is in fact a bug. Currently it's impossible to make "bin" point to Node code that needs command-line arguments and works cross-platform.
If one writes #!/usr/bin/env node --enable-source-maps (for example), then this works on platforms using shims: the shim runs node --enable-source-maps. However, on Linux where there are no shims, this fails because env receives a single argument node --enable-source-maps; env needs the -S flag to split this up.
If one writes #!/usr/bin/env -S node --enable-source-maps (for example), then this works on Linux. But on platforms using shims, the shim attempts to run -S node --enable-source-maps which of course fails.
A simple fix would be to use shims on Linux; then we wouldn't need -S at all.
But a probably better fix would be to detect -S when detecting /usr/bin/env in shim creation, and strip that off as well. Oh, #55 does this already.
The best workaround I found is to write a NodeJS wrapper that runs Node with the desired command-line options (so two Nodes end up running). Slows things down a bit, but at least it's functional.