From 7cc37e7e4057f620db3a9b56fc2f8831fa52f459 Mon Sep 17 00:00:00 2001 From: anticipate218 <3481402509@qq.com> Date: Wed, 18 Nov 2026 07:21:35 +0800 Subject: [PATCH 1/2] fix: resolve file URL working directories on Windows --- src/env.ts | 3 +-- src/normalize.ts | 5 +++- src/test/env_test.ts | 12 ++++++++- src/test/main_test.ts | 53 ++++++++++++++++++++++++++++++++++++++ src/test/normalize_test.ts | 15 ++++++++++- 5 files changed, 83 insertions(+), 5 deletions(-) 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..4ecdcf9 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('cwd with spaces', 'café #%'); + 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..15b89a4 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -98,6 +98,59 @@ 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-')); + const dir = path.join(root, 'cwd with spaces café #%'); + const previousCwd = process.cwd(); + + try { + fs.mkdirSync(dir); + fs.writeFileSync( + path.join(dir, 'print-cwd'), + '#!/usr/bin/env node\nprocess.stdout.write(process.cwd());\n' + ); + + const result = await x('./print-cwd', [], { + nodeOptions: {cwd: pathToFileURL(dir)}, + throwOnError: true + }); + + expect(result.stdout).toBe(dir); + expect(process.cwd()).toBe(previousCwd); + } 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 dir = path.join(root, 'cwd with spaces café #%'); + const bin = path.join(dir, '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(dir)}, + 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']); From d24546cf04f865907dd20d9ecbdccaa65470c442 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:02:12 +0000 Subject: [PATCH 2/2] test: simplify tests The cwd never changed anyway so the assertion did nothing. The special chars test doesn't need 2 parts. --- src/test/env_test.ts | 2 +- src/test/main_test.ts | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/test/env_test.ts b/src/test/env_test.ts index 4ecdcf9..7a3add0 100644 --- a/src/test/env_test.ts +++ b/src/test/env_test.ts @@ -146,7 +146,7 @@ describe('computeEnv', async () => { }); test('file URL and path cwd produce the same PATH with encoded characters', () => { - const cwd = path.resolve('cwd with spaces', 'café #%'); + const cwd = path.resolve('special % chars'); const env = {PATH: 'existing-path'}; expect(computeEnv(pathToFileURL(cwd), env)[pathKey]).toBe( diff --git a/src/test/main_test.ts b/src/test/main_test.ts index 15b89a4..d49e06c 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -103,23 +103,19 @@ describe.for(variants)('exec ($name)', ({x, isAsync}) => { 'runs relative shebang commands with a file URL cwd', async () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'tinyexec-url-')); - const dir = path.join(root, 'cwd with spaces café #%'); - const previousCwd = process.cwd(); try { - fs.mkdirSync(dir); fs.writeFileSync( - path.join(dir, 'print-cwd'), + path.join(root, 'print-cwd'), '#!/usr/bin/env node\nprocess.stdout.write(process.cwd());\n' ); const result = await x('./print-cwd', [], { - nodeOptions: {cwd: pathToFileURL(dir)}, + nodeOptions: {cwd: pathToFileURL(root)}, throwOnError: true }); - expect(result.stdout).toBe(dir); - expect(process.cwd()).toBe(previousCwd); + expect(result.stdout).toBe(root); } finally { fs.rmSync(root, {recursive: true, force: true}); } @@ -130,8 +126,7 @@ describe.for(variants)('exec ($name)', ({x, isAsync}) => { 'finds local node binaries with a file URL cwd', async () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'tinyexec-url-bin-')); - const dir = path.join(root, 'cwd with spaces café #%'); - const bin = path.join(dir, 'node_modules', '.bin'); + const bin = path.join(root, 'node_modules', '.bin'); try { fs.mkdirSync(bin, {recursive: true}); @@ -141,7 +136,7 @@ describe.for(variants)('exec ($name)', ({x, isAsync}) => { ); const result = await x('cwd-url-test', [], { - nodeOptions: {cwd: pathToFileURL(dir)}, + nodeOptions: {cwd: pathToFileURL(root)}, throwOnError: true });