Skip to content

Commit 51f60af

Browse files
committed
Use resolved Windows batch file paths
1 parent 3d77820 commit 51f60af

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

lib/arguments/command-file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const escapeWindowsCommand = parsed => {
4949

5050
const isDoubleEscape = resolvedFile !== undefined && batchFileRegExp.test(resolvedFile);
5151
// POSIX separators must become Windows ones (`foo/bar` -> `foo\bar`), otherwise resolution always fails with ENOENT
52-
const escapedFile = escapeMetaChars(path.normalize(parsed.file));
52+
const escapedFile = escapeMetaChars(path.normalize(resolvedFile ?? parsed.file));
5353
const escapedArguments = parsed.commandArguments.map(argument => escapeArgument(argument, isDoubleEscape));
5454
const commandLine = `"${[escapedFile, ...escapedArguments].join(' ')}"`;
5555

test/arguments/command-resolution.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import {cp, mkdir, unlink} from 'node:fs/promises';
1+
import {
2+
cp,
3+
mkdir,
4+
unlink,
5+
writeFile,
6+
} from 'node:fs/promises';
27
import path from 'node:path';
38
import process from 'node:process';
49
import test from 'ava';
@@ -227,7 +232,7 @@ if (isWindows) {
227232

228233
const environmentName = 'NoDefaultCurrentDirectoryInExePath';
229234
const originalValue = process.env[environmentName];
230-
process.env[environmentName] = '1';
235+
delete process.env[environmentName];
231236
t.teardown(async () => {
232237
if (originalValue === undefined) {
233238
delete process.env[environmentName];
@@ -246,13 +251,59 @@ if (isWindows) {
246251
PathExt: '.EXE',
247252
},
248253
};
254+
const {stdout: currentDirectoryStdout} = await execa(command, ['--print', 'process.execPath'], options);
255+
t.is(currentDirectoryStdout.toLowerCase(), currentDirectoryExecutable.toLowerCase());
256+
257+
const {stdout: currentDirectoryStdoutSync} = execaSync(command, ['--print', 'process.execPath'], options);
258+
t.is(currentDirectoryStdoutSync.toLowerCase(), currentDirectoryExecutable.toLowerCase());
259+
260+
process.env[environmentName] = '1';
249261
const {stdout} = await execa(command, ['--print', 'process.execPath'], options);
250262
t.is(stdout.toLowerCase(), pathExecutable.toLowerCase());
251263

252264
const {stdout: stdoutSync} = execaSync(command, ['--print', 'process.execPath'], options);
253265
t.is(stdoutSync.toLowerCase(), pathExecutable.toLowerCase());
254266
});
255267

268+
test.serial('Uses the resolved batch file with NoDefaultCurrentDirectoryInExePath', async t => {
269+
const binaryDirectory = path.join(FIXTURES_DIRECTORY, 'node_modules', '.bin');
270+
await mkdir(binaryDirectory, {recursive: true});
271+
const command = 'batch-current-directory';
272+
const currentDirectoryBatchFile = path.join(FIXTURES_DIRECTORY, `${command}.cmd`);
273+
const pathBatchFile = path.join(binaryDirectory, `${command}.cmd`);
274+
await Promise.all([
275+
writeFile(currentDirectoryBatchFile, '@echo current directory'),
276+
writeFile(pathBatchFile, '@echo PATH'),
277+
]);
278+
279+
const environmentName = 'NoDefaultCurrentDirectoryInExePath';
280+
const originalValue = process.env[environmentName];
281+
process.env[environmentName] = '1';
282+
t.teardown(async () => {
283+
if (originalValue === undefined) {
284+
delete process.env[environmentName];
285+
} else {
286+
process.env[environmentName] = originalValue;
287+
}
288+
289+
await Promise.all([unlink(currentDirectoryBatchFile), unlink(pathBatchFile)]);
290+
});
291+
292+
const options = {
293+
cwd: FIXTURES_DIRECTORY,
294+
extendEnv: false,
295+
env: {
296+
Path: binaryDirectory,
297+
PathExt: '.CMD',
298+
},
299+
};
300+
const {stdout} = await execa(command, options);
301+
t.is(stdout, 'PATH');
302+
303+
const {stdout: stdoutSync} = execaSync(command, options);
304+
t.is(stdoutSync, 'PATH');
305+
});
306+
256307
test('Does not search PATH for drive-relative commands', async t => {
257308
const binaryDirectory = path.join(FIXTURES_DIRECTORY, 'node_modules', '.bin');
258309
await mkdir(binaryDirectory, {recursive: true});

0 commit comments

Comments
 (0)