Skip to content

Commit cb0d461

Browse files
committed
fix: correct shebang regex
We were incorrectly looking for the end of the input, which meant most shebangs were not being matched 👀
1 parent 6d499f2 commit cb0d461

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/normalize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {getPathFromEnv} from './env.js';
1111

1212
// See http://www.robvanderwoude.com/escapechars.php
1313
const metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
14-
const shebangRegExp = /^#!\s*(.+)$/;
14+
const shebangRegExp = /^#!\s*(.+)/;
1515
const isWindowsExecutableRegExp = /\.(?:com|exe)$/i;
1616
const isNodeModulesCmdRegExp = /node_modules[\\/]\.bin[\\/][^\\/]+\.cmd$/i;
1717
const isWindows = process.platform === 'win32';

src/test/normalize_test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {fileURLToPath} from 'node:url';
77
const isWindows = os.platform() === 'win32';
88
const fixturesPath = path.join(
99
path.dirname(fileURLToPath(import.meta.url)),
10-
'../test/fixtures'
10+
'../../test/fixtures'
1111
);
1212
const cwd = process.cwd();
1313

@@ -61,6 +61,16 @@ describe('normalizeSpawnCommand', () => {
6161
});
6262
});
6363

64+
test('detects shebang and rewrites command to interpreter', () => {
65+
const scriptPath = path.join(fixturesPath, 'shebang_script.js');
66+
const normalized = normalizeSpawnCommand(scriptPath, []);
67+
68+
// where resolves to where.exe (always a .exe in System32, never a
69+
// shim), so the cmd.exe wrapping branch is skipped
70+
expect(normalized.command).toBe('where');
71+
expect(normalized.args).toEqual([scriptPath]);
72+
});
73+
6474
test('handles relative commands without extension', () => {
6575
const relativePath = path.relative(
6676
cwd,

test/fixtures/shebang_script.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!where
2+
not a real script, just used to verify shebang parsing

0 commit comments

Comments
 (0)