diff --git a/cli/src/commands/flow/flow.ts b/cli/src/commands/flow/flow.ts index 956f2ec5db..743049933f 100644 --- a/cli/src/commands/flow/flow.ts +++ b/cli/src/commands/flow/flow.ts @@ -39,7 +39,7 @@ import { import { generateFlowLockInternal } from "./flow_metadata.ts"; import { exts } from "../script/script.ts"; import type { SyncCodebase } from "../../utils/codebase.ts"; -import { listSyncCodebases } from "../../utils/codebase.ts"; +import { listSyncCodebases, sameCodebaseDigest } from "../../utils/codebase.ts"; import { createPreviewLocalScriptReader, resolvePreviewLocalScriptState, @@ -106,7 +106,7 @@ async function findDivergedLocalPathScripts( normalizeComparableContent(localScript.lock) !== normalizeComparableContent(remoteLock)) || localScript.tag !== normalizeOptionalString(remoteScript.tag) || - localScript.codebaseDigest !== normalizeOptionalString(remoteScript.codebase); + !sameCodebaseDigest(localScript.codebaseDigest, remoteScript.codebase); if (diverged) { changed.push(scriptPath); diff --git a/cli/src/commands/sync/sync.ts b/cli/src/commands/sync/sync.ts index f4962d3a65..2f28b63ffd 100644 --- a/cli/src/commands/sync/sync.ts +++ b/cli/src/commands/sync/sync.ts @@ -123,7 +123,11 @@ import { } from "../../utils/git.ts"; import { Workspace } from "../workspace/workspace.ts"; import { removePathPrefix } from "../../types.ts"; -import { listSyncCodebases, SyncCodebase } from "../../utils/codebase.ts"; +import { + listSyncCodebases, + sameCodebaseDigest, + SyncCodebase, +} from "../../utils/codebase.ts"; import { beginLockfileBatch, flushLockfileBatch, @@ -567,6 +571,17 @@ export function findCodebase( } } +/** Script metadata that differs only by the `.tar` a multi-output bundle adds to the + * deployed codebase digest. */ +function sameScriptMetadataModuloTar(a: any, b: any): boolean { + if (!a || !b || typeof a != "object" || typeof b != "object") { + return false; + } + const { codebase: c1, ...rest1 } = a; + const { codebase: c2, ...rest2 } = b; + return sameCodebaseDigest(c1, c2) && deepEqual(rest1, rest2); +} + async function addCodebaseDigestIfRelevant( path: string, content: string, @@ -2942,7 +2957,10 @@ export async function compareDynFSElement( if (isEls1Remote === false && isWorkspaceSettingsFile(k)) { stripUnappliedSettingsFields(after, before); } - if (deepEqual(before, after)) { + if ( + deepEqual(before, after) || + (isScriptMetadata && sameScriptMetadataModuloTar(before, after)) + ) { continue; } if (!ignoreCodebaseChanges) { @@ -2956,7 +2974,7 @@ export async function compareDynFSElement( delete after.codebase; v = yamlStringify(after, yamlOptions); } - if (beforeCodebase != afterCodebase) { + if (!sameCodebaseDigest(beforeCodebase, afterCodebase)) { codebaseChanges[k] = afterCodebase ?? beforeCodebase ?? ""; } } @@ -3008,7 +3026,7 @@ export async function compareDynFSElement( continue; } const c = findCodebase(tsFile, codebases); - if ((await c?.getDigest()) != v) { + if (!sameCodebaseDigest(await c?.getDigest(), v)) { changes.push({ name: "edited", path: tsFile, diff --git a/cli/src/utils/codebase.ts b/cli/src/utils/codebase.ts index 2eb63caeec..debdf012bb 100644 --- a/cli/src/utils/codebase.ts +++ b/cli/src/utils/codebase.ts @@ -43,6 +43,20 @@ export function uncoveredBundleInputs( export type SyncCodebase = Codebase & { getDigest: (forceTar?: boolean) => Promise; }; + +/** Whether two codebase digests name the same bundle. A push adds `.tar` when esbuild emits + * more than one output file, which only bundling reveals, so a diff computing `getDigest()` + * must accept the deployed digest with that suffix. The layout follows from the sources and + * the codebase config, both already in the digest. */ +export function sameCodebaseDigest( + a: string | null | undefined, + b: string | null | undefined +): boolean { + const base = (d: string | null | undefined) => + d ? (d.endsWith(".tar") ? d.slice(0, -4) : d) : undefined; + return base(a) === base(b); +} + export function listSyncCodebases(options: SyncOptions): SyncCodebase[] { const res: SyncCodebase[] = []; const nb_codebase = options?.codebases?.length ?? 0; diff --git a/cli/test/push_diff_convergence_unit.test.ts b/cli/test/push_diff_convergence_unit.test.ts index fd20f0d263..8fe1bf484c 100644 --- a/cli/test/push_diff_convergence_unit.test.ts +++ b/cli/test/push_diff_convergence_unit.test.ts @@ -495,3 +495,28 @@ test("push: an unchanged custom_path is not sent", () => { expect(changedCustomPath("", { custom_path: "my-dashboard" })).toBe(""); expect(changedCustomPath("new", undefined)).toBe("new"); }); + +const SCRIPT_META = (extra: string) => + `summary: s\ndescription: ''\nlock: ''\nkind: script\n${extra}`; +const EXPANDED_EMPTY_SCHEMA = + "schema:\n $schema: https://json-schema.org/draft/2020-12/schema\n type: object\n properties: {}\n required: []\n"; + +test("push: a multi-output bundle's `.tar` digest matches the local codebase digest", async () => { + const remote = local({ + "f/c/multi.script.yaml": SCRIPT_META(`codebase: abc123.tar\n${EXPANDED_EMPTY_SCHEMA}`), + }); + expect( + await diff( + local({ "f/c/multi.script.yaml": SCRIPT_META(`codebase: abc123\n${EXPANDED_EMPTY_SCHEMA}`) }), + remote, + {}, + ), + ).toEqual([]); + expect( + await diff( + local({ "f/c/multi.script.yaml": SCRIPT_META(`codebase: def456\n${EXPANDED_EMPTY_SCHEMA}`) }), + remote, + {}, + ), + ).toEqual(["edited f/c/multi.script.yaml"]); +});