@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.
- package/README.md +19 -8
- package/dist/cargo-toolchain-policy.d.ts +61 -0
- package/dist/cargo-toolchain-policy.d.ts.map +1 -0
- package/dist/cargo-toolchain-policy.js +65 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +59 -55
- package/dist/package-target-policy.d.ts +0 -1
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +50 -23
- package/dist/workspace-config-policy.d.ts +21 -0
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +27 -0
- package/package.json +7 -2
- package/src/cargo-toolchain-policy.ts +68 -0
- package/src/index.test.ts +130 -40
- package/src/index.ts +59 -58
- package/src/package-target-policy.test.ts +4 -28
- package/src/package-target-policy.ts +55 -31
- package/src/workspace-config-policy.ts +33 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smoothbricks/nx-plugin",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./loader.js",
|
|
@@ -52,6 +52,11 @@
|
|
|
52
52
|
"types": "./dist/cross-check-policy.d.ts",
|
|
53
53
|
"import": "./dist/cross-check-policy.js",
|
|
54
54
|
"default": "./dist/cross-check-policy.js"
|
|
55
|
+
},
|
|
56
|
+
"./cargo-toolchain-policy": {
|
|
57
|
+
"types": "./dist/cargo-toolchain-policy.d.ts",
|
|
58
|
+
"import": "./dist/cargo-toolchain-policy.js",
|
|
59
|
+
"default": "./dist/cargo-toolchain-policy.js"
|
|
55
60
|
}
|
|
56
61
|
},
|
|
57
62
|
"bin": {
|
|
@@ -108,7 +113,7 @@
|
|
|
108
113
|
"typia": "14.0.2"
|
|
109
114
|
},
|
|
110
115
|
"devDependencies": {
|
|
111
|
-
"@smoothbricks/lmao": "0.3.
|
|
116
|
+
"@smoothbricks/lmao": "0.3.13",
|
|
112
117
|
"@smoothbricks/validation": "0.1.8"
|
|
113
118
|
}
|
|
114
119
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The toolchain half of a cargo task's identity, shared by the two things that
|
|
3
|
+
* must agree about it: target inference, which attaches it, and the monorepo
|
|
4
|
+
* policy, which refuses a repository's own declaration that drops it.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The DECLARED pin, hashed as an ordinary file. devenv resolves this lock into
|
|
9
|
+
* the rustc, cargo, linker, C toolchain and SDK that every cargo command
|
|
10
|
+
* inherits, so a bump here is exactly the event that must invalidate a cached
|
|
11
|
+
* artifact — and it is the only such event the workspace can state portably.
|
|
12
|
+
*
|
|
13
|
+
* This replaces a runtime input that dumped the ambient cargo environment
|
|
14
|
+
* (every `CARGO_`, `RUST`, `CC`, `CXX`, `AR`, `SDKROOT`, `LIBCLANG_PATH`,
|
|
15
|
+
* `CMAKE_` and `ZIG` prefixed variable, plus the contents of
|
|
16
|
+
* `$CARGO_HOME/config{,.toml}`). That bought the same
|
|
17
|
+
* invalidation at the price of two defects. Its values are absolute
|
|
18
|
+
* `/nix/store/<hash>-…` paths and per-checkout devenv state directories, so no
|
|
19
|
+
* two machines ever agree and no cargo target can share a cache entry with a
|
|
20
|
+
* peer or with CI — a remote cache for Rust was dead on arrival. And the dump
|
|
21
|
+
* differs between a bare shell and a devenv profile for the SAME sources: in
|
|
22
|
+
* the `linux-cross` profile `AR` becomes `x86_64-unknown-linux-gnu-ar`,
|
|
23
|
+
* `CARGO_INSTALL_ROOT` moves under `profiles/linux-cross/`, and
|
|
24
|
+
* `CC_x86_64_unknown_linux_gnu` appears, so the entry `check:linux` writes
|
|
25
|
+
* inside the profile could never be hit by the pre-push probe, which runs
|
|
26
|
+
* bare. Measured on one repository-root archive target with the dump still in
|
|
27
|
+
* place: bare 18197306115812424011, in-profile 12567954759860890767.
|
|
28
|
+
*
|
|
29
|
+
* A file input has neither defect. The lock's bytes are identical bare and
|
|
30
|
+
* in-profile and on every machine holding the checkout, while the toolchain
|
|
31
|
+
* VERSIONS the dump was really guarding stay covered twice over — by the pin
|
|
32
|
+
* itself, and by the `rustc -vV && cargo -V` runtime input attached beside it,
|
|
33
|
+
* whose stdout is byte-identical in both shells (measured). Nothing
|
|
34
|
+
* machine-local survives. A NARROWED dump was rejected rather than kept: a
|
|
35
|
+
* subset re-acquires a store path the moment anyone widens it.
|
|
36
|
+
*
|
|
37
|
+
* Both fleet locations are stated: the pin lives under `tooling/direnv/` in
|
|
38
|
+
* every repository and additionally at the root in some. A fileset that
|
|
39
|
+
* matches nothing contributes nothing — the same property the inert
|
|
40
|
+
* `rust-toolchain*` entries already rely on.
|
|
41
|
+
*/
|
|
42
|
+
export const CARGO_TOOLCHAIN_PIN_INPUTS: readonly string[] = [
|
|
43
|
+
'{workspaceRoot}/devenv.lock',
|
|
44
|
+
'{workspaceRoot}/tooling/direnv/devenv.lock',
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The name a repository's own `inputs` declaration uses to reach the pin
|
|
49
|
+
* without restating its paths.
|
|
50
|
+
*
|
|
51
|
+
* A declared target REPLACES the inferred input list, pin included, so a
|
|
52
|
+
* hand-written `inputs` array silently drops the toolchain identity and the
|
|
53
|
+
* target stops seeing a toolchain bump at all. Inference defines this named
|
|
54
|
+
* input on every project that has cargo targets so a declaration can name one
|
|
55
|
+
* token that cannot drift from what the plugin attaches.
|
|
56
|
+
*/
|
|
57
|
+
export const CARGO_TOOLCHAIN_NAMED_INPUT = 'cargoToolchain';
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Does one resolved input string carry the toolchain identity?
|
|
61
|
+
*
|
|
62
|
+
* Resolved: named inputs already expanded. The check is by suffix rather than
|
|
63
|
+
* by exact path because a repository may keep its lock at a location this list
|
|
64
|
+
* does not name; what matters is that a devenv lock is hashed, not which one.
|
|
65
|
+
*/
|
|
66
|
+
export function isCargoToolchainInput(input: string): boolean {
|
|
67
|
+
return !input.startsWith('!') && input.endsWith('/devenv.lock');
|
|
68
|
+
}
|
package/src/index.test.ts
CHANGED
|
@@ -250,10 +250,10 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
250
250
|
cwd: 'packages/example',
|
|
251
251
|
});
|
|
252
252
|
|
|
253
|
-
expect(targets['test
|
|
254
|
-
expect(targets['test
|
|
255
|
-
expect(targets['test
|
|
256
|
-
expect(targets['test
|
|
253
|
+
expect(targets['test-watch']?.executor).toBe('nx:run-commands');
|
|
254
|
+
expect(targets['test-watch']?.continuous).toBe(true);
|
|
255
|
+
expect(targets['test-watch']?.dependsOn).toEqual(['typecheck-tests']);
|
|
256
|
+
expect(targets['test-watch']?.options).toMatchObject({
|
|
257
257
|
command: 'bun test --watch --pass-with-no-tests',
|
|
258
258
|
cwd: 'packages/example',
|
|
259
259
|
});
|
|
@@ -437,8 +437,8 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
437
437
|
|
|
438
438
|
const targets = await inferProjectTargets(workspace, 'packages/example/package.json');
|
|
439
439
|
|
|
440
|
-
expect(targets['test
|
|
441
|
-
expect(targets['test
|
|
440
|
+
expect(targets['test-watch']?.continuous).toBe(true);
|
|
441
|
+
expect(targets['test-watch']?.options).toMatchObject({
|
|
442
442
|
command: 'vitest --coverage',
|
|
443
443
|
cwd: 'packages/example',
|
|
444
444
|
});
|
|
@@ -480,7 +480,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
480
480
|
// sweep it into `build`, which is a serialized test suite behind
|
|
481
481
|
// every consumer's build.
|
|
482
482
|
'test-bun': { executor: 'nx:run-commands' },
|
|
483
|
-
'test
|
|
483
|
+
'test-watch': { executor: 'nx:run-commands' },
|
|
484
484
|
// A real emitter in the same family, to prove the exclusion is
|
|
485
485
|
// the test-runner class and not the family itself.
|
|
486
486
|
'bundle-bun': { executor: 'nx:run-commands' },
|
|
@@ -882,6 +882,11 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
882
882
|
).toBe(true);
|
|
883
883
|
expect(napiInputs).not.toContain('{workspaceRoot}/bun.lock');
|
|
884
884
|
expect(napiInputs.some((input) => typeof input === 'string' && input.endsWith('/package.json'))).toBe(false);
|
|
885
|
+
// ... and on the toolchain pin, which it must state itself: `napi build`
|
|
886
|
+
// compiles with the pinned rustc but never spells `cargo --frozen`, so
|
|
887
|
+
// the sweep that gives every other cargo target its identity skips it.
|
|
888
|
+
expect(napiInputs).toContain('{workspaceRoot}/devenv.lock');
|
|
889
|
+
expect(napiInputs).toContain('{workspaceRoot}/tooling/direnv/devenv.lock');
|
|
885
890
|
expect(native['napi-debug']?.options).toMatchObject({
|
|
886
891
|
cwd: '.',
|
|
887
892
|
command:
|
|
@@ -984,45 +989,74 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
984
989
|
}
|
|
985
990
|
});
|
|
986
991
|
|
|
987
|
-
it('
|
|
992
|
+
it('keys every cached cargo target on the declared toolchain pin', async () => {
|
|
988
993
|
const workspace = await createWorkspace();
|
|
989
994
|
try {
|
|
990
|
-
await workspace
|
|
991
|
-
await workspace
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
new Response(child.stdout).text(),
|
|
1010
|
-
new Response(child.stderr).text(),
|
|
1011
|
-
]);
|
|
1012
|
-
expect({ exitCode, stderr }).toMatchObject({ exitCode: 0 });
|
|
1013
|
-
return stdout;
|
|
1014
|
-
}),
|
|
995
|
+
await writeCargoIdentityFixture(workspace);
|
|
996
|
+
const targets = await inferProjectTargets(workspace, 'packages/ferris/package.json');
|
|
997
|
+
const cached = cachedCargoTargets(targets);
|
|
998
|
+
|
|
999
|
+
// Named, so a family that stops being cached — or a new one that starts —
|
|
1000
|
+
// shows up here rather than silently leaving the sweep below empty.
|
|
1001
|
+
expect(cached.map(([name]) => name).sort()).toEqual([
|
|
1002
|
+
'cargo-cross-test-archive-aarch64-apple-darwin',
|
|
1003
|
+
'cargo-lint',
|
|
1004
|
+
'cargo-lint-cross',
|
|
1005
|
+
'cargo-test-archive',
|
|
1006
|
+
'cargo-test-ferris-core',
|
|
1007
|
+
]);
|
|
1008
|
+
// An SDK or toolchain change must invalidate every one of them. The pin
|
|
1009
|
+
// is the only thing in the identity that can see one, and it is declared
|
|
1010
|
+
// at both fleet locations because a fileset matching nothing is free.
|
|
1011
|
+
for (const [name, target] of cached) {
|
|
1012
|
+
const declared = (target.inputs ?? []).filter(
|
|
1013
|
+
(input) => typeof input === 'string' && input.endsWith('devenv.lock'),
|
|
1015
1014
|
);
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1015
|
+
expect([name, declared]).toEqual([
|
|
1016
|
+
name,
|
|
1017
|
+
['{workspaceRoot}/devenv.lock', '{workspaceRoot}/tooling/direnv/devenv.lock'],
|
|
1018
|
+
]);
|
|
1019
|
+
}
|
|
1020
|
+
} finally {
|
|
1021
|
+
await workspace.cleanup();
|
|
1022
|
+
}
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
// The property that decides whether Rust can ever use a shared cache. A cargo
|
|
1026
|
+
// target's identity used to include a dump of the ambient cargo environment,
|
|
1027
|
+
// whose values are absolute `/nix/store/…` and per-checkout devenv state
|
|
1028
|
+
// paths: no two machines agreed, and the same target hashed differently in a
|
|
1029
|
+
// bare shell and inside a devenv profile, which made the pre-push
|
|
1030
|
+
// cross-compile probe unsatisfiable by construction.
|
|
1031
|
+
//
|
|
1032
|
+
// Runtime inputs are checked by their OUTPUT, not their command text, because
|
|
1033
|
+
// that is what Nx hashes: measured, two workspaces at different absolute
|
|
1034
|
+
// paths whose only difference is the spelling of a runtime command producing
|
|
1035
|
+
// identical stdout hash identically.
|
|
1036
|
+
it('hashes no machine-local value into a cached cargo target', async () => {
|
|
1037
|
+
const workspace = await createWorkspace();
|
|
1038
|
+
try {
|
|
1039
|
+
await writeCargoIdentityFixture(workspace);
|
|
1040
|
+
const targets = await inferProjectTargets(workspace, 'packages/ferris/package.json');
|
|
1041
|
+
const offenders: string[] = [];
|
|
1042
|
+
for (const [name, target] of cachedCargoTargets(targets)) {
|
|
1043
|
+
for (const input of target.inputs ?? []) {
|
|
1044
|
+
if (typeof input === 'string') {
|
|
1045
|
+
if (!WORKSPACE_ANCHORED_INPUT.test(input)) offenders.push(`${name}: fileset ${input}`);
|
|
1046
|
+
continue;
|
|
1047
|
+
}
|
|
1048
|
+
if ('env' in input) offenders.push(`${name}: environment input ${String(input.env)}`);
|
|
1049
|
+
if (!('runtime' in input)) continue;
|
|
1050
|
+
const value = await runtimeInputValue(input.runtime, workspace.context.workspaceRoot);
|
|
1051
|
+
const rooted = PATH_OUTSIDE_WORKSPACE.exec(value);
|
|
1052
|
+
if (rooted) offenders.push(`${name}: runtime input yields ${rooted[0].trim()} — from ${input.runtime}`);
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
expect(offenders).toEqual([]);
|
|
1022
1056
|
} finally {
|
|
1023
1057
|
await workspace.cleanup();
|
|
1024
1058
|
}
|
|
1025
|
-
},
|
|
1059
|
+
}, 60_000);
|
|
1026
1060
|
|
|
1027
1061
|
it('scopes each per-crate cargo-test command to exactly one package', async () => {
|
|
1028
1062
|
const workspace = await createWorkspace();
|
|
@@ -1983,6 +2017,62 @@ interface WorkspaceFixture {
|
|
|
1983
2017
|
cleanup(): Promise<void>;
|
|
1984
2018
|
}
|
|
1985
2019
|
|
|
2020
|
+
/**
|
|
2021
|
+
* A fileset Nx resolves against the workspace, or a named input nx.json
|
|
2022
|
+
* declares. Anything else is a path this machine happens to have.
|
|
2023
|
+
*/
|
|
2024
|
+
const WORKSPACE_ANCHORED_INPUT = /^!?(?:\{(?:projectRoot|workspaceRoot)\}\/|\^?[A-Za-z][\w-]*$)/;
|
|
2025
|
+
|
|
2026
|
+
/** A path rooted outside the workspace, wherever it appears in a hashed value. */
|
|
2027
|
+
const PATH_OUTSIDE_WORKSPACE = /(?:^|[^\w.\-~])(\/[\w.-]+\/[\w.-]+)/;
|
|
2028
|
+
|
|
2029
|
+
/**
|
|
2030
|
+
* One Cargo workspace producing every cached cargo target family inference can
|
|
2031
|
+
* make: the host archive, a per-crate run, both lint verdicts, and — through
|
|
2032
|
+
* the `cross-targets` declaration — the foreign-triple archive, which states
|
|
2033
|
+
* its own inputs instead of receiving them from the `cargo --frozen` sweep.
|
|
2034
|
+
*/
|
|
2035
|
+
async function writeCargoIdentityFixture(workspace: WorkspaceFixture): Promise<void> {
|
|
2036
|
+
await workspace.write('packages/ferris/package.json', '{"name":"ferris"}\n');
|
|
2037
|
+
await workspace.write(
|
|
2038
|
+
'packages/ferris/Cargo.toml',
|
|
2039
|
+
'[workspace]\nmembers = ["crates/ferris-core"]\n\n' +
|
|
2040
|
+
'[workspace.metadata.smoothbricks.test]\n' +
|
|
2041
|
+
'cross-targets = [{ target = "aarch64-apple-darwin", cargo = "scripts/cargo-for-nextest.sh" }]\n',
|
|
2042
|
+
);
|
|
2043
|
+
await workspace.write('packages/ferris/crates/ferris-core/Cargo.toml', '[package]\nname = "ferris-core"\n');
|
|
2044
|
+
await workspace.write('packages/ferris/scripts/cargo-for-nextest.sh', '#!/bin/sh\nexec cargo "$@"\n');
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
/**
|
|
2048
|
+
* The targets a cache verdict can be wrong about: cached, and running cargo.
|
|
2049
|
+
* Derived rather than listed, so a new cargo family joins these assertions the
|
|
2050
|
+
* day it is inferred instead of the day someone remembers to add it.
|
|
2051
|
+
*/
|
|
2052
|
+
function cachedCargoTargets(targets: Record<string, TargetConfiguration>): [string, TargetConfiguration][] {
|
|
2053
|
+
return Object.entries(targets).filter(([, target]) => {
|
|
2054
|
+
const command: unknown = target.options?.command;
|
|
2055
|
+
const commands: unknown = target.options?.commands;
|
|
2056
|
+
const text = [
|
|
2057
|
+
typeof command === 'string' ? command : '',
|
|
2058
|
+
...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
|
|
2059
|
+
].join('\n');
|
|
2060
|
+
return target.cache === true && /\bcargo/.test(text);
|
|
2061
|
+
});
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
/** What Nx actually hashes for a `runtime` input: the command's stdout. */
|
|
2065
|
+
async function runtimeInputValue(command: string, cwd: string): Promise<string> {
|
|
2066
|
+
const child = Bun.spawn(['sh', '-c', command], { cwd, stdout: 'pipe', stderr: 'pipe' });
|
|
2067
|
+
const [exitCode, stdout, stderr] = await Promise.all([
|
|
2068
|
+
child.exited,
|
|
2069
|
+
new Response(child.stdout).text(),
|
|
2070
|
+
new Response(child.stderr).text(),
|
|
2071
|
+
]);
|
|
2072
|
+
expect({ command, exitCode, stderr }).toMatchObject({ command, exitCode: 0 });
|
|
2073
|
+
return stdout;
|
|
2074
|
+
}
|
|
2075
|
+
|
|
1986
2076
|
async function inferProject(
|
|
1987
2077
|
workspace: WorkspaceFixture,
|
|
1988
2078
|
packageJsonPath: string,
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
|
|
|
15
15
|
import { parse as parseToml } from 'smol-toml';
|
|
16
16
|
|
|
17
17
|
import { BOUNDED_TEST_KILL_AFTER_MS, BOUNDED_TEST_TIMEOUT_MS } from './bounded-test-policy.js';
|
|
18
|
+
import { CARGO_TOOLCHAIN_NAMED_INPUT, CARGO_TOOLCHAIN_PIN_INPUTS } from './cargo-toolchain-policy.js';
|
|
18
19
|
import {
|
|
19
20
|
type AttributedCargoWorkspacePackage,
|
|
20
21
|
attributeCargoWorkspacePackages,
|
|
@@ -48,7 +49,7 @@ import {
|
|
|
48
49
|
} from './cross-check-policy.js';
|
|
49
50
|
import { hashVersionlessCrateManifests } from './manifest-hash.js';
|
|
50
51
|
import { isNonSourceDirectory } from './source-directories.js';
|
|
51
|
-
import { PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
|
|
52
|
+
import { isBuildOutputTargetName, isTestRunnerTargetName, PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
|
|
52
53
|
|
|
53
54
|
export { CARGO_TEST_COMPILE_TARGET };
|
|
54
55
|
|
|
@@ -57,7 +58,6 @@ export const DEPLOY_TARGET = 'deploy';
|
|
|
57
58
|
/** The purely file-derived half a deploy needs built first; cached. */
|
|
58
59
|
export const DEPLOY_BUILD_TARGET = 'deploy-build';
|
|
59
60
|
|
|
60
|
-
const BUILD_OUTPUT_TARGET_PATTERN = /-(?:js|web|html|css|android|native|napi|bun|wasm)$/;
|
|
61
61
|
/** The workspace-wide manifests a release rewrites: the version of every member, and the lockfile mirror of it. */
|
|
62
62
|
const RELEASE_REWRITTEN_WORKSPACE_INPUTS = ['{workspaceRoot}/package.json', '{workspaceRoot}/bun.lock'];
|
|
63
63
|
const TYPESCRIPT_TOOLCHAIN_INPUTS = [
|
|
@@ -203,36 +203,18 @@ function hostPlatformTargetNames(targetNames: Iterable<string>, hostPlatform: Na
|
|
|
203
203
|
* its name ends in a family: `*-js`, `*-web`, `*-html`, `*-css`, `*-android`,
|
|
204
204
|
* `*-native`, `*-napi`, `*-bun`, `*-wasm`.
|
|
205
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
206
|
* So the rule is the CLASS, not one namespace: a target that RUNS tests is
|
|
215
207
|
* 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
|
|
208
|
+
* rather than delegating to Nx's matcher is what makes that expressible, and
|
|
209
|
+
* `package-target-policy` reads the same two predicates to decide whether a
|
|
210
|
+
* hand-declared aggregate is naming an edge this list already produces.
|
|
217
211
|
*/
|
|
218
212
|
function buildOutputTargetNames(targetNames: Iterable<string>): string[] {
|
|
219
213
|
return [...new Set(targetNames)]
|
|
220
|
-
.filter((name) =>
|
|
214
|
+
.filter((name) => isBuildOutputTargetName(name) && !isTestRunnerTargetName(name))
|
|
221
215
|
.sort();
|
|
222
216
|
}
|
|
223
217
|
|
|
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
|
-
|
|
236
218
|
//#region smoo!n/rust-output-target-inference
|
|
237
219
|
// Cargo workspace inference: a package.json sitting next to a Cargo.toml that
|
|
238
220
|
// declares [workspace] gets direct cargo-test/test targets, cargo-lint feeding
|
|
@@ -269,15 +251,10 @@ const REPO_ROOT_CARGO_OUTPUT_INPUTS = [
|
|
|
269
251
|
'{workspaceRoot}/scripts/*.sh',
|
|
270
252
|
'!{workspaceRoot}/**/target/**',
|
|
271
253
|
];
|
|
272
|
-
// The
|
|
273
|
-
//
|
|
274
|
-
//
|
|
275
|
-
//
|
|
276
|
-
// read ZIG*. An output linked against one sysroot must never hash equal to
|
|
277
|
-
// the same sources linked against another.
|
|
278
|
-
const CARGO_ENVIRONMENT_INPUT = {
|
|
279
|
-
runtime: `bun -e 'const fs = require("node:fs"); const path = require("node:path"); const home = process.env.CARGO_HOME || path.join(require("node:os").homedir(), ".cargo"); console.log(JSON.stringify({env: Object.entries(process.env).filter(([name]) => /^(?:CARGO_|RUST|NEXTEST_|CLIPPY_|CC(?:_|$)|CXX(?:_|$)|AR(?:_|$)|CFLAGS|CXXFLAGS|CPPFLAGS|LDFLAGS|PKG_CONFIG|TARGET_|HOST_|SDKROOT$|DEVELOPER_DIR$|MACOSX_DEPLOYMENT_TARGET$|IPHONEOS_DEPLOYMENT_TARGET$|LIBCLANG_PATH$|BINDGEN_EXTRA_CLANG_ARGS|CMAKE_|ZIG)/.test(name)).sort(([a], [b]) => a.localeCompare(b)), config: ["config", "config.toml"].map(name => { const file = path.join(home, name); return fs.existsSync(file) ? fs.readFileSync(file, "utf8") : null; })}));'`,
|
|
280
|
-
};
|
|
254
|
+
// The toolchain half of a cargo task's identity lives in its own module
|
|
255
|
+
// because `smoo monorepo validate` reads the same two constants to refuse a
|
|
256
|
+
// declaration that drops it. See cargo-toolchain-policy.ts for why it is the
|
|
257
|
+
// declared pin and not the ambient environment.
|
|
281
258
|
|
|
282
259
|
function cargoRuntimeInput(projectRoot: string, command: string): { runtime: string } {
|
|
283
260
|
const quotedRoot = `'${projectRoot.replaceAll("'", "'\"'\"'")}'`;
|
|
@@ -299,10 +276,17 @@ function targetCommandText(target: TargetConfiguration): string {
|
|
|
299
276
|
].join('\n');
|
|
300
277
|
}
|
|
301
278
|
/**
|
|
302
|
-
* The N-API build's inputs beyond the Cargo sources: the
|
|
303
|
-
* block, which names the binary and the targets, and the napi
|
|
304
|
-
* version from the lockfile. Neither the package version nor
|
|
305
|
-
* lockfile entry is part of the artifact.
|
|
279
|
+
* The N-API build's inputs beyond the Cargo sources: the toolchain pin, the
|
|
280
|
+
* package's `napi` block, which names the binary and the targets, and the napi
|
|
281
|
+
* CLI's resolved version from the lockfile. Neither the package version nor
|
|
282
|
+
* any other lockfile entry is part of the artifact.
|
|
283
|
+
*
|
|
284
|
+
* The pin is stated here rather than inherited: `napi build` compiles with the
|
|
285
|
+
* same rustc and links against the same SDK as any other cargo target, but its
|
|
286
|
+
* command does not spell `cargo --frozen`, which is how every other cached
|
|
287
|
+
* cargo target acquires its toolchain identity. Without it a `.node` linked
|
|
288
|
+
* against one toolchain hashed equal to the same sources linked against the
|
|
289
|
+
* next one.
|
|
306
290
|
*/
|
|
307
291
|
function napiInputs(projectRoot: string, repoRooted: boolean): TargetConfiguration['inputs'] {
|
|
308
292
|
// Runtime inputs execute from the workspace root, so the manifest is named
|
|
@@ -310,6 +294,7 @@ function napiInputs(projectRoot: string, repoRooted: boolean): TargetConfigurati
|
|
|
310
294
|
const packageJson = `./${posix.join(projectRoot, 'package.json')}`;
|
|
311
295
|
return [
|
|
312
296
|
...(repoRooted ? REPO_ROOT_CARGO_OUTPUT_INPUTS : CARGO_OUTPUT_INPUTS),
|
|
297
|
+
...CARGO_TOOLCHAIN_PIN_INPUTS,
|
|
313
298
|
{ runtime: `bun -e 'console.log(JSON.stringify(require(${JSON.stringify(packageJson)}).napi ?? null))'` },
|
|
314
299
|
{ externalDependencies: ['@napi-rs/cli'] },
|
|
315
300
|
];
|
|
@@ -496,7 +481,7 @@ function createCargoTestArchiveTarget(projectRoot: string, toolConfig: string):
|
|
|
496
481
|
* (measured: `metadata … --frozen` and `test --no-run … --frozen`).
|
|
497
482
|
*
|
|
498
483
|
* The archive target therefore states `CARGO_FETCH_TARGET` and the toolchain
|
|
499
|
-
*
|
|
484
|
+
* inputs itself: both are otherwise attached by matching the
|
|
500
485
|
* `cargo --frozen` prefix in a command, and this command does not carry it.
|
|
501
486
|
*
|
|
502
487
|
* The RUNNER declares no dependency on the archive and is not cached. That is
|
|
@@ -522,18 +507,21 @@ function createCargoCrossTestTargets(
|
|
|
522
507
|
[cargoCrossTestArchiveTargetName(triple)]: {
|
|
523
508
|
executor: 'nx:run-commands',
|
|
524
509
|
cache: true,
|
|
525
|
-
// The
|
|
526
|
-
// declared driver's own bytes.
|
|
527
|
-
// the `cargo --frozen` prefix in a command,
|
|
528
|
-
// spell — and
|
|
529
|
-
//
|
|
530
|
-
//
|
|
531
|
-
//
|
|
532
|
-
//
|
|
510
|
+
// The toolchain inputs every cached cargo target carries, plus the
|
|
511
|
+
// declared driver's own bytes. Those toolchain inputs are attached
|
|
512
|
+
// elsewhere by matching the `cargo --frozen` prefix in a command,
|
|
513
|
+
// which this one does not spell — and the pin matters MORE here than
|
|
514
|
+
// anywhere else: the cross regime this archive links under (the C
|
|
515
|
+
// toolchain for the triple, its sysroot, and the `CARGO` driver below)
|
|
516
|
+
// comes out of the same devenv lock, so an archive built under one pin
|
|
517
|
+
// must never hash equal to the same sources built under another. The
|
|
518
|
+
// declared driver is hashed by its own bytes; nothing samples the shell
|
|
519
|
+
// that supplies it, which is why this hash is the same bare and inside
|
|
520
|
+
// the cross profile.
|
|
533
521
|
inputs: [
|
|
534
522
|
...archiveInputs,
|
|
523
|
+
...CARGO_TOOLCHAIN_PIN_INPUTS,
|
|
535
524
|
...(cargo === undefined ? [] : [`{projectRoot}/${cargo}`]),
|
|
536
|
-
cargoRuntimeInput(projectRoot, CARGO_ENVIRONMENT_INPUT.runtime),
|
|
537
525
|
cargoRuntimeInput(projectRoot, 'rustc -vV && cargo -V && cargo nextest --version'),
|
|
538
526
|
],
|
|
539
527
|
outputs: [`{projectRoot}/${archiveFile}`],
|
|
@@ -947,7 +935,7 @@ async function createProjectTargets(
|
|
|
947
935
|
};
|
|
948
936
|
const inferredTestWatchCommand = inferTestWatchCommand(packageJson);
|
|
949
937
|
if (inferredTestWatchCommand) {
|
|
950
|
-
targets['test
|
|
938
|
+
targets['test-watch'] = {
|
|
951
939
|
executor: 'nx:run-commands',
|
|
952
940
|
continuous: true,
|
|
953
941
|
dependsOn: ['typecheck-tests'],
|
|
@@ -1320,7 +1308,7 @@ async function createProjectTargets(
|
|
|
1320
1308
|
: '';
|
|
1321
1309
|
target.inputs = [
|
|
1322
1310
|
...(target.inputs ?? CARGO_INPUTS),
|
|
1323
|
-
|
|
1311
|
+
...CARGO_TOOLCHAIN_PIN_INPUTS,
|
|
1324
1312
|
cargoRuntimeInput(typeof cwd === 'string' ? cwd : projectRoot, `rustc -vV && cargo -V${versions}`),
|
|
1325
1313
|
];
|
|
1326
1314
|
}
|
|
@@ -1575,6 +1563,26 @@ async function createProjectTargets(
|
|
|
1575
1563
|
};
|
|
1576
1564
|
}
|
|
1577
1565
|
|
|
1566
|
+
// A declared target REPLACES the inferred input list, pin included, so a
|
|
1567
|
+
// repository that hand-writes `inputs` for a cargo target silently loses its
|
|
1568
|
+
// toolchain identity. Name the pin once here so such a declaration can
|
|
1569
|
+
// reference it as `cargoToolchain` instead of restating two paths that could
|
|
1570
|
+
// drift from the plugin; `smoo monorepo validate` refuses a cached cargo
|
|
1571
|
+
// target whose resolved inputs reach neither. Defined only for projects that
|
|
1572
|
+
// HAVE cargo targets: a project's namedInputs definitions are hashed into
|
|
1573
|
+
// its configuration hash, so defining it everywhere would invalidate every
|
|
1574
|
+
// task in the repository to serve projects that can never reference it.
|
|
1575
|
+
const namedInputs: Record<string, NonNullable<TargetConfiguration['inputs']>> = {
|
|
1576
|
+
...(versionless === null
|
|
1577
|
+
? {}
|
|
1578
|
+
: {
|
|
1579
|
+
[VERSIONLESS_PRODUCTION_INPUT]: ['production', ...versionlessProjectInputs],
|
|
1580
|
+
[VERSIONLESS_DEFAULT_INPUT]: ['default', ...versionlessProjectInputs],
|
|
1581
|
+
}),
|
|
1582
|
+
...(Object.keys(targets).some((name) => name.startsWith('cargo-') || name.startsWith('napi-'))
|
|
1583
|
+
? { [CARGO_TOOLCHAIN_NAMED_INPUT]: [...CARGO_TOOLCHAIN_PIN_INPUTS] }
|
|
1584
|
+
: {}),
|
|
1585
|
+
};
|
|
1578
1586
|
return {
|
|
1579
1587
|
projects: {
|
|
1580
1588
|
[projectRoot]: {
|
|
@@ -1588,14 +1596,7 @@ async function createProjectTargets(
|
|
|
1588
1596
|
// Nx resolves a named input from the project first and nx.json second,
|
|
1589
1597
|
// so a workspace-wide definition still covers projects this plugin
|
|
1590
1598
|
// never inferred.
|
|
1591
|
-
...(
|
|
1592
|
-
? {}
|
|
1593
|
-
: {
|
|
1594
|
-
namedInputs: {
|
|
1595
|
-
[VERSIONLESS_PRODUCTION_INPUT]: ['production', ...versionlessProjectInputs],
|
|
1596
|
-
[VERSIONLESS_DEFAULT_INPUT]: ['default', ...versionlessProjectInputs],
|
|
1597
|
-
},
|
|
1598
|
-
}),
|
|
1599
|
+
...(Object.keys(namedInputs).length === 0 ? {} : { namedInputs }),
|
|
1599
1600
|
},
|
|
1600
1601
|
},
|
|
1601
1602
|
};
|
|
@@ -2021,7 +2022,7 @@ function classifyPackageLocalBuildOutputs(packageJson: PackageJson): { ordinary:
|
|
|
2021
2022
|
const targets = packageJson.nx?.targets;
|
|
2022
2023
|
const targetNames = isRecord(targets) ? Object.keys(targets) : [];
|
|
2023
2024
|
return {
|
|
2024
|
-
ordinary: targetNames.some(
|
|
2025
|
+
ordinary: targetNames.some(isBuildOutputTargetName),
|
|
2025
2026
|
platform: targetNames.some((targetName) =>
|
|
2026
2027
|
PLATFORM_TARGET_GLOBS.some((glob) => targetName.endsWith(glob.slice(1))),
|
|
2027
2028
|
),
|
|
@@ -175,13 +175,7 @@ describe('package target policy', () => {
|
|
|
175
175
|
});
|
|
176
176
|
|
|
177
177
|
const resolvedTargetsByProject = new Map<string, ResolvedProjectTargets>([
|
|
178
|
-
[
|
|
179
|
-
'lib',
|
|
180
|
-
{
|
|
181
|
-
targets: new Set(['build', 'tsc-js']),
|
|
182
|
-
buildDependsOn: ['^build', '*-js'],
|
|
183
|
-
},
|
|
184
|
-
],
|
|
178
|
+
['lib', { targets: new Set(['build', 'tsc-js']) }],
|
|
185
179
|
]);
|
|
186
180
|
|
|
187
181
|
expect(applyPackageTargetPolicy(root, { resolvedTargetsByProject })).toBe(true);
|
|
@@ -216,13 +210,7 @@ describe('package target policy', () => {
|
|
|
216
210
|
});
|
|
217
211
|
|
|
218
212
|
const resolvedTargetsByProject = new Map<string, ResolvedProjectTargets>([
|
|
219
|
-
[
|
|
220
|
-
'lib',
|
|
221
|
-
{
|
|
222
|
-
targets: new Set(['build', 'tsc-js']),
|
|
223
|
-
buildDependsOn: ['^build', '*-js'],
|
|
224
|
-
},
|
|
225
|
-
],
|
|
213
|
+
['lib', { targets: new Set(['build', 'tsc-js']) }],
|
|
226
214
|
]);
|
|
227
215
|
|
|
228
216
|
expect(applyPackageTargetPolicy(root, { resolvedTargetsByProject })).toBe(false);
|
|
@@ -911,13 +899,7 @@ describe('applyPackageTargetPolicyTree', () => {
|
|
|
911
899
|
});
|
|
912
900
|
|
|
913
901
|
const resolvedTargetsByProject = new Map<string, ResolvedProjectTargets>([
|
|
914
|
-
[
|
|
915
|
-
'lib',
|
|
916
|
-
{
|
|
917
|
-
targets: new Set(['build', 'tsc-js']),
|
|
918
|
-
buildDependsOn: ['^build', '*-js'],
|
|
919
|
-
},
|
|
920
|
-
],
|
|
902
|
+
['lib', { targets: new Set(['build', 'tsc-js']) }],
|
|
921
903
|
]);
|
|
922
904
|
|
|
923
905
|
expect(applyPackageTargetPolicyTree(tree, { resolvedTargetsByProject })).toBe(true);
|
|
@@ -943,13 +925,7 @@ describe('applyPackageTargetPolicyTree', () => {
|
|
|
943
925
|
});
|
|
944
926
|
|
|
945
927
|
const resolvedTargetsByProject = new Map<string, ResolvedProjectTargets>([
|
|
946
|
-
[
|
|
947
|
-
'lib',
|
|
948
|
-
{
|
|
949
|
-
targets: new Set(['build', 'tsc-js']),
|
|
950
|
-
buildDependsOn: ['^build', '*-js'],
|
|
951
|
-
},
|
|
952
|
-
],
|
|
928
|
+
['lib', { targets: new Set(['build', 'tsc-js']) }],
|
|
953
929
|
]);
|
|
954
930
|
|
|
955
931
|
expect(applyPackageTargetPolicyTree(tree, { resolvedTargetsByProject })).toBe(false);
|