From bfadf5b2b32814c36ada261428fb4e2d9c1317f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 24 Nov 2026 08:31:39 +0000 Subject: [PATCH] fix(cli): lambda deploy and destroy use only the checkout the CLI itself is in (#8664) A folder above the CLI's own package that holds packages/aws-lambda no longer counts as a checkout, and a source checkout is named before it is built or deployed from. --- .../cli/src/commands/lambda/deploy.test.ts | 3 + packages/cli/src/commands/lambda/deploy.ts | 1 + .../cli/src/commands/lambda/destroy.test.ts | 16 ++++- packages/cli/src/commands/lambda/destroy.ts | 1 + .../cli/src/commands/lambda/repoRoot.test.ts | 69 ++++++++++++++++++- packages/cli/src/commands/lambda/repoRoot.ts | 51 ++++++++++---- 6 files changed, 122 insertions(+), 19 deletions(-) diff --git a/packages/cli/src/commands/lambda/deploy.test.ts b/packages/cli/src/commands/lambda/deploy.test.ts index baddc13524..de49e52c46 100644 --- a/packages/cli/src/commands/lambda/deploy.test.ts +++ b/packages/cli/src/commands/lambda/deploy.test.ts @@ -69,6 +69,9 @@ describe("runDeploy", () => { await runDeploy({ skipBuild: true, videoArch: "x86_64" }); expect(vi.mocked(samDeploy).mock.calls[0]?.[0]).toMatchObject({ samDir }); + expect(vi.mocked(console.log).mock.calls.flat().join("\n")).toContain( + `Using the source checkout at ${dir}`, + ); expect(vi.mocked(samDeploy).mock.calls[0]?.[0].videoImageUri).toBeUndefined(); expect(pushVideoImage).not.toHaveBeenCalled(); }); diff --git a/packages/cli/src/commands/lambda/deploy.ts b/packages/cli/src/commands/lambda/deploy.ts index 375bd72662..84125ff964 100644 --- a/packages/cli/src/commands/lambda/deploy.ts +++ b/packages/cli/src/commands/lambda/deploy.ts @@ -153,6 +153,7 @@ function warmVideoFunction(functionName: string | undefined, args: DeployArgs): /** Checkout: build the ZIP from source. Otherwise: stage the adapter's pinned prebuilt ZIP. */ async function prepareSamDir(root: string | undefined, args: DeployArgs): Promise { if (!root) return stagePrebuiltHandler(); + console.log(c.dim(terminalText(`→ Using the source checkout at ${root}`))); const samDir = dirname(locateSamTemplate(root)); if (!args.skipBuild) { console.log(c.dim("→ Building handler ZIP")); diff --git a/packages/cli/src/commands/lambda/destroy.test.ts b/packages/cli/src/commands/lambda/destroy.test.ts index dc2fcc6089..4f0d27a6c2 100644 --- a/packages/cli/src/commands/lambda/destroy.test.ts +++ b/packages/cli/src/commands/lambda/destroy.test.ts @@ -5,7 +5,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { stateFilePath, writeStackOutputs } from "./state.js"; const samDirs = vi.hoisted(() => [] as string[]); -vi.mock("./repoRoot.js", () => ({ repoRoot: () => undefined })); +const root = vi.hoisted(() => ({ value: undefined as string | undefined })); +vi.mock("./repoRoot.js", () => ({ repoRoot: () => root.value })); vi.mock("./sam.js", () => ({ samDelete: vi.fn((opts: { samDir: string }) => { samDirs.push(opts.samDir); @@ -36,6 +37,7 @@ beforeEach(() => { afterEach(() => { quiet.mockRestore(); + root.value = undefined; process.chdir(previousCwd); rmSync(cwd, { recursive: true, force: true }); }); @@ -62,6 +64,18 @@ describe("runDestroy", () => { expect(existsSync(stateFilePath("Demo", cwd))).toBe(false); }); + it("in a source checkout, says which checkout it deletes from and runs sam there", async () => { + writeStackOutputs(stack, cwd); + root.value = cwd; + + await runDestroy({ stackName: "Demo" }); + + expect(samDirs).toEqual([join(cwd, "examples", "aws-lambda")]); + expect(vi.mocked(console.log).mock.calls.flat().join("\n")).toContain( + `Using the source checkout at ${cwd}`, + ); + }); + it("deletes the stack's own video repository, whatever name the state file records", async () => { writeStackOutputs({ ...stack, videoRepository: "someone-elses-repo" }, cwd); diff --git a/packages/cli/src/commands/lambda/destroy.ts b/packages/cli/src/commands/lambda/destroy.ts index 821655a647..5e54b1010b 100644 --- a/packages/cli/src/commands/lambda/destroy.ts +++ b/packages/cli/src/commands/lambda/destroy.ts @@ -30,6 +30,7 @@ export async function runDestroy(args: DestroyArgs): Promise { c.dim(terminalText(`→ sam delete (stack=${stack.stackName} region=${stack.region})`)), ); const root = repoRoot(); + if (root) console.log(c.dim(terminalText(`→ Using the source checkout at ${root}`))); const awsProfile = args.awsProfile ?? process.env.AWS_PROFILE; // Mirror deploy.ts's AWS_PROFILE env fallback — `AWS_PROFILE=prod // hyperframes lambda destroy` should hit the same account `deploy` diff --git a/packages/cli/src/commands/lambda/repoRoot.test.ts b/packages/cli/src/commands/lambda/repoRoot.test.ts index 9642c69b89..fc915e0f6f 100644 --- a/packages/cli/src/commands/lambda/repoRoot.test.ts +++ b/packages/cli/src/commands/lambda/repoRoot.test.ts @@ -1,6 +1,7 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { dirname, join, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; import { afterEach, describe, expect, it, vi } from "vitest"; import { repoRoot } from "./repoRoot.js"; @@ -17,10 +18,20 @@ describe("repoRoot", () => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); + const writePackage = (dir: string, name: string) => { + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, "package.json"), JSON.stringify({ name })); + }; + // A tree that looks like a checkout: the adapter package and its SAM template. + const plantCheckout = (root: string) => { + writePackage(join(root, "packages", "aws-lambda"), "@hyperframes/aws-lambda"); + mkdirSync(join(root, "examples", "aws-lambda"), { recursive: true }); + writeFileSync(join(root, "examples", "aws-lambda", "template.yaml"), "Resources: {}\n"); + }; + it("returns HYPERFRAMES_REPO_ROOT when it is a hyperframes checkout", () => { const root = tempDir(); - mkdirSync(join(root, "packages", "aws-lambda"), { recursive: true }); - writeFileSync(join(root, "packages", "aws-lambda", "package.json"), "{}"); + writePackage(join(root, "packages", "aws-lambda"), "@hyperframes/aws-lambda"); vi.stubEnv("HYPERFRAMES_REPO_ROOT", root); expect(repoRoot()).toBe(root); }); @@ -31,4 +42,56 @@ describe("repoRoot", () => { expect(() => repoRoot()).toThrow("HYPERFRAMES_REPO_ROOT is not a hyperframes checkout"); expect(() => repoRoot()).not.toThrow(root); }); + + it("throws on a HYPERFRAMES_REPO_ROOT whose packages/aws-lambda is another package", () => { + const root = tempDir(); + writePackage(join(root, "packages", "aws-lambda"), "not-hyperframes"); + vi.stubEnv("HYPERFRAMES_REPO_ROOT", root); + expect(() => repoRoot()).toThrow("HYPERFRAMES_REPO_ROOT is not a hyperframes checkout"); + }); + + // fails without the fix: the old walk took the planted folder above the install as the checkout. + it("ignores a checkout planted in a folder above an installed CLI", () => { + const drive = tempDir(); + plantCheckout(drive); + const cli = join(drive, "Users", "me", "npm", "node_modules", "@hyperframes", "cli"); + writePackage(cli, "@hyperframes/cli"); + mkdirSync(join(cli, "dist"), { recursive: true }); + expect(repoRoot(join(cli, "dist"))).toBeUndefined(); + }); + + it("ignores an adapter package two folders above a CLI that is not under packages/", () => { + const drive = tempDir(); + plantCheckout(drive); + const cli = join(drive, "tools", "cli"); + writePackage(cli, "@hyperframes/cli"); + expect(repoRoot(join(cli, "dist"))).toBeUndefined(); + }); + + it("ignores a checkout when this module is bundled into another package", () => { + const root = tempDir(); + plantCheckout(root); + writePackage(join(root, "packages", "app"), "someone-elses-app"); + expect(repoRoot(join(root, "packages", "app", "dist"))).toBeUndefined(); + }); + + it("returns the checkout the CLI's own package sits in", () => { + const root = tempDir(); + plantCheckout(root); + writePackage(join(root, "packages", "cli"), "@hyperframes/cli"); + mkdirSync(join(root, "packages", "cli", "dist"), { recursive: true }); + expect(repoRoot(join(root, "packages", "cli", "dist"))).toBe(root); + }); + + it("returns undefined when the CLI's checkout has no adapter package", () => { + const root = tempDir(); + writePackage(join(root, "packages", "cli"), "@hyperframes/cli"); + writePackage(join(root, "packages", "aws-lambda"), "something-else"); + expect(repoRoot(join(root, "packages", "cli", "src"))).toBeUndefined(); + }); + + it("finds this repository from its own source", () => { + const here = dirname(fileURLToPath(import.meta.url)); + expect(repoRoot()).toBe(resolve(here, "..", "..", "..", "..", "..")); + }); }); diff --git a/packages/cli/src/commands/lambda/repoRoot.ts b/packages/cli/src/commands/lambda/repoRoot.ts index 13de3aac17..ecc5e2f5ea 100644 --- a/packages/cli/src/commands/lambda/repoRoot.ts +++ b/packages/cli/src/commands/lambda/repoRoot.ts @@ -3,32 +3,53 @@ * to find `examples/aws-lambda/template.yaml` and the handler ZIP build * script. * - * Walks up from this file's location until it finds a directory that - * contains `packages/aws-lambda/`. Undefined outside a checkout (the - * published CLI), where deploy uses the adapter's prebuilt handler. + * Only the checkout this CLI itself sits in counts: `/packages/cli` + * must be the running CLI's own package, and `/packages/aws-lambda` + * must be the adapter package. Undefined for a published install, where + * deploy uses the adapter's prebuilt handler. */ -import { existsSync } from "node:fs"; -import { dirname, resolve } from "node:path"; +import { existsSync, readFileSync } from "node:fs"; +import { basename, dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; -export function repoRoot(): string | undefined { +const CLI_PACKAGE = "@hyperframes/cli"; +const LAMBDA_PACKAGE = "@hyperframes/aws-lambda"; + +export function repoRoot( + moduleDir: string = dirname(fileURLToPath(import.meta.url)), +): string | undefined { const override = process.env.HYPERFRAMES_REPO_ROOT; if (override) { - if (existsSync(resolve(override, "packages", "aws-lambda", "package.json"))) return override; + if (packageName(resolve(override, "packages", "aws-lambda")) === LAMBDA_PACKAGE) + return override; throw new Error( "[hyperframes lambda] HYPERFRAMES_REPO_ROOT is not a hyperframes checkout " + - "(no packages/aws-lambda/package.json there). Point it at the repo root, or unset it.", + `(no ${LAMBDA_PACKAGE} package at packages/aws-lambda there). Point it at the repo root, or unset it.`, ); } - let dir = dirname(fileURLToPath(import.meta.url)); - for (let depth = 0; depth < 12; depth++) { - if (existsSync(resolve(dir, "packages", "aws-lambda", "package.json"))) { - return dir; - } + const cliDir = ownPackageDir(moduleDir); + if (!cliDir || packageName(cliDir) !== CLI_PACKAGE) return undefined; + const packagesDir = dirname(cliDir); + if (basename(packagesDir) !== "packages") return undefined; + const root = dirname(packagesDir); + return packageName(join(root, "packages", "aws-lambda")) === LAMBDA_PACKAGE ? root : undefined; +} + +/** The nearest folder at or above `dir` holding a package.json: this module's own package. */ +function ownPackageDir(dir: string): string | undefined { + for (;;) { + if (existsSync(join(dir, "package.json"))) return dir; const parent = dirname(dir); - if (parent === dir) break; + if (parent === dir) return undefined; dir = parent; } - return undefined; +} + +function packageName(dir: string): unknown { + try { + return (JSON.parse(readFileSync(join(dir, "package.json"), "utf8")) as { name?: unknown }).name; + } catch { + return undefined; + } }