Skip to content

Commit 9a7a162

Browse files
authored
Use which-command instead of which (#1253)
1 parent c31c94c commit 9a7a162

3 files changed

Lines changed: 10 additions & 36 deletions

File tree

lib/arguments/command-file.js

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {openSync, readSync, closeSync} from 'node:fs';
22
import {Buffer} from 'node:buffer';
33
import path from 'node:path';
44
import process from 'node:process';
5-
import which from 'which';
5+
import {whichCommandSync} from 'which-command';
66
import pathKey from 'path-key';
77

88
/*
@@ -72,39 +72,13 @@ const resolveWithShebang = parsed => {
7272
return resolvePath(parsed);
7373
};
7474

75-
// Search `PATH` for the command, first honoring `PATHEXT`, then ignoring it as a fallback
76-
const resolvePath = parsed => resolveInCwd(parsed, false) || resolveInCwd(parsed, true);
77-
78-
const resolveInCwd = (parsed, ignorePathExtension) => {
75+
// Search `PATH` for the command, resolving its Windows executable extension via `PATHEXT`
76+
const resolvePath = parsed => {
7977
const environment = parsed.options.env || process.env;
80-
81-
/*
82-
`which` runs `stat` relative to the current directory but has no `cwd` option, so we temporarily switch into the target directory and restore it afterwards.
83-
84-
`process.chdir()` is disabled in worker threads and might be absent in non-Node runtimes.
85-
*/
86-
const canChangeDirectory = process.chdir !== undefined && !process.chdir.disabled;
87-
const originalDirectory = process.cwd();
88-
if (canChangeDirectory) {
89-
try {
90-
process.chdir(parsed.options.cwd);
91-
} catch {}
92-
}
93-
94-
try {
95-
const resolved = which.sync(parsed.file, {
96-
path: environment[pathKey({env: environment})],
97-
pathExt: ignorePathExtension ? path.delimiter : undefined,
98-
});
99-
// `cwd` is already an absolute path, so this returns an absolute path too
100-
return path.resolve(parsed.options.cwd, resolved);
101-
} catch {
102-
return undefined;
103-
} finally {
104-
if (canChangeDirectory) {
105-
process.chdir(originalDirectory);
106-
}
107-
}
78+
return whichCommandSync(parsed.file, {
79+
cwd: parsed.options.cwd,
80+
path: environment[pathKey({env: environment})],
81+
});
10882
};
10983

11084
const SHEBANG_BYTE_LENGTH = 150;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"pretty-ms": "^9.3.0",
6363
"signal-exit": "^4.1.0",
6464
"strip-final-newline": "^4.0.0",
65-
"which": "^7.0.0",
65+
"which-command": "^0.1.0",
6666
"yoctocolors": "^2.1.2"
6767
},
6868
"devDependencies": {

test/arguments/shell.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import process from 'node:process';
22
import {pathToFileURL} from 'node:url';
33
import test from 'ava';
4-
import which from 'which';
4+
import {whichCommand} from 'which-command';
55
import {execa} from '../../index.js';
66
import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
77
import {identity} from '../helpers/stdio.js';
@@ -18,7 +18,7 @@ test('can use `options.shell: true`', async t => {
1818

1919
const testShellPath = async (t, mapPath) => {
2020
const shellPath = isWindows ? 'cmd.exe' : 'bash';
21-
const shell = mapPath(await which(shellPath));
21+
const shell = mapPath(await whichCommand(shellPath));
2222
const {stdout} = await execa('node test/fixtures/noop.js foo', {shell});
2323
t.is(stdout, 'foo');
2424
};

0 commit comments

Comments
 (0)