@smoothbricks/nx-plugin 0.4.13 → 0.4.15

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.
@@ -7,6 +7,8 @@ import { boundedTestScriptAlias } from './bounded-test-policy.js';
7
7
  import { isNonSourceDirectory } from './source-directories.js';
8
8
  import {
9
9
  BUILD_OUTPUT_DEPENDENCIES,
10
+ isBuildOutputTargetName,
11
+ isTestRunnerTargetName,
10
12
  LINUX_PLATFORM_TARGET_GLOBS,
11
13
  MACOS_PLATFORM_TARGET_GLOBS,
12
14
  type NxPolicyIssue,
@@ -18,7 +20,6 @@ export type { NxPolicyIssue };
18
20
  export interface ResolvedProjectTargets {
19
21
  root?: string;
20
22
  targets: ReadonlySet<string>;
21
- buildDependsOn?: readonly string[];
22
23
  targetDependencies?: ReadonlyMap<string, readonly string[]>;
23
24
  targetExecutors?: ReadonlyMap<string, string>;
24
25
  targetOptions?: ReadonlyMap<string, Readonly<Record<string, unknown>>>;
@@ -190,15 +191,6 @@ function resolvedProjectTargetNames(
190
191
  return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targets : resolvedProject;
191
192
  }
192
193
 
193
- function resolvedProjectBuildDependsOn(
194
- resolvedProject?: ReadonlySet<string> | ResolvedProjectTargets,
195
- ): readonly string[] | undefined {
196
- if (!isResolvedProjectTargets(resolvedProject)) {
197
- return undefined;
198
- }
199
- return resolvedProject.buildDependsOn;
200
- }
201
-
202
194
  function resolvedProjectTargetExecutor(
203
195
  resolvedProject: ReadonlySet<string> | ResolvedProjectTargets | undefined,
204
196
  targetName: string,
@@ -286,43 +278,75 @@ function isNoopTarget(target: Record<string, unknown>): boolean {
286
278
  return !options || stringProperty(options, 'command') === null;
287
279
  }
288
280
 
289
- function targetDependenciesMatchResolvedBuild(
290
- target: Record<string, unknown>,
291
- resolvedBuildDependsOn: readonly string[],
292
- ): boolean {
293
- if (!Array.isArray(target.dependsOn)) {
281
+ /**
282
+ * Properties the inferred aggregate already sets to exactly these values. A
283
+ * declaration carrying anything else — `inputs`, `outputs`, `cache: false`, a
284
+ * `configurations` block — is CHANGING the aggregate, not restating it.
285
+ */
286
+ const inferredNoopBuildProperties: Record<string, true> = { executor: true, cache: true, dependsOn: true };
287
+
288
+ /**
289
+ * Can the build-aggregate inference produce this edge on its own?
290
+ *
291
+ * The inferred aggregate is `^build` plus the project's own build-output
292
+ * targets — `isBuildOutputTargetName`, test runners excluded — and
293
+ * `BUILD_OUTPUT_DEPENDENCIES` is the glob spelling of that same list. Anything
294
+ * else is an edge only the declaration carries: a project-local target outside
295
+ * the families (`build-cli`), or a `project:target` edge to a sibling target
296
+ * that is not named `build`, which `^build` never visits.
297
+ */
298
+ function isInferredBuildDependency(dependency: unknown, resolvedTargets: ReadonlySet<string>): boolean {
299
+ if (typeof dependency !== 'string') {
294
300
  return false;
295
301
  }
296
- const expected = new Set(resolvedBuildDependsOn);
297
- if (target.dependsOn.length !== expected.size) {
302
+ if (dependency === '^build' || isBuildOutputDependencyPattern(dependency)) {
303
+ return true;
304
+ }
305
+ return isBuildOutputTargetName(dependency) && !isTestRunnerTargetName(dependency) && resolvedTargets.has(dependency);
306
+ }
307
+
308
+ function isRedundantBuildDeclaration(build: Record<string, unknown>, resolvedTargets: ReadonlySet<string>): boolean {
309
+ if (!Object.keys(build).every((key) => inferredNoopBuildProperties[key] === true)) {
298
310
  return false;
299
311
  }
300
- return target.dependsOn.every((dependency) => {
301
- if (typeof dependency !== 'string') {
302
- return false;
303
- }
304
- return expected.has(dependency);
305
- });
312
+ if ('cache' in build && build.cache !== true) {
313
+ return false;
314
+ }
315
+ // An empty list is a deliberate cut — "this aggregate waits for nothing" —
316
+ // and restoring the inferred edges by deleting it is a behaviour change.
317
+ return (
318
+ Array.isArray(build.dependsOn) &&
319
+ build.dependsOn.length > 0 &&
320
+ build.dependsOn.every((dependency) => isInferredBuildDependency(dependency, resolvedTargets))
321
+ );
306
322
  }
307
323
 
324
+ /**
325
+ * A hand-declared `build` is deleted only when inference provably rebuilds it.
326
+ *
327
+ * Deliberately NOT decided against the resolved project graph. Nx replaces a
328
+ * named property of an inferred target with the declared one, so the resolved
329
+ * `build.dependsOn` IS the declaration: comparing them answered "redundant"
330
+ * for every noop aggregate anyone ever wrote. AxE's `containium` lost seven
331
+ * edges that way — four of them targets no inference can reach — and a clean
332
+ * checkout then built it without its producers. The question this has to
333
+ * answer is what inference yields WITHOUT the declaration, so the test is
334
+ * structural: every declared edge must be one the aggregate already collects.
335
+ */
308
336
  function removeRedundantNoopBuildTarget(
309
337
  pkg: Record<string, unknown>,
310
338
  resolvedProject?: ReadonlySet<string> | ResolvedProjectTargets,
311
339
  ): boolean {
312
340
  const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
313
- const resolvedBuildDependsOn = resolvedProjectBuildDependsOn(resolvedProject);
314
- if (!resolvedTargets?.has('build') || !resolvedBuildDependsOn) {
341
+ // No build-output target in the project, no inferred aggregate to fall back
342
+ // on: deleting the declaration would leave it with no `build` at all.
343
+ if (!resolvedTargets?.has('build') || ![...resolvedTargets].some(isBuildOutputTargetName)) {
315
344
  return false;
316
345
  }
317
346
  const nx = recordProperty(pkg, 'nx');
318
347
  const targets = nx ? recordProperty(nx, 'targets') : null;
319
348
  const build = targets ? recordProperty(targets, 'build') : null;
320
- if (
321
- !targets ||
322
- !build ||
323
- !isNoopTarget(build) ||
324
- !targetDependenciesMatchResolvedBuild(build, resolvedBuildDependsOn)
325
- ) {
349
+ if (!targets || !build || !isNoopTarget(build) || !isRedundantBuildDeclaration(build, resolvedTargets)) {
326
350
  return false;
327
351
  }
328
352
  delete targets.build;
@@ -3,6 +3,8 @@ import { join } from 'node:path';
3
3
  import type { Tree } from 'nx/src/devkit-exports.js';
4
4
  import { readJson, updateJson } from 'nx/src/devkit-exports.js';
5
5
 
6
+ import { CARGO_TEST_TARGET } from './cargo-workspace.js';
7
+
6
8
  export interface NxPolicyIssue {
7
9
  path: string;
8
10
  message: string;
@@ -24,6 +26,37 @@ export const MACOS_PLATFORM_TARGET_GLOBS = ['*-macos', '*-ios'] as const;
24
26
  export const LINUX_PLATFORM_TARGET_GLOBS = ['*-linux'] as const;
25
27
  export const PLATFORM_TARGET_GLOBS = [...MACOS_PLATFORM_TARGET_GLOBS, ...LINUX_PLATFORM_TARGET_GLOBS] as const;
26
28
 
29
+ const buildOutputTargetSuffixes = BUILD_OUTPUT_DEPENDENCIES.map((glob) => glob.slice(1));
30
+
31
+ /**
32
+ * The glob list above, spelled as a target name: a target whose name ends in a
33
+ * reserved family suffix EMITS that kind of artifact. Inference collects
34
+ * exactly these names into the `nx:noop` build aggregate, which makes this the
35
+ * same rule that decides whether a hand-declared aggregate names an edge
36
+ * inference could have produced on its own — one definition, two readers, so
37
+ * the removal policy cannot drift away from what it judges redundant.
38
+ */
39
+ export function isBuildOutputTargetName(name: string): boolean {
40
+ return buildOutputTargetSuffixes.some((suffix) => name.endsWith(suffix));
41
+ }
42
+
43
+ /**
44
+ * A target whose job is to RUN tests. `test`, anything under `test-`/`test:`,
45
+ * and the plugin's own `cargo-test-<crate>` namespace. Deliberately name-based:
46
+ * the aggregate is assembled from declared AND inferred target names, and a
47
+ * declaration carries no marker saying "this executes rather than emits".
48
+ *
49
+ * A runner collides with the families by construction whenever its name ends in
50
+ * one: a crate named `…-js` puts `cargo-test-<crate>` in the family, and a bun
51
+ * suite named `test-bun` reads as "tool=test, output=bun". Both would drag a
52
+ * test suite into `build`, so the rule is the CLASS, not one namespace.
53
+ */
54
+ export function isTestRunnerTargetName(name: string): boolean {
55
+ return (
56
+ name === 'test' || name.startsWith('test-') || name.startsWith('test:') || name.startsWith(`${CARGO_TEST_TARGET}-`)
57
+ );
58
+ }
59
+
27
60
  const nxJsTypescriptPlugin = '@nx/js/typescript';
28
61
  const smoothBricksNxPlugin = '@smoothbricks/nx-plugin';
29
62
  const cleanOutputsExecutor = '@smoothbricks/nx-plugin:clean-outputs';