diff --git a/src/normalize.ts b/src/normalize.ts index c8c8ef8..984b3cc 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 ef6b1fc..c0eaa82 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