Skip to content

Commit 16cfff0

Browse files
authored
fix(infra): preserve package paths from root cwd
1 parent d823cb6 commit 16cfff0

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/infra/path-env.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,30 @@ describe("ensureOpenClawCliOnPath", () => {
356356
expectPathsAfter(updated, "/usr/bin", [pnpmHome, pnpmHomeBin, npmPrefixBin]);
357357
});
358358

359+
it("keeps package-manager env roots when cwd is the filesystem root", () => {
360+
const { tmp, appCli } = setupAppCliRoot("case-package-manager-root-cwd");
361+
const pnpmHome = path.join(tmp, "pnpm-home");
362+
const pnpmHomeBin = path.join(pnpmHome, "bin");
363+
const npmPrefix = path.join(tmp, "npm-prefix");
364+
const npmPrefixBin = path.join(npmPrefix, "bin");
365+
for (const dir of [pnpmHome, pnpmHomeBin, npmPrefix, npmPrefixBin]) {
366+
setDir(dir);
367+
}
368+
369+
resetBootstrapEnv("/usr/bin:/bin");
370+
process.env.PNPM_HOME = pnpmHome;
371+
process.env.NPM_CONFIG_PREFIX = npmPrefix;
372+
373+
const updated = bootstrapPath({
374+
execPath: appCli,
375+
cwd: path.parse(tmp).root,
376+
homeDir: tmp,
377+
platform: "linux",
378+
});
379+
380+
expectPathsAfter(updated, "/usr/bin", [pnpmHome, pnpmHomeBin, npmPrefixBin]);
381+
});
382+
359383
it("ignores relative package-manager env roots", () => {
360384
const { tmp, appCli } = setupAppCliRoot("case-package-manager-relative");
361385
resetBootstrapEnv("/usr/bin:/bin");

src/infra/path-env.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ function isSameOrChildPath(candidate: string, parent: string): boolean {
7272
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
7373
}
7474

75+
function isFilesystemRoot(dirPath: string): boolean {
76+
return path.dirname(dirPath) === dirPath;
77+
}
78+
7579
function normalizeTrustedPackageManagerRoot(params: {
7680
value: string | undefined;
7781
cwd: string | undefined;
@@ -91,7 +95,7 @@ function normalizeTrustedPackageManagerRoot(params: {
9195

9296
const cwd = path.resolve(params.cwd);
9397
const homeDir = path.resolve(params.homeDir);
94-
if (cwd === homeDir) {
98+
if (cwd === homeDir || isFilesystemRoot(cwd)) {
9599
return normalized;
96100
}
97101
if (isSameOrChildPath(normalized, cwd)) {
@@ -104,6 +108,7 @@ function normalizeTrustedPackageManagerRoot(params: {
104108
if (
105109
realCwd &&
106110
realCwd !== realHome &&
111+
!isFilesystemRoot(realCwd) &&
107112
realCandidate &&
108113
isSameOrChildPath(realCandidate, realCwd)
109114
) {

0 commit comments

Comments
 (0)