From a4847297b85c87b56bfae04abe27668a3ba295ce Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 1 Jun 2026 16:16:21 +0200 Subject: [PATCH] fix(ci): clean check-changed pnpm shim temp dirs --- scripts/check-changed.mjs | 29 ++++++++++++++++++++++++++- test/scripts/changed-lanes.test.ts | 32 +++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/scripts/check-changed.mjs b/scripts/check-changed.mjs index ff894315392..556a271eb7f 100644 --- a/scripts/check-changed.mjs +++ b/scripts/check-changed.mjs @@ -1,4 +1,12 @@ -import { accessSync, chmodSync, constants, existsSync, mkdtempSync, writeFileSync } from "node:fs"; +import { + accessSync, + chmodSync, + constants, + existsSync, + mkdtempSync, + rmSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { performance } from "node:perf_hooks"; @@ -36,6 +44,7 @@ const LINTABLE_CORE_PATH_RE = /^(?:src|ui|packages)\/.+\.[cm]?[jt]sx?$/u; const CORE_LINT_OPTIMIZATION_NEUTRAL_PATH_RE = /^(?:scripts|test\/scripts)\/|^\.github\/workflows\/ci\.yml$/u; let corepackPnpmShimDir; +let corepackPnpmShimCleanupRegistered = false; export function createChangedCheckChildEnv(baseEnv = process.env) { const resolvedBaseEnv = resolveLocalHeavyCheckEnv(baseEnv); @@ -464,9 +473,27 @@ function ensureCorepackPnpmShimDir() { chmodSync(pnpmPath, 0o755); writeFileSync(path.join(dir, "pnpm.cmd"), "@echo off\r\ncorepack pnpm %*\r\n", "utf8"); corepackPnpmShimDir = dir; + registerCorepackPnpmShimCleanup(); return dir; } +function registerCorepackPnpmShimCleanup() { + if (corepackPnpmShimCleanupRegistered) { + return; + } + corepackPnpmShimCleanupRegistered = true; + process.once("exit", cleanupCorepackPnpmShimDir); +} + +export function cleanupCorepackPnpmShimDir() { + if (!corepackPnpmShimDir) { + return; + } + const dir = corepackPnpmShimDir; + corepackPnpmShimDir = undefined; + rmSync(dir, { recursive: true, force: true }); +} + async function runCommand(command, timings) { const startedAt = performance.now(); console.error(`\n[check:changed] ${command.name}`); diff --git a/test/scripts/changed-lanes.test.ts b/test/scripts/changed-lanes.test.ts index 2d8de90b48b..1ff894e4e38 100644 --- a/test/scripts/changed-lanes.test.ts +++ b/test/scripts/changed-lanes.test.ts @@ -1,5 +1,5 @@ import { execFileSync, spawnSync } from "node:child_process"; -import { mkdirSync, unlinkSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, unlinkSync, writeFileSync } from "node:fs"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { @@ -11,6 +11,7 @@ import { } from "../../scripts/changed-lanes.mjs"; import { buildChangedCheckCrabboxArgs, + cleanupCorepackPnpmShimDir, createChangedCheckChildEnv, createChangedCheckPlan, createPnpmManagedCommand, @@ -71,6 +72,7 @@ function parseChangedLaneOutput(output: string): { } afterEach(() => { + cleanupCorepackPnpmShimDir(); cleanupTempDirs(tempDirs); }); @@ -228,14 +230,15 @@ describe("scripts/changed-lanes", () => { mkdirSync(path.join(dir, "src"), { recursive: true }); writeFileSync(path.join(dir, "src", "feature.ts"), "export const value = 1;\n", "utf8"); - const normalPaths = listChangedPathsFromGit({ base: "origin/main", cwd: dir }); - expect(normalPaths.length).toBeGreaterThan(200); - expect(normalPaths).toContain("baseline-0.txt"); - expect(normalPaths).toContain("src/feature.ts"); - const previousRawSync = process.env.OPENCLAW_CHANGED_LANES_RAW_SYNC; - process.env.OPENCLAW_CHANGED_LANES_RAW_SYNC = "1"; + delete process.env.OPENCLAW_CHANGED_LANES_RAW_SYNC; try { + const normalPaths = listChangedPathsFromGit({ base: "origin/main", cwd: dir }); + expect(normalPaths.length).toBeGreaterThan(200); + expect(normalPaths).toContain("baseline-0.txt"); + expect(normalPaths).toContain("src/feature.ts"); + + process.env.OPENCLAW_CHANGED_LANES_RAW_SYNC = "1"; expect(listChangedPathsFromGit({ base: "origin/main", cwd: dir })).toEqual([ "src/feature.ts", ]); @@ -501,6 +504,21 @@ describe("scripts/changed-lanes", () => { expect(command.args).toEqual(["pnpm", "check:no-conflict-markers"]); }); + it("cleans CI Corepack pnpm shim temp dirs", () => { + const command = createPnpmManagedCommand( + { name: "conflict markers", args: ["check:no-conflict-markers"] }, + { CI: "1", PATH: "/usr/bin" }, + ); + const [shimDir] = String(command.env?.PATH ?? "").split(path.delimiter); + + expect(path.basename(shimDir)).toMatch(/^openclaw-corepack-pnpm-/u); + expect(existsSync(path.join(shimDir, "pnpm"))).toBe(true); + + cleanupCorepackPnpmShimDir(); + + expect(existsSync(shimDir)).toBe(false); + }); + it("keeps local changed-check children on the repo pnpm shim", () => { const command = createPnpmManagedCommand( { name: "conflict markers", args: ["check:no-conflict-markers"] },