@smoothbricks/nx-plugin 0.4.10 → 0.4.12
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/README.md +49 -0
- package/dist/cargo-workspace.d.ts +35 -0
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +59 -0
- package/dist/cross-check-policy.d.ts +49 -3
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +61 -3
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +363 -19
- package/dist/manifest-hash.d.ts +39 -0
- package/dist/manifest-hash.d.ts.map +1 -0
- package/dist/manifest-hash.js +343 -0
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +77 -3
- package/package.json +2 -2
- package/src/cargo-workspace.ts +83 -0
- package/src/cross-check-policy.test.ts +4 -1
- package/src/cross-check-policy.ts +66 -3
- package/src/index.test.ts +478 -6
- package/src/index.ts +436 -16
- package/src/manifest-hash.test.ts +199 -0
- package/src/manifest-hash.ts +335 -0
- package/src/typecheck-test-policy.test.ts +36 -0
- package/src/typecheck-test-policy.ts +77 -3
package/src/index.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs';
|
|
2
2
|
import { readdir, readFile } from 'node:fs/promises';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
3
4
|
import { dirname, join, posix, relative } from 'node:path';
|
|
4
5
|
import { fileURLToPath } from 'node:url';
|
|
5
6
|
|
|
6
7
|
import {
|
|
8
|
+
type CreateNodesContextV2,
|
|
7
9
|
type CreateNodesResultV2,
|
|
8
10
|
type CreateNodesV2,
|
|
9
11
|
readJsonFile,
|
|
@@ -21,11 +23,13 @@ import {
|
|
|
21
23
|
CARGO_TEST_COMPILE_TARGET,
|
|
22
24
|
CARGO_TEST_EXCEPTIONS_SUFFIX,
|
|
23
25
|
CARGO_TEST_TARGET,
|
|
26
|
+
type CargoCrossTestTarget,
|
|
24
27
|
type CargoInputsCache,
|
|
25
28
|
cargoPackageTestInputs,
|
|
26
29
|
cargoTestPackageTargetName,
|
|
27
30
|
createCargoInputsCache,
|
|
28
31
|
exceptionalTestFilter,
|
|
32
|
+
listCargoCrossTestTargets,
|
|
29
33
|
listCargoWorkspacePackages,
|
|
30
34
|
NEXTEST_REPO_CONFIG_PATH,
|
|
31
35
|
nextestToolConfigArg,
|
|
@@ -37,20 +41,86 @@ import {
|
|
|
37
41
|
CARGO_FETCH_TARGET,
|
|
38
42
|
CARGO_FROZEN_PREFIX,
|
|
39
43
|
CARGO_LINT_CLIPPY_COMMAND,
|
|
44
|
+
cargoCrossTestArchiveFile,
|
|
45
|
+
cargoCrossTestArchiveTargetName,
|
|
46
|
+
cargoCrossTestTargetName,
|
|
40
47
|
cargoFrozen,
|
|
41
48
|
} from './cross-check-policy.js';
|
|
49
|
+
import { hashVersionlessCrateManifests } from './manifest-hash.js';
|
|
42
50
|
import { isNonSourceDirectory } from './source-directories.js';
|
|
43
51
|
import { PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
|
|
44
52
|
|
|
45
53
|
export { CARGO_TEST_COMPILE_TARGET };
|
|
46
54
|
|
|
55
|
+
/** Makes a version live and proves it answers; never cached. */
|
|
56
|
+
export const DEPLOY_TARGET = 'deploy';
|
|
57
|
+
/** The purely file-derived half a deploy needs built first; cached. */
|
|
58
|
+
export const DEPLOY_BUILD_TARGET = 'deploy-build';
|
|
59
|
+
|
|
47
60
|
const BUILD_OUTPUT_TARGET_PATTERN = /-(?:js|web|html|css|android|native|napi|bun|wasm)$/;
|
|
61
|
+
/** The workspace-wide manifests a release rewrites: the version of every member, and the lockfile mirror of it. */
|
|
62
|
+
const RELEASE_REWRITTEN_WORKSPACE_INPUTS = ['{workspaceRoot}/package.json', '{workspaceRoot}/bun.lock'];
|
|
48
63
|
const TYPESCRIPT_TOOLCHAIN_INPUTS = [
|
|
49
|
-
|
|
50
|
-
'{workspaceRoot}/bun.lock',
|
|
64
|
+
...RELEASE_REWRITTEN_WORKSPACE_INPUTS,
|
|
51
65
|
'{workspaceRoot}/patches/**/*',
|
|
52
66
|
'{workspaceRoot}/tsconfig.base.json',
|
|
53
67
|
];
|
|
68
|
+
/**
|
|
69
|
+
* The same toolchain for a check, with the two release-rewritten files hashed
|
|
70
|
+
* by field instead of by byte.
|
|
71
|
+
*
|
|
72
|
+
* A `json` input is hashed by Nx's own hasher, so this costs no process. The
|
|
73
|
+
* lockfile's `workspaces` section is the mirror of each member's manifest —
|
|
74
|
+
* name, version and dependency specifiers — and every one of those is already
|
|
75
|
+
* hashed from the member's own `package.json`. Dropping that section keeps the
|
|
76
|
+
* resolution table, which is the reason the lockfile is an input at all.
|
|
77
|
+
*/
|
|
78
|
+
const VERSIONLESS_TOOLCHAIN_INPUTS = [
|
|
79
|
+
...TYPESCRIPT_TOOLCHAIN_INPUTS.filter((input) => !RELEASE_REWRITTEN_WORKSPACE_INPUTS.includes(input)),
|
|
80
|
+
{ json: '{workspaceRoot}/package.json', excludeFields: ['version'] },
|
|
81
|
+
{ json: '{workspaceRoot}/bun.lock', excludeFields: ['workspaces'] },
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A project's own manifest, hashed without the version a release rewrites.
|
|
86
|
+
*
|
|
87
|
+
* ONLY targets that read source may exclude the raw file. A target that
|
|
88
|
+
* produces a shipped artifact keeps hashing it: Rust embeds its version at
|
|
89
|
+
* compile time through `env!("CARGO_PKG_VERSION")`, so a version-insensitive
|
|
90
|
+
* build hash lets a post-bump `build` hit a pre-bump artifact and ship a
|
|
91
|
+
* binary that reports the previous version. `tsc-js`, `build`, `pack` and
|
|
92
|
+
* every cargo target are therefore left alone.
|
|
93
|
+
*/
|
|
94
|
+
const VERSIONLESS_PACKAGE_INPUTS = [
|
|
95
|
+
'!{projectRoot}/package.json',
|
|
96
|
+
{ json: '{projectRoot}/package.json', excludeFields: ['version'] },
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Crate manifests, for the projects that have any.
|
|
101
|
+
*
|
|
102
|
+
* TOML has no `json` input, so this is the one manifest kind the plugin
|
|
103
|
+
* hashes itself — in this process, over bytes it is already reading, during
|
|
104
|
+
* the graph construction that needs the answer.
|
|
105
|
+
*
|
|
106
|
+
* The digest reaches the hash as a literal, with no command behind it. Nx
|
|
107
|
+
* hashes a project's `namedInputs` DEFINITIONS into that project's
|
|
108
|
+
* configuration hash, and that hash is part of every task in the project and
|
|
109
|
+
* of every task that depends on the project — which is exactly the reach the
|
|
110
|
+
* transitive half needs. So the digest is written as an input path that
|
|
111
|
+
* matches no file and is only ever read as a string. It is a positive
|
|
112
|
+
* pattern, never an exclusion: a pattern that matched something unexpected
|
|
113
|
+
* could then only ADD to the hash, never quietly remove a real manifest from
|
|
114
|
+
* it.
|
|
115
|
+
*/
|
|
116
|
+
const VERSIONLESS_CRATE_EXCLUSION = '!{projectRoot}/**/Cargo.toml';
|
|
117
|
+
const VERSIONLESS_CRATE_DIGEST_PREFIX = '{projectRoot}/.versionless-crate-manifests/';
|
|
118
|
+
|
|
119
|
+
/** Plugin-owned named inputs: a project's `production`/`default` with the manifest versions ignored. */
|
|
120
|
+
const VERSIONLESS_PRODUCTION_INPUT = 'versionlessProduction';
|
|
121
|
+
const VERSIONLESS_DEFAULT_INPUT = 'versionlessDefault';
|
|
122
|
+
/** `json` inputs with `excludeFields` are Nx 23.2; an older hasher rejects the entry outright. */
|
|
123
|
+
const JSON_INPUT_MINIMUM_NX = [23, 2] as const;
|
|
54
124
|
|
|
55
125
|
const BIOME_CONFIG_FILES = ['biome.json', 'biome.jsonc'];
|
|
56
126
|
const ESLINT_CONFIG_FILES = [
|
|
@@ -129,20 +199,40 @@ function hostPlatformTargetNames(targetNames: Iterable<string>, hostPlatform: Na
|
|
|
129
199
|
/**
|
|
130
200
|
* The reserved suffixes name targets that EMIT an artifact, and the `build`
|
|
131
201
|
* aggregate is a list of those. Handing Nx the raw `*-<family>` globs let it
|
|
132
|
-
* match on suffix alone, and
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
202
|
+
* match on suffix alone, and a test runner collides by construction whenever
|
|
203
|
+
* its name ends in a family: `*-js`, `*-web`, `*-html`, `*-css`, `*-android`,
|
|
204
|
+
* `*-native`, `*-napi`, `*-bun`, `*-wasm`.
|
|
205
|
+
*
|
|
206
|
+
* Two spellings of the same collision. A per-crate cargo test runner is
|
|
207
|
+
* `cargo-test-<crate>`, so any crate whose name ends in a family put its RUNNER
|
|
208
|
+
* in the aggregate; the runners are one serialized chain, so a single such
|
|
209
|
+
* crate made `build` pull an entire cargo test suite. A bun suite named
|
|
210
|
+
* `test-bun` is the collision without the prefix — a runner reading as
|
|
211
|
+
* "tool=test, output=bun", swept into `build` by suffix alone. That one forced
|
|
212
|
+
* a consumer to hand-declare `build` purely to replace the inferred list.
|
|
213
|
+
*
|
|
214
|
+
* So the rule is the CLASS, not one namespace: a target that RUNS tests is
|
|
215
|
+
* never a build output, however it is spelled. Expanding the families here
|
|
216
|
+
* rather than delegating to Nx's matcher is what makes that expressible.
|
|
139
217
|
*/
|
|
140
218
|
function buildOutputTargetNames(targetNames: Iterable<string>): string[] {
|
|
141
219
|
return [...new Set(targetNames)]
|
|
142
|
-
.filter((name) => BUILD_OUTPUT_TARGET_PATTERN.test(name) && !name
|
|
220
|
+
.filter((name) => BUILD_OUTPUT_TARGET_PATTERN.test(name) && !isTestRunnerTargetName(name))
|
|
143
221
|
.sort();
|
|
144
222
|
}
|
|
145
223
|
|
|
224
|
+
/**
|
|
225
|
+
* A target whose job is to RUN tests. `test`, anything under `test-`/`test:`,
|
|
226
|
+
* and the plugin's own `cargo-test-<crate>` namespace. Deliberately name-based:
|
|
227
|
+
* the aggregate is assembled from declared AND inferred target names, and a
|
|
228
|
+
* declaration carries no marker saying "this executes rather than emits".
|
|
229
|
+
*/
|
|
230
|
+
function isTestRunnerTargetName(name: string): boolean {
|
|
231
|
+
return (
|
|
232
|
+
name === 'test' || name.startsWith('test-') || name.startsWith('test:') || name.startsWith(`${CARGO_TEST_TARGET}-`)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
146
236
|
//#region smoo!n/rust-output-target-inference
|
|
147
237
|
// Cargo workspace inference: a package.json sitting next to a Cargo.toml that
|
|
148
238
|
// declares [workspace] gets direct cargo-test/test targets, cargo-lint feeding
|
|
@@ -387,6 +477,106 @@ function createCargoTestArchiveTarget(projectRoot: string, toolConfig: string):
|
|
|
387
477
|
};
|
|
388
478
|
}
|
|
389
479
|
|
|
480
|
+
/**
|
|
481
|
+
* One foreign triple's test archive, and the target that EXECUTES it.
|
|
482
|
+
*
|
|
483
|
+
* A target-triple VARIANT of `cargo-test-archive`, not a second mechanism: same
|
|
484
|
+
* `nextest archive --workspace`, same tool config layering, same
|
|
485
|
+
* `--workspace-remap .` on the run — only `--target <triple>` and a distinct
|
|
486
|
+
* archive file are added, because both archives coexist as declared Nx outputs
|
|
487
|
+
* and one path for two triples would let a restored cache serve the wrong
|
|
488
|
+
* machine's binaries.
|
|
489
|
+
*
|
|
490
|
+
* `cargo-nextest` where every other cargo target spells `cargo --frozen`, and
|
|
491
|
+
* that is measured, not stylistic: cargo overwrites `CARGO` with its own path
|
|
492
|
+
* for every subcommand it launches, so `cargo nextest` erases the cross cargo
|
|
493
|
+
* driver a repository exports through `CARGO` — the only seam nextest has for
|
|
494
|
+
* one. Invoking `cargo-nextest` directly leaves that driver intact, and
|
|
495
|
+
* `--frozen` moves onto nextest, which forwards it to both inner cargo calls
|
|
496
|
+
* (measured: `metadata … --frozen` and `test --no-run … --frozen`).
|
|
497
|
+
*
|
|
498
|
+
* The archive target therefore states `CARGO_FETCH_TARGET` and the toolchain
|
|
499
|
+
* runtime inputs itself: both are otherwise attached by matching the
|
|
500
|
+
* `cargo --frozen` prefix in a command, and this command does not carry it.
|
|
501
|
+
*
|
|
502
|
+
* The RUNNER declares no dependency on the archive and is not cached. That is
|
|
503
|
+
* the whole point of splitting the pair: it runs on a machine where the triple
|
|
504
|
+
* is native and the archive arrived as a file, so a dependency edge would ask
|
|
505
|
+
* that machine to cross-build what it was sent, and a cache verdict would be
|
|
506
|
+
* keyed on sources whose binaries it did not build. It is also the one cargo
|
|
507
|
+
* target that needs no cargo: `nextest run --archive-file` extracts prebuilt
|
|
508
|
+
* binaries and never invokes cargo (measured, zero invocations).
|
|
509
|
+
*/
|
|
510
|
+
function createCargoCrossTestTargets(
|
|
511
|
+
projectRoot: string,
|
|
512
|
+
toolConfig: string,
|
|
513
|
+
crossTarget: CargoCrossTestTarget,
|
|
514
|
+
archiveInputs: NonNullable<TargetConfiguration['inputs']>,
|
|
515
|
+
): Record<string, TargetConfiguration> {
|
|
516
|
+
const { target: triple, cargo } = crossTarget;
|
|
517
|
+
const archiveFile = cargoCrossTestArchiveFile(triple);
|
|
518
|
+
const archiveCommand = (profile: string): string =>
|
|
519
|
+
`cargo-nextest nextest archive --workspace${profile} --target ${triple} --frozen ` +
|
|
520
|
+
`--archive-file ${archiveFile} --user-config-file none ${toolConfig}`;
|
|
521
|
+
return {
|
|
522
|
+
[cargoCrossTestArchiveTargetName(triple)]: {
|
|
523
|
+
executor: 'nx:run-commands',
|
|
524
|
+
cache: true,
|
|
525
|
+
// The two runtime inputs every cached cargo target carries, plus the
|
|
526
|
+
// declared driver's own bytes. They are attached elsewhere by matching
|
|
527
|
+
// the `cargo --frozen` prefix in a command, which this one does not
|
|
528
|
+
// spell — and they matter MORE here than anywhere else: the cross
|
|
529
|
+
// environment (SDKROOT, CC_<triple>, ZIG*, and CARGO itself) is what
|
|
530
|
+
// decides whether these bytes are Mach-O at all, so an archive linked
|
|
531
|
+
// against one sysroot must never hash equal to the same sources linked
|
|
532
|
+
// against another.
|
|
533
|
+
inputs: [
|
|
534
|
+
...archiveInputs,
|
|
535
|
+
...(cargo === undefined ? [] : [`{projectRoot}/${cargo}`]),
|
|
536
|
+
cargoRuntimeInput(projectRoot, CARGO_ENVIRONMENT_INPUT.runtime),
|
|
537
|
+
cargoRuntimeInput(projectRoot, 'rustc -vV && cargo -V && cargo nextest --version'),
|
|
538
|
+
],
|
|
539
|
+
outputs: [`{projectRoot}/${archiveFile}`],
|
|
540
|
+
dependsOn: [CARGO_FETCH_TARGET],
|
|
541
|
+
options: {
|
|
542
|
+
commands: [`mkdir -p ${posix.dirname(archiveFile)}`, archiveCommand('')],
|
|
543
|
+
cwd: projectRoot,
|
|
544
|
+
parallel: false,
|
|
545
|
+
// `CARGO` is cargo's own driver variable and the only seam nextest
|
|
546
|
+
// offers: it shells out to `$CARGO metadata` and
|
|
547
|
+
// `$CARGO test --no-run --target <triple>`, so the declared script
|
|
548
|
+
// decides how the foreign link happens without the generated command
|
|
549
|
+
// knowing anything about zig, an SDK, or a sysroot. Target-scoped, so
|
|
550
|
+
// it applies to this archive and to nothing else cargo runs.
|
|
551
|
+
...(cargo === undefined ? {} : { env: { CARGO: cargo } }),
|
|
552
|
+
},
|
|
553
|
+
configurations: {
|
|
554
|
+
production: {
|
|
555
|
+
commands: [`mkdir -p ${posix.dirname(archiveFile)}`, archiveCommand(' --release')],
|
|
556
|
+
},
|
|
557
|
+
},
|
|
558
|
+
},
|
|
559
|
+
[cargoCrossTestTargetName(triple)]: {
|
|
560
|
+
executor: '@smoothbricks/nx-plugin:bounded-exec',
|
|
561
|
+
cache: false,
|
|
562
|
+
inputs: [],
|
|
563
|
+
outputs: [],
|
|
564
|
+
dependsOn: [],
|
|
565
|
+
options: {
|
|
566
|
+
command:
|
|
567
|
+
`cargo-nextest nextest run --archive-file ${archiveFile} --workspace-remap . ` +
|
|
568
|
+
`--no-tests=pass --user-config-file none ${toolConfig}`,
|
|
569
|
+
cwd: projectRoot,
|
|
570
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
571
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
572
|
+
},
|
|
573
|
+
// The archive already holds the profile it was built with, and nextest
|
|
574
|
+
// rejects `--release` beside `--archive-file`.
|
|
575
|
+
configurations: { production: {} },
|
|
576
|
+
},
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
|
|
390
580
|
function createCargoTestTarget(projectRoot: string): TargetConfiguration {
|
|
391
581
|
return {
|
|
392
582
|
executor: '@smoothbricks/nx-plugin:bounded-exec',
|
|
@@ -407,6 +597,80 @@ function createCargoTestTarget(projectRoot: string): TargetConfiguration {
|
|
|
407
597
|
|
|
408
598
|
const PLUGIN_NEXTEST_CONFIG = fileURLToPath(new URL('../nextest.toml', import.meta.url));
|
|
409
599
|
|
|
600
|
+
/**
|
|
601
|
+
* How far the versionless manifest treatment reaches in this workspace.
|
|
602
|
+
*
|
|
603
|
+
* The JSON half costs nothing: Nx hashes a `json` input in its own hasher.
|
|
604
|
+
* `null` means even that is off, because the running Nx predates the input
|
|
605
|
+
* type and would reject the entry rather than ignore it — the plugin is
|
|
606
|
+
* published, so an older consumer gets today's inputs instead of a broken
|
|
607
|
+
* hash.
|
|
608
|
+
*
|
|
609
|
+
* The crate half asks nothing of the workspace at all. The digest is computed
|
|
610
|
+
* right here, in the process that is already reading those manifests, and
|
|
611
|
+
* rides into the project's named inputs as a literal — so there is no command
|
|
612
|
+
* to install and nothing to look for before the treatment is safe to declare.
|
|
613
|
+
*/
|
|
614
|
+
interface VersionlessManifestPolicy {
|
|
615
|
+
/**
|
|
616
|
+
* Whether a dependency's manifests can be hashed versionlessly too.
|
|
617
|
+
*
|
|
618
|
+
* `{ input, dependencies: true }` resolves the named input in EACH
|
|
619
|
+
* dependency's own context, and Nx throws when a dependency has no
|
|
620
|
+
* definition for it. A project this plugin never inferred — a project.json
|
|
621
|
+
* project with no package.json — has none, so the transitive half is only
|
|
622
|
+
* safe once nx.json defines the name workspace-wide. That definition is what
|
|
623
|
+
* a consumer opts in with; without it the dependency half stays `^production`.
|
|
624
|
+
*/
|
|
625
|
+
transitive: boolean;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function versionlessManifestPolicy(context: CreateNodesContextV2): VersionlessManifestPolicy | null {
|
|
629
|
+
if (!supportsJsonInputs()) return null;
|
|
630
|
+
return { transitive: context.nxJsonConfiguration.namedInputs?.[VERSIONLESS_PRODUCTION_INPUT] !== undefined };
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* The crate manifests' exclusion and the digest that stands in for them, or
|
|
635
|
+
* nothing at all when this process cannot read them.
|
|
636
|
+
*
|
|
637
|
+
* Fail-closed, and the only direction that is safe: excluding the manifests
|
|
638
|
+
* with no digest replacing them would stop a real dependency change from
|
|
639
|
+
* invalidating anything, and neither Nx nor this plugin would say a word.
|
|
640
|
+
* Handing back the empty list leaves the raw manifests in the fileset — one
|
|
641
|
+
* cache miss per release, which is what the workspace has today.
|
|
642
|
+
*/
|
|
643
|
+
async function versionlessCrateInputs(projectRoot: string, workspaceRoot: string): Promise<string[]> {
|
|
644
|
+
try {
|
|
645
|
+
const digest = await hashVersionlessCrateManifests(projectRoot, workspaceRoot);
|
|
646
|
+
return [VERSIONLESS_CRATE_EXCLUSION, `${VERSIONLESS_CRATE_DIGEST_PREFIX}${digest}`];
|
|
647
|
+
} catch (error) {
|
|
648
|
+
// Out loud: a silent degrade here reads as "the release stopped costing a
|
|
649
|
+
// rebuild", which is the same thing a wrong hash reads as.
|
|
650
|
+
process.stderr.write(
|
|
651
|
+
`@smoothbricks/nx-plugin: hashing the crate manifests under ${projectRoot} failed (${
|
|
652
|
+
error instanceof Error ? error.message : String(error)
|
|
653
|
+
}); hashing them raw instead, so a release invalidates this project's checks.\n`,
|
|
654
|
+
);
|
|
655
|
+
return [];
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Whether the Nx that is RUNNING understands a `json` input. Read from the
|
|
661
|
+
* loaded module rather than the workspace, because that is the hasher which
|
|
662
|
+
* will receive the entry.
|
|
663
|
+
*/
|
|
664
|
+
function supportsJsonInputs(): boolean {
|
|
665
|
+
const version = readJsonFile<{ version?: unknown }>(
|
|
666
|
+
createRequire(import.meta.url).resolve('nx/package.json'),
|
|
667
|
+
).version;
|
|
668
|
+
if (typeof version !== 'string') return false;
|
|
669
|
+
const [major = 0, minor = 0] = version.split('.', 2).map(Number);
|
|
670
|
+
const [minimumMajor, minimumMinor] = JSON_INPUT_MINIMUM_NX;
|
|
671
|
+
return major > minimumMajor || (major === minimumMajor && minor >= minimumMinor);
|
|
672
|
+
}
|
|
673
|
+
|
|
410
674
|
type CreateNodesHandler = CreateNodesV2[1];
|
|
411
675
|
|
|
412
676
|
function createNodesHandler(hostPlatform: NapiPlatform | null): CreateNodesHandler {
|
|
@@ -419,6 +683,7 @@ function createNodesHandler(hostPlatform: NapiPlatform | null): CreateNodesHandl
|
|
|
419
683
|
} catch (error) {
|
|
420
684
|
throw new AggregateCreateNodesError([[null, error instanceof Error ? error : new Error(String(error))]], results);
|
|
421
685
|
}
|
|
686
|
+
const versionless = versionlessManifestPolicy(context);
|
|
422
687
|
|
|
423
688
|
await Promise.all(
|
|
424
689
|
projectConfigurationFiles.map(async (packageJsonPath) => {
|
|
@@ -435,7 +700,13 @@ function createNodesHandler(hostPlatform: NapiPlatform | null): CreateNodesHandl
|
|
|
435
700
|
}
|
|
436
701
|
results.push([
|
|
437
702
|
packageJsonPath,
|
|
438
|
-
await createProjectTargets(
|
|
703
|
+
await createProjectTargets(
|
|
704
|
+
packageJsonPath,
|
|
705
|
+
context.workspaceRoot,
|
|
706
|
+
hostPlatform,
|
|
707
|
+
cargoWorkspaces,
|
|
708
|
+
versionless,
|
|
709
|
+
),
|
|
439
710
|
]);
|
|
440
711
|
} catch (error) {
|
|
441
712
|
errors.push([packageJsonPath, error instanceof Error ? error : new Error(String(error))]);
|
|
@@ -509,11 +780,52 @@ function packageBinOutputs(packageJson: PackageJson, packageJsonPath: string): s
|
|
|
509
780
|
return [...outputs];
|
|
510
781
|
}
|
|
511
782
|
|
|
783
|
+
/**
|
|
784
|
+
* The deployment manifests wrangler itself looks for, in its own precedence
|
|
785
|
+
* order. Finding one IS the detection: a project that hands Cloudflare a
|
|
786
|
+
* worker has one of these, and nothing else in a workspace does.
|
|
787
|
+
*
|
|
788
|
+
* Deploy POLICY is deliberately NOT read from the manifest. `smoo.wrangler.stages`
|
|
789
|
+
* belongs to the CLI, which reads it to decide which secrets a stage requires; a
|
|
790
|
+
* second copy in the graph would disagree the first time a stage was added. And
|
|
791
|
+
* which stages CI actually runs is the four-way vocabulary in the CLI's
|
|
792
|
+
* `deploy-tags.ts` — every stage, staging only, excluded, production-on-push —
|
|
793
|
+
* that a flag here could express exactly one of. So every wrangler project gets
|
|
794
|
+
* the target, and CI selection keeps excluding by tag.
|
|
795
|
+
*
|
|
796
|
+
* A worker deployed by some other mechanism declares its own `options.command`,
|
|
797
|
+
* which replaces this one; `isStageDerivedDeploy` then correctly stops matching
|
|
798
|
+
* it, because the `deploy-stage` string went with it.
|
|
799
|
+
*/
|
|
800
|
+
const WRANGLER_CONFIG_FILES = ['wrangler.jsonc', 'wrangler.json', 'wrangler.toml'];
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* ONE target that deploys whichever stage it is handed, never a configuration
|
|
804
|
+
* per stage. Pull-request stages are `prN` for unbounded N, so an enumerated
|
|
805
|
+
* `configurations` map cannot describe the surface that actually exists, and a
|
|
806
|
+
* project carrying both spellings would have two ways to say one thing.
|
|
807
|
+
*
|
|
808
|
+
* No `stage-deploy-target` tag either, deliberately: the CLI's
|
|
809
|
+
* `isStageDerivedDeploy` recognises a stage deploy by the tag OR by this exact
|
|
810
|
+
* command string, and the inferred target always carries the command. Adding
|
|
811
|
+
* the tag would be a second mechanism answering a question the first already
|
|
812
|
+
* answers, and the two would drift the first time either moved.
|
|
813
|
+
*
|
|
814
|
+
* `{args.stage}` rather than `forwardAllArgs`: run-commands'
|
|
815
|
+
* `interpolateArgsIntoCommand` returns as soon as it sees `{args.` and never
|
|
816
|
+
* appends forwarded arguments, so `nx run web:deploy --stage=pr42` yields
|
|
817
|
+
* exactly one `--stage`, for an arbitrary preview stage no map could enumerate.
|
|
818
|
+
* An omitted stage interpolates to empty and `deploy-stage`'s required
|
|
819
|
+
* `--stage` refuses it, which is the failure we want at the boundary.
|
|
820
|
+
*/
|
|
821
|
+
const WRANGLER_DEPLOY_COMMAND = 'smoo wrangler deploy-stage --stage {args.stage}';
|
|
822
|
+
|
|
512
823
|
async function createProjectTargets(
|
|
513
824
|
packageJsonPath: string,
|
|
514
825
|
workspaceRoot: string,
|
|
515
826
|
hostPlatform: NapiPlatform | null,
|
|
516
827
|
cargoWorkspaces: readonly CargoWorkspace[],
|
|
828
|
+
versionless: VersionlessManifestPolicy | null,
|
|
517
829
|
) {
|
|
518
830
|
const projectRoot = dirname(packageJsonPath);
|
|
519
831
|
const absoluteProjectRoot = join(workspaceRoot, projectRoot);
|
|
@@ -537,6 +849,22 @@ async function createProjectTargets(
|
|
|
537
849
|
const isCargoProject = cargoWorkspace !== undefined;
|
|
538
850
|
const repoRootPackages = cargoWorkspace?.projectRoot === '.' ? cargoPackagePlans.map((plan) => plan.package) : [];
|
|
539
851
|
const isRepoRootedCargoProject = cargoWorkspace?.projectRoot === '.';
|
|
852
|
+
// What a check that only READS the manifests hashes instead of their raw
|
|
853
|
+
// bytes. Each piece degrades to today's input on its own: an Nx without
|
|
854
|
+
// `json` inputs gets no treatment at all; crate manifests this process
|
|
855
|
+
// cannot read stay raw; no workspace-wide named input leaves the
|
|
856
|
+
// transitive half `^production`.
|
|
857
|
+
const hasCrateManifests = isCargoProject || existsSync(cargoTomlPath);
|
|
858
|
+
const crateInputs =
|
|
859
|
+
versionless !== null && hasCrateManifests ? await versionlessCrateInputs(projectRoot, workspaceRoot) : [];
|
|
860
|
+
const versionlessProjectInputs = versionless === null ? [] : [...VERSIONLESS_PACKAGE_INPUTS, ...crateInputs];
|
|
861
|
+
const checkInputs = versionless === null ? TYPESCRIPT_TOOLCHAIN_INPUTS : VERSIONLESS_TOOLCHAIN_INPUTS;
|
|
862
|
+
const selfProduction = versionless === null ? 'production' : VERSIONLESS_PRODUCTION_INPUT;
|
|
863
|
+
const selfDefault = versionless === null ? 'default' : VERSIONLESS_DEFAULT_INPUT;
|
|
864
|
+
const dependencyProduction =
|
|
865
|
+
versionless?.transitive === true
|
|
866
|
+
? { input: VERSIONLESS_PRODUCTION_INPUT, dependencies: true as const }
|
|
867
|
+
: '^production';
|
|
540
868
|
const napiConfig = resolveNapiConfig(
|
|
541
869
|
packageJson,
|
|
542
870
|
packageJsonPath,
|
|
@@ -556,6 +884,7 @@ async function createProjectTargets(
|
|
|
556
884
|
const hasOrdinaryBuildOutputTarget =
|
|
557
885
|
hasLibTsconfig || napiConfig !== null || cargoWasmConfig !== null || packageLocalBuildOutputs.ordinary;
|
|
558
886
|
const hasAnyBuildOutputTarget = hasOrdinaryBuildOutputTarget || packageLocalBuildOutputs.platform;
|
|
887
|
+
const isWranglerProject = WRANGLER_CONFIG_FILES.some((file) => existsSync(join(absoluteProjectRoot, file)));
|
|
559
888
|
|
|
560
889
|
if (hasLibTsconfig) {
|
|
561
890
|
const executableOutputs = packageBinOutputs(packageJson, packageJsonPath);
|
|
@@ -582,7 +911,7 @@ async function createProjectTargets(
|
|
|
582
911
|
targets.typecheck = {
|
|
583
912
|
executor: 'nx:run-commands',
|
|
584
913
|
cache: true,
|
|
585
|
-
inputs: [
|
|
914
|
+
inputs: [selfProduction, dependencyProduction, ...checkInputs, '{projectRoot}/tsconfig.lib.json'],
|
|
586
915
|
outputs: [],
|
|
587
916
|
dependsOn: ['^*-js', ...(cargoWasmConfig ? ['cargo-wasm'] : [])],
|
|
588
917
|
options: {
|
|
@@ -597,7 +926,7 @@ async function createProjectTargets(
|
|
|
597
926
|
targets['typecheck-tests'] = {
|
|
598
927
|
executor: 'nx:run-commands',
|
|
599
928
|
cache: true,
|
|
600
|
-
inputs: [
|
|
929
|
+
inputs: [selfDefault, dependencyProduction, ...checkInputs, '{projectRoot}/tsconfig.test.json'],
|
|
601
930
|
dependsOn: ['typecheck'],
|
|
602
931
|
options: {
|
|
603
932
|
command: 'tsc -p tsconfig.test.json --noEmit',
|
|
@@ -734,6 +1063,21 @@ async function createProjectTargets(
|
|
|
734
1063
|
// `archive.include` lives in the repository's nextest config, so that file
|
|
735
1064
|
// decides what the archive CONTAINS, not merely how a run behaves.
|
|
736
1065
|
targets[CARGO_TEST_ARCHIVE_TARGET].inputs = [...workspaceCargoInputs, `{projectRoot}/${NEXTEST_REPO_CONFIG_PATH}`];
|
|
1066
|
+
// One archive per declared foreign triple, from the same inputs and the
|
|
1067
|
+
// same repository nextest config: an archive is the workspace's test
|
|
1068
|
+
// binaries whichever machine they are for, so a change that invalidates
|
|
1069
|
+
// the host archive must invalidate every cross one.
|
|
1070
|
+
for (const crossTarget of listCargoCrossTestTargets(join(workspaceRoot, cargoWorkspaceRoot))) {
|
|
1071
|
+
Object.assign(
|
|
1072
|
+
targets,
|
|
1073
|
+
createCargoCrossTestTargets(
|
|
1074
|
+
cargoWorkspaceRoot,
|
|
1075
|
+
nextestToolConfigArg(workspaceRoot, cargoWorkspaceRoot, PLUGIN_NEXTEST_CONFIG),
|
|
1076
|
+
crossTarget,
|
|
1077
|
+
[...workspaceCargoInputs, `{projectRoot}/${NEXTEST_REPO_CONFIG_PATH}`],
|
|
1078
|
+
),
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
737
1081
|
const aggregateDependencies = cargoWorkspace.packages.flatMap((plan) =>
|
|
738
1082
|
plan.pieces.map((piece) =>
|
|
739
1083
|
cargoTargetDependency(projectName, {
|
|
@@ -1064,6 +1408,63 @@ async function createProjectTargets(
|
|
|
1064
1408
|
}
|
|
1065
1409
|
}
|
|
1066
1410
|
|
|
1411
|
+
// A deploy is never cached; the build half beside it always is.
|
|
1412
|
+
//
|
|
1413
|
+
// Nx's cache answers "did this exact input set already produce an output". A deploy's real
|
|
1414
|
+
// question is "is this exact version live", and the two agree only until someone rolls back.
|
|
1415
|
+
// After a rollback the workspace is unchanged, so the hash is unchanged, so a cached `deploy`
|
|
1416
|
+
// reports success while the previous version keeps serving. No input tuning repairs that: a
|
|
1417
|
+
// `runtime` input cannot even learn WHICH STAGE is being deployed. Nx hashes runtime inputs with
|
|
1418
|
+
// the env from `getTaskSpecificEnv`, which deliberately excludes the task coordinates; input
|
|
1419
|
+
// strings interpolate only `{workspaceRoot}`/`{projectRoot}`, never `{args.*}`; and overrides
|
|
1420
|
+
// are not forwarded to `dependsOn` dependencies, so an auxiliary task cannot carry the stage
|
|
1421
|
+
// either. (An executor's custom hasher is the single Nx API that does see `task.overrides`, if
|
|
1422
|
+
// hash-level participation is ever genuinely wanted.)
|
|
1423
|
+
//
|
|
1424
|
+
// So the target is split along the line the question actually falls on. `deploy-build` is purely
|
|
1425
|
+
// file-derived — build the artifact and whatever the build registers — and caches honestly.
|
|
1426
|
+
// `deploy` reads live state every run and no-ops when live already equals the desired tag, which
|
|
1427
|
+
// costs two Cloudflare calls and makes a redundant deploy provably free rather than assumed
|
|
1428
|
+
// free. That is what lets ordering be an ordinary `dependsOn: ["<other>:deploy"]` edge at any
|
|
1429
|
+
// depth, and it is why such an edge must point at `deploy` and not at `deploy-build`: a
|
|
1430
|
+
// dependent has to re-check liveness, not inherit someone else's cache hit.
|
|
1431
|
+
//
|
|
1432
|
+
// Both are bases for a package-local declaration to land on, per the merge rules above: a
|
|
1433
|
+
// declaration naming `cache`, `inputs` or `dependsOn` REPLACES what is set here and inherits
|
|
1434
|
+
// everything it does not name, while Nx's `"..."` token expands to the inferred list at its
|
|
1435
|
+
// position. So a project adding a cross-project edge writes
|
|
1436
|
+
// `"dependsOn": ["...", "backend:deploy"]` and keeps both the edge below and this command —
|
|
1437
|
+
// measured, not assumed, against an inferred base.
|
|
1438
|
+
//
|
|
1439
|
+
// `deploy` is INFERRED for a wrangler project rather than patched onto a hand-written one. The
|
|
1440
|
+
// command is a constant of the convention, not of the project: every consumer wrote the same
|
|
1441
|
+
// `nx:run-commands` wrapper around it, and one that spells it differently silently stops being
|
|
1442
|
+
// a stage deploy to `isStageDerivedDeploy`, which reads that exact string. `deploy-build` stays
|
|
1443
|
+
// declaration-driven in the other direction, because only the project knows what building it
|
|
1444
|
+
// means — the plugin supplies its POLICY (cache honestly) and nothing else.
|
|
1445
|
+
if (DEPLOY_BUILD_TARGET in declaredTargets) {
|
|
1446
|
+
targets[DEPLOY_BUILD_TARGET] = { cache: true };
|
|
1447
|
+
}
|
|
1448
|
+
if (isWranglerProject) {
|
|
1449
|
+
// Naming only an edge that exists is tidiness, not a guard: Nx tolerates a `dependsOn` on a
|
|
1450
|
+
// target the project lacks — measured, the task just runs alone — and a wrangler worker can
|
|
1451
|
+
// legitimately have no build at all, since wrangler bundles its own TypeScript. Pointing at a
|
|
1452
|
+
// target that is not there would only make `nx graph` claim a prerequisite nothing satisfies.
|
|
1453
|
+
const fileDerivedHalf =
|
|
1454
|
+
DEPLOY_BUILD_TARGET in declaredTargets
|
|
1455
|
+
? DEPLOY_BUILD_TARGET
|
|
1456
|
+
: hasOrdinaryBuildOutputTarget || 'build' in declaredTargets
|
|
1457
|
+
? 'build'
|
|
1458
|
+
: null;
|
|
1459
|
+
targets[DEPLOY_TARGET] = {
|
|
1460
|
+
executor: 'nx:run-commands',
|
|
1461
|
+
cache: false,
|
|
1462
|
+
outputs: [],
|
|
1463
|
+
...(fileDerivedHalf === null ? {} : { dependsOn: [fileDerivedHalf] }),
|
|
1464
|
+
options: { command: WRANGLER_DEPLOY_COMMAND, cwd: projectRoot },
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1067
1468
|
// No `outputs`, deliberately: this aggregate runs no command, so every file
|
|
1068
1469
|
// under dist belongs to the concrete target that emitted it. Claiming
|
|
1069
1470
|
// `{projectRoot}/dist` here would cache the children's bytes a second time
|
|
@@ -1151,8 +1552,8 @@ async function createProjectTargets(
|
|
|
1151
1552
|
dependsOn: validationTargets,
|
|
1152
1553
|
outputs: [],
|
|
1153
1554
|
inputs: [
|
|
1154
|
-
|
|
1155
|
-
...
|
|
1555
|
+
selfDefault,
|
|
1556
|
+
...checkInputs,
|
|
1156
1557
|
...[...BIOME_CONFIG_FILES, ...ESLINT_CONFIG_FILES].flatMap((name) => [
|
|
1157
1558
|
`{workspaceRoot}/${name}`,
|
|
1158
1559
|
`{projectRoot}/${name}`,
|
|
@@ -1169,7 +1570,26 @@ async function createProjectTargets(
|
|
|
1169
1570
|
|
|
1170
1571
|
return {
|
|
1171
1572
|
projects: {
|
|
1172
|
-
[projectRoot]: {
|
|
1573
|
+
[projectRoot]: {
|
|
1574
|
+
name: projectName,
|
|
1575
|
+
targets,
|
|
1576
|
+
// A named input is what `{ input, dependencies: true }` resolves in
|
|
1577
|
+
// each DEPENDENCY's own context, so the dependency half needs one to
|
|
1578
|
+
// exist per project. A `json` path interpolates `{projectRoot}` per
|
|
1579
|
+
// project, but a `runtime` command does NOT — it reaches the shell
|
|
1580
|
+
// literally — so a crate digest can only name its own root from here.
|
|
1581
|
+
// Nx resolves a named input from the project first and nx.json second,
|
|
1582
|
+
// so a workspace-wide definition still covers projects this plugin
|
|
1583
|
+
// never inferred.
|
|
1584
|
+
...(versionless === null
|
|
1585
|
+
? {}
|
|
1586
|
+
: {
|
|
1587
|
+
namedInputs: {
|
|
1588
|
+
[VERSIONLESS_PRODUCTION_INPUT]: ['production', ...versionlessProjectInputs],
|
|
1589
|
+
[VERSIONLESS_DEFAULT_INPUT]: ['default', ...versionlessProjectInputs],
|
|
1590
|
+
},
|
|
1591
|
+
}),
|
|
1592
|
+
},
|
|
1173
1593
|
},
|
|
1174
1594
|
};
|
|
1175
1595
|
}
|