@intentius/chant 0.124.67 → 0.124.68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,11 @@
3
3
  * the release the environment runs it from.
4
4
  */
5
5
  import { describe, test, expect } from "vitest";
6
- import { compareInputs, unchangedRecord, type ChangedPaths } from "./unchanged-inputs";
6
+ import { spawnSync } from "node:child_process";
7
+ import { mkdirSync, writeFileSync } from "node:fs";
8
+ import { join } from "node:path";
9
+ import { withTestDir } from "@intentius/chant-test-utils";
10
+ import { compareInputs, gitNormalizedCompare, unchangedRecord, type ChangedPaths, type NormalizedCompare } from "./unchanged-inputs";
7
11
  import { makeRelease, type Release } from "../lifecycle/release-store";
8
12
  import type { ReleaseRecord } from "../lifecycle/release-ledger";
9
13
 
@@ -96,3 +100,132 @@ describe("unchangedRecord", () => {
96
100
  });
97
101
  });
98
102
  });
103
+
104
+ describe("compareInputs with inputs.normalize (#4718)", () => {
105
+ const blank = "scripts/release blank {path}";
106
+ const inputs = {
107
+ paths: ["../crates/control", "../Cargo.lock", "../Cargo.toml"],
108
+ normalize: [{ paths: ["../Cargo.lock", "../Cargo.toml"], command: blank }],
109
+ };
110
+ /** git diff over `paths`: the changed files, and of those the ones a normalize entry's `../<file>` names. */
111
+ const changedPaths = (changed: string[]): ChangedPaths =>
112
+ (_from, _to, paths) => (paths === inputs.paths ? changed : changed.filter((f) => paths.includes(`../${f}`)));
113
+ const compareN = (changed: string[], normalizedCompare?: NormalizedCompare, over: { inputs?: typeof inputs } = {}) =>
114
+ compareInputs({
115
+ component: "control",
116
+ inputs: over.inputs ?? inputs,
117
+ env: "prod",
118
+ release: release("b"),
119
+ current: record(),
120
+ readRelease: async () => before,
121
+ changedPaths: changedPaths(changed),
122
+ ...(normalizedCompare ? { normalizedCompare } : {}),
123
+ });
124
+
125
+ test("a file that is the same after its normalization is unchanged, and the verdict names the normalization", async () => {
126
+ const calls: unknown[] = [];
127
+ const verdict = await compareN(["Cargo.lock", "Cargo.toml"], (command, path, from, to) => (calls.push([command, path, from, to]), { same: true }));
128
+ expect(verdict).toEqual({
129
+ unchanged: true,
130
+ since: record(),
131
+ recorded: false,
132
+ normalized: [
133
+ { path: "Cargo.lock", command: blank },
134
+ { path: "Cargo.toml", command: blank },
135
+ ],
136
+ });
137
+ expect(calls).toEqual([
138
+ [blank, "Cargo.lock", "a".repeat(40), "b".repeat(40)],
139
+ [blank, "Cargo.toml", "a".repeat(40), "b".repeat(40)],
140
+ ]);
141
+ });
142
+
143
+ test("a file that differs after its normalization deploys, and says so", async () => {
144
+ expect(await compareN(["Cargo.lock"], () => ({ same: false }))).toEqual({
145
+ unchanged: false,
146
+ reason: `input file Cargo.lock changed since ${"a".repeat(12)}, also after \`${blank}\``,
147
+ });
148
+ });
149
+
150
+ test("a normalizer that fails counts the file as changed, with its message", async () => {
151
+ expect(await compareN(["Cargo.lock"], () => ({ error: "`scripts/release blank 'Cargo.lock'` exited 2 on Cargo.lock at aaaaaaaaaaaa: no such kind" }))).toEqual({
152
+ unchanged: false,
153
+ reason: "the normalization of Cargo.lock failed, so it counts as changed: `scripts/release blank 'Cargo.lock'` exited 2 on Cargo.lock at aaaaaaaaaaaa: no such kind",
154
+ });
155
+ // With nothing to run the normalizer, a covered file is changed too.
156
+ expect(await compareN(["Cargo.lock"])).toMatchObject({ unchanged: false, reason: expect.stringMatching(/normalization of Cargo\.lock failed, so it counts as changed: no normalizer runs here/) });
157
+ });
158
+
159
+ test("a changed file no normalization covers is compared as before, and no normalizer runs", async () => {
160
+ let ran = 0;
161
+ const counting: NormalizedCompare = () => (ran++, { same: true });
162
+ expect(await compareN(["Cargo.lock", "crates/control/src/main.rs"], counting)).toEqual({
163
+ unchanged: false,
164
+ reason: `1 input file(s) changed since ${"a".repeat(12)}, such as crates/control/src/main.rs`,
165
+ });
166
+ expect(ran).toBe(0);
167
+ // No normalize at all: the plain diff, as before #4718.
168
+ expect(await compareN(["Cargo.lock"], counting, { inputs: { ...inputs, normalize: [] } })).toMatchObject({ unchanged: false, reason: expect.stringMatching(/1 input file\(s\) changed .* such as Cargo\.lock/) });
169
+ expect(ran).toBe(0);
170
+ // Nothing changed: unchanged with nothing normalized.
171
+ expect(await compareN([], counting)).toEqual({ unchanged: true, since: record(), recorded: false });
172
+ });
173
+
174
+ test("the first entry that matches a file is the one used", async () => {
175
+ const two = { ...inputs, normalize: [{ paths: ["../Cargo.lock"], command: "first {path}" }, { paths: ["../Cargo.lock", "../Cargo.toml"], command: blank }] };
176
+ const verdict = await compareN(["Cargo.lock", "Cargo.toml"], () => ({ same: true }), { inputs: two });
177
+ expect(verdict).toMatchObject({ unchanged: true, normalized: [{ path: "Cargo.lock", command: "first {path}" }, { path: "Cargo.toml", command: blank }] });
178
+ });
179
+ });
180
+
181
+ describe("gitNormalizedCompare (#4718)", () => {
182
+ const git = (dir: string, ...args: string[]): string => {
183
+ const r = spawnSync("git", args, { cwd: dir, encoding: "utf-8" });
184
+ if (r.status !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr}`);
185
+ return r.stdout.trim();
186
+ };
187
+ const commitFile = (dir: string, file: string, content: string): string => {
188
+ mkdirSync(join(dir, file, ".."), { recursive: true });
189
+ writeFileSync(join(dir, file), content);
190
+ git(dir, "add", ".");
191
+ git(dir, "commit", "-q", "-m", `change ${file}`);
192
+ return git(dir, "rev-parse", "HEAD");
193
+ };
194
+ // Blanks the version under [package], as Arugula's `scripts/release blank` does for its own crates.
195
+ const blankVersion = `sed -e 's/^version = ".*"/version = "@V@"/'`;
196
+
197
+ test("runs the command from the top level on each commit's copy, with {path} from the top level", async () => {
198
+ await withTestDir(async (dir) => {
199
+ git(dir, "init", "-q", "-b", "main");
200
+ git(dir, "config", "user.email", "test@chant.dev");
201
+ git(dir, "config", "user.name", "Test");
202
+ const a = commitFile(dir, "crates/control/Cargo.toml", `[package]\nname = "control"\nversion = "1.0.0"\n`);
203
+ const b = commitFile(dir, "crates/control/Cargo.toml", `[package]\nname = "control"\nversion = "1.0.1"\n`);
204
+ const c = commitFile(dir, "crates/control/Cargo.toml", `[package]\nname = "control"\nversion = "1.0.1"\n\n[dependencies]\nserde = "1"\n`);
205
+ mkdirSync(join(dir, "ops"), { recursive: true });
206
+ const compare = gitNormalizedCompare(join(dir, "ops"));
207
+ const path = "crates/control/Cargo.toml";
208
+
209
+ expect(compare(blankVersion, path, a, b)).toEqual({ same: true });
210
+ expect(compare(blankVersion, path, b, c)).toEqual({ same: false });
211
+ // {path} is the path from the top level, and the command's directory is the top level.
212
+ expect(compare(`test -f {path} && ${blankVersion}`, path, a, b)).toEqual({ same: true });
213
+ // A file missing at one commit is changed.
214
+ expect(compare(blankVersion, "crates/control/Cargo.lock", a, b)).toEqual({ same: false });
215
+ // A failing command is an error naming the command, the file, the commit and what it said.
216
+ expect(compare("echo no such kind >&2; exit 2", path, a, b)).toEqual({ error: `\`echo no such kind >&2; exit 2\` exited 2 on ${path} at ${a.slice(0, 12)}: no such kind` });
217
+ });
218
+ });
219
+ });
220
+
221
+ describe("unchangedRecord with normalized inputs (#4718)", () => {
222
+ test("names each file and its normalization in normalizedInputs", () => {
223
+ const now = release("b");
224
+ const out = unchangedRecord(record(), { releaseDigest: now.releaseDigest, runId: "run-2", timestamp: "2026-11-02T10:00:00.000Z", actor: "ci" }, [
225
+ { path: "Cargo.lock", command: "scripts/release blank {path}" },
226
+ ]);
227
+ expect(out.normalizedInputs).toEqual([{ path: "Cargo.lock", command: "scripts/release blank {path}" }]);
228
+ expect(out.unchangedSince).toEqual({ env: "prod", runId: "run-1", timestamp: "2026-11-01T10:00:00.000Z" });
229
+ expect(unchangedRecord(record(), { releaseDigest: now.releaseDigest, runId: "run-2", timestamp: "2026-11-02T10:00:00.000Z", actor: "ci" }, [])).not.toHaveProperty("normalizedInputs");
230
+ });
231
+ });
@@ -30,13 +30,36 @@
30
30
  * environment, a record made from no release, a release that isn't stored, a
31
31
  * commit git doesn't have. `--deploy-unchanged` deploys it anyway.
32
32
  *
33
+ * A changed file that one of `normalize`'s entries covers (#4718) is compared
34
+ * after that entry's command. A daemon release bumps Arugula's own crate
35
+ * versions in every Cargo.toml and in Cargo.lock, which the hub lists among
36
+ * its inputs; with
37
+ *
38
+ * ```ts
39
+ * normalize: [{ paths: ["../Cargo.lock", "../crates/control/Cargo.toml"], command: "scripts/release blank {path}" }]
40
+ * ```
41
+ *
42
+ * each such file is read at the two commits (`git show <commit>:<path>`),
43
+ * each copy is piped through the command, and the file counts as unchanged
44
+ * when the two outputs are the same bytes. The command runs with `sh -c` from
45
+ * the repository's top level in the checkout, the commit being released, so
46
+ * the same normalizer reads both copies; `{path}` is the file's path from the
47
+ * top level, shell-quoted. It must print the same bytes for the same input.
48
+ * A command that exits non-zero, times out or can't start counts the file as
49
+ * changed and the reason says so. A file that is missing at either commit is
50
+ * changed. A changed file no entry covers is changed as before, and then no
51
+ * command runs. The first entry whose `paths` match a file is the one used.
52
+ *
33
53
  * A skip is recorded ({@link unchangedRecord}): a release record of this
34
54
  * release for the component that repeats the digest of the deploy it keeps
35
- * and names that deploy in `unchangedSince`.
55
+ * and names that deploy in `unchangedSince`. When a file was the same only
56
+ * after a normalization, the record lists it with the command in
57
+ * `normalizedInputs`.
36
58
  */
37
59
 
38
- import { execFileSync } from "node:child_process";
60
+ import { execFileSync, spawnSync } from "node:child_process";
39
61
  import type { ComponentInputs } from "./component";
62
+ import { NEXT_RELEASE } from "../release-floors";
40
63
  import type { Release } from "../lifecycle/release-store";
41
64
  import type { ReleaseRecord, ReleaseRecordInput, RunOrigin } from "../lifecycle/release-ledger";
42
65
  import { releaseTemplatesDigest } from "./release-promote";
@@ -44,6 +67,17 @@ import { releaseTemplatesDigest } from "./release-promote";
44
67
  /** The first chant that reads a component's `inputs` and writes `unchangedSince` on a release record (#4695). */
45
68
  export const UNCHANGED_INPUTS_FLOOR = "0.124.65";
46
69
 
70
+ /** The first chant that reads `inputs.normalize` and writes `normalizedInputs` on a skip record (#4718). */
71
+ export const NORMALIZED_INPUTS_FLOOR = NEXT_RELEASE;
72
+
73
+ /** A file that differed between the two commits and was the same after a normalization (#4718). */
74
+ export interface NormalizedInput {
75
+ /** From the repository's top level, as git prints it. */
76
+ path: string;
77
+ /** The `command` of the `normalize` entry that covered it, as declared. */
78
+ command: string;
79
+ }
80
+
47
81
  /** Whether a component's inputs are the same as in the release the environment runs it from. */
48
82
  export type InputsVerdict =
49
83
  | {
@@ -52,6 +86,8 @@ export type InputsVerdict =
52
86
  since: ReleaseRecord;
53
87
  /** The environment's record already names this release: there is nothing new to record. */
54
88
  recorded: boolean;
89
+ /** The files that differed and were the same after a normalization (#4718), when there were any. */
90
+ normalized?: NormalizedInput[];
55
91
  }
56
92
  | { unchanged: false; reason: string };
57
93
 
@@ -62,7 +98,7 @@ export type ChangedPaths = (from: string, to: string, paths: readonly string[])
62
98
  export function gitChangedPaths(memberDir: string): ChangedPaths {
63
99
  return (from, to, paths) => {
64
100
  try {
65
- const out = execFileSync("git", ["diff", "--name-only", "--no-renames", from, to, "--", ...paths], {
101
+ const out = execFileSync("git", ["diff", "--name-only", "--no-renames", "--no-relative", from, to, "--", ...paths], {
66
102
  cwd: memberDir,
67
103
  encoding: "utf-8",
68
104
  stdio: ["ignore", "pipe", "pipe"],
@@ -74,6 +110,52 @@ export function gitChangedPaths(memberDir: string): ChangedPaths {
74
110
  };
75
111
  }
76
112
 
113
+ /**
114
+ * Whether the file at `path` (from the top level) is the same at two commits
115
+ * after `command` reads each copy on stdin (#4718), or why it can't tell.
116
+ */
117
+ export type NormalizedCompare = (command: string, path: string, from: string, to: string) => { same: boolean } | { error: string };
118
+
119
+ /** How long one run of a normalizer may take. */
120
+ const NORMALIZE_TIMEOUT_MS = 60_000;
121
+
122
+ const shellQuote = (s: string): string => `'${s.replace(/'/g, `'\\''`)}'`;
123
+
124
+ /**
125
+ * {@link NormalizedCompare} through git and `sh -c`, from the top level of
126
+ * the repository `memberDir` is in. A file missing at either commit is not
127
+ * the same.
128
+ */
129
+ export function gitNormalizedCompare(memberDir: string): NormalizedCompare {
130
+ let top: string | undefined;
131
+ return (command, path, from, to) => {
132
+ try {
133
+ top ??= execFileSync("git", ["rev-parse", "--show-toplevel"], { cwd: memberDir, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] }).trim();
134
+ } catch {
135
+ return { error: `${memberDir} is not in a git work tree` };
136
+ }
137
+ const outputs: Buffer[] = [];
138
+ for (const commit of [from, to]) {
139
+ const show = spawnSync("git", ["show", `${commit}:${path}`], { cwd: top, maxBuffer: 256 * 1024 * 1024 });
140
+ if (show.status !== 0) return { same: false };
141
+ const run = spawnSync("sh", ["-c", command.replaceAll("{path}", shellQuote(path))], {
142
+ cwd: top,
143
+ input: show.stdout,
144
+ timeout: NORMALIZE_TIMEOUT_MS,
145
+ maxBuffer: 256 * 1024 * 1024,
146
+ });
147
+ const at = `${path} at ${commit.slice(0, 12)}`;
148
+ if (run.error) return { error: `\`${command}\` could not run on ${at}: ${run.error.message}` };
149
+ if (run.status !== 0) {
150
+ const said = run.stderr.toString("utf-8").trim().split("\n")[0];
151
+ return { error: `\`${command}\` ${run.signal ? `was killed (${run.signal})` : `exited ${run.status}`} on ${at}${said ? `: ${said}` : ""}` };
152
+ }
153
+ outputs.push(run.stdout);
154
+ }
155
+ return { same: outputs[0]!.equals(outputs[1]!) };
156
+ };
157
+ }
158
+
77
159
  const shortDigest = (d: string): string => d.replace(/^jcs1-sha256:/, "").slice(0, 12);
78
160
 
79
161
  /** Compare `component`'s declared inputs in `release` with the release the environment's `current` record names. */
@@ -86,8 +168,11 @@ export async function compareInputs(input: {
86
168
  current: ReleaseRecord | undefined;
87
169
  readRelease: (digest: string) => Promise<Release | null>;
88
170
  changedPaths: ChangedPaths;
171
+ /** Compares a file `inputs.normalize` covers (#4718); without it such a file counts as changed. */
172
+ normalizedCompare?: NormalizedCompare;
89
173
  }): Promise<InputsVerdict> {
90
174
  const { component, inputs, env, release, current } = input;
175
+ const normalized: NormalizedInput[] = [];
91
176
  const paths = inputs.paths ?? [];
92
177
  const params = inputs.params ?? [];
93
178
  if (paths.length === 0 && params.length === 0) return { unchanged: false, reason: "it declares no paths or params in inputs" };
@@ -114,11 +199,48 @@ export async function compareInputs(input: {
114
199
  if (changed === null) {
115
200
  return { unchanged: false, reason: `git can't compare ${prior.commit.slice(0, 12)} and ${release.commit.slice(0, 12)} here; fetch the history between them` };
116
201
  }
117
- if (changed.length > 0) {
118
- return { unchanged: false, reason: `${changed.length} input file(s) changed since ${prior.commit.slice(0, 12)}, such as ${changed[0]}` };
202
+ const since = prior.commit.slice(0, 12);
203
+ const covering = normalizeCoverage(inputs, changed, (p) => input.changedPaths(prior.commit, release.commit, p));
204
+ if ("error" in covering) return { unchanged: false, reason: covering.error };
205
+ const plain = changed.filter((f) => !covering.byFile.has(f));
206
+ if (plain.length > 0) {
207
+ return { unchanged: false, reason: `${plain.length} input file(s) changed since ${since}, such as ${plain[0]}` };
208
+ }
209
+ for (const [file, command] of covering.byFile) {
210
+ const result = input.normalizedCompare
211
+ ? input.normalizedCompare(command, file, prior.commit, release.commit)
212
+ : { error: "no normalizer runs here" };
213
+ if ("error" in result) {
214
+ return { unchanged: false, reason: `the normalization of ${file} failed, so it counts as changed: ${result.error}` };
215
+ }
216
+ if (!result.same) {
217
+ return { unchanged: false, reason: `input file ${file} changed since ${since}, also after \`${command}\`` };
218
+ }
219
+ normalized.push({ path: file, command });
119
220
  }
120
221
  }
121
- return { unchanged: true, since: current, recorded: false };
222
+ return { unchanged: true, since: current, recorded: false, ...(normalized.length > 0 ? { normalized } : {}) };
223
+ }
224
+
225
+ /**
226
+ * The `normalize` entry, by its command, that covers each of the `changed`
227
+ * files (#4718): the first entry whose `paths` git matches the file with.
228
+ */
229
+ function normalizeCoverage(
230
+ inputs: ComponentInputs,
231
+ changed: readonly string[],
232
+ matching: (paths: readonly string[]) => string[] | null,
233
+ ): { byFile: Map<string, string> } | { error: string } {
234
+ const byFile = new Map<string, string>();
235
+ if (changed.length === 0) return { byFile };
236
+ const pending = new Set(changed);
237
+ for (const entry of inputs.normalize ?? []) {
238
+ if (entry.paths.length === 0) continue;
239
+ const matched = matching(entry.paths);
240
+ if (matched === null) return { error: `git can't match the paths of the normalization \`${entry.command}\`, so its files count as changed` };
241
+ for (const f of matched) if (pending.delete(f)) byFile.set(f, entry.command);
242
+ }
243
+ return { byFile };
122
244
  }
123
245
 
124
246
  /**
@@ -130,6 +252,7 @@ export async function compareInputs(input: {
130
252
  export function unchangedRecord(
131
253
  since: ReleaseRecord,
132
254
  run: { releaseDigest: string; runId: string; runOrigin?: RunOrigin; timestamp: string; actor: string },
255
+ normalized?: readonly NormalizedInput[],
133
256
  ): ReleaseRecordInput {
134
257
  return {
135
258
  component: since.component,
@@ -146,5 +269,6 @@ export function unchangedRecord(
146
269
  actor: run.actor,
147
270
  releaseDigest: run.releaseDigest,
148
271
  unchangedSince: { env: since.env, runId: since.runId, timestamp: since.timestamp },
272
+ ...(normalized && normalized.length > 0 ? { normalizedInputs: normalized.map((n) => ({ path: n.path, command: n.command })) } : {}),
149
273
  };
150
274
  }
@@ -253,6 +253,15 @@ export interface ReleaseRecord {
253
253
  * every deploy.
254
254
  */
255
255
  unchangedSince?: ReleaseRef;
256
+ /**
257
+ * Optional: on a skip record (`unchangedSince`), the input files that
258
+ * differed between the two releases' commits and were the same after a
259
+ * normalization the component declares in `inputs.normalize` (#4718), each
260
+ * with that normalization's command. Written from chant
261
+ * `NORMALIZED_INPUTS_FLOOR` (../components/unchanged-inputs.ts); absent
262
+ * when no file needed one.
263
+ */
264
+ normalizedInputs?: { path: string; command: string }[];
256
265
  /**
257
266
  * Optional: the release this deploy came from (#4026, ws-104), the
258
267
  * `releaseDigest` of `_releases/<releaseDigest>.json`. Written by
@@ -19,4 +19,4 @@
19
19
  * this constant to the next patch (#4501). The check fails while a floor
20
20
  * still names this constant after its version shipped.
21
21
  */
22
- export const NEXT_RELEASE = "0.124.67";
22
+ export const NEXT_RELEASE = "0.124.68";