@smoothbricks/nx-plugin 0.4.24 → 0.4.26

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 (58) hide show
  1. package/README.md +147 -37
  2. package/dist/bin/smoo-nx-exec.js +3 -14
  3. package/dist/cargo-closure-input.d.ts +13 -15
  4. package/dist/cargo-closure-input.d.ts.map +1 -1
  5. package/dist/cargo-closure-input.js +29 -37
  6. package/dist/cargo-source-hash.d.ts +12 -7
  7. package/dist/cargo-source-hash.d.ts.map +1 -1
  8. package/dist/cargo-source-hash.js +416 -32
  9. package/dist/cargo-workspace.d.ts +7 -7
  10. package/dist/cargo-workspace.js +8 -8
  11. package/dist/ensure-built.d.ts +6 -0
  12. package/dist/ensure-built.d.ts.map +1 -1
  13. package/dist/ensure-built.js +79 -6
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +69 -96
  16. package/dist/managed-assets.d.ts +6 -0
  17. package/dist/managed-assets.d.ts.map +1 -1
  18. package/dist/managed-files/files.d.ts.map +1 -1
  19. package/dist/managed-files/files.js +6 -0
  20. package/dist/manifest-hash.d.ts.map +1 -1
  21. package/dist/manifest-hash.js +6 -2
  22. package/dist/workspace-manifest.d.ts +0 -1
  23. package/dist/workspace-manifest.d.ts.map +1 -1
  24. package/managed/raw/envrc +15 -0
  25. package/managed/raw/git-format-staged.yml +1 -0
  26. package/managed/raw/tooling/direnv/developer-links.ts +43 -11
  27. package/managed/raw/tooling/direnv/devenv.smoo.nix +5 -0
  28. package/managed/raw/tooling/direnv/inherited-devenv.ts +665 -0
  29. package/managed/raw/tooling/direnv/setup-environment.ts +123 -7
  30. package/nextest.toml +3 -37
  31. package/package.json +40 -3
  32. package/src/__tests__/counted-cargo-process.ts +286 -0
  33. package/src/__tests__/counted-cargo.ts +711 -0
  34. package/src/__tests__/fixture-cargo-home.ts +36 -0
  35. package/src/__tests__/fixture-nx-env.ts +93 -3
  36. package/src/bin/smoo-nx-exec.ts +3 -14
  37. package/src/bounded-test-policy.test.ts +0 -1
  38. package/src/cargo-closure-input.test.ts +313 -15
  39. package/src/cargo-closure-input.ts +42 -50
  40. package/src/cargo-resolve-cache.test.ts +975 -0
  41. package/src/cargo-source-hash.test.ts +17 -3
  42. package/src/cargo-source-hash.ts +447 -37
  43. package/src/cargo-workspace.ts +8 -8
  44. package/src/ensure-built.test.ts +122 -20
  45. package/src/ensure-built.ts +82 -5
  46. package/src/index.test.ts +203 -107
  47. package/src/index.ts +86 -115
  48. package/src/managed-assets.ts +12 -0
  49. package/src/managed-files/files.ts +6 -0
  50. package/src/manifest-hash.ts +7 -2
  51. package/src/nextest-partitions.test.ts +150 -0
  52. package/src/nx-cache-bound.test.ts +114 -0
  53. package/src/nx-resident-graph-workers.test.ts +121 -0
  54. package/src/nx-store-resolution.test.ts +98 -0
  55. package/src/task-history-namespace.test.ts +183 -0
  56. package/src/workspace-manifest.ts +0 -1
  57. package/test-timings.json +35 -0
  58. package/src/__tests__/in-process-nx-index.ts +0 -9
@@ -2,13 +2,13 @@ import { realpath } from 'node:fs/promises';
2
2
  import { isAbsolute, join, posix, relative, sep } from 'node:path';
3
3
 
4
4
  import type { TargetConfiguration } from 'nx/src/devkit-exports.js';
5
- import { globWithWorkspaceContext } from 'nx/src/utils/workspace-context.js';
5
+ import { globWithWorkspaceContext, globWithWorkspaceContextSync } from 'nx/src/utils/workspace-context.js';
6
+ import { workspaceRoot as daemonWorkspaceRoot } from 'nx/src/utils/workspace-root.js';
6
7
 
7
8
  import {
8
9
  CARGO_ANCESTOR_INPUTS,
9
10
  type CargoResolve,
10
11
  dependencyClosure,
11
- governingManifest,
12
12
  HASH_SKIPPED_DIRECTORIES,
13
13
  readCargoResolve,
14
14
  } from './cargo-source-hash.js';
@@ -22,6 +22,22 @@ export const CARGO_CLOSURE_INPUT = 'cargoClosure';
22
22
 
23
23
  type Input = NonNullable<TargetConfiguration['inputs']>[number];
24
24
 
25
+ /** A closure Nx cannot express exactly is a refusal, never a broader cache key. */
26
+ export class CargoClosureInputError extends Error {
27
+ readonly projectRoot: string;
28
+
29
+ constructor(projectRoot: string, cause: unknown) {
30
+ super(
31
+ `Cannot infer ${CARGO_CLOSURE_INPUT} for ${projectRoot}: ${cause instanceof Error ? cause.message : String(cause)}`,
32
+ {
33
+ cause,
34
+ },
35
+ );
36
+ this.name = 'CargoClosureInputError';
37
+ this.projectRoot = projectRoot;
38
+ }
39
+ }
40
+
25
41
  /**
26
42
  * How a runtime input reaches the hash command. Nx runs it with `sh -c` from
27
43
  * the workspace root and puts no `node_modules/.bin` on its PATH; a plugin Nx
@@ -40,17 +56,14 @@ const GLOB_SYNTAX = /[*?[\]{}()!,]/;
40
56
  * One Cargo workspace as one project-graph computation sees it. `manifest` is
41
57
  * the workspace's root manifest relative to the Nx workspace root.
42
58
  */
43
- export type CargoClosureSource =
44
- | {
45
- readonly kind: 'resolved';
46
- readonly manifest: string;
47
- /** The canonical Nx workspace root. */
48
- readonly root: string;
49
- readonly cargo: CargoResolve;
50
- /** Every `Cargo.toml` in Nx's file index, relative to the workspace root. */
51
- readonly indexed: ReadonlySet<string>;
52
- }
53
- | { readonly kind: 'unresolved'; readonly manifest: string };
59
+ export interface CargoClosureSource {
60
+ readonly manifest: string;
61
+ /** The canonical Nx workspace root. */
62
+ readonly root: string;
63
+ readonly cargo: CargoResolve;
64
+ /** Every `Cargo.toml` in Nx's file index, relative to the workspace root. */
65
+ readonly indexed: ReadonlySet<string>;
66
+ }
54
67
 
55
68
  /**
56
69
  * Every `Cargo.toml` Nx's workspace file index holds: .gitignore and .nxignore
@@ -58,40 +71,26 @@ export type CargoClosureSource =
58
71
  * here has no files a fileset input can hash.
59
72
  */
60
73
  export async function indexedCargoManifests(workspaceRoot: string): Promise<ReadonlySet<string>> {
61
- return new Set(await globWithWorkspaceContext(workspaceRoot, ['**/Cargo.toml']));
74
+ const manifests =
75
+ workspaceRoot === daemonWorkspaceRoot
76
+ ? await globWithWorkspaceContext(workspaceRoot, ['**/Cargo.toml'])
77
+ : globWithWorkspaceContextSync(workspaceRoot, ['**/Cargo.toml']);
78
+ return new Set(manifests);
62
79
  }
63
80
 
64
81
  /**
65
- * Locked, offline `cargo metadata` for one Cargo workspace, once per graph
66
- * computation. Never rejects: a workspace Cargo cannot resolve right now (no
67
- * cargo on PATH, a stale lockfile, an unfetched dependency) still gets its
68
- * closure inputs, hashed at run time by the command that reports the same
69
- * failure wherever a task needs it. Said once here, not once per project.
82
+ * Content-keyed locked offline metadata. A refusal fails graph inference:
83
+ * a stale closure or runtime whole-workspace fallback cannot establish the
84
+ * precise set of cache inputs.
70
85
  */
71
86
  export async function resolveCargoClosureSource(
72
87
  manifest: string,
73
88
  workspaceRoot: string,
74
89
  indexed: Promise<ReadonlySet<string>>,
75
90
  ): Promise<CargoClosureSource> {
76
- try {
77
- // One Promise.all, entered before any await, so a failed index listing is
78
- // handled here even when it settles before Cargo does.
79
- const [{ root, cargo }, index] = await Promise.all([
80
- realpath(workspaceRoot).then(async (root) => ({
81
- root,
82
- cargo: await readCargoResolve(join(root, manifest), root),
83
- })),
84
- indexed,
85
- ]);
86
- return { kind: 'resolved', manifest, root, cargo, indexed: index };
87
- } catch (error) {
88
- const message = error instanceof Error ? error.message : String(error);
89
- process.stderr.write(
90
- `@smoothbricks/nx-plugin: resolving ${manifest} failed (${message.trim().split('\n').slice(0, 3).join(' ')}); ` +
91
- `every ${CARGO_CLOSURE_INPUT} under it runs smoo-nx-cargo-hash on each hash instead of reading Nx's file index.\n`,
92
- );
93
- return { kind: 'unresolved', manifest };
94
- }
91
+ const [root, index] = await Promise.all([realpath(workspaceRoot), indexed]);
92
+ const cargo = await readCargoResolve(join(root, manifest), root, index);
93
+ return { manifest, root, cargo, indexed: index };
95
94
  }
96
95
 
97
96
  /**
@@ -110,10 +109,9 @@ export async function resolveCargoClosureSource(
110
109
  * exist. That command resolves the closure again when it runs, so a member
111
110
  * that only an edit outside the workspace brings in is still covered.
112
111
  *
113
- * When the closure cannot be expressed that way — Cargo could not resolve it
114
- * here, or a member inside the workspace is missing from Nx's file index —
115
- * the definition is the complete runtime hash instead: correct, slower, and
116
- * reported on stderr.
112
+ * An in-workspace member missing from Nx's file index is a typed refusal,
113
+ * never a whole-workspace runtime hash. Only outside-workspace members need
114
+ * the external-only runtime input.
117
115
  */
118
116
  export async function cargoClosureInputs(
119
117
  projectRoot: string,
@@ -138,8 +136,6 @@ export async function cargoClosureInputs(
138
136
  async function workspaceClosureInputs(projectRoot: string, source: CargoClosureSource): Promise<Input[]> {
139
137
  const lock = posix.join('{workspaceRoot}', posix.dirname(source.manifest), 'Cargo.lock');
140
138
  const closureArguments = `--closure ${shellWord(projectRoot)} ${shellWord(source.manifest)}`;
141
- const runtimeHash: Input[] = [lock, { runtime: `${CARGO_HASH_COMMAND} --include-workspace ${closureArguments}` }];
142
- if (source.kind === 'unresolved') return runtimeHash;
143
139
 
144
140
  const { root, cargo, indexed } = source;
145
141
  const packageDirectories: string[] = [];
@@ -158,7 +154,7 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
158
154
  throw new Error(`the Cargo package ${directory} is not in Nx's file index (an ignored directory)`);
159
155
  }
160
156
  packageDirectories.push(directory);
161
- for (const file of [...pkg.sources, await governingManifest(cargo, pkg, root)]) {
157
+ for (const file of [...pkg.sources, pkg.governingManifest]) {
162
158
  const path = indexablePath(root, file);
163
159
  if (path === null) throw new Error(`${directory} compiles ${file}, which Nx's file index cannot hold`);
164
160
  files.push(path);
@@ -167,11 +163,7 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
167
163
  const unglobbable = [...packageDirectories, ...files].find((path) => GLOB_SYNTAX.test(path));
168
164
  if (unglobbable !== undefined) throw new Error(`${unglobbable} contains glob syntax, so no fileset can name it`);
169
165
  } catch (error) {
170
- process.stderr.write(
171
- `@smoothbricks/nx-plugin: ${projectRoot}'s ${CARGO_CLOSURE_INPUT} runs smoo-nx-cargo-hash on each hash ` +
172
- `instead of reading Nx's file index: ${error instanceof Error ? error.message : String(error)}\n`,
173
- );
174
- return runtimeHash;
166
+ throw new CargoClosureInputError(projectRoot, error);
175
167
  }
176
168
 
177
169
  // A package nested in another member's directory is already covered by it.