mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
* fix(cli): one trusted PATH rule for the engine and CLI; say why a project copy was skipped The Windows PATH rules (quoted entries, the %SystemRoot% folders) now live in the parsers' trustedPathDirs, so engine helper lookups and the CLI's PATH rewrite apply the same rule. When sam, aws, gcloud, docker, git or npx is not found but a copy sits in a project folder the rule skipped, the message names that copy by its path relative to the project and says why it was not used. cloudrun now reports a missing gcloud or docker as not found instead of "exited with null". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(cli): join a simulated Windows PATH with the Windows separator The shared trusted-folder rule now splits PATH on ';' whenever the platform is win32, as Windows does. Two tests that pretend to be Windows on Linux built PATH with ':', so the rule saw one folder instead of two. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): name skipped copies above the working folder; pin Windows PATH to the shared rule The not-found hint now names any copy the trusted PATH rule skipped, relative to the working folder, including ../node_modules/.bin when an npm or pnpm script runs from a subfolder. A Windows test pins that quoted PATH entries still go through the shared rule, so the project root's and ancestors' node_modules/.bin stay skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(cli): trim projectCopyHint comments to the package's comment share Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
197 lines
6.9 KiB
TypeScript
197 lines
6.9 KiB
TypeScript
import { spawnSync } from "node:child_process";
|
|
import {
|
|
existsSync,
|
|
mkdirSync,
|
|
mkdtempSync,
|
|
readFileSync,
|
|
realpathSync,
|
|
rmSync,
|
|
writeFileSync,
|
|
} from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { delimiter, dirname, isAbsolute, join, win32 } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { describe, expect, it, onTestFinished, vi } from "vitest";
|
|
import {
|
|
searchOnlyTrustedFolders,
|
|
skipWorkingDirectoryInExeSearch,
|
|
withOnlyTrustedPathFolders,
|
|
} from "./windowsExeSearch.js";
|
|
|
|
function tempDir(prefix: string): string {
|
|
const dir = realpathSync(mkdtempSync(join(tmpdir(), prefix)));
|
|
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
|
|
return dir;
|
|
}
|
|
|
|
function inProject(): { project: string; outside: string[] } {
|
|
const project = tempDir("hf-exe-search-project-");
|
|
const outside = [tempDir("hf-exe-search-a-"), tempDir("hf-exe-search-b-")];
|
|
mkdirSync(join(project, "node_modules", ".bin"), { recursive: true });
|
|
vi.spyOn(process, "cwd").mockReturnValue(project);
|
|
onTestFinished(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
return { project, outside };
|
|
}
|
|
|
|
describe("skipWorkingDirectoryInExeSearch", () => {
|
|
it("turns off the working-directory lookup on Windows", () => {
|
|
const env: NodeJS.ProcessEnv = {};
|
|
skipWorkingDirectoryInExeSearch(env, "win32");
|
|
expect(env.NoDefaultCurrentDirectoryInExePath).toBe("1");
|
|
});
|
|
|
|
it("keeps a value the user already set", () => {
|
|
const env: NodeJS.ProcessEnv = { NoDefaultCurrentDirectoryInExePath: "yes" };
|
|
skipWorkingDirectoryInExeSearch(env, "win32");
|
|
expect(env.NoDefaultCurrentDirectoryInExePath).toBe("yes");
|
|
});
|
|
|
|
it("leaves other platforms alone", () => {
|
|
const env: NodeJS.ProcessEnv = {};
|
|
skipWorkingDirectoryInExeSearch(env, "darwin");
|
|
expect(env).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("searchOnlyTrustedFolders", () => {
|
|
it.each(["linux", "darwin", "win32"] as const)(
|
|
"keeps only absolute PATH folders outside the project, in order, on %s",
|
|
(platform) => {
|
|
const { project, outside } = inProject();
|
|
const separator = platform === "win32" ? win32.delimiter : delimiter;
|
|
const env: NodeJS.ProcessEnv = {
|
|
PATH: [
|
|
"",
|
|
".",
|
|
join("node_modules", ".bin"),
|
|
outside[0],
|
|
join(project, "node_modules", ".bin"),
|
|
project,
|
|
outside[1],
|
|
].join(separator),
|
|
};
|
|
|
|
searchOnlyTrustedFolders(env, platform);
|
|
|
|
expect(env.PATH).toBe([outside[0], outside[1]].join(separator));
|
|
},
|
|
);
|
|
|
|
// fails without the fix: the project root's node_modules/.bin that npx adds stayed on PATH when
|
|
// the CLI ran from a subfolder, because only folders inside the working directory were dropped.
|
|
it.each(["linux", "darwin", "win32"] as const)(
|
|
"drops the project root's node_modules/.bin when run from a subfolder, on %s",
|
|
(platform) => {
|
|
const { project, outside } = inProject();
|
|
const sub = join(project, "video");
|
|
mkdirSync(join(sub, "node_modules", ".bin"), { recursive: true });
|
|
// The temp folder is home, so a marker in a folder above it cannot move the project root.
|
|
vi.stubEnv("HOME", dirname(project));
|
|
vi.stubEnv("USERPROFILE", dirname(project));
|
|
onTestFinished(() => {
|
|
vi.unstubAllEnvs();
|
|
});
|
|
vi.spyOn(process, "cwd").mockReturnValue(sub);
|
|
const separator = platform === "win32" ? win32.delimiter : delimiter;
|
|
const env: NodeJS.ProcessEnv = {
|
|
PATH: [
|
|
join(sub, "node_modules", ".bin"),
|
|
join(project, "node_modules", ".bin"),
|
|
join(dirname(project), "node_modules", ".bin"),
|
|
outside[0],
|
|
].join(separator),
|
|
};
|
|
|
|
searchOnlyTrustedFolders(env, platform);
|
|
|
|
expect(env.PATH).toBe(outside[0]);
|
|
},
|
|
);
|
|
|
|
it("finds the PATH key whatever its case, as Windows spells it Path", () => {
|
|
const { project, outside } = inProject();
|
|
const env: NodeJS.ProcessEnv = { Path: [project, outside[0], "."].join(win32.delimiter) };
|
|
|
|
searchOnlyTrustedFolders(env, "win32");
|
|
|
|
expect(env).toEqual({ Path: outside[0], NoDefaultCurrentDirectoryInExePath: "1" });
|
|
});
|
|
|
|
it("accepts a Windows PATH entry wrapped in double quotes", () => {
|
|
const { project, outside } = inProject();
|
|
const env: NodeJS.ProcessEnv = {
|
|
Path: [`"${outside[0]}"`, `"${project}"`, outside[1]].join(win32.delimiter),
|
|
};
|
|
|
|
expect(withOnlyTrustedPathFolders(env, "win32").Path).toBe(
|
|
[outside[0], outside[1]].join(win32.delimiter),
|
|
);
|
|
});
|
|
|
|
it("never leaves an empty PATH, which would mean the working directory", () => {
|
|
const { project } = inProject();
|
|
const env: NodeJS.ProcessEnv = { PATH: ["", ".", project].join(delimiter) };
|
|
|
|
searchOnlyTrustedFolders(env, "linux");
|
|
|
|
expect(env.PATH?.split(delimiter)).toHaveLength(1);
|
|
expect(isAbsolute(env.PATH ?? "")).toBe(true);
|
|
expect(existsSync(env.PATH ?? "")).toBe(false);
|
|
expect(env.PATH?.startsWith(project)).toBe(false);
|
|
});
|
|
|
|
it.skipIf(process.platform === "win32")(
|
|
"a helper started by bare name never runs a same-named file from the project folder",
|
|
() => {
|
|
const { project, outside } = inProject();
|
|
const marker = join(outside[1]!, "ran");
|
|
mkdirSync(join(project, "bin"));
|
|
for (const dir of [project, join(project, "bin")]) {
|
|
writeFileSync(join(dir, "hf-planted-helper"), `#!/bin/sh\necho ran > "${marker}"\n`, {
|
|
mode: 0o755,
|
|
});
|
|
}
|
|
const raw: NodeJS.ProcessEnv = { PATH: [".", "", "bin", outside[0]].join(delimiter) };
|
|
const run = (env: NodeJS.ProcessEnv) =>
|
|
spawnSync("hf-planted-helper", [], { cwd: project, env, stdio: "ignore" });
|
|
|
|
expect(run(raw).status).toBe(0);
|
|
expect(existsSync(marker)).toBe(true);
|
|
rmSync(marker);
|
|
|
|
const env = { ...raw };
|
|
searchOnlyTrustedFolders(env, process.platform);
|
|
|
|
expect((run(env).error as NodeJS.ErrnoException | undefined)?.code).toBe("ENOENT");
|
|
expect(existsSync(marker)).toBe(false);
|
|
},
|
|
);
|
|
|
|
it("adds no PATH when there was none", () => {
|
|
inProject();
|
|
const env: NodeJS.ProcessEnv = {};
|
|
|
|
searchOnlyTrustedFolders(env, "linux");
|
|
|
|
expect(env).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("program entries", () => {
|
|
it("each one that starts its own process turns the lookup off before it spawns anything", () => {
|
|
const cliRoot = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
|
const tsup = readFileSync(join(cliRoot, "tsup.config.ts"), "utf8");
|
|
const entries = [...tsup.matchAll(/^\s+\w+: "(src\/[^"]+\.ts)",$/gm)].map((m) => m[1]!);
|
|
const programs = entries.filter((entry) =>
|
|
readFileSync(join(cliRoot, entry), "utf8").includes('hideChildConsoles.js";'),
|
|
);
|
|
expect(programs).toContain("src/warmCheck.ts");
|
|
const missing = programs.filter(
|
|
(entry) => !readFileSync(join(cliRoot, entry), "utf8").includes('windowsExeSearch.js";'),
|
|
);
|
|
expect(missing).toEqual([]);
|
|
});
|
|
});
|