@smoothbricks/nx-plugin 0.5.1 → 0.5.2

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 (53) hide show
  1. package/README.md +55 -11
  2. package/dist/bin/smoo-nx-test-clocks.js +2 -2
  3. package/dist/bin/smoo-nx-test-walks.js +6 -9
  4. package/dist/cargo-closure-input.d.ts.map +1 -1
  5. package/dist/cargo-closure-input.js +25 -15
  6. package/dist/ensure-built-witness.d.ts +33 -5
  7. package/dist/ensure-built-witness.d.ts.map +1 -1
  8. package/dist/ensure-built-witness.js +84 -18
  9. package/dist/ensure-built.d.ts.map +1 -1
  10. package/dist/ensure-built.js +19 -5
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +4 -0
  13. package/dist/owned-installation.d.ts +6 -5
  14. package/dist/owned-installation.d.ts.map +1 -1
  15. package/dist/owned-installation.js +324 -201
  16. package/dist/test-walk-policy.d.ts +8 -0
  17. package/dist/test-walk-policy.d.ts.map +1 -1
  18. package/dist/test-walk-policy.js +34 -0
  19. package/dist/workspace-manifest.d.ts +5 -5
  20. package/dist/workspace-manifest.d.ts.map +1 -1
  21. package/managed/raw/patches/nx@23.2.1.patch +14 -3
  22. package/managed/raw/tooling/direnv/developer-links.ts +37 -10
  23. package/managed/raw/tooling/direnv/linked-snapshots.ts +31 -4
  24. package/managed/raw/tooling/direnv/setup-environment.ts +2 -0
  25. package/managed/raw/tooling/git-hooks/pre-push.sh +34 -11
  26. package/package.json +3 -3
  27. package/runtime-input.sh +21 -9
  28. package/src/__tests__/cargo-closure-filesets.ts +94 -0
  29. package/src/__tests__/owned-installation.ts +5 -6
  30. package/src/bin/smoo-nx-test-clocks.ts +2 -2
  31. package/src/bin/smoo-nx-test-walks.ts +6 -9
  32. package/src/cargo-closure-filesets.test.ts +63 -0
  33. package/src/cargo-closure-input.test.ts +1 -1
  34. package/src/cargo-closure-input.ts +23 -15
  35. package/src/cargo-closure-key.test.ts +1 -1
  36. package/src/cargo-resolve-cache.test.ts +1 -1
  37. package/src/cross-check-policy.test.ts +53 -12
  38. package/src/ensure-built-witness.test.ts +53 -2
  39. package/src/ensure-built-witness.ts +103 -18
  40. package/src/ensure-built.test.ts +40 -5
  41. package/src/ensure-built.ts +28 -6
  42. package/src/index.ts +4 -0
  43. package/src/linked-snapshots.nx.test.ts +1 -1
  44. package/src/nextest-partitions.test.ts +1 -1
  45. package/src/nx-daemon-validity.test.ts +51 -0
  46. package/src/owned-installation.test.ts +105 -0
  47. package/src/owned-installation.ts +335 -93
  48. package/src/runtime-input.test.ts +22 -1
  49. package/src/task-results.test.ts +1 -1
  50. package/src/test-walk-policy.test.ts +35 -0
  51. package/src/test-walk-policy.ts +32 -0
  52. package/src/workspace-manifest.ts +5 -5
  53. package/test-timings.json +94 -92
@@ -1,6 +1,6 @@
1
1
  import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'bun:test';
2
2
  import { spawn, spawnSync } from 'node:child_process';
3
- import { chmod, mkdir, mkdtemp, readdir, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
3
+ import { chmod, cp, mkdir, mkdtemp, readdir, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, isAbsolute, join } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
@@ -727,8 +727,8 @@ const FIXTURE_NX_JSON = {
727
727
  plugins: ['@smoothbricks/nx-plugin'],
728
728
  };
729
729
 
730
- /** The Nx installation this file's fixtures share: one copy per run of the file, not one per fixture. */
731
- const installation = testFileInstallation('ensure-built-installation-');
730
+ /** The Nx installation this file's fixtures share: one copy kept for this file, not one per fixture. */
731
+ const installation = testFileInstallation('ensure-built-installation');
732
732
 
733
733
  /**
734
734
  * Two projects, `app:build` reading `lib:build`'s outputs through
@@ -834,6 +834,17 @@ describe('smoo-nx-exec with a fresh daemon', () => {
834
834
  async ({ root, workspace, socketDirectory }) => {
835
835
  const builds = join(root, 'lib-builds.log');
836
836
  await writeTwoProjectWorkspace(workspace, builds);
837
+ // The workspace takes its own copy of TypeScript, which the census resolves from the workspace root, so the
838
+ // installation the script below writes into is the fixture's, not the copy this file's fixtures share.
839
+ const shared = await realpath(join(workspace, 'node_modules'));
840
+ await rm(join(workspace, 'node_modules'));
841
+ await mkdir(join(workspace, 'node_modules'));
842
+ for (const entry of await readdir(shared)) {
843
+ if (entry !== 'typescript') await symlink(join(shared, entry), join(workspace, 'node_modules', entry));
844
+ }
845
+ await cp(await realpath(join(shared, 'typescript')), join(workspace, 'node_modules', 'typescript'), {
846
+ recursive: true,
847
+ });
837
848
  const built = await run(
838
849
  'node',
839
850
  nxEntry(workspace),
@@ -850,7 +861,7 @@ describe('smoo-nx-exec with a fresh daemon', () => {
850
861
  import assert from 'node:assert/strict';
851
862
  import { existsSync } from 'node:fs';
852
863
  import { createRequire } from 'node:module';
853
- import { chmod, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
864
+ import { chmod, readFile, rename, rm, stat, utimes, writeFile } from 'node:fs/promises';
854
865
  import { join } from 'node:path';
855
866
  import { ensureBuilt } from ${JSON.stringify(join(packageRoot, 'dist', 'ensure-built.js'))};
856
867
  const requireNx = createRequire(process.cwd() + '/package.json');
@@ -894,6 +905,17 @@ await writeFile(project, JSON.stringify(config));
894
905
  await check('dependency');
895
906
  await writeFile(project, original);
896
907
  await check('dependency-restored');
908
+ // Every call censuses the installation again, taking the hashes of files whose stamps have not moved from the
909
+ // witness. A same-size rewrite that keeps its file's mtime moves its ctime: the next call reads it and sees the
910
+ // changed installation, and the call after that reuses the graph the changed installation was proved with.
911
+ const installed = join(process.cwd(), 'node_modules/typescript/README.md');
912
+ const readme = await readFile(installed);
913
+ const { atime, mtime } = await stat(installed);
914
+ readme[0] ^= 1;
915
+ await writeFile(installed, readme);
916
+ await utimes(installed, atime, mtime);
917
+ await check('installation');
918
+ await check('installation-reuse');
897
919
  // Save only evidence the real daemon generation actually authorized, outside
898
920
  // the workspace: the later missing-witness RPC is not obliged to replace it.
899
921
  const authoritativeWitness = await readFile(witness, 'utf8');
@@ -937,7 +959,14 @@ await writeFile(${JSON.stringify(resultsPath)}, JSON.stringify(results));
937
959
  disposition: 'hit',
938
960
  witnessExists: true,
939
961
  });
940
- for (const label of ['created', 'deleted', 'dependency', 'dependency-restored', 'unreadable-witness']) {
962
+ for (const label of [
963
+ 'created',
964
+ 'deleted',
965
+ 'dependency',
966
+ 'dependency-restored',
967
+ 'installation',
968
+ 'unreadable-witness',
969
+ ]) {
941
970
  expect(graphChecks.find((check) => check.label === label)?.requests, label).toBeGreaterThan(0);
942
971
  }
943
972
  expect(graphChecks.find(({ label }) => label === 'missing-witness')).toEqual({
@@ -946,6 +975,12 @@ await writeFile(${JSON.stringify(resultsPath)}, JSON.stringify(results));
946
975
  disposition: 'hit',
947
976
  witnessExists: false,
948
977
  });
978
+ expect(graphChecks.find(({ label }) => label === 'installation-reuse')).toEqual({
979
+ label: 'installation-reuse',
980
+ requests: 0,
981
+ disposition: 'hit',
982
+ witnessExists: true,
983
+ });
949
984
  expect(graphChecks.find(({ label }) => label === 'unreadable-witness')?.disposition).toBe('hit');
950
985
  expect(await readFile(builds, 'utf8')).toBe('lib\n');
951
986
  expect(await readFile(join(workspace, 'packages/app/dist/marker.txt'), 'utf8')).toBe('built\nlib\n');
@@ -45,11 +45,14 @@ import {
45
45
  censusStart,
46
46
  type GraphWitness,
47
47
  graphInstallationIdentity,
48
+ installationIdentityOf,
48
49
  movedNodes,
49
50
  nodeStamp,
50
51
  nodesUnchanged,
52
+ provenInstallationCensus,
51
53
  readArtifactWitness,
52
54
  readGraphWitness,
55
+ recordedInstallationCensus,
53
56
  recordedStamp,
54
57
  reusableGraph,
55
58
  snapshotsEqual,
@@ -1065,31 +1068,50 @@ async function currentProjectGraph(
1065
1068
  await daemonClient.updateWorkspaceContext(created, updated, deleted);
1066
1069
  }
1067
1070
  performance.measure('ensureBuilt:inputs', 'ensureBuilt:inputs:start');
1068
- const installation = graphInstallationIdentity(
1071
+ // The witness's census is a hash source: a file whose proven stamp has not moved since it was read is not read again.
1072
+ const recorded =
1073
+ witness === null ? undefined : recordedInstallationCensus(witness.installation, witness.installationUnproven);
1074
+ const localInputs = graphInputContractIdentity(workspaceRoot, nxJson, before);
1075
+ const census = provenInstallationCensus(
1069
1076
  workspaceRoot,
1070
1077
  nxJson,
1071
1078
  packageResolution.readModulePackageJson,
1072
1079
  expandOutputs,
1073
- graphInputContractIdentity(workspaceRoot, nxJson, before),
1080
+ localInputs,
1081
+ undefined,
1082
+ recorded,
1074
1083
  );
1084
+ const installation = census === null ? null : installationIdentityOf(census, localInputs);
1075
1085
  const cached = nodesUnchanged(before.nodes)
1076
1086
  ? reusableGraph(witness, workspaceRoot, environment, installation, before)
1077
1087
  : null;
1078
1088
  if (!forceGraphRpc && cached !== null) return cached;
1079
1089
  const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
1080
1090
  const after = workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), before);
1081
- const finalInstallation = graphInstallationIdentity(
1091
+ const finalInputs = graphInputContractIdentity(workspaceRoot, nxJson, after);
1092
+ const finalCensus = provenInstallationCensus(
1082
1093
  workspaceRoot,
1083
1094
  nxJson,
1084
1095
  packageResolution.readModulePackageJson,
1085
1096
  expandOutputs,
1086
- graphInputContractIdentity(workspaceRoot, nxJson, after),
1097
+ finalInputs,
1098
+ undefined,
1099
+ census ?? recorded,
1087
1100
  );
1101
+ const finalInstallation = finalCensus === null ? null : installationIdentityOf(finalCensus, finalInputs);
1088
1102
  // `after` re-hashed every node whose stamp does not prove it (its ctime alone moved, or `before` read it in the tick
1089
1103
  // it changed in), and snapshotsEqual compares those hashes.
1090
1104
  const stableGeneration = snapshotsEqual(before, after) && movedNodes(before.nodes, () => true) === null;
1091
- if (installation !== null && installation === finalInstallation && stableGeneration) {
1092
- const next: GraphWitness = { version: 1, workspaceRoot, environment, installation, snapshot: after, projectGraph };
1105
+ if (installation !== null && finalCensus !== null && installation === finalInstallation && stableGeneration) {
1106
+ const next: GraphWitness = {
1107
+ version: 2,
1108
+ workspaceRoot,
1109
+ environment,
1110
+ installation,
1111
+ installationUnproven: finalCensus.unproven,
1112
+ snapshot: after,
1113
+ projectGraph,
1114
+ };
1093
1115
  writeWitness(witnessPath, next);
1094
1116
  }
1095
1117
  // A write during the await cannot authorize a saved graph. Feed the final
package/src/index.ts CHANGED
@@ -83,6 +83,7 @@ import { runtimeInput, shellWord } from './runtime-input.js';
83
83
  import { isNonSourceDirectory } from './source-directories.js';
84
84
  import { postTasksExecution as postTaskResults, preTasksExecution as preTaskResults } from './task-results.js';
85
85
  import { SHARD_TIMINGS_EXECUTOR, SHARD_TIMINGS_TARGET, timingsShardedSuite } from './test-timings.js';
86
+ import { assertUpstreamBasePrecondition } from './test-walk-policy.js';
86
87
  import {
87
88
  isBuildOutputTargetName,
88
89
  isTestRunnerTargetName,
@@ -1076,6 +1077,9 @@ function isBuildOutputPackageJson(packageJsonPath: string): boolean {
1076
1077
  * (`run-cpu-pool.ts`), and closes it when the run ends.
1077
1078
  */
1078
1079
  export const preTasksExecution: PreTasksExecution<ProducerReceiptsOptions> = async (options, context) => {
1080
+ // Before any task is hashed: a workspace that declares the test-walk check
1081
+ // must be able to name its upstream base, or the run fails with that reason.
1082
+ assertUpstreamBasePrecondition(context.workspaceRoot);
1079
1083
  preTaskResults(options, context);
1080
1084
  await preProducerReceipts(options, context);
1081
1085
  await serveRunCpuPool(context);
@@ -14,7 +14,7 @@ import { testFileInstallation } from './__tests__/owned-installation.js';
14
14
  import { managedAssetsRoot } from './managed-assets.js';
15
15
 
16
16
  /** The Nx and the plugin product the provider and the consumer link, one copy this file owns; none may write into it. */
17
- const installation = testFileInstallation('linked-snapshot-installation-');
17
+ const installation = testFileInstallation('linked-snapshot-installation');
18
18
  const SNAPSHOTS = join(managedAssetsRoot, 'raw/tooling/direnv/linked-snapshots.ts');
19
19
  const DECLARATION = { targets: ['linked-api:build'], artifactModules: [] };
20
20
 
@@ -66,7 +66,7 @@ let bodyDone: Promise<void> = Promise.resolve();
66
66
  /** A failed barrier keeps the fixture, even if a later cleanup attempt succeeds. */
67
67
  let barrierFailure: string | undefined;
68
68
  /** The Nx installation the fixture links; it must leave it as it was made. */
69
- const installation = testFileInstallation('smoo-nextest-partitions-installation-');
69
+ const installation = testFileInstallation('smoo-nextest-partitions-installation');
70
70
 
71
71
  beforeAll(
72
72
  caseBody(async () => {
@@ -698,6 +698,57 @@ it('does not serve a request sent on a connection opened before a lockfile chang
698
698
  );
699
699
  });
700
700
 
701
+ it('stops a kept daemon on a lockfile change instead of spawning a successor its keeper does not own', async () => {
702
+ await withNxFixture(
703
+ 'nx-daemon-validity-kept-',
704
+ async ({ socketDirectory, workspace }) => {
705
+ await fixtureWorkspace(workspace);
706
+ const lock = join(workspace, 'yarn.lock');
707
+ await writeFile(lock, 'lock one\n');
708
+ const env = fixtureNxEnv(workspace, socketDirectory);
709
+ // As cowshed's keeper job starts it: the daemon's own entry in the foreground, kept.
710
+ const kept = spawn('node', [daemonStart, '--kept'], { cwd: workspace, env, stdio: ['ignore', 'pipe', 'pipe'] });
711
+ let log = '';
712
+ kept.stdout.setEncoding('utf8').on('data', (text: string) => {
713
+ log += text;
714
+ });
715
+ kept.stderr.setEncoding('utf8').on('data', (text: string) => {
716
+ log += text;
717
+ });
718
+ const exited = Promise.withResolvers<number | null>();
719
+ kept.once('exit', (code) => exited.resolve(code));
720
+ try {
721
+ const record = await watchUntil(
722
+ dirname(recordPath(workspace)),
723
+ 'the kept daemon to write its record',
724
+ async () => {
725
+ const current = await daemonRecord(workspace);
726
+ return current !== null && current.processId === kept.pid ? current : null;
727
+ },
728
+ async () => {},
729
+ );
730
+ const daemon = await readyDaemon(workspace, record);
731
+ await writeFile(lock, 'lock two\n');
732
+ assertNotServed(
733
+ await requestOnce(daemon.record.socketPath, ping(workspace), 'a PING sent after the lockfile changed'),
734
+ 'a PING sent to the kept daemon after the lockfile changed',
735
+ );
736
+ await guardEvent(exited.promise, 'the kept daemon to exit after the lockfile changed', () => log);
737
+ // The decision is in the daemon's own log, written before it exits: it stopped, and started nothing.
738
+ expect(log, 'the kept daemon did not say it left its successor to its keeper').toContain(
739
+ 'Stopping: the process that started this daemon (--kept) starts its successor',
740
+ );
741
+ expect(log, 'the kept daemon spawned a successor of its own').not.toContain('Restarting daemon');
742
+ expect(log, 'the kept daemon spawned a successor of its own').not.toContain('Started new daemon process');
743
+ } finally {
744
+ if (kept.exitCode === null && kept.signalCode === null && kept.pid !== undefined) terminate(kept.pid);
745
+ await exited.promise;
746
+ }
747
+ },
748
+ FIXTURE_WORKSPACE,
749
+ );
750
+ });
751
+
701
752
  it('stops a daemon at the next request after an ignored lockfile changes, then its successor within its safety poll with no request', async () => {
702
753
  await withNxFixture(
703
754
  'nx-daemon-validity-ignored-',
@@ -0,0 +1,105 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
2
+ import { lstat, mkdir, mkdtemp, readdir, readlink, realpath, rm, stat, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { basename, dirname, join } from 'node:path';
5
+
6
+ /** The tool cache a census root keeps its copies in, a real directory as on a fresh CI checkout. */
7
+ const KEPT = join('node_modules', '.cache', 'smoothbricks-nx-plugin', 'owned-installations');
8
+
9
+ let censusRoot = '';
10
+
11
+ /**
12
+ * A census root whose closure is three small packages: Nx, this plugin and the dependency the plugin declares,
13
+ * installed in its own `node_modules` beside a real (not linked) `node_modules/.cache`. A copy kept in that cache lies
14
+ * beneath this `node_modules`, so Node's ancestor lookup from the copy reaches every host package.
15
+ */
16
+ beforeEach(async () => {
17
+ censusRoot = await realpath(await mkdtemp(join(tmpdir(), 'owned-installation-')));
18
+ await writeFile(join(censusRoot, 'package.json'), '{"name":"census-root","private":true}');
19
+ const install = async (name: string, manifest: object) => {
20
+ const root = join(censusRoot, 'node_modules', name);
21
+ await mkdir(root, { recursive: true });
22
+ await writeFile(join(root, 'package.json'), JSON.stringify({ name, version: '1.0.0', ...manifest }));
23
+ await writeFile(join(root, 'index.js'), `module.exports = ${JSON.stringify(name)};\n`);
24
+ };
25
+ await install('nx', {});
26
+ await install('@smoothbricks/nx-plugin', { dependencies: { dependency: '1.0.0' } });
27
+ await install('dependency', {});
28
+ await mkdir(join(censusRoot, KEPT), { recursive: true });
29
+ });
30
+ afterEach(async () => {
31
+ await rm(censusRoot, { recursive: true, force: true });
32
+ });
33
+
34
+ /**
35
+ * Take the copy kept as `name` and retire it in a process of its own, as one run of a test file does: a process
36
+ * resolves each module once, so a take that shares this one's resolutions would not stand for the next run.
37
+ */
38
+ function take(name: string): { readonly modules: string; readonly stderr: string } {
39
+ const run = Bun.spawnSync(
40
+ [
41
+ process.execPath,
42
+ '--preload',
43
+ '@smoothbricks/validation/bun/preload',
44
+ '-e',
45
+ `const { ownedInstallation } = await import(${JSON.stringify(join(import.meta.dir, 'owned-installation.ts'))});
46
+ const installation = ownedInstallation(${JSON.stringify(censusRoot)}, ${JSON.stringify(name)});
47
+ process.stdout.write(await installation.nodeModules());
48
+ await installation.retire();`,
49
+ ],
50
+ { cwd: dirname(import.meta.dir) },
51
+ );
52
+ const stderr = run.stderr.toString();
53
+ expect(run.exitCode, stderr).toBe(0);
54
+ return { modules: run.stdout.toString(), stderr };
55
+ }
56
+
57
+ /** Where `path`, read through every link, lies; asserted to be inside the copy whose `node_modules` is `modules`. */
58
+ async function inCopy(modules: string, path: string): Promise<void> {
59
+ expect(await realpath(path)).toStartWith(`${dirname(modules)}/`);
60
+ }
61
+
62
+ describe('a kept fixture installation', () => {
63
+ it('is made from an empty cache whose ancestors hold the host packages, and resolves nothing outside itself', async () => {
64
+ const { modules, stderr } = take('fresh');
65
+ expect(stderr).toBe('');
66
+ expect(dirname(dirname(modules))).toBe(join(censusRoot, KEPT, 'fresh'));
67
+ await inCopy(modules, join(modules, 'nx'));
68
+ await inCopy(modules, join(modules, '@smoothbricks/nx-plugin'));
69
+ await inCopy(modules, join(await realpath(join(modules, '@smoothbricks/nx-plugin')), '..', '..', 'dependency'));
70
+ // Kept: the next run proves the same files and copies nothing.
71
+ const index = join(modules, 'nx', 'index.js');
72
+ const made = (await stat(index)).ino;
73
+ expect(take('fresh')).toEqual({ modules, stderr: '' });
74
+ expect((await stat(index)).ino).toBe(made);
75
+ });
76
+
77
+ it('makes again, elsewhere, a kept copy whose census would resolve a package from the host, and says why', async () => {
78
+ const first = take('escaping');
79
+ await rm(join(first.modules, 'nx'));
80
+ const again = take('escaping');
81
+ const host = join(censusRoot, 'node_modules', 'nx', 'package.json');
82
+ expect(again.stderr).toContain('failed its proof, copying again');
83
+ expect(again.stderr).toContain(`resolves nx from ${dirname(first.modules)} to ${host}, outside the copy`);
84
+ expect(again.modules).not.toBe(first.modules);
85
+ expect((await lstat(join(again.modules, 'nx'))).isSymbolicLink()).toBe(true);
86
+ await inCopy(again.modules, join(again.modules, 'nx'));
87
+ expect((await readdir(join(censusRoot, KEPT, 'escaping'))).sort()).toEqual(
88
+ ['censuses.json', basename(dirname(again.modules))].sort(),
89
+ );
90
+ expect(await readlink(join(again.modules, 'nx'))).not.toStartWith('/');
91
+ });
92
+
93
+ it('copies again only the package whose host bytes changed, naming it', async () => {
94
+ const { modules } = take('changed');
95
+ const kept = join(modules, '@smoothbricks/nx-plugin', 'index.js');
96
+ const untouched = (await stat(kept)).ino;
97
+ await writeFile(join(censusRoot, 'node_modules', 'nx', 'index.js'), 'module.exports = "changed";\n');
98
+ const again = take('changed');
99
+ expect(again.modules).toBe(modules);
100
+ expect(again.stderr).toContain('1 of 3 packages no longer hold what the host closure holds');
101
+ expect(again.stderr).toContain(join(censusRoot, 'node_modules', 'nx'));
102
+ expect(await Bun.file(join(modules, 'nx', 'index.js')).text()).toBe('module.exports = "changed";\n');
103
+ expect((await stat(kept)).ino).toBe(untouched);
104
+ });
105
+ });