@smoothbricks/nx-plugin 0.3.12 → 0.3.14

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/src/index.test.ts CHANGED
@@ -4,17 +4,15 @@ import { tmpdir } from 'node:os';
4
4
  import { dirname, join } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
 
7
- import type { CreateNodesContextV2, TargetConfiguration } from 'nx/src/devkit-exports.js';
7
+ import type { CreateNodesContextV2, CreateNodesV2, TargetConfiguration } from 'nx/src/devkit-exports.js';
8
8
  import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
9
9
  import { mergeTargetConfigurations } from 'nx/src/project-graph/utils/project-configuration-utils.js';
10
10
  import { BOUNDED_TEST_TIMEOUT_MS } from './bounded-test-policy.js';
11
11
  import { exceptionalTestFilter } from './cargo-workspace.js';
12
12
  import { CARGO_CROSS_LINT_COMMAND, CARGO_CROSS_LINT_TARGET, CARGO_LINT_CLIPPY_COMMAND } from './cross-check-policy.js';
13
- import { createNodesV2 } from './index.js';
14
- import { BUILD_OUTPUT_DEPENDENCIES } from './workspace-config-policy.js';
13
+ import { createNodesV2, createNodesV2ForPlatform } from './index.js';
15
14
 
16
15
  const [, inferTargets] = createNodesV2;
17
- const buildOutputDependencies = ['^build', ...BUILD_OUTPUT_DEPENDENCIES];
18
16
 
19
17
  describe('@smoothbricks/nx-plugin inferred targets', () => {
20
18
  it('names standalone package projects from package metadata', async () => {
@@ -109,7 +107,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
109
107
  ]);
110
108
  expect(targets.build?.executor).toBe('nx:noop');
111
109
  expect(targets.build?.cache).toBe(true);
112
- expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
110
+ expect(targets.build?.dependsOn).toEqual(['^build', 'tsc-js']);
113
111
  // An `nx:noop` aggregate writes no file, so it must claim none: a
114
112
  // `{projectRoot}/dist` claim here would double-cache its children's bytes
115
113
  // and make `github-ci nx-run-many --collect-outputs` attribute every file
@@ -206,7 +204,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
206
204
  expect(targets['tsc-js']).toBeUndefined();
207
205
  expect(targets['tsdown-js']).toBeUndefined();
208
206
  expect(targets.build?.executor).toBe('nx:noop');
209
- expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
207
+ expect(targets.build?.dependsOn).toEqual(['^build', 'tsdown-js']);
210
208
  expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
211
209
  } finally {
212
210
  await workspace.cleanup();
@@ -234,9 +232,10 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
234
232
 
235
233
  expect(targets.build).toBeUndefined();
236
234
  expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
237
- expect(BUILD_OUTPUT_DEPENDENCIES).not.toContain('*-ios');
238
- expect(BUILD_OUTPUT_DEPENDENCIES).not.toContain('*-macos');
239
- expect(BUILD_OUTPUT_DEPENDENCIES).not.toContain('*-linux');
235
+ // No ordinary aggregate above is the whole assertion: a platform suffix
236
+ // is not an output family, so `bundle-ios`/`-macos`/`-linux` give this
237
+ // project nothing for `build` to aggregate — only a `clean` to own their
238
+ // outputs.
240
239
  } finally {
241
240
  await workspace.cleanup();
242
241
  }
@@ -431,7 +430,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
431
430
  });
432
431
  expect(targets['tsc-js']?.dependsOn).toEqual(['^*-js', 'cargo-wasm']);
433
432
  expect(targets.typecheck?.dependsOn).toEqual(['^*-js', 'cargo-wasm']);
434
- expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
433
+ expect(targets.build?.dependsOn).toEqual(['^build', 'cargo-wasm', 'tsc-js']);
435
434
  expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
436
435
  // A nested crate manifest is enough for output inference, but not for
437
436
  // workspace-wide cargo-test/lint policy.
@@ -482,83 +481,161 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
482
481
 
483
482
  expect(targets['cargo-wasm']).toBeUndefined();
484
483
  expect(targets['tsc-js']?.outputs).toEqual(['{projectRoot}/dist/ts']);
485
- // Match the production host suffix so both native-platform and dedicated
486
- // cargo-napi host-provider regimes are covered on every runner.
484
+ // This fixture declares all four supported triples, so on EVERY supported
485
+ // runner the host's own triple is present and native: the host provider is
486
+ // that platform target and `cargo-napi` is never inferred. Tying the native
487
+ // regime to `-macos` asserted a macOS-only model and failed on Linux with
488
+ // zero host providers found. The dedicated `cargo-napi` regime is covered
489
+ // below against a platform no declared triple can serve, which reaches it
490
+ // on every runner instead of only on Linux.
487
491
  const hostOs = process.platform === 'darwin' ? 'macos' : process.platform === 'linux' ? 'linux' : null;
488
492
  const hostArch = process.arch === 'arm64' ? 'arm64' : process.arch === 'x64' ? 'x64' : null;
489
493
  const hostSuffix = hostOs !== null && hostArch !== null ? `-${hostArch}-${hostOs}` : null;
490
- const hostPlatformNapiTarget = hostSuffix === null ? null : `napi${hostSuffix}`;
491
- const nativeHostProvider = hostPlatformNapiTarget?.endsWith('-macos') ? hostPlatformNapiTarget : null;
492
- const hostProvider = nativeHostProvider ?? 'cargo-napi';
493
- const hostProviders = ['cargo-napi', nativeHostProvider].filter(
494
- (name): name is string => name !== null && targets[name] !== undefined,
495
- );
496
- expect(hostProviders).toHaveLength(1);
497
- expect(hostProviders[0]).toBe(hostProvider);
498
- if (hostProvider === 'cargo-napi') {
499
- expect(targets['cargo-napi']).toMatchObject({
500
- executor: 'nx:run-commands',
501
- cache: true,
502
- dependsOn: ['^build'],
503
- outputs: ['{projectRoot}/dist/native/host'],
504
- options: {
505
- cwd: 'packages/cowshed',
506
- command:
507
- 'napi build --release --platform --no-js --dts cowshed.napi.d.ts --manifest-path crates/cowshed-napi/Cargo.toml --package cowshed-napi --output-dir dist/native/host',
508
- },
509
- });
510
- } else {
511
- expect(targets['cargo-napi']).toBeUndefined();
512
- }
494
+ expect(hostSuffix).not.toBeNull();
495
+ expect(targets[`napi${hostSuffix}`]).toBeDefined();
496
+ expect(targets['cargo-napi']).toBeUndefined();
497
+
498
+ // A platform outside NAPI_TARGET_CONVENTIONS leaves no native host target,
499
+ // which is precisely the condition that infers the dedicated host build.
500
+ const unservedHostTargets = await inferProjectTargets(
501
+ workspace,
502
+ 'packages/cowshed/package.json',
503
+ createNodesV2ForPlatform('win32', 'x64'),
504
+ );
505
+ expect(unservedHostTargets['cargo-napi']).toMatchObject({
506
+ executor: 'nx:run-commands',
507
+ cache: true,
508
+ // The dedicated host build is another cargo writer on the default
509
+ // `target/`, and `build` lists it beside cargo-test-compile.
510
+ dependsOn: ['^build', 'cargo-test-compile'],
511
+ outputs: ['{projectRoot}/dist/native/host'],
512
+ options: {
513
+ cwd: 'packages/cowshed',
514
+ command:
515
+ 'napi build --release --platform --no-js --dts cowshed.napi.d.ts --manifest-path crates/cowshed-napi/Cargo.toml --package cowshed-napi --output-dir dist/native/host',
516
+ },
517
+ });
518
+ expect(unservedHostTargets.build?.dependsOn).toEqual(['^build', 'cargo-test-compile', 'cargo-napi', 'tsc-js']);
513
519
  expect(targets['napi-arm64-macos']?.outputs).toEqual(['{projectRoot}/dist/native/darwin-arm64']);
514
520
  expect(targets['napi-arm64-macos']?.options).toMatchObject({
515
521
  command:
516
522
  'napi build --release --platform --no-js --dts cowshed.darwin-arm64.d.ts --target aarch64-apple-darwin --manifest-path crates/cowshed-napi/Cargo.toml --package cowshed-napi --output-dir dist/native/darwin-arm64',
517
523
  });
518
524
  expect(targets['napi-arm64-macos']?.options?.env).toBeUndefined();
525
+ // No cargo-test-compile edge here, on ANY host: a platform target's
526
+ // dependency closure may only reach its own family
527
+ // (`validatePlatformTargetDependencies`), so this pair serializes on
528
+ // cargo's flock rather than on a graph edge.
519
529
  expect(targets['napi-arm64-macos']?.dependsOn).toBeUndefined();
520
- expect(targets['napi-toolchain-arm64-linux']).toEqual({
530
+ // A macOS triple never gets a cross toolchain: `usesNapiCross` is
531
+ // `family === 'linux' && target !== host`, so the family decides this one
532
+ // and no host can change it.
533
+ expect(targets['napi-toolchain-x64-macos']).toBeUndefined();
534
+ // Everything else about the cross regime depends on WHICH host is
535
+ // inferring — on an x64 Linux runner `napi-x64-linux` is the native build,
536
+ // with no `--use-napi-cross` and no toolchain prerequisite at all. Those
537
+ // facts are asserted against forced platforms below instead of against
538
+ // whichever machine happens to run the suite.
539
+
540
+ // Exercise both host branches explicitly: this suite usually runs on
541
+ // Darwin, but Linux's native compiler selection is the contract at risk.
542
+ const linuxX64Targets = await inferProjectTargets(
543
+ workspace,
544
+ 'packages/cowshed/package.json',
545
+ createNodesV2ForPlatform('linux', 'x64'),
546
+ );
547
+ expect(linuxX64Targets['napi-x64-linux']?.options?.command).not.toContain('--use-napi-cross');
548
+ expect(linuxX64Targets['napi-x64-linux']?.options?.env).toEqual({ CC: 'cc', CXX: 'c++' });
549
+ expect(linuxX64Targets['napi-x64-linux']?.dependsOn).toBeUndefined();
550
+ expect(linuxX64Targets['napi-toolchain-x64-linux']).toBeUndefined();
551
+ expect(linuxX64Targets['napi-arm64-linux']?.options?.command).toContain('--use-napi-cross');
552
+ expect(linuxX64Targets['napi-arm64-linux']?.options?.env).toEqual({
553
+ TARGET_CC: 'clang',
554
+ TARGET_CXX: 'clang++',
555
+ });
556
+ expect(linuxX64Targets['napi-debug']?.options?.env).toEqual({ CC: 'cc', CXX: 'c++' });
557
+
558
+ const darwinArm64Targets = await inferProjectTargets(
559
+ workspace,
560
+ 'packages/cowshed/package.json',
561
+ createNodesV2ForPlatform('darwin', 'arm64'),
562
+ );
563
+ expect(darwinArm64Targets['napi-x64-linux']?.options?.command).toContain('--use-napi-cross');
564
+ expect(darwinArm64Targets['napi-x64-linux']?.options?.env).toEqual({
565
+ TARGET_CC: 'clang',
566
+ TARGET_CXX: 'clang++',
567
+ });
568
+ expect(darwinArm64Targets['napi-x64-linux']?.dependsOn).toEqual(['napi-toolchain-x64-linux']);
569
+ expect(darwinArm64Targets['napi-debug']?.options?.env).toBeUndefined();
570
+ expect(darwinArm64Targets['napi-toolchain-arm64-linux']).toEqual({
521
571
  executor: '@smoothbricks/nx-plugin:napi-cross-toolchain',
522
572
  cache: false,
523
573
  options: { triple: 'aarch64-unknown-linux-gnu' },
524
574
  });
525
- expect(targets['napi-toolchain-x64-linux']).toEqual({
575
+ expect(darwinArm64Targets['napi-toolchain-x64-linux']).toEqual({
526
576
  executor: '@smoothbricks/nx-plugin:napi-cross-toolchain',
527
577
  cache: false,
528
578
  options: { triple: 'x86_64-unknown-linux-gnu' },
529
579
  });
530
- expect(targets['napi-x64-linux']?.dependsOn).toEqual(['napi-toolchain-x64-linux']);
531
- expect(targets['napi-arm64-linux']?.dependsOn).toEqual(['napi-toolchain-arm64-linux']);
532
- expect(targets['napi-toolchain-x64-macos']).toBeUndefined();
533
- expect(targets['napi-x64-linux']?.outputs).toEqual(['{projectRoot}/dist/native/linux-x64-gnu']);
534
- expect(targets['napi-x64-linux']?.options).toMatchObject({
535
- command:
536
- 'napi build --release --platform --no-js --dts cowshed.linux-x64-gnu.d.ts --target x86_64-unknown-linux-gnu --use-napi-cross --manifest-path crates/cowshed-napi/Cargo.toml --package cowshed-napi --output-dir dist/native/linux-x64-gnu',
537
- env: { TARGET_CC: 'clang', TARGET_CXX: 'clang++' },
538
- });
539
- // The aggregate build pulls in exactly the HOST's platform-suffixed
540
- // targets (publish still owns foreign platforms).
541
- const hostNapiTargets = [
542
- 'cli-arm64-macos',
543
- 'cli-x64-linux',
544
- 'napi-arm64-macos',
545
- 'napi-x64-macos',
546
- 'napi-arm64-linux',
547
- 'napi-x64-linux',
548
- ]
549
- .filter((name) => hostSuffix !== null && name.endsWith(hostSuffix))
550
- .sort();
551
- expect(targets.build?.dependsOn).toEqual([...buildOutputDependencies, ...hostNapiTargets]);
552
- const platformBuildDependencies = (targets.build?.dependsOn ?? []).filter(
553
- (dependency): dependency is string =>
554
- typeof dependency === 'string' && /-(?:arm64|x64)-(?:macos|linux)$/.test(dependency),
555
- );
556
- if (hostSuffix !== null) {
557
- for (const name of platformBuildDependencies) {
558
- expect(name.endsWith(hostSuffix)).toBe(true);
580
+ expect(darwinArm64Targets['napi-arm64-linux']?.dependsOn).toEqual(['napi-toolchain-arm64-linux']);
581
+ expect(darwinArm64Targets['napi-x64-linux']?.outputs).toEqual(['{projectRoot}/dist/native/linux-x64-gnu']);
582
+ expect(darwinArm64Targets['napi-x64-linux']?.options?.command).toBe(
583
+ 'napi build --release --platform --no-js --dts cowshed.linux-x64-gnu.d.ts --target x86_64-unknown-linux-gnu --use-napi-cross --manifest-path crates/cowshed-napi/Cargo.toml --package cowshed-napi --output-dir dist/native/linux-x64-gnu',
584
+ );
585
+
586
+ // The whole rule, stated once per forced host: a triple gets a cross
587
+ // toolchain exactly when it is a linux triple that is not this host's own.
588
+ // Asserting the pair by name is what broke on Linux, where
589
+ // napi-toolchain-x64-linux legitimately does not exist.
590
+ for (const [inferred, hostTriple] of [
591
+ [linuxX64Targets, 'x64-linux'],
592
+ [darwinArm64Targets, 'arm64-macos'],
593
+ ] as const) {
594
+ for (const suffix of ['arm64-linux', 'x64-linux', 'arm64-macos', 'x64-macos']) {
595
+ const expectsToolchain = suffix.endsWith('-linux') && suffix !== hostTriple;
596
+ expect(inferred[`napi-toolchain-${suffix}`] === undefined).toBe(!expectsToolchain);
597
+ expect(inferred[`napi-${suffix}`]?.options?.command).toContain(
598
+ expectsToolchain ? '--use-napi-cross' : '--target',
599
+ );
600
+ }
601
+ }
602
+ // The aggregate build pulls in exactly the inferring host's
603
+ // platform-suffixed targets (publish still owns foreign platforms), and
604
+ // the only cargo-test work it owns is compiling the executables — every
605
+ // RUNNER is reached through `test`. Both hosts are asserted here so the
606
+ // expectation does not depend on which machine runs the suite: the
607
+ // previous form read `process.platform`, and the macOS answer was the only
608
+ // one anyone checked.
609
+ for (const [inferred, hostSuffix] of [
610
+ [linuxX64Targets, '-x64-linux'],
611
+ [darwinArm64Targets, '-arm64-macos'],
612
+ ] as const) {
613
+ const hostNapiTargets = [
614
+ 'cli-arm64-macos',
615
+ 'cli-x64-linux',
616
+ 'napi-arm64-macos',
617
+ 'napi-x64-macos',
618
+ 'napi-arm64-linux',
619
+ 'napi-x64-linux',
620
+ ]
621
+ .filter((name) => name.endsWith(hostSuffix))
622
+ .sort();
623
+ expect(hostNapiTargets.length).toBeGreaterThan(0);
624
+ expect(inferred.build?.dependsOn).toEqual(['^build', 'cargo-test-compile', 'tsc-js', ...hostNapiTargets]);
625
+ // Crate `cowshed-napi` names its bounded runner
626
+ // `cargo-test-cowshed-napi`, which the retired `*-napi` output-family
627
+ // glob matched on suffix alone. The runners are one serialized chain, so
628
+ // that single edge put the whole cargo test suite inside
629
+ // `nx run-many -t build`.
630
+ expect(inferred['cargo-test-cowshed-napi']).toBeDefined();
631
+ for (const dependency of inferred.build?.dependsOn ?? []) {
632
+ expect(String(dependency).startsWith('cargo-test-')).toBe(dependency === 'cargo-test-compile');
633
+ }
634
+ for (const dependency of inferred.build?.dependsOn ?? []) {
635
+ if (typeof dependency === 'string' && /-(?:arm64|x64)-(?:macos|linux)$/.test(dependency)) {
636
+ expect(dependency.endsWith(hostSuffix)).toBe(true);
637
+ }
559
638
  }
560
- } else {
561
- expect(platformBuildDependencies).toEqual([]);
562
639
  }
563
640
  expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
564
641
  // The test suite runs against the dev-profile addon, never a release
@@ -634,10 +711,11 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
634
711
  // The declared target stays the only cargo-wasm: inference never emits a
635
712
  // duplicate for Nx to merge, even with a cdylib *-wasm member crate present.
636
713
  expect(targets['cargo-wasm']).toBeUndefined();
637
- // Its *-wasm output-family name feeds the aggregate build and clean.
638
- expect(buildOutputDependencies).toContain('*-wasm');
714
+ // Its declared *-wasm output-family name feeds the aggregate build and
715
+ // clean by concrete name, and the cargo workspace's test-executable
716
+ // compilation comes with it.
639
717
  expect(targets.build?.executor).toBe('nx:noop');
640
- expect(targets.build?.dependsOn).toEqual(buildOutputDependencies);
718
+ expect(targets.build?.dependsOn).toEqual(['^build', 'cargo-test-compile', 'cargo-wasm']);
641
719
  expect(targets.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
642
720
  } finally {
643
721
  await workspace.cleanup();
@@ -891,16 +969,22 @@ interface WorkspaceFixture {
891
969
  cleanup(): Promise<void>;
892
970
  }
893
971
 
894
- async function inferProject(workspace: WorkspaceFixture, packageJsonPath: string) {
895
- const result = await inferTargets([packageJsonPath], undefined, workspace.context);
972
+ async function inferProject(
973
+ workspace: WorkspaceFixture,
974
+ packageJsonPath: string,
975
+ createNodes: CreateNodesV2 = createNodesV2,
976
+ ) {
977
+ const [, infer] = createNodes;
978
+ const result = await infer([packageJsonPath], undefined, workspace.context);
896
979
  return result[0]?.[1].projects?.[dirname(packageJsonPath)];
897
980
  }
898
981
 
899
982
  async function inferProjectTargets(
900
983
  workspace: WorkspaceFixture,
901
984
  packageJsonPath: string,
985
+ createNodes?: CreateNodesV2,
902
986
  ): Promise<Record<string, TargetConfiguration>> {
903
- return (await inferProject(workspace, packageJsonPath))?.targets ?? {};
987
+ return (await inferProject(workspace, packageJsonPath, createNodes))?.targets ?? {};
904
988
  }
905
989
 
906
990
  /**