diff --git a/src/env.ts b/src/env.ts index a269c8a..0ced642 100644 --- a/src/env.ts +++ b/src/env.ts @@ -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 { diff --git a/src/normalize.ts b/src/normalize.ts index 3ab6221..2ec2336 100644 --- a/src/normalize.ts +++ b/src/normalize.ts @@ -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 @@ -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; diff --git a/src/test/env_test.ts b/src/test/env_test.ts index 9f92aa3..7a3add0 100644 --- a/src/test/env_test.ts +++ b/src/test/env_test.ts @@ -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; @@ -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, { @@ -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] + ); + }); }); diff --git a/src/test/main_test.ts b/src/test/main_test.ts index a852620..d49e06c 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -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)', () => { diff --git a/src/test/normalize_test.ts b/src/test/normalize_test.ts index d66513f..f3093eb 100644 --- a/src/test/normalize_test.ts +++ b/src/test/normalize_test.ts @@ -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( @@ -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']);