From fa304ca4dcade8c63a9f3eb400c0627620b85ebe Mon Sep 17 00:00:00 2001 From: yunyingk Date: Mon, 12 Oct 2026 12:27:46 +0800 Subject: [PATCH] Keep copied source packages out of parent Git repositories --- src/cli/commands/homebrew-skill.test.ts | 11 ++++++++++- src/cli/commands/update.ts | 6 +++++- src/version.test.ts | 12 +++++++++++- src/version.ts | 8 +++++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/cli/commands/homebrew-skill.test.ts b/src/cli/commands/homebrew-skill.test.ts index 30cfc1a..475011c 100644 --- a/src/cli/commands/homebrew-skill.test.ts +++ b/src/cli/commands/homebrew-skill.test.ts @@ -2,9 +2,18 @@ import { describe, expect, test } from "bun:test"; import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { homebrewFormulaPrefix, installHomebrewSkill } from "./update.js"; +import { handleUpdateCommand, homebrewFormulaPrefix, installHomebrewSkill } from "./update.js"; describe("Homebrew skill refresh", () => { + test("a copied source package cannot update its parent application's Git repository", async () => { + const dir = mkdtempSync(join(tmpdir(), "seal-source-update-test-")); + try { + await expect(handleUpdateCommand({ appRoot: dir, entryFile: join(dir, "src", "cli.ts") })) + .rejects.toMatchObject({ exitCode: 13 }); + expect(existsSync(join(dir, "bun.lock"))).toBe(false); + } finally { rmSync(dir, { recursive: true, force: true }); } + }); + test("uses the upgraded formula executable even when PATH contains an older standalone CLI", () => { const dir = mkdtempSync(join(tmpdir(), "seal-brew-skill-test-")); const originalPath = process.env.PATH; diff --git a/src/cli/commands/update.ts b/src/cli/commands/update.ts index e430b73..025c1bf 100644 --- a/src/cli/commands/update.ts +++ b/src/cli/commands/update.ts @@ -1,4 +1,4 @@ -import { chmodSync, mkdirSync, readdirSync, realpathSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs"; +import { chmodSync, existsSync, mkdirSync, readdirSync, realpathSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs"; import { basename, dirname, isAbsolute, join, relative } from "node:path"; import { spawn, spawnSync } from "node:child_process"; import { isCompiledBinary } from "../../core/runtime.js"; @@ -131,6 +131,10 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp return; } + // Source updates must operate on this package's checkout, never a parent app's repository. + if (!existsSync(join(runtime.appRoot, ".git"))) { + throw validationError("当前源码安装目录不是 seal-use 的 Git 工作区。请使用发行版二进制,或从 seal-use 仓库通过 npm link 安装。"); + } try { runCommand("git", ["-C", runtime.appRoot, "pull", "--ff-only"]); } catch (err) { diff --git a/src/version.test.ts b/src/version.test.ts index c12d51f..0f575b8 100644 --- a/src/version.test.ts +++ b/src/version.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test"; import { spawnSync } from "node:child_process"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { formatDevVersion, parseGitDescribe, resolveVersionInfo } from "./version.js"; @@ -84,6 +84,16 @@ describe("resolveVersionInfo configuration modes", () => { expect(resolveVersionInfo({ env: emptyBuildEnv })).toEqual(expected); // Explicit cwd remains available for callers/tests that intentionally inspect another checkout. expect(resolveVersionInfo({ env: emptyBuildEnv, cwd: dir }).git?.baseVersion).toBe("99.99.99"); + const copy = join(dir, "node_modules", "seal-use"); + mkdirSync(join(copy, "src"), { recursive: true }); + copyFileSync(join(repoRoot, "src", "version.ts"), join(copy, "src", "version.ts")); + copyFileSync(join(repoRoot, "package.json"), join(copy, "package.json")); + const child = spawnSync(process.execPath, ["-e", `import { VERSION_INFO } from ${JSON.stringify(join(copy, "src", "version.ts"))}; console.log(JSON.stringify(VERSION_INFO))`], { + cwd: dir, encoding: "utf8", env: { ...process.env, ...emptyBuildEnv } + }); + expect(child.status).toBe(0); + expect(JSON.parse(child.stdout)).toMatchObject({ version: expect.stringContaining("+local"), channel: "dev" }); + expect(JSON.parse(child.stdout).git).toBeUndefined(); } finally { process.chdir(originalCwd); rmSync(dir, { recursive: true, force: true }); diff --git a/src/version.ts b/src/version.ts index 5da6db7..7d3b442 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,6 +1,7 @@ import { spawnSync } from "node:child_process"; -import { dirname, resolve } from "node:path"; +import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; +import { existsSync } from "node:fs"; import packageJson from "../package.json" with { type: "json" }; export interface GitBuildInfo { @@ -124,6 +125,11 @@ export function resolveVersionInfo(options?: { // 3. Dynamic resolution from git repository in dev mode try { + // A copied npm package has no checkout. Git would otherwise walk up into + // the consuming application's repository and report its version as ours. + if (options?.cwd === undefined && !existsSync(join(cwd, ".git"))) { + return { name: "seal-use", version: `${packageJson.version}-dev.0+local`, channel: "dev", isRelease: false }; + } const res = spawnSync("git", ["describe", "--tags", "--long", "--dirty", "--always"], { cwd, encoding: "utf8",