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
3 changes: 1 addition & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ function addNodeBinToPath(cwd: URL | string, path: EnvPathInfo): EnvPathInfo {
const parts = path.value.split(pathDelimiter);
const nodeBinPaths: string[] = [];

let currentPath =
typeof cwd === 'string' ? cwd : fileURLToPath(cwd, {windows: false});
let currentPath = typeof cwd === 'string' ? cwd : fileURLToPath(cwd);
let lastPath: string;

do {
Expand Down
5 changes: 4 additions & 1 deletion src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
basename
} from 'node:path';
import {cwd as getCwd} from 'node:process';
import {fileURLToPath} from 'node:url';
import {getPathFromEnv} from './env.js';

// See http://www.robvanderwoude.com/escapechars.php
Expand Down Expand Up @@ -142,7 +143,9 @@ export function normalizeSpawnCommand(
* Handles things like traversing PATH and adding extensions from PATHEXT
*/
function resolveCommand(command: string, options: SpawnOptions): string | null {
const cwd = (options.cwd ?? getCwd()).toString();
const optionCwd = options.cwd ?? getCwd();
const cwd =
typeof optionCwd === 'string' ? optionCwd : fileURLToPath(optionCwd);
const env = options.env ?? process.env;
const PATH = getPathFromEnv(env).value;

Expand Down
12 changes: 11 additions & 1 deletion src/test/env_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {computeEnv, getPathFromEnv} from '../env.js';
import {expect, test, describe} from 'vitest';
import process from 'node:process';
import path, {sep as pathSep, delimiter as pathDelimiter} from 'node:path';
import {pathToFileURL} from 'node:url';

const pathKey = getPathFromEnv(process.env).key;

Expand Down Expand Up @@ -125,7 +126,7 @@ describe('computeEnv', async () => {
});

test('supports file URL for cwd', () => {
const cwd = new URL('file:///one/two/three');
const cwd = pathToFileURL(path.resolve(pathSep, 'one', 'two', 'three'));
const originalPath = path.join(pathSep, 'usr', 'local', 'bin');

const env = computeEnv(cwd, {
Expand All @@ -143,4 +144,13 @@ describe('computeEnv', async () => {

expect(env[pathKey]).toBe(expected);
});

test('file URL and path cwd produce the same PATH with encoded characters', () => {
const cwd = path.resolve('special % chars');
const env = {PATH: 'existing-path'};

expect(computeEnv(pathToFileURL(cwd), env)[pathKey]).toBe(
computeEnv(cwd, env)[pathKey]
);
});
});
48 changes: 48 additions & 0 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,54 @@ describe.for(variants)('exec ($name)', ({x, isAsync}) => {
path.join(repoRootDir, 'node_modules', '.bin')
);
});

test.runIf(isWindows)(
'runs relative shebang commands with a file URL cwd',
async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'tinyexec-url-'));

try {
fs.writeFileSync(
path.join(root, 'print-cwd'),
'#!/usr/bin/env node\nprocess.stdout.write(process.cwd());\n'
);

const result = await x('./print-cwd', [], {
nodeOptions: {cwd: pathToFileURL(root)},
throwOnError: true
});

expect(result.stdout).toBe(root);
} finally {
fs.rmSync(root, {recursive: true, force: true});
}
}
);

test.runIf(isWindows)(
'finds local node binaries with a file URL cwd',
async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'tinyexec-url-bin-'));
const bin = path.join(root, 'node_modules', '.bin');

try {
fs.mkdirSync(bin, {recursive: true});
fs.writeFileSync(
path.join(bin, 'cwd-url-test.cmd'),
'@echo url-bin\r\n'
);

const result = await x('cwd-url-test', [], {
nodeOptions: {cwd: pathToFileURL(root)},
throwOnError: true
});

expect(result.stdout.trim()).toBe('url-bin');
} finally {
fs.rmSync(root, {recursive: true, force: true});
}
}
);
});

describe('exec (async)', () => {
Expand Down
15 changes: 14 additions & 1 deletion src/test/normalize_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {normalizeSpawnCommand} from '../normalize.js';
import {describe, test, expect} from 'vitest';
import os from 'node:os';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {fileURLToPath, pathToFileURL} from 'node:url';

const isWindows = os.platform() === 'win32';
const fixturesPath = path.join(
Expand Down Expand Up @@ -79,6 +79,19 @@ describe('normalizeSpawnCommand', () => {
expect(normalized.args).toEqual([scriptPath]);
});

test('resolves relative shebang commands from a file URL cwd', () => {
const cwd = pathToFileURL(fixturesPath);
const normalized = normalizeSpawnCommand('./shebang_script_noext', [], {
cwd
});

expect(normalized.command).toBe('where');
expect(normalized.args).toEqual([
path.join(fixturesPath, 'shebang_script_noext')
]);
expect(normalized.options.cwd).toBe(cwd);
});

test('double escapes meta chars in args to a batch file', () => {
const cmdPath = path.join(fixturesPath, 'echo_args.cmd');
const normalized = normalizeSpawnCommand(cmdPath, ['"&injected']);
Expand Down