From f322d1ce6d85a97f53a0b639fa36699d49d41098 Mon Sep 17 00:00:00 2001 From: monosans Date: Sat, 28 Nov 2026 21:03:01 +0300 Subject: [PATCH] fix: pass a shebang's argument to its interpreter --- src/normalize.ts | 11 +++++++++-- src/test/normalize_test.ts | 8 ++++++++ test/fixtures/shebang_script_arg | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/shebang_script_arg diff --git a/src/normalize.ts b/src/normalize.ts index 2ec2336..f03e1da 100644 --- a/src/normalize.ts +++ b/src/normalize.ts @@ -44,6 +44,7 @@ export function normalizeSpawnCommand( // Detect & add support for shebangs let file = resolveCommand(command, options); let shebang: string | null = null; + let shebangArgs: string[] = []; if (file !== null) { // Read the first 150 bytes from the file @@ -72,12 +73,18 @@ export function normalizeSpawnCommand( separatorIndex !== -1 ? line.slice(separatorIndex + 1) : ''; const binary = basename(path); - shebang = binary === 'env' ? argument || null : binary; + if (binary === 'env') { + shebang = argument || null; + } else { + shebang = binary; + // Like on Unix, the interpreter gets the rest of the line as one argument + shebangArgs = argument ? [argument] : []; + } } } if (shebang !== null && file !== null) { - args = [file, ...args]; + args = [...shebangArgs, file, ...args]; command = shebang; file = resolveCommand(command, options); diff --git a/src/test/normalize_test.ts b/src/test/normalize_test.ts index f3093eb..388c9dc 100644 --- a/src/test/normalize_test.ts +++ b/src/test/normalize_test.ts @@ -79,6 +79,14 @@ describe('normalizeSpawnCommand', () => { expect(normalized.args).toEqual([scriptPath]); }); + test('passes the shebang argument to the interpreter', () => { + const scriptPath = path.join(fixturesPath, 'shebang_script_arg'); + const normalized = normalizeSpawnCommand(scriptPath, ['x']); + + expect(normalized.command).toBe('where'); + expect(normalized.args).toEqual(['/q', scriptPath, 'x']); + }); + test('resolves relative shebang commands from a file URL cwd', () => { const cwd = pathToFileURL(fixturesPath); const normalized = normalizeSpawnCommand('./shebang_script_noext', [], { diff --git a/test/fixtures/shebang_script_arg b/test/fixtures/shebang_script_arg new file mode 100644 index 0000000..16e8912 --- /dev/null +++ b/test/fixtures/shebang_script_arg @@ -0,0 +1,2 @@ +#!where /q +not a real script, just used to verify shebang parsing