Skip to content
Merged
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
40 changes: 21 additions & 19 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const isWindowsExecutableRegExp = /\.(?:com|exe)$/i;
const isNodeModulesCmdRegExp = /node_modules[\\/]\.bin[\\/][^\\/]+\.cmd$/i;
const isWindows = process.platform === 'win32';
const defaultPathExt = ['.EXE', '.CMD', '.BAT', '.COM'];
const noPathExt = [''];

interface NormalizedSpawnCommand {
command: string;
Expand Down Expand Up @@ -148,30 +149,31 @@ function resolveCommand(command: string, options: SpawnOptions): string | null {
command.includes('/') || command.includes('\\')
? ['']
: [cwd, ...PATH.split(pathDelimiter)];
const pathExt = env.PATHEXT
? env.PATHEXT.split(pathDelimiter)
: defaultPathExt;
let pathExt = env.PATHEXT ? env.PATHEXT.split(pathDelimiter) : defaultPathExt;

if (command.includes('.') && pathExt[0] !== '') {
pathExt.unshift('');
pathExt = ['', ...pathExt];
}

for (const path of pathEnv) {
const unquoted =
path.startsWith('"') && path.endsWith('"') && path.length > 1
? path.slice(1, -1)
: path;
const dest = resolvePath(cwd, unquoted, command);

for (const ext of pathExt) {
const destWithExt = dest + ext;

try {
if (statSync(destWithExt).isFile()) {
return destWithExt;
// The second pass tries to resolve the command with no extension
for (const extensions of [pathExt, noPathExt]) {
for (const path of pathEnv) {
const unquoted =
path.startsWith('"') && path.endsWith('"') && path.length > 1
? path.slice(1, -1)
: path;
const dest = resolvePath(cwd, unquoted, command);

for (const ext of extensions) {
const destWithExt = dest + ext;

try {
if (statSync(destWithExt).isFile()) {
return destWithExt;
}
} catch {
// do nothing, it didn't exist
}
} catch {
// do nothing, it didn't exist
}
}
}
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 @@ -71,6 +71,14 @@ describe('normalizeSpawnCommand', () => {
expect(normalized.args).toEqual([scriptPath]);
});

test('detects shebang of a command without extension', () => {
const scriptPath = path.join(fixturesPath, 'shebang_script_noext');
const normalized = normalizeSpawnCommand(scriptPath, []);

expect(normalized.command).toBe('where');
expect(normalized.args).toEqual([scriptPath]);
});

test('handles relative commands without extension', () => {
const relativePath = path.relative(
cwd,
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/shebang_script_noext
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!where
not a real script, just used to verify shebang parsing