fix(process): resolve Windows root-relative commands on the selected drive (#11763)

* fix(process): resolve Windows root-relative commands on the selected drive

* test(process): pin drive-less PATH entries and UNC root-relative commands

A Windows PATH entry rooted without a drive now counts as relative, so
programOnPath skips it. Pin that with a Windows case, test a
root-relative command under a UNC working directory, and say in the
relativeEntry and windowsPathCandidates docs that drive-less entries
count as relative.

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
RRFZandmorluto authored and GitHub committed 2026-11-18 10:23:14 -05:00
1 parent 23adfc610f
commit 5aa1fc5737
3 files changed
+68 -11

No files matched your search

+26 -2
View File
@@ -1,11 +1,14 @@
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { expect, it, onTestFinished, vi } from "vitest";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import { PRODUCT_IDENTITY } from "../identity.js";
import { resolveBuildComputerEnvironment } from "./BuildComputerEnvironment.js";
import {
programOnPath,
resolveBuildComputerEnvironment,
} from "./BuildComputerEnvironment.js";
const host = async () => {
const root = await createTestTempDirectory("rea-build-environment-");
@@ -29,6 +32,27 @@ const host = async () => {
return { bin, archive, environment };
};
it.runIf(process.platform === "win32")(
"skips a Windows PATH entry without a drive, which follows the current drive",
async () => {
const root = await createTestTempDirectory("rea-build-program-drive-");
const bin = join(root, "bin");
await mkdir(bin);
await writeFile(join(bin, "agent-env.exe"), "");
// On the same drive, `\...\bin` names the same existing file.
const cwd = vi.spyOn(process, "cwd").mockReturnValue(root);
onTestFinished(() => cwd.mockRestore());
const driveless = bin.slice(2);
expect(await programOnPath("agent-env", { PATH: driveless })).toBe(
undefined,
);
expect(await programOnPath("agent-env", { PATH: bin })).toBe(
join(bin, "agent-env.EXE"),
);
},
);
it.skipIf(process.platform === "win32")(
"resolves every build setting on a host that exports none of them",
async () => {
+27
View File
@@ -142,6 +142,33 @@ describe("windowsPathCandidates", () => {
expect(paths("npm.cmd", environment, { cwd: "C:\\" })).toEqual([]);
});
it("uses the selected working drive for a root-relative command", () => {
expect(
paths("\\bin\\tool", { PATHEXT: ".EXE" }, { cwd: "Z:\\work" }),
).toEqual(["Z:\\bin\\tool.EXE"]);
// A UNC working directory roots it at the share, as Win32 does.
expect(
paths(
"\\bin\\tool",
{ PATHEXT: ".EXE" },
{ cwd: "\\\\server\\share\\work" },
),
).toEqual(["\\\\server\\share\\bin\\tool.EXE"]);
});
it("marks a PATH entry without a drive as relative to the working drive", () => {
const search = windowsPathCandidates(
"tool.exe",
{ PATH: "\\bin;D:\\tools" },
{ cwd: "Z:\\work" },
);
if (!search.ok) throw new Error(search.error.extension);
expect([...search.value]).toEqual([
{ path: "Z:\\bin\\tool.exe", relativeEntry: true },
{ path: "D:\\tools\\tool.exe", relativeEntry: false },
]);
});
it("tries every launcher name in an entry before the next entry", () => {
expect(
paths(
+15 -9
View File
@@ -3,6 +3,7 @@ import { posix, win32 } from "node:path";
import {
absoluteCallerPath,
appendCallerPath,
isQualifiedCallerPath,
} from "../filesystem/callerPath.js";
import { err, ok, type Result } from "../domain/result.js";
import { snapshotEnvironment } from "./snapshotEnvironment.js";
@@ -11,7 +12,10 @@ import { snapshotEnvironment } from "./snapshotEnvironment.js";
export interface PathSearchCandidate {
/** The absolute file the launch would execute from this entry. */
readonly path: string;
/** An empty or relative PATH entry, which depends on the working directory. */
/**
* An empty or relative PATH entry, or on Windows a rooted entry without a
* drive (`\tools`), which depends on the working directory or its drive.
*/
readonly relativeEntry: boolean;
}
@@ -118,9 +122,10 @@ export interface WindowsPathSearchFailure {
* The files Windows' PATH/PATHEXT search tries for commands, in order: a
* path-qualified command (`C:\Python\python`, `.venv\Scripts\python`) only at
* its own location, resolved against `cwd`, and never on PATH; then for each
* PATH entry (relative ones resolve against `cwd`, or against this process's
* directory, read only when such an entry is reached and skipped once
* removed), each bare command with every PATHEXT extension,
* PATH entry (relative ones, including a rooted entry without a drive such as
* `\tools`, resolve against `cwd`, or against this process's directory, read
* only when such an entry is reached and skipped once removed; they are
* marked `relativeEntry`), each bare command with every PATHEXT extension,
* or as written when it already names an extension. Only `.com` and `.exe`
* images are searched unless `allowBatch`; a `.bat` or `.cmd` launcher must
* then be started through src/process/WindowsBatchCommand. An invalid
@@ -166,7 +171,7 @@ export const windowsPathCandidates = (
return ok({
*[Symbol.iterator]() {
for (const name of located) {
const workingDirectory = win32.isAbsolute(name)
const workingDirectory = isQualifiedCallerPath(name, "win32")
? name
: (search.cwd ?? currentDirectory());
if (workingDirectory === undefined) continue;
@@ -179,14 +184,15 @@ export const windowsPathCandidates = (
for (const directory of entries) {
// Only an entry without a drive or UNC root reads the working
// directory; a removed one holds nothing, and later entries count.
const workingDirectory = /^(?:[A-Za-z]:[\\/]|[\\/]{2})/u.test(directory)
? directory
: (search.cwd ?? currentDirectory());
const relativeEntry = !isQualifiedCallerPath(directory, "win32");
const workingDirectory = relativeEntry
? (search.cwd ?? currentDirectory())
: directory;
if (workingDirectory === undefined) continue;
for (const name of names)
yield {
path: win32.resolve(workingDirectory, directory, name),
relativeEntry: !win32.isAbsolute(directory),
relativeEntry,
};
}
},