@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
@@ -1,6 +1,6 @@
1
1
  import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
2
2
  import { spawn } from 'node:child_process';
3
- import { chmod, mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
3
+ import { chmod, mkdir, mkdtemp, readdir, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, join } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
@@ -9,7 +9,7 @@ import { stripVTControlCharacters } from 'node:util';
9
9
  import type { Task } from 'nx/src/config/task-graph';
10
10
 
11
11
  import { signalToCode } from 'nx/src/utils/exit-codes';
12
-
12
+ import { stopFixtureNxDaemon } from './__tests__/fixture-nx-env.js';
13
13
  import { cliExitOutcome, describeMiss, firstCacheMiss, parseTargetSelector, unvouched } from './ensure-built.js';
14
14
 
15
15
  function task(overrides: Partial<Task> & Pick<Task, 'id'>): Task {
@@ -163,6 +163,12 @@ const repoRoot = dirname(dirname(packageRoot));
163
163
  const binEntry = join(packageRoot, 'src', 'bin', 'smoo-nx-exec.ts');
164
164
  const builtBinEntry = join(packageRoot, 'dist', 'bin', 'smoo-nx-exec.js');
165
165
  const MARKER = 'EXEC_OK';
166
+ /**
167
+ * Caller directory overrides are deliberately stripped below. The fixture's
168
+ * own Nx configuration keeps both its cache and database under its removable
169
+ * root, even when smoo-nx-exec enters it from another workspace.
170
+ */
171
+ const FIXTURE_NX_JSON = { useDaemonProcess: true, cacheDirectory: '.nx/cache' };
166
172
  /** Nx keys controlled and reported by the fixture rather than inherited from its parent test task. */
167
173
  const NX_ENV_KEYS = [
168
174
  'NX_WORKSPACE_ROOT_PATH',
@@ -175,6 +181,12 @@ const NX_ENV_KEYS = [
175
181
  'NX_WORKSPACE_DATA_DIRECTORY',
176
182
  ];
177
183
  const SHARED_NX_DIRECTORY_KEYS = ['NX_CACHE_DIRECTORY', 'NX_WORKSPACE_DATA_DIRECTORY'] as const;
184
+ /**
185
+ * The caller's socket directory names one workspace's daemon, and Nx takes it
186
+ * literally. A fixture never inherits it, so a daemon it starts or stops can
187
+ * only touch its own socket; a test that wants one passes it explicitly.
188
+ */
189
+ const NX_SOCKET_ENV_KEYS = ['NX_SOCKET_DIR', 'NX_DAEMON_SOCKET_DIR'] as const;
178
190
 
179
191
  interface BinRun {
180
192
  readonly code: number | null;
@@ -188,6 +200,9 @@ function fixtureNxEnvironment(env: Readonly<NodeJS.ProcessEnv> = {}): NodeJS.Pro
188
200
  for (const key of NX_ENV_KEYS) {
189
201
  delete childEnv[key];
190
202
  }
203
+ for (const key of NX_SOCKET_ENV_KEYS) {
204
+ delete childEnv[key];
205
+ }
191
206
  // The parent Nx process may set FORCE_COLOR while the shell exports
192
207
  // NO_COLOR. Passing both to Bun emits a warning, which would make a genuine
193
208
  // full hit look noisy.
@@ -284,7 +299,7 @@ describe('smoo-nx-exec', () => {
284
299
  // this package is written against, including node_modules/.bin/nx for the
285
300
  // daemon-disabled path.
286
301
  await symlink(join(repoRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
287
- await writeFile(join(workspace, 'nx.json'), JSON.stringify({ useDaemonProcess: true }));
302
+ await writeFile(join(workspace, 'nx.json'), JSON.stringify(FIXTURE_NX_JSON));
288
303
  await writeFile(
289
304
  join(workspace, 'packages', 'lib', 'project.json'),
290
305
  JSON.stringify({
@@ -363,7 +378,7 @@ describe('smoo-nx-exec', () => {
363
378
 
364
379
  afterAll(async () => {
365
380
  if (workspace) {
366
- await nx(workspace, ['daemon', '--stop']);
381
+ await stopFixtureNxDaemon(workspace);
367
382
  await rm(workspace, { recursive: true, force: true });
368
383
  await rm(builds(), { force: true });
369
384
  await rm(ships(), { force: true });
@@ -395,6 +410,21 @@ describe('smoo-nx-exec', () => {
395
410
  expect(await readFile(builds(), 'utf-8')).toBe('lib\n');
396
411
  });
397
412
 
413
+ it('owns its cache and database after caller directory overrides are stripped', async () => {
414
+ const run = await runBin(workspace, [
415
+ 'app:build',
416
+ '--',
417
+ process.execPath,
418
+ '-e',
419
+ 'const nx = require("nx/src/utils/cache-directory"); ' +
420
+ 'console.log(JSON.stringify({ cache: nx.cacheDir, data: nx.sharedDataDirectory(process.cwd(), "workspace-data") }));',
421
+ ]);
422
+ expect(run.code, run.stdout + run.stderr).toBe(0);
423
+ expect(run.stdout).toContain(
424
+ JSON.stringify({ cache: join(workspace, '.nx/cache'), data: join(workspace, '.nx/workspace-data') }),
425
+ );
426
+ });
427
+
398
428
  it('stays silent through an uncacheable nx:noop aggregate, which has no command to run', async () => {
399
429
  const first = await runBin(workspace, ['app:ship', '--', './report']);
400
430
  expect(first.code, first.stdout + first.stderr).toBe(0);
@@ -610,16 +640,6 @@ describe('smoo-nx-exec', () => {
610
640
  expect(run.stdout).not.toContain(MARKER);
611
641
  });
612
642
 
613
- it('falls back to the workspace nx CLI when the daemon is disabled', async () => {
614
- const run = await runBin(workspace, ['app:build', '--', './report'], {
615
- NX_DAEMON: 'false',
616
- NX_VERBOSE_LOGGING: 'true',
617
- });
618
- expect(run.code).toBe(0);
619
- expect(run.stderr).toContain('daemon is disabled');
620
- expect(run.stdout.split('\n')[0], run.stdout).toBe(MARKER);
621
- });
622
-
623
643
  it('prints the message from a plain-object Nx rejection', async () => {
624
644
  const rejectingWorkspace = await realpath(await mkdtemp(join(tmpdir(), 'ensure-built-rejection-')));
625
645
  try {
@@ -674,13 +694,28 @@ describe('smoo-nx-exec', () => {
674
694
  describe('smoo-nx-exec signal forwarding', () => {
675
695
  let workspace = '';
676
696
 
677
- // A workspace whose `nx` is a script that kills itself. The daemon-disabled
678
- // path spawns exactly that binary, so this is the whole signal path end to
679
- // end — and deterministic, unlike racing a real build with a kill.
697
+ // A workspace whose `nx` is a script that kills itself. A target that has
698
+ // never run is a miss, and a miss spawns exactly that binary, so this is the
699
+ // whole signal path end to end — and deterministic, unlike racing a real
700
+ // build with a kill. The probe ahead of the miss is the real daemon's, so the
701
+ // workspace carries the repository's own Nx beside the fake CLI.
680
702
  beforeAll(async () => {
681
703
  workspace = await realpath(await mkdtemp(join(tmpdir(), 'ensure-built-signal-')));
704
+ const initialized = Bun.spawnSync(['git', 'init', '--quiet', workspace]);
705
+ expect(initialized.exitCode).toBe(0);
682
706
  await mkdir(join(workspace, 'node_modules', '.bin'), { recursive: true });
683
- await writeFile(join(workspace, 'nx.json'), '{}');
707
+ await symlink(join(repoRoot, 'node_modules', 'nx'), join(workspace, 'node_modules', 'nx'), 'dir');
708
+ await writeFile(join(workspace, 'nx.json'), JSON.stringify(FIXTURE_NX_JSON));
709
+ await mkdir(join(workspace, 'packages', 'app'), { recursive: true });
710
+ await writeFile(
711
+ join(workspace, 'packages', 'app', 'project.json'),
712
+ JSON.stringify({
713
+ name: 'app',
714
+ targets: {
715
+ build: { executor: 'nx:run-commands', cache: true, options: { command: 'true', cwd: '{projectRoot}' } },
716
+ },
717
+ }),
718
+ );
684
719
  const fakeNx = join(workspace, 'node_modules', '.bin', 'nx');
685
720
  await writeFile(fakeNx, '#!/bin/sh\nkill -TERM $$\n');
686
721
  await chmod(fakeNx, 0o755);
@@ -690,13 +725,80 @@ describe('smoo-nx-exec signal forwarding', () => {
690
725
 
691
726
  afterAll(async () => {
692
727
  if (workspace) {
693
- await rm(workspace, { recursive: true, force: true });
728
+ try {
729
+ await stopFixtureNxDaemon(workspace);
730
+ } finally {
731
+ await rm(workspace, { recursive: true, force: true });
732
+ }
694
733
  }
695
734
  });
696
735
 
697
736
  it('re-raises the signal that killed nx instead of flattening it to a code', async () => {
698
- const run = await runBin(workspace, ['app:build', '--', './report'], { NX_DAEMON: 'false' });
737
+ const run = await runBin(workspace, ['app:build', '--', './report']);
699
738
  expect(run.signal).toBe('SIGTERM');
700
739
  expect(run.stdout).not.toContain(MARKER);
701
740
  });
702
741
  });
742
+
743
+ describe('smoo-nx-exec daemon socket', () => {
744
+ let workspace = '';
745
+ let socketDir = '';
746
+ let callerDir = '';
747
+ const record = () => readFile(join(workspace, '.nx', 'workspace-data', 'd', 'server-process.json'), 'utf-8');
748
+
749
+ beforeAll(async () => {
750
+ workspace = await realpath(await mkdtemp(join(tmpdir(), 'ensure-built-socket-')));
751
+ // Under /tmp, not the platform temp directory: a socket path has a 95
752
+ // character budget and macOS's temp directory spends half of it.
753
+ socketDir = await mkdtemp(join('/tmp', 'eb-sock-'));
754
+ callerDir = await realpath(await mkdtemp(join(tmpdir(), 'ensure-built-caller-')));
755
+ const initialized = Bun.spawnSync(['git', 'init', '--quiet', workspace]);
756
+ expect(initialized.exitCode).toBe(0);
757
+ await symlink(join(repoRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
758
+ await writeFile(join(workspace, 'nx.json'), JSON.stringify(FIXTURE_NX_JSON));
759
+ await mkdir(join(workspace, 'packages', 'app'), { recursive: true });
760
+ await writeFile(
761
+ join(workspace, 'packages', 'app', 'project.json'),
762
+ JSON.stringify({
763
+ name: 'app',
764
+ targets: {
765
+ build: { executor: 'nx:run-commands', cache: true, options: { command: 'true', cwd: '{projectRoot}' } },
766
+ },
767
+ }),
768
+ );
769
+ });
770
+
771
+ afterAll(async () => {
772
+ try {
773
+ if (workspace) {
774
+ await stopFixtureNxDaemon(workspace);
775
+ }
776
+ } finally {
777
+ await rm(workspace, { recursive: true, force: true });
778
+ await rm(socketDir, { recursive: true, force: true });
779
+ await rm(callerDir, { recursive: true, force: true });
780
+ }
781
+ });
782
+
783
+ it('starts the daemon of a root the caller does not stand in on its own socket, not the caller\u0027s', async () => {
784
+ // The caller's environment was set up for another workspace, as a managed
785
+ // devenv's is: its socket directory belongs to that workspace's daemon,
786
+ // and a second daemon listening there would take the socket from it.
787
+ const stopped = await nx(workspace, ['daemon', '--stop']);
788
+ expect(stopped.code, stopped.stdout + stopped.stderr).toBe(0);
789
+ const run = await runBinWith('bun', binEntry, callerDir, ['app:build', '--workspace-root', workspace], {
790
+ NX_SOCKET_DIR: socketDir,
791
+ });
792
+ expect(run.code, run.stdout + run.stderr).toBe(0);
793
+ expect(await readdir(socketDir)).toEqual([]);
794
+ expect(await record()).not.toContain(socketDir);
795
+ });
796
+
797
+ it('keeps the socket directory of a caller standing in the root it was set up for', async () => {
798
+ const stopped = await nx(workspace, ['daemon', '--stop']);
799
+ expect(stopped.code, stopped.stdout + stopped.stderr).toBe(0);
800
+ const run = await runBin(workspace, ['app:build'], { NX_SOCKET_DIR: socketDir });
801
+ expect(run.code, run.stdout + run.stderr).toBe(0);
802
+ expect(await record()).toContain(join(socketDir, 'd.sock'));
803
+ });
804
+ });
@@ -1,9 +1,9 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { randomUUID } from 'node:crypto';
3
3
  import type { BigIntStats } from 'node:fs';
4
- import { closeSync, existsSync, lstatSync, openSync, readdirSync, readlinkSync, readSync } from 'node:fs';
4
+ import { closeSync, existsSync, lstatSync, openSync, readdirSync, readlinkSync, readSync, realpathSync } from 'node:fs';
5
5
  import { createRequire } from 'node:module';
6
- import { isAbsolute, join, resolve } from 'node:path';
6
+ import { dirname, isAbsolute, join, resolve } from 'node:path';
7
7
 
8
8
  import type * as NxConfiguration from 'nx/src/config/nx-json';
9
9
  import type { NxJsonConfiguration } from 'nx/src/config/nx-json';
@@ -239,13 +239,22 @@ export async function ensureBuilt(options: EnsureBuiltOptions): Promise<EnsureBu
239
239
  const workspaceRoot = resolve(options.cwd);
240
240
  const callerCwd = process.cwd();
241
241
  const callerEnv = { ...process.env };
242
+ // Whether the environment this process was launched with belongs to the
243
+ // workspace asked for, decided before the chdir and the rebinding below. The
244
+ // caller's workspace is the one its own Nx would bind: `NX_WORKSPACE_ROOT_PATH`
245
+ // when set, else the nearest `nx.json` above where it stands (none, when it
246
+ // stands in no workspace).
247
+ const callerRoot = callerEnv.NX_WORKSPACE_ROOT_PATH
248
+ ? resolve(callerCwd, callerEnv.NX_WORKSPACE_ROOT_PATH)
249
+ : findNxWorkspaceRoot(callerCwd);
250
+ const callerOwnsRoot = callerRoot !== null && isSameDirectory(callerRoot, workspaceRoot);
242
251
  try {
243
252
  // `cd <root> && nx run ...` is the invocation whose hashes are in the
244
253
  // cache. Everything below — plugin hooks, `runtime` inputs, and the hasher,
245
254
  // which keys against `process.cwd()` — has to see the same directory or the
246
255
  // probe keys differently from the CLI and never hits.
247
256
  process.chdir(workspaceRoot);
248
- return await ensureBuiltInWorkspace(workspaceRoot, selector, options.onMiss ?? 'stream');
257
+ return await ensureBuiltInWorkspace(workspaceRoot, selector, options.onMiss ?? 'stream', callerOwnsRoot);
249
258
  } finally {
250
259
  // Nx configures itself through the environment and this function runs
251
260
  // in-process, so without this the caller — and anything it goes on to exec
@@ -266,11 +275,79 @@ export async function ensureBuilt(options: EnsureBuiltOptions): Promise<EnsureBu
266
275
  }
267
276
  }
268
277
 
278
+ /**
279
+ * Nx's own overrides for where one workspace keeps its daemon socket, daemon
280
+ * record, task database and cache. Each names the state of a single
281
+ * workspace, and Nx takes it literally: a daemon spawned for another root
282
+ * with `NX_SOCKET_DIR` inherited listens on, and on stop deletes, the socket
283
+ * directory of the workspace that exported it; with
284
+ * `NX_WORKSPACE_DATA_DIRECTORY` it reads and overwrites that workspace's
285
+ * daemon record and task database; and the cache has to move with the
286
+ * database or a hit names artifacts the run never wrote.
287
+ */
288
+ const WORKSPACE_STATE_ENV_KEYS = [
289
+ 'NX_SOCKET_DIR',
290
+ 'NX_DAEMON_SOCKET_DIR',
291
+ 'NX_WORKSPACE_DATA_DIRECTORY',
292
+ 'NX_CACHE_DIRECTORY',
293
+ ] as const;
294
+
295
+ /**
296
+ * The nearest directory at or above `from` holding an `nx.json`, or null.
297
+ * The marker this wrapper roots a workspace at when `--workspace-root` is
298
+ * not given.
299
+ */
300
+ export function findNxWorkspaceRoot(from: string): string | null {
301
+ let directory = from;
302
+ for (;;) {
303
+ if (existsSync(join(directory, 'nx.json'))) {
304
+ return directory;
305
+ }
306
+ const parent = dirname(directory);
307
+ if (parent === directory) {
308
+ return null;
309
+ }
310
+ directory = parent;
311
+ }
312
+ }
313
+
314
+ function isSameDirectory(left: string, right: string): boolean {
315
+ try {
316
+ return realpathSync(left) === realpathSync(right);
317
+ } catch {
318
+ // A path that cannot be resolved cannot be shown to be the same
319
+ // workspace, and "not the same" is the isolating answer.
320
+ return false;
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Point every Nx this call starts — the daemon client loaded below and the
326
+ * `nx` CLI a miss spawns — at `workspaceRoot`, and give it that workspace's
327
+ * state locations. A caller that was set up for this same root keeps its
328
+ * overrides: they are the owner's cache and sandbox boundary. A caller set up
329
+ * for another root, or for none, passes none of them on, so Nx falls back to
330
+ * the per-root defaults. Nothing is reset or bypassed: the root's own daemon
331
+ * and cache are used as they stand. `ensureBuilt` restores the caller's
332
+ * environment when it returns.
333
+ */
334
+ function isolateWorkspaceEnvironment(workspaceRoot: string, callerOwnsRoot: boolean): void {
335
+ process.env.NX_WORKSPACE_ROOT_PATH = workspaceRoot;
336
+ if (callerOwnsRoot) {
337
+ return;
338
+ }
339
+ for (const key of WORKSPACE_STATE_ENV_KEYS) {
340
+ delete process.env[key];
341
+ }
342
+ }
343
+
269
344
  async function ensureBuiltInWorkspace(
270
345
  workspaceRoot: string,
271
346
  selector: TargetSelector,
272
347
  onMiss: 'stream',
348
+ callerOwnsRoot: boolean,
273
349
  ): Promise<EnsureBuiltResult> {
350
+ isolateWorkspaceEnvironment(workspaceRoot, callerOwnsRoot);
274
351
  // The environment has precedence over nx.json. Avoid loading Nx at all on
275
352
  // this explicit fallback path, which also lets a deliberately minimal
276
353
  // checkout-local CLI stand in for Nx.
@@ -388,10 +465,10 @@ function selectorTaskId(selector: TargetSelector): string {
388
465
  * a module-level singleton that reads `nx.json` from that root. So the root has
389
466
  * to be bound before the first Nx module loads, which is why this module
390
467
  * imports Nx dynamically throughout. A static import at the top of the file
391
- * would bind the caller's launch directory instead.
468
+ * would bind the caller's launch directory instead. `isolateWorkspaceEnvironment`
469
+ * has already set `NX_WORKSPACE_ROOT_PATH`; this loads Nx and checks it took.
392
470
  */
393
471
  function bindWorkspaceRoot(workspaceRoot: string, requireNx: WorkspaceNxRequire): void {
394
- process.env.NX_WORKSPACE_ROOT_PATH = workspaceRoot;
395
472
  if (process.env.NX_PERF_LOGGING === 'true') {
396
473
  // Installs Nx's PerformanceObserver, which reports every `performance
397
474
  // .measure` this module and Nx itself record. Loading it unconditionally