@smoothbricks/nx-plugin 0.4.19 → 0.4.21

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.
Files changed (44) hide show
  1. package/README.md +68 -18
  2. package/dist/bin/smoo-nx-cargo-hash.js +5 -3
  3. package/dist/cargo-source-hash.d.ts +3 -11
  4. package/dist/cargo-source-hash.d.ts.map +1 -1
  5. package/dist/cargo-source-hash.js +25 -14
  6. package/dist/cargo-workspace.d.ts +0 -1
  7. package/dist/cargo-workspace.d.ts.map +1 -1
  8. package/dist/cargo-workspace.js +9 -2
  9. package/dist/ensure-built.d.ts +6 -5
  10. package/dist/ensure-built.d.ts.map +1 -1
  11. package/dist/ensure-built.js +227 -30
  12. package/dist/index.js +18 -10
  13. package/dist/managed-files/ci-workflow.d.ts +20 -0
  14. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  15. package/dist/managed-files/ci-workflow.js +42 -13
  16. package/dist/managed-files/files.d.ts +1 -1
  17. package/dist/managed-files/files.d.ts.map +1 -1
  18. package/dist/managed-files/files.js +8 -1
  19. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +37 -0
  20. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
  21. package/dist/managed-files/pr-preview-cleanup-workflow.js +34 -6
  22. package/dist/workspace-manifest.d.ts +3 -1
  23. package/dist/workspace-manifest.d.ts.map +1 -1
  24. package/managed/raw/tooling/direnv/setup-environment.ts +23 -91
  25. package/managed/raw/tooling/git-hooks/commit-msg.sh +6 -0
  26. package/managed/raw/tooling/git-hooks/pre-commit.sh +6 -0
  27. package/managed/raw/tooling/git-hooks/pre-push.sh +6 -0
  28. package/managed/templates/github/actions/setup-devenv/action.yml +19 -7
  29. package/package.json +1 -1
  30. package/src/bin/smoo-nx-cargo-hash.ts +7 -3
  31. package/src/cargo-source-hash.test.ts +237 -1
  32. package/src/cargo-source-hash.ts +32 -16
  33. package/src/cargo-workspace.test.ts +17 -10
  34. package/src/cargo-workspace.ts +9 -3
  35. package/src/ensure-built.test.ts +87 -29
  36. package/src/ensure-built.ts +274 -29
  37. package/src/index.test.ts +117 -20
  38. package/src/index.ts +18 -11
  39. package/src/managed-files/ci-workflow.ts +45 -14
  40. package/src/managed-files/files.ts +9 -2
  41. package/src/managed-files/generator.test.ts +5 -1
  42. package/src/managed-files/github-host-cache-isolation.test.ts +32 -6
  43. package/src/managed-files/pr-preview-cleanup-workflow.ts +87 -6
  44. package/src/workspace-manifest.ts +3 -1
@@ -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 ref. Pushes to the staging push branch queue behind a
244
- # running workflow instead of canceling it, so a newer push never cancels the
245
- # deploy job mid-flight. Pull requests and other branches keep canceling
246
- # superseded runs.
247
- group: \${{ github.workflow }}-\${{ github.ref }}
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 ref cancels its superseded
251
- # runs. Queuing them instead serializes the staging branch, and a burst of
252
- # pushes then reports the newest commit one full run per queued push late.
253
- group: \${{ github.workflow }}-\${{ github.ref }}
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
- function environmentLine(name: string | undefined): string {
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: Record<string, string> =
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({ runsOn: context.ciRunsOn });
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
- const first = workspace();
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
- { GITHUB_REF: 'refs/pull/11/merge' },
45
- { GITHUB_REF: 'refs/heads/main' },
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({ GITHUB_REF: 'refs/heads/feature/a' })).not.toBe(scope({ GITHUB_REF: 'refs/heads/feature-a' }));
57
- expect(scope({ GITHUB_REF: 'refs/heads/Feature' })).not.toBe(scope({ GITHUB_REF: 'refs/heads/feature' }));
58
- expect(scope({ GITHUB_REF: 'refs/heads/../../other' })).toMatch(/^[a-f0-9]{40}$/);
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
- env:
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 e2e jobs. */
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;