@smoothbricks/nx-plugin 0.4.20 → 0.4.22
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.
- package/README.md +89 -18
- package/dist/bin/smoo-nx-cargo-hash.js +32 -4
- package/dist/cargo-source-hash.d.ts +10 -1
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +251 -41
- package/dist/cargo-workspace.d.ts +0 -1
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +9 -2
- package/dist/ensure-built.d.ts +6 -5
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +227 -30
- package/dist/index.js +18 -10
- package/dist/managed-files/ci-workflow.d.ts +20 -0
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +42 -13
- package/dist/managed-files/files.d.ts +1 -1
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +8 -1
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +37 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
- package/dist/managed-files/pr-preview-cleanup-workflow.js +34 -6
- package/dist/workspace-manifest.d.ts +3 -1
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/managed/raw/tooling/direnv/setup-environment.ts +23 -91
- package/managed/raw/tooling/git-hooks/commit-msg.sh +6 -0
- package/managed/raw/tooling/git-hooks/pre-commit.sh +6 -0
- package/managed/raw/tooling/git-hooks/pre-push.sh +6 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +19 -7
- package/package.json +1 -1
- package/src/bin/smoo-nx-cargo-hash.ts +29 -5
- package/src/cargo-source-hash.test.ts +291 -1
- package/src/cargo-source-hash.ts +190 -28
- package/src/cargo-workspace.test.ts +17 -10
- package/src/cargo-workspace.ts +9 -3
- package/src/ensure-built.test.ts +87 -29
- package/src/ensure-built.ts +274 -29
- package/src/index.test.ts +117 -20
- package/src/index.ts +18 -11
- package/src/managed-files/ci-workflow.ts +45 -14
- package/src/managed-files/files.ts +9 -2
- package/src/managed-files/generator.test.ts +5 -1
- package/src/managed-files/github-host-cache-isolation.test.ts +32 -6
- package/src/managed-files/pr-preview-cleanup-workflow.ts +87 -6
- package/src/workspace-manifest.ts +3 -1
package/src/index.test.ts
CHANGED
|
@@ -93,6 +93,90 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
93
93
|
}
|
|
94
94
|
}, 120_000);
|
|
95
95
|
|
|
96
|
+
it('never restores a stale child artifact from the cached build aggregate', async () => {
|
|
97
|
+
const workspace = await createWorkspace();
|
|
98
|
+
const root = workspace.context.workspaceRoot;
|
|
99
|
+
const repositoryRoot = fileURLToPath(new URL('../../../', import.meta.url));
|
|
100
|
+
try {
|
|
101
|
+
await symlink(join(repositoryRoot, 'node_modules'), join(root, 'node_modules'), 'dir');
|
|
102
|
+
await workspace.write(
|
|
103
|
+
'package.json',
|
|
104
|
+
'{"name":"aggregate-workspace","private":true,"workspaces":["packages/*"]}\n',
|
|
105
|
+
);
|
|
106
|
+
await workspace.write(
|
|
107
|
+
'nx.json',
|
|
108
|
+
JSON.stringify({
|
|
109
|
+
plugins: [fileURLToPath(new URL('../dist/index.js', import.meta.url))],
|
|
110
|
+
namedInputs: { default: ['{projectRoot}/**/*'] },
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
// Emitted artifacts are not sources, so the aggregate's own inputs never see them.
|
|
114
|
+
await workspace.write('.gitignore', 'dist\n.nx\nnode_modules\n');
|
|
115
|
+
await workspace.write('toolchain.txt', 'toolchain-1\n');
|
|
116
|
+
await workspace.write('packages/app/source.txt', 'source\n');
|
|
117
|
+
// The emitter keys on one input outside its project, as a transform or
|
|
118
|
+
// toolchain does. The inferred `build` aggregate hashes only the project,
|
|
119
|
+
// so a toolchain change moves the emitter's key and not the aggregate's.
|
|
120
|
+
await workspace.write(
|
|
121
|
+
'packages/app/package.json',
|
|
122
|
+
JSON.stringify({
|
|
123
|
+
name: 'app',
|
|
124
|
+
private: true,
|
|
125
|
+
nx: {
|
|
126
|
+
targets: {
|
|
127
|
+
'emit-js': {
|
|
128
|
+
executor: 'nx:run-commands',
|
|
129
|
+
cache: true,
|
|
130
|
+
inputs: ['{projectRoot}/source.txt', '{workspaceRoot}/toolchain.txt'],
|
|
131
|
+
outputs: ['{projectRoot}/dist'],
|
|
132
|
+
options: {
|
|
133
|
+
command: 'mkdir -p dist && cat source.txt ../../toolchain.txt > dist/app.js',
|
|
134
|
+
cwd: '{projectRoot}',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
const build = async () => {
|
|
142
|
+
const child = Bun.spawn(['bun', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:build'], {
|
|
143
|
+
cwd: root,
|
|
144
|
+
env: {
|
|
145
|
+
...process.env,
|
|
146
|
+
PATH: `${join(repositoryRoot, 'node_modules/.bin')}:${process.env.PATH ?? ''}`,
|
|
147
|
+
NX_DAEMON: 'false',
|
|
148
|
+
NX_ISOLATE_PLUGINS: 'false',
|
|
149
|
+
NX_WORKSPACE_DATA_DIRECTORY: join(root, '.nx/workspace-data'),
|
|
150
|
+
NX_CACHE_DIRECTORY: join(root, '.nx/cache'),
|
|
151
|
+
},
|
|
152
|
+
stdout: 'pipe',
|
|
153
|
+
stderr: 'pipe',
|
|
154
|
+
});
|
|
155
|
+
const [exitCode, stdout, stderr] = await Promise.all([
|
|
156
|
+
child.exited,
|
|
157
|
+
new Response(child.stdout).text(),
|
|
158
|
+
new Response(child.stderr).text(),
|
|
159
|
+
]);
|
|
160
|
+
return { exitCode, output: stdout + stderr };
|
|
161
|
+
};
|
|
162
|
+
const artifact = () => readFile(join(root, 'packages/app/dist/app.js'), 'utf8');
|
|
163
|
+
|
|
164
|
+
const first = await build();
|
|
165
|
+
expect(first.exitCode, first.output).toBe(0);
|
|
166
|
+
expect(await artifact()).toBe('source\ntoolchain-1\n');
|
|
167
|
+
|
|
168
|
+
await workspace.write('toolchain.txt', 'toolchain-2\n');
|
|
169
|
+
const second = await build();
|
|
170
|
+
expect(second.exitCode, second.output).toBe(0);
|
|
171
|
+
// The emitter reran on its new key. The aggregate's key did not move, so
|
|
172
|
+
// it replays from cache — and a replay must not put back the dist it
|
|
173
|
+
// would have claimed on the first run.
|
|
174
|
+
expect(await artifact(), second.output).toBe('source\ntoolchain-2\n');
|
|
175
|
+
} finally {
|
|
176
|
+
await workspace.cleanup();
|
|
177
|
+
}
|
|
178
|
+
}, 120_000);
|
|
179
|
+
|
|
96
180
|
it('names standalone package projects from package metadata', async () => {
|
|
97
181
|
const workspace = await createWorkspace();
|
|
98
182
|
try {
|
|
@@ -220,12 +304,6 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
220
304
|
expect(targets.build?.executor).toBe('nx:noop');
|
|
221
305
|
expect(targets.build?.cache).toBe(true);
|
|
222
306
|
expect(targets.build?.dependsOn).toEqual(['^build', 'tsc-js']);
|
|
223
|
-
// An `nx:noop` aggregate writes no file, so it must claim none: a
|
|
224
|
-
// `{projectRoot}/dist` claim here would double-cache its children's bytes
|
|
225
|
-
// and make `github-ci nx-run-many --collect-outputs` attribute every file
|
|
226
|
-
// under dist to `build`, including the platform artifacts that collect
|
|
227
|
-
// deliberately leaves to the platform step.
|
|
228
|
-
expect(targets.build?.outputs).toBeUndefined();
|
|
229
307
|
expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
|
|
230
308
|
expect(targets.clean?.cache).toBe(false);
|
|
231
309
|
|
|
@@ -787,7 +865,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
787
865
|
for (const name of ['cargo-test-compile', 'cargo-test-archive', 'cargo-lint', CARGO_CROSS_LINT_TARGET]) {
|
|
788
866
|
const inputs = targets[name]?.inputs ?? [];
|
|
789
867
|
expect(inputs).toContainEqual({ dependentTasksOutputFiles: '**/*', transitive: false });
|
|
790
|
-
expect(inputs).toContain('{
|
|
868
|
+
expect(inputs).toContain('{workspaceRoot}/packages/ferris/crates/ferris-core/**/*');
|
|
791
869
|
}
|
|
792
870
|
expect(targets['cargo-test-compile']?.executor).toBe('nx:run-commands');
|
|
793
871
|
expect(targets['cargo-test-compile']?.cache).toBe(false);
|
|
@@ -830,16 +908,20 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
830
908
|
expect(targets['cargo-test-ferris-core']?.options?.command).toMatch(
|
|
831
909
|
/^cargo --frozen nextest run --archive-file target\/nextest\/archive\.tar\.zst --workspace-remap \. -E 'package\(ferris-core\)' --no-tests=pass --user-config-file none --tool-config-file "smoo:\$PWD\/.*nextest\.toml"$/,
|
|
832
910
|
);
|
|
833
|
-
expect(targets['cargo-test-ferris-core']?.inputs).toContain(
|
|
911
|
+
expect(targets['cargo-test-ferris-core']?.inputs).toContain(
|
|
912
|
+
'{workspaceRoot}/packages/ferris/.config/nextest.toml',
|
|
913
|
+
);
|
|
834
914
|
expect(targets['cargo-test-ferris-core']?.configurations?.production).toEqual({});
|
|
835
915
|
expect(targets['cargo-test-ferris-core']?.inputs).toEqual(
|
|
836
916
|
expect.arrayContaining([
|
|
837
|
-
'{
|
|
838
|
-
'{
|
|
839
|
-
'{
|
|
917
|
+
'{workspaceRoot}/packages/ferris/Cargo.toml',
|
|
918
|
+
'{workspaceRoot}/packages/ferris/Cargo.lock',
|
|
919
|
+
'{workspaceRoot}/packages/ferris/crates/ferris-core/**/*',
|
|
840
920
|
]),
|
|
841
921
|
);
|
|
842
|
-
expect(targets['cargo-test-ferris-core']?.inputs).not.toContain(
|
|
922
|
+
expect(targets['cargo-test-ferris-core']?.inputs).not.toContain(
|
|
923
|
+
'{workspaceRoot}/packages/ferris/crates/ferris-wasm/**/*',
|
|
924
|
+
);
|
|
843
925
|
// One clippy for the workspace in one target dir: a per-crate closure
|
|
844
926
|
// compiled every shared dependency once per crate.
|
|
845
927
|
expect(targets['cargo-lint']?.options).toMatchObject({
|
|
@@ -852,7 +934,10 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
852
934
|
expect(targets['cargo-lint']?.cache).toBe(true);
|
|
853
935
|
expect(targets['cargo-lint']?.outputs).toEqual([]);
|
|
854
936
|
expect(targets['cargo-lint']?.inputs).toEqual(
|
|
855
|
-
expect.arrayContaining([
|
|
937
|
+
expect.arrayContaining([
|
|
938
|
+
'{workspaceRoot}/packages/ferris/crates/ferris-core/**/*',
|
|
939
|
+
'{workspaceRoot}/packages/ferris/crates/ferris-wasm/**/*',
|
|
940
|
+
]),
|
|
856
941
|
);
|
|
857
942
|
expect(targets.lint?.dependsOn).toEqual(['cargo-lint']);
|
|
858
943
|
expect(targets[CARGO_CROSS_LINT_TARGET]?.options).toMatchObject({
|
|
@@ -1153,12 +1238,23 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
1153
1238
|
'cargo --frozen clippy --workspace --all-targets --target-dir target/cargo-lint -- -D warnings',
|
|
1154
1239
|
],
|
|
1155
1240
|
});
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1241
|
+
// Workspace-anchored, because Nx hashes a `{projectRoot}` glob against
|
|
1242
|
+
// only the files its project owns, and the group project owns alpha and
|
|
1243
|
+
// beta: spelled from the root project, those globs hash nothing and the
|
|
1244
|
+
// archive replays stale binaries against edited crates.
|
|
1245
|
+
for (const name of ['cargo-lint', 'cargo-test-archive']) {
|
|
1246
|
+
expect(outer[name]?.inputs).toEqual(
|
|
1247
|
+
expect.arrayContaining([
|
|
1248
|
+
'{workspaceRoot}/packages/group/crates/alpha/**/*',
|
|
1249
|
+
'{workspaceRoot}/packages/group/crates/beta/**/*',
|
|
1250
|
+
]),
|
|
1251
|
+
);
|
|
1252
|
+
expect(
|
|
1253
|
+
(outer[name]?.inputs ?? []).filter(
|
|
1254
|
+
(input) => typeof input === 'string' && input.startsWith('{projectRoot}/packages/'),
|
|
1255
|
+
),
|
|
1256
|
+
).toEqual([]);
|
|
1257
|
+
}
|
|
1162
1258
|
expect(group['cargo-lint']?.dependsOn).toEqual([{ projects: ['outer-root'], target: 'cargo-lint' }]);
|
|
1163
1259
|
expect(group.build?.dependsOn).toEqual([
|
|
1164
1260
|
'^build',
|
|
@@ -1167,7 +1263,8 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
1167
1263
|
]);
|
|
1168
1264
|
expect(outer['cargo-lint-host-core']).toBeUndefined();
|
|
1169
1265
|
expect(inner['cargo-lint']?.options).toMatchObject({ cwd: 'nested' });
|
|
1170
|
-
expect(inner['cargo-lint']?.inputs).toContain('{
|
|
1266
|
+
expect(inner['cargo-lint']?.inputs).toContain('{workspaceRoot}/nested/**/*');
|
|
1267
|
+
expect(inner['cargo-lint']?.inputs).toContain('{workspaceRoot}/nested/modules/leaf/**/*');
|
|
1171
1268
|
expect(inner['cargo-lint']?.inputs).not.toContain('{workspaceRoot}/packages/group/crates/alpha/**/*');
|
|
1172
1269
|
expect(leaf['cargo-lint']?.dependsOn).toEqual([{ projects: ['inner-root'], target: 'cargo-lint' }]);
|
|
1173
1270
|
expect(leaf['cargo-lint-inner-leaf']).toBeUndefined();
|
package/src/index.ts
CHANGED
|
@@ -1627,20 +1627,26 @@ async function createProjectTargets(
|
|
|
1627
1627
|
};
|
|
1628
1628
|
}
|
|
1629
1629
|
|
|
1630
|
-
//
|
|
1631
|
-
// under dist belongs to the concrete target that
|
|
1632
|
-
// `{projectRoot}/dist` here would cache the children's
|
|
1633
|
-
// under the aggregate's hash, and would make `smoo
|
|
1634
|
-
// --collect-outputs` attribute to `build` whatever sits
|
|
1635
|
-
// platform-target artifacts the collect excludes on
|
|
1636
|
-
// artifacts of targets that never ran. Two collected trees
|
|
1637
|
-
// binary and `apply-outputs` rejects the overlap.
|
|
1630
|
+
// Empty `outputs`, deliberately and explicitly: this aggregate runs no
|
|
1631
|
+
// command, so every file under dist belongs to the concrete target that
|
|
1632
|
+
// emitted it. Claiming `{projectRoot}/dist` here would cache the children's
|
|
1633
|
+
// bytes a second time under the aggregate's hash, and would make `smoo
|
|
1634
|
+
// github-ci nx-run-many --collect-outputs` attribute to `build` whatever sits
|
|
1635
|
+
// under dist — including platform-target artifacts the collect excludes on
|
|
1636
|
+
// purpose and stale artifacts of targets that never ran. Two collected trees
|
|
1637
|
+
// then claim one binary and `apply-outputs` rejects the overlap. Omitting the
|
|
1638
|
+
// key is not the same thing: Nx gives a `build` or `prepare` target without
|
|
1639
|
+
// `outputs` its legacy defaults (`{projectRoot}/dist`, `/build`, `/public`,
|
|
1640
|
+
// `dist/{projectRoot}`), so a cache hit on the aggregate — whose key does
|
|
1641
|
+
// not move when only a child's own inputs do — restored the children's
|
|
1642
|
+
// previous dist over what they had just emitted. `lint` and `test` are
|
|
1638
1643
|
// aggregates on the same terms. Same reason nx.json must not put `outputs` in
|
|
1639
1644
|
// targetDefaults.build (see validateBuildTargetDefault).
|
|
1640
1645
|
if (hasOrdinaryBuildOutputTarget) {
|
|
1641
1646
|
targets.build = {
|
|
1642
1647
|
executor: 'nx:noop',
|
|
1643
1648
|
cache: true,
|
|
1649
|
+
outputs: [],
|
|
1644
1650
|
// `cargo-napi` is both an output family and a serialized cargo writer, so
|
|
1645
1651
|
// the two sources overlap; the Set keeps one edge per target while holding
|
|
1646
1652
|
// the order the families are listed in.
|
|
@@ -2358,8 +2364,10 @@ async function addCargoTestTargets(
|
|
|
2358
2364
|
if (plan.package.projectRoot !== projectRoot) {
|
|
2359
2365
|
continue;
|
|
2360
2366
|
}
|
|
2361
|
-
|
|
2362
|
-
|
|
2367
|
+
// Workspace-anchored for the reason `cargoPackageTestInputs` gives: a
|
|
2368
|
+
// `{projectRoot}` glob hashes only files this project owns, and sibling
|
|
2369
|
+
// manifests belong to whichever nested project holds them.
|
|
2370
|
+
const inputRoot = posix.join('{workspaceRoot}', workspace.projectRoot);
|
|
2363
2371
|
// A workspace nextest build unifies features across every selected member.
|
|
2364
2372
|
// A sibling manifest can therefore change this crate's tests without being
|
|
2365
2373
|
// a path dependency. Track those manifests, not unrelated sibling sources.
|
|
@@ -2373,7 +2381,6 @@ async function addCargoTestTargets(
|
|
|
2373
2381
|
workspaceRoot,
|
|
2374
2382
|
absoluteProjectRoot: join(workspaceRoot, workspace.projectRoot),
|
|
2375
2383
|
memberDir: plan.package.dir,
|
|
2376
|
-
inputRoot,
|
|
2377
2384
|
cache: workspace.inputsCache,
|
|
2378
2385
|
})),
|
|
2379
2386
|
...workspace.packages.map((member) => posix.join(inputRoot, member.package.dir, 'Cargo.toml')),
|
|
@@ -211,6 +211,18 @@ export function defineCiWorkflow(options: CiWorkflowDefinitionOptions): CiWorkfl
|
|
|
211
211
|
return steps.map((step, index) => ({ ...step, number: index + 2 }));
|
|
212
212
|
}
|
|
213
213
|
|
|
214
|
+
/**
|
|
215
|
+
* CI's concurrency group: one per pull request, by its number, and one per ref
|
|
216
|
+
* for pushes. The PR preview cleanup joins the same group, so a pull request's
|
|
217
|
+
* close cancels its running CI and the cleanup starts only after that run's
|
|
218
|
+
* stage deploy has stopped. The number, not `github.ref`, names a pull
|
|
219
|
+
* request's group: a close that merged it carries the base branch's ref, which
|
|
220
|
+
* would put the cleanup in the base branch's own group, and a forge may spell a
|
|
221
|
+
* pull request's ref after its merge or after its head. Spelled literally
|
|
222
|
+
* rather than from `github.workflow`, which a forge may fill with the file name.
|
|
223
|
+
*/
|
|
224
|
+
export const CI_CONCURRENCY_GROUP = 'CI-${{ github.event.pull_request.number || github.ref }}';
|
|
225
|
+
|
|
214
226
|
export function renderCiWorkflowYaml(options: CiWorkflowDefinitionOptions): string {
|
|
215
227
|
const steps = defineCiWorkflow(options);
|
|
216
228
|
return [
|
|
@@ -240,17 +252,18 @@ ${options.deploy ? ' deployments: write\n' : ''} statuses: write
|
|
|
240
252
|
concurrency:
|
|
241
253
|
${
|
|
242
254
|
options.deploy
|
|
243
|
-
? ` # One in-flight run per
|
|
244
|
-
# running workflow instead of canceling it,
|
|
245
|
-
# deploy job mid-flight. Pull requests and
|
|
246
|
-
# superseded runs.
|
|
247
|
-
group:
|
|
255
|
+
? ` # One in-flight run per pull request, and per ref for pushes. Pushes to the
|
|
256
|
+
# staging push branch queue behind a running workflow instead of canceling it,
|
|
257
|
+
# so a newer push never cancels the deploy job mid-flight. Pull requests and
|
|
258
|
+
# other branches keep canceling superseded runs.
|
|
259
|
+
group: ${CI_CONCURRENCY_GROUP}
|
|
248
260
|
cancel-in-progress: \${{ github.ref != ${stagingRefLiteral(options)} }}`
|
|
249
261
|
: ` # This workflow validates and never deploys, so there is no in-flight
|
|
250
|
-
# deployment for a newer push to protect: every
|
|
251
|
-
# runs. Queuing them instead serializes
|
|
252
|
-
#
|
|
253
|
-
|
|
262
|
+
# deployment for a newer push to protect: every pull request, and every ref
|
|
263
|
+
# for pushes, cancels its superseded runs. Queuing them instead serializes
|
|
264
|
+
# the staging branch, and a burst of pushes then reports the newest commit
|
|
265
|
+
# one full run per queued push late.
|
|
266
|
+
group: ${CI_CONCURRENCY_GROUP}
|
|
254
267
|
cancel-in-progress: true`
|
|
255
268
|
}
|
|
256
269
|
|
|
@@ -1041,10 +1054,17 @@ function stagingRefLiteral(options: CiWorkflowDefinitionOptions): string {
|
|
|
1041
1054
|
return `'refs/heads/${stagingPushBranch(options).replaceAll("'", "''")}'`;
|
|
1042
1055
|
}
|
|
1043
1056
|
|
|
1044
|
-
|
|
1057
|
+
/** A job's `environment:` line, or nothing when no GitHub Environment is declared. */
|
|
1058
|
+
export function environmentLine(name: string | undefined): string {
|
|
1045
1059
|
return name ? ` environment: ${yamlScalar(name)}\n` : '';
|
|
1046
1060
|
}
|
|
1047
1061
|
|
|
1062
|
+
/** The Cloudflare credentials a deploy step carries, env var name → default repository secret name. */
|
|
1063
|
+
const CLOUDFLARE_DEPLOY_SECRETS: Record<string, string> = {
|
|
1064
|
+
CLOUDFLARE_API_TOKEN: 'CLOUDFLARE_API_TOKEN',
|
|
1065
|
+
CLOUDFLARE_ACCOUNT_ID: 'CLOUDFLARE_ACCOUNT_ID',
|
|
1066
|
+
};
|
|
1067
|
+
|
|
1048
1068
|
/**
|
|
1049
1069
|
* The step-scoped `env:` block for a deploy step. Step scope is the property
|
|
1050
1070
|
* being rendered, not an accident of indentation: job env would hand the
|
|
@@ -1057,13 +1077,24 @@ function environmentLine(name: string | undefined): string {
|
|
|
1057
1077
|
* once in `deploySecrets` instead of being overridden here.
|
|
1058
1078
|
*/
|
|
1059
1079
|
export function deployStepSecretEnvLines(config: DeployStepSecretConfig): string[] {
|
|
1060
|
-
const cloudflare
|
|
1061
|
-
config.deployProvider === 'cloudflare'
|
|
1062
|
-
? { CLOUDFLARE_API_TOKEN: 'CLOUDFLARE_API_TOKEN', CLOUDFLARE_ACCOUNT_ID: 'CLOUDFLARE_ACCOUNT_ID' }
|
|
1063
|
-
: {};
|
|
1080
|
+
const cloudflare = config.deployProvider === 'cloudflare' ? CLOUDFLARE_DEPLOY_SECRETS : {};
|
|
1064
1081
|
return secretEnvLines({ ...cloudflare, ...config.deploySecrets });
|
|
1065
1082
|
}
|
|
1066
1083
|
|
|
1084
|
+
/**
|
|
1085
|
+
* The step env for a step that acts on Cloudflare as the stage deploy did: the
|
|
1086
|
+
* two Cloudflare credentials, read from the secrets `deployStepSecretEnvLines`
|
|
1087
|
+
* names for them, and none of the other deploy secrets, which such a step
|
|
1088
|
+
* never uses.
|
|
1089
|
+
*/
|
|
1090
|
+
export function cloudflareCredentialEnvLines(deploySecrets: Record<string, string> | undefined): string[] {
|
|
1091
|
+
const credentials = Object.entries(CLOUDFLARE_DEPLOY_SECRETS).map(([name, secret]) => [
|
|
1092
|
+
name,
|
|
1093
|
+
deploySecrets?.[name] ?? secret,
|
|
1094
|
+
]);
|
|
1095
|
+
return secretEnvLines(Object.fromEntries(credentials));
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1067
1098
|
/** A step-level `env:` block mapping env var names to repository secrets; nothing when there are none. */
|
|
1068
1099
|
function secretEnvLines(secrets: Record<string, string>): string[] {
|
|
1069
1100
|
const entries = Object.entries(secrets);
|
|
@@ -249,7 +249,7 @@ export function renderManagedFiles(context: ManagedFileContext): ManagedFile[] {
|
|
|
249
249
|
|
|
250
250
|
/** Renders a generated workflow through its real managed-file descriptor, so the context wiring is covered. */
|
|
251
251
|
export function renderManagedWorkflowForTest(
|
|
252
|
-
source: 'ci-workflow' | 'publish-workflow',
|
|
252
|
+
source: 'ci-workflow' | 'publish-workflow' | 'pr-preview-cleanup-workflow',
|
|
253
253
|
context: ManagedFileContext,
|
|
254
254
|
): string {
|
|
255
255
|
const file = managedFiles.find((candidate) => candidate.kind === 'generated' && candidate.source === source);
|
|
@@ -307,7 +307,14 @@ function getManagedContent(file: ManagedFileDescriptor, context: ManagedFileCont
|
|
|
307
307
|
});
|
|
308
308
|
}
|
|
309
309
|
if (file.source === 'pr-preview-cleanup-workflow') {
|
|
310
|
-
return renderPrPreviewCleanupWorkflowYaml({
|
|
310
|
+
return renderPrPreviewCleanupWorkflowYaml({
|
|
311
|
+
runsOn: context.ciRunsOn,
|
|
312
|
+
privateNpm: context.privateNpm,
|
|
313
|
+
sourceCheckouts: context.sourceCheckouts,
|
|
314
|
+
cargoCredentials: context.cargoCredentials,
|
|
315
|
+
environments: context.ciEnvironments,
|
|
316
|
+
deploySecrets: context.ciDeploySecrets,
|
|
317
|
+
});
|
|
311
318
|
}
|
|
312
319
|
throw new Error(`Unknown generated managed file source ${file.source}`);
|
|
313
320
|
}
|
|
@@ -138,7 +138,11 @@ describe('managed-files generator', () => {
|
|
|
138
138
|
it('stages nothing on a fresh Tree after applying the first generation', async () => {
|
|
139
139
|
const root = mkdtempSync(join(tmpdir(), 'smoo-generator-'));
|
|
140
140
|
temporaryRoots.push(root);
|
|
141
|
-
|
|
141
|
+
// Both passes must resolve the same consumer formatter configuration.
|
|
142
|
+
// A /virtual first pass and a real second pass disagree when TMPDIR is
|
|
143
|
+
// inside a workspace with inherited Prettier settings.
|
|
144
|
+
flushChanges(root, workspace().listChanges());
|
|
145
|
+
const first = new FsTree(root, false);
|
|
142
146
|
await generate(first);
|
|
143
147
|
flushChanges(root, first.listChanges());
|
|
144
148
|
const second = new FsTree(root, false);
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { describe, expect, test } from 'bun:test';
|
|
2
2
|
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { CI_CONCURRENCY_GROUP } from './ci-workflow.js';
|
|
3
4
|
|
|
4
5
|
const action = readFileSync(
|
|
5
6
|
new URL('../../managed/templates/github/actions/setup-devenv/action.yml', import.meta.url),
|
|
6
7
|
'utf8',
|
|
7
8
|
);
|
|
8
9
|
const assignments = action.split('\n').filter((line) => line.trimStart().startsWith('scope=$('));
|
|
10
|
+
/** The steps that scope a mutable host cache, each from its `- name:` line to the next step's. */
|
|
11
|
+
const scopedSteps = action.split(/\n(?= {4}- name: )/).filter((step) => step.includes('scope=$('));
|
|
12
|
+
/** What CI's concurrency group is named after: a pull request's number, or the ref for pushes. */
|
|
13
|
+
const concurrencyIdentity = CI_CONCURRENCY_GROUP.slice('CI-'.length);
|
|
9
14
|
|
|
10
15
|
function scope(overrides: Record<string, string> = {}): string {
|
|
11
16
|
const assignment = assignments[0];
|
|
@@ -15,6 +20,7 @@ function scope(overrides: Record<string, string> = {}): string {
|
|
|
15
20
|
...process.env,
|
|
16
21
|
GITHUB_WORKFLOW: 'CI',
|
|
17
22
|
GITHUB_REF: 'refs/pull/10/merge',
|
|
23
|
+
SCOPE_REF: '10',
|
|
18
24
|
GITHUB_JOB: 'main',
|
|
19
25
|
RUNNER_OS: 'Linux',
|
|
20
26
|
RUNNER_ARCH: 'X64',
|
|
@@ -26,7 +32,7 @@ function scope(overrides: Record<string, string> = {}): string {
|
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
describe('host CI mutable cache isolation', () => {
|
|
29
|
-
test('Nx and devenv use the same exact workflow/ref/job/platform identity', () => {
|
|
35
|
+
test('Nx and devenv use the same exact workflow/pull request or ref/job/platform identity', () => {
|
|
30
36
|
expect(assignments).toHaveLength(2);
|
|
31
37
|
expect(assignments[0]).toBe(assignments[1]);
|
|
32
38
|
expect(action).toContain('/${lane:-default}/$scope');
|
|
@@ -35,14 +41,34 @@ describe('host CI mutable cache isolation', () => {
|
|
|
35
41
|
expect(action).toContain('NX_WORKSPACE_DATA_DIRECTORY=$NX_CACHE_ROOT/workspace-data');
|
|
36
42
|
});
|
|
37
43
|
|
|
44
|
+
// A merged close carries the base branch's ref, so hashing github.ref gave two
|
|
45
|
+
// pull requests' cleanups one scope while they ran in different concurrency
|
|
46
|
+
// groups: two writers on one workspace-data, one .devenv and one bind target.
|
|
47
|
+
test('both scopes hash what the CI concurrency group is named after, never the ref alone', () => {
|
|
48
|
+
expect(scopedSteps).toHaveLength(2);
|
|
49
|
+
for (const step of scopedSteps) {
|
|
50
|
+
expect(step).toContain(`\n SCOPE_REF: ${concurrencyIdentity}\n`);
|
|
51
|
+
}
|
|
52
|
+
for (const assignment of assignments) {
|
|
53
|
+
expect(assignment).toContain('"$SCOPE_REF"');
|
|
54
|
+
expect(assignment).not.toContain('GITHUB_REF');
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('pull requests merged into one base branch keep their own scopes', () => {
|
|
59
|
+
const mergedClose = { GITHUB_REF: 'refs/heads/main' };
|
|
60
|
+
expect(scope({ ...mergedClose, SCOPE_REF: '101' })).not.toBe(scope({ ...mergedClose, SCOPE_REF: '102' }));
|
|
61
|
+
expect(scope({ ...mergedClose, SCOPE_REF: '10' })).toBe(scope());
|
|
62
|
+
});
|
|
63
|
+
|
|
38
64
|
test('repeat runs stay warm but distinct PRs, jobs and platforms cannot share graph state', () => {
|
|
39
65
|
const original = scope();
|
|
40
66
|
expect(original).toMatch(/^[a-f0-9]{40}$/);
|
|
41
67
|
expect(scope({ GITHUB_RUN_ID: 'another-run', GITHUB_RUN_ATTEMPT: '2' })).toBe(original);
|
|
42
68
|
const identities: Record<string, string>[] = [
|
|
43
69
|
{ GITHUB_WORKFLOW: 'Publish' },
|
|
44
|
-
{
|
|
45
|
-
{
|
|
70
|
+
{ SCOPE_REF: '11' },
|
|
71
|
+
{ SCOPE_REF: 'refs/heads/main' },
|
|
46
72
|
{ GITHUB_JOB: 'e2e-deployment' },
|
|
47
73
|
{ RUNNER_OS: 'macOS' },
|
|
48
74
|
{ RUNNER_ARCH: 'ARM64' },
|
|
@@ -53,8 +79,8 @@ describe('host CI mutable cache isolation', () => {
|
|
|
53
79
|
});
|
|
54
80
|
|
|
55
81
|
test('similar-looking refs do not collide or become filesystem paths', () => {
|
|
56
|
-
expect(scope({
|
|
57
|
-
expect(scope({
|
|
58
|
-
expect(scope({
|
|
82
|
+
expect(scope({ SCOPE_REF: 'refs/heads/feature/a' })).not.toBe(scope({ SCOPE_REF: 'refs/heads/feature-a' }));
|
|
83
|
+
expect(scope({ SCOPE_REF: 'refs/heads/Feature' })).not.toBe(scope({ SCOPE_REF: 'refs/heads/feature' }));
|
|
84
|
+
expect(scope({ SCOPE_REF: 'refs/heads/../../other' })).toMatch(/^[a-f0-9]{40}$/);
|
|
59
85
|
});
|
|
60
86
|
});
|
|
@@ -1,9 +1,61 @@
|
|
|
1
1
|
/* biome-ignore-all lint/suspicious/noTemplateCurlyInString: GitHub Actions expressions are emitted literally. */
|
|
2
2
|
|
|
3
|
+
import type {
|
|
4
|
+
PackageCargoCredentialsConfig,
|
|
5
|
+
PackagePrivateNpmConfig,
|
|
6
|
+
PackageSmooGithubEnvironments,
|
|
7
|
+
PackageSourceCheckoutConfig,
|
|
8
|
+
} from '../workspace-manifest.js';
|
|
9
|
+
import {
|
|
10
|
+
CI_CONCURRENCY_GROUP,
|
|
11
|
+
CiWorkflowStepKind,
|
|
12
|
+
cargoCredentialJobEnvLines,
|
|
13
|
+
cargoCredentialStepLines,
|
|
14
|
+
cloudflareCredentialEnvLines,
|
|
15
|
+
environmentLine,
|
|
16
|
+
privateNpmReadTokenJobEnv,
|
|
17
|
+
sourceCheckoutsStepLines,
|
|
18
|
+
} from './ci-workflow.js';
|
|
3
19
|
import { renderRunsOnLine } from './github-runs-on.js';
|
|
4
20
|
|
|
5
21
|
export interface PrPreviewCleanupWorkflowOptions {
|
|
6
22
|
runsOn?: string | string[];
|
|
23
|
+
/**
|
|
24
|
+
* Declared private-npm opt-in. The job runs setup-devenv, whose dependency
|
|
25
|
+
* install refuses in CI when a declared registry credential is absent from
|
|
26
|
+
* the job environment, so the read token rides the job env exactly as it
|
|
27
|
+
* does on every CI job that runs setup-devenv. The remote-cache token stays
|
|
28
|
+
* out: shell entry defers it, and nothing in this job runs Nx.
|
|
29
|
+
*/
|
|
30
|
+
privateNpm?: PackagePrivateNpmConfig;
|
|
31
|
+
/**
|
|
32
|
+
* Declared sibling source checkouts, cloned before setup-devenv as on every
|
|
33
|
+
* CI job that runs it: the devenv shell and the install resolve path
|
|
34
|
+
* dependencies on them.
|
|
35
|
+
*/
|
|
36
|
+
sourceCheckouts?: PackageSourceCheckoutConfig[];
|
|
37
|
+
/**
|
|
38
|
+
* Declared Cargo credentials, rendered as on every CI job that runs
|
|
39
|
+
* setup-devenv: the tokens at job level, where setup resolves the declared
|
|
40
|
+
* secrets, and the credential step before the sibling clones. That step is
|
|
41
|
+
* not only for Cargo: later git commands in the job read the git
|
|
42
|
+
* configuration it writes to GITHUB_ENV (the credential helper, the runner
|
|
43
|
+
* proxy, the rewrite from each origin to its internal mirror), so a sibling
|
|
44
|
+
* clone declared without `tokenEnv` takes the mirror as it does in CI. One
|
|
45
|
+
* declared with `tokenEnv` sets GIT_CONFIG_COUNT=1 for its own auth header,
|
|
46
|
+
* which replaces that configuration: it gets no helper, proxy or rewrite.
|
|
47
|
+
*/
|
|
48
|
+
cargoCredentials?: PackageCargoCredentialsConfig;
|
|
49
|
+
/**
|
|
50
|
+
* The cleanup deletes what the stage deploy created, in the account the
|
|
51
|
+
* deploy's CLOUDFLARE_ACCOUNT_ID names, so it must read the credentials
|
|
52
|
+
* that deploy read. The job therefore runs in the staging Environment the
|
|
53
|
+
* stage deploy runs in, whose secrets shadow the repository's, and reads
|
|
54
|
+
* the Cloudflare credentials through the same `deploySecrets` mapping.
|
|
55
|
+
*/
|
|
56
|
+
environments?: PackageSmooGithubEnvironments;
|
|
57
|
+
/** Deploy-step secrets; only the two Cloudflare credentials reach the cleanup. */
|
|
58
|
+
deploySecrets?: Record<string, string>;
|
|
7
59
|
}
|
|
8
60
|
|
|
9
61
|
export function renderPrPreviewCleanupWorkflowYaml(options: PrPreviewCleanupWorkflowOptions = {}): string {
|
|
@@ -16,29 +68,58 @@ on:
|
|
|
16
68
|
permissions:
|
|
17
69
|
contents: read
|
|
18
70
|
|
|
71
|
+
# The pull request's CI group, named by its number whether the close merged it
|
|
72
|
+
# or not: closing it cancels its running CI run, so the cleanup starts only
|
|
73
|
+
# after that run's stage deploy has stopped.
|
|
74
|
+
concurrency:
|
|
75
|
+
group: ${CI_CONCURRENCY_GROUP}
|
|
76
|
+
cancel-in-progress: true
|
|
77
|
+
|
|
19
78
|
jobs:
|
|
20
79
|
cleanup:
|
|
21
80
|
name: Cleanup PR environment
|
|
22
81
|
if: github.event.pull_request.head.repo.full_name == github.repository
|
|
23
82
|
${renderRunsOnLine(options.runsOn)}
|
|
24
83
|
timeout-minutes: 15
|
|
25
|
-
defaults:
|
|
84
|
+
${environmentLine(options.environments?.staging)} defaults:
|
|
26
85
|
run:
|
|
27
86
|
working-directory: tooling/direnv
|
|
28
|
-
steps:
|
|
87
|
+
${setupJobEnv(options)} steps:
|
|
29
88
|
- name: Checkout
|
|
30
89
|
uses: actions/checkout@v6.0.2
|
|
31
90
|
with:
|
|
32
91
|
filter: blob:none
|
|
33
92
|
fetch-depth: 1
|
|
34
|
-
|
|
93
|
+
${preflightSteps(options)}
|
|
35
94
|
- name: Setup Nix/devenv
|
|
36
95
|
uses: ./.github/actions/setup-devenv
|
|
37
96
|
|
|
38
97
|
- name: Cleanup PR environment
|
|
39
|
-
|
|
40
|
-
CLOUDFLARE_API_TOKEN: \${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
41
|
-
CLOUDFLARE_ACCOUNT_ID: \${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
98
|
+
${cloudflareCredentialEnvLines(options.deploySecrets).join('\n')}
|
|
42
99
|
run: smoo wrangler cleanup-pr --pr \${{ github.event.pull_request.number }}
|
|
43
100
|
`;
|
|
44
101
|
}
|
|
102
|
+
|
|
103
|
+
/** The job env setup-devenv needs, as the CI jobs spell it; nothing for a repository that declares no credential. */
|
|
104
|
+
function setupJobEnv(options: PrPreviewCleanupWorkflowOptions): string {
|
|
105
|
+
const tokenLines = `${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)}`;
|
|
106
|
+
return tokenLines === '' ? '' : ` env:\n${tokenLines}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The CI jobs' steps between checkout and setup-devenv, in their order and each
|
|
111
|
+
* set off by blank lines: the Cargo credential step, then the sibling-source
|
|
112
|
+
* clones. Nothing when neither is declared.
|
|
113
|
+
*/
|
|
114
|
+
function preflightSteps(options: PrPreviewCleanupWorkflowOptions): string {
|
|
115
|
+
const steps: string[][] = [];
|
|
116
|
+
if (options.cargoCredentials !== undefined) {
|
|
117
|
+
const step = { kind: CiWorkflowStepKind.CargoCredentials, name: 'Prepare Cargo credentials', number: 0 };
|
|
118
|
+
steps.push(cargoCredentialStepLines(step, options.cargoCredentials));
|
|
119
|
+
}
|
|
120
|
+
if (options.sourceCheckouts?.length) {
|
|
121
|
+
const step = { kind: CiWorkflowStepKind.SourceCheckouts, name: 'Check out sibling sources', number: 0 };
|
|
122
|
+
steps.push(sourceCheckoutsStepLines(step, options.sourceCheckouts));
|
|
123
|
+
}
|
|
124
|
+
return steps.map((lines) => `\n${lines.join('\n')}\n`).join('');
|
|
125
|
+
}
|
|
@@ -47,7 +47,7 @@ export interface PackagePublishConfig {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
export interface PackageSmooGithubEnvironments {
|
|
50
|
-
/** GitHub Environment for the staging deploy and
|
|
50
|
+
/** GitHub Environment for the staging deploy, e2e and PR preview cleanup jobs. */
|
|
51
51
|
staging?: string;
|
|
52
52
|
/** GitHub Environment for the production-on-push deploy job. */
|
|
53
53
|
production?: string;
|
|
@@ -273,6 +273,8 @@ export interface PackageJson {
|
|
|
273
273
|
optionalDependencies?: StringMap;
|
|
274
274
|
/** Bun patchedDependencies map (package@version → patch path). */
|
|
275
275
|
patchedDependencies?: StringMap;
|
|
276
|
+
overrides?: StringMap;
|
|
277
|
+
resolutions?: StringMap;
|
|
276
278
|
engines?: StringMap;
|
|
277
279
|
scripts?: StringMap;
|
|
278
280
|
publishConfig?: PackagePublishConfig;
|