Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/test/normalize_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', [], {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/shebang_script_arg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!where /q
not a real script, just used to verify shebang parsing