@smoothbricks/nx-plugin 0.2.4 → 0.3.0
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/dist/index.d.ts.map +1 -1
- package/dist/index.js +136 -1
- package/dist/package-target-policy.d.ts +2 -0
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +36 -4
- package/package.json +1 -1
- package/src/index.test.ts +144 -4
- package/src/index.ts +153 -1
- package/src/package-target-policy.test.ts +135 -0
- package/src/package-target-policy.ts +66 -7
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAuB,aAAa,EAAuB,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAuB,aAAa,EAAuB,MAAM,0BAA0B,CAAC;AAgDxG,eAAO,MAAM,aAAa,EAAE,aAsB3B,CAAC;;;;AAEF,wBAAiC"}
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,42 @@ const RESERVED_ZIG_STEPS = new Set(['all', 'clean', 'install', 'test']);
|
|
|
7
7
|
const ZIG_STEP_PATTERN = /\bb\.step\(\s*["']([^"']+)["']\s*,/g;
|
|
8
8
|
const VALID_ZIG_STEP_NAME = /^[A-Za-z0-9_-]+$/;
|
|
9
9
|
const BUILD_OUTPUT_TARGET_PATTERN = /-(?:js|web|html|css|ios|android|native|napi|bun|wasm)$/;
|
|
10
|
+
// Cargo workspace inference: a package.json sitting next to a Cargo.toml that
|
|
11
|
+
// declares [workspace] gets direct cargo-test/test targets, cargo-lint feeding
|
|
12
|
+
// the lint aggregate, mutation (cargo-mutants), bench, and — per cdylib member
|
|
13
|
+
// crate whose package name ends in `-wasm` — a cargo-wasm build producing
|
|
14
|
+
// dist/<crate>.wasm. Explicit nx.targets entries in the package.json always win
|
|
15
|
+
// over inference.
|
|
16
|
+
const CARGO_WORKSPACE_PATTERN = /^\s*\[workspace\]/m;
|
|
17
|
+
const CARGO_MEMBERS_PATTERN = /^\s*members\s*=\s*\[([^\]]*)\]/m;
|
|
18
|
+
const CARGO_MEMBER_ENTRY_PATTERN = /["']([^"']+)["']/g;
|
|
19
|
+
const CARGO_PACKAGE_NAME_PATTERN = /^\s*name\s*=\s*["']([^"']+)["']/m;
|
|
20
|
+
const CARGO_WASM_CRATE_NAME_PATTERN = /-wasm$/;
|
|
21
|
+
const CARGO_CDYLIB_CRATE_TYPE_PATTERN = /^\s*crate-type\s*=\s*\[[^\]]*["']cdylib["']/m;
|
|
22
|
+
const CARGO_WASM_RELEASE_PROFILE_PATTERN = /^\s*\[profile\.wasm-release\]/m;
|
|
23
|
+
const CARGO_INPUTS = [
|
|
24
|
+
'{projectRoot}/**/*.rs',
|
|
25
|
+
'{projectRoot}/**/Cargo.toml',
|
|
26
|
+
'{projectRoot}/Cargo.lock',
|
|
27
|
+
'{projectRoot}/.cargo/config.toml',
|
|
28
|
+
'!{projectRoot}/target/**',
|
|
29
|
+
];
|
|
30
|
+
function createCargoTestTarget(projectRoot) {
|
|
31
|
+
return {
|
|
32
|
+
executor: '@smoothbricks/nx-plugin:bounded-exec',
|
|
33
|
+
cache: true,
|
|
34
|
+
inputs: CARGO_INPUTS,
|
|
35
|
+
options: {
|
|
36
|
+
command: 'cargo test --workspace',
|
|
37
|
+
cwd: projectRoot,
|
|
38
|
+
timeoutMs: 600000,
|
|
39
|
+
killAfterMs: 10000,
|
|
40
|
+
},
|
|
41
|
+
configurations: {
|
|
42
|
+
production: { command: 'cargo test --workspace --release' },
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
10
46
|
export const createNodesV2 = [
|
|
11
47
|
'**/package.json',
|
|
12
48
|
async (projectConfigurationFiles, _options, context) => {
|
|
@@ -70,6 +106,72 @@ async function createProjectTargets(packageJsonPath, workspaceRoot) {
|
|
|
70
106
|
else if (hasLibTsconfig) {
|
|
71
107
|
validationTargets.push('typecheck');
|
|
72
108
|
}
|
|
109
|
+
const cargo = await readCargoWorkspace(absoluteProjectRoot);
|
|
110
|
+
if (cargo) {
|
|
111
|
+
const declared = packageJson.nx?.targets ?? {};
|
|
112
|
+
if (!('cargo-test' in declared)) {
|
|
113
|
+
targets['cargo-test'] = createCargoTestTarget(projectRoot);
|
|
114
|
+
}
|
|
115
|
+
if (!('cargo-lint' in declared)) {
|
|
116
|
+
targets['cargo-lint'] = {
|
|
117
|
+
executor: 'nx:run-commands',
|
|
118
|
+
cache: true,
|
|
119
|
+
inputs: CARGO_INPUTS,
|
|
120
|
+
options: {
|
|
121
|
+
commands: ['cargo fmt --all --check', 'cargo clippy --workspace --all-targets -- -D warnings'],
|
|
122
|
+
cwd: projectRoot,
|
|
123
|
+
parallel: false,
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
validationTargets.push('cargo-lint');
|
|
128
|
+
if (!('test' in declared) && typeof packageJson.scripts?.test !== 'string') {
|
|
129
|
+
// Execute Cargo directly: workspace targetDefaults may replace test.dependsOn.
|
|
130
|
+
targets.test = createCargoTestTarget(projectRoot);
|
|
131
|
+
}
|
|
132
|
+
if (!('mutation' in declared)) {
|
|
133
|
+
// Mutation runs are minutes-to-hours: never cached, never part of build/lint.
|
|
134
|
+
// CI runs these per-PR via `cargo mutants --in-diff` (see mutants.toml docs).
|
|
135
|
+
targets.mutation = {
|
|
136
|
+
executor: 'nx:run-commands',
|
|
137
|
+
cache: false,
|
|
138
|
+
options: { command: 'cargo mutants --workspace', cwd: projectRoot },
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
if (!('bench' in declared)) {
|
|
142
|
+
targets.bench = {
|
|
143
|
+
executor: 'nx:run-commands',
|
|
144
|
+
cache: false,
|
|
145
|
+
options: { command: 'cargo bench --workspace', cwd: projectRoot },
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
if (cargo.wasmCrates.length > 0 && !('cargo-wasm' in declared)) {
|
|
149
|
+
const profile = cargo.hasWasmReleaseProfile ? 'wasm-release' : 'release';
|
|
150
|
+
const buildAndCopy = (crate, profileName) => {
|
|
151
|
+
const artifact = crate.replace(/-/g, '_');
|
|
152
|
+
const profileFlag = profileName === null ? '' : ` --profile ${profileName}`;
|
|
153
|
+
const outputDirectory = profileName ?? 'debug';
|
|
154
|
+
return `cargo build${profileFlag} --target wasm32-unknown-unknown -p ${crate} && mkdir -p dist && cp target/wasm32-unknown-unknown/${outputDirectory}/${artifact}.wasm dist/${artifact}.wasm`;
|
|
155
|
+
};
|
|
156
|
+
targets['cargo-wasm'] = {
|
|
157
|
+
executor: 'nx:run-commands',
|
|
158
|
+
cache: true,
|
|
159
|
+
inputs: CARGO_INPUTS,
|
|
160
|
+
outputs: ['{projectRoot}/dist/**/*.wasm'],
|
|
161
|
+
options: {
|
|
162
|
+
commands: cargo.wasmCrates.map((crate) => buildAndCopy(crate, profile)),
|
|
163
|
+
cwd: projectRoot,
|
|
164
|
+
parallel: false,
|
|
165
|
+
},
|
|
166
|
+
configurations: {
|
|
167
|
+
development: {
|
|
168
|
+
commands: cargo.wasmCrates.map((crate) => buildAndCopy(crate, null)),
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
hasBuildOutputTarget = true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
73
175
|
const zigSteps = await readZigSteps(absoluteProjectRoot, projectRoot);
|
|
74
176
|
for (const step of zigSteps) {
|
|
75
177
|
const targetName = `zig-${step}`;
|
|
@@ -118,9 +220,13 @@ async function createProjectTargets(packageJsonPath, workspaceRoot) {
|
|
|
118
220
|
dependsOn: validationTargets,
|
|
119
221
|
};
|
|
120
222
|
}
|
|
223
|
+
const projectName = packageJson.nx?.name ?? packageJson.name;
|
|
224
|
+
if (typeof projectName !== 'string' || projectName.length === 0) {
|
|
225
|
+
throw new Error(`${packageJsonPath} must declare a non-empty package or nx project name`);
|
|
226
|
+
}
|
|
121
227
|
return {
|
|
122
228
|
projects: {
|
|
123
|
-
[projectRoot]: { targets },
|
|
229
|
+
[projectRoot]: { name: projectName, targets },
|
|
124
230
|
},
|
|
125
231
|
};
|
|
126
232
|
}
|
|
@@ -175,6 +281,35 @@ function hasPackageLocalBuildOutputTarget(packageJson) {
|
|
|
175
281
|
}
|
|
176
282
|
return Object.keys(targets).some((targetName) => BUILD_OUTPUT_TARGET_PATTERN.test(targetName));
|
|
177
283
|
}
|
|
284
|
+
async function readCargoWorkspace(absoluteProjectRoot) {
|
|
285
|
+
const cargoTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
|
|
286
|
+
if (!existsSync(cargoTomlPath)) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
const cargoToml = await readFile(cargoTomlPath, 'utf-8');
|
|
290
|
+
if (!CARGO_WORKSPACE_PATTERN.test(cargoToml)) {
|
|
291
|
+
// Member crates get their targets from the workspace-root package.json,
|
|
292
|
+
// never per-crate — one Nx project per Cargo workspace.
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
const wasmCrates = [];
|
|
296
|
+
const membersList = CARGO_MEMBERS_PATTERN.exec(cargoToml)?.[1] ?? '';
|
|
297
|
+
for (const match of membersList.matchAll(CARGO_MEMBER_ENTRY_PATTERN)) {
|
|
298
|
+
const memberTomlPath = join(absoluteProjectRoot, match[1], 'Cargo.toml');
|
|
299
|
+
if (!existsSync(memberTomlPath)) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const memberToml = await readFile(memberTomlPath, 'utf-8');
|
|
303
|
+
const name = CARGO_PACKAGE_NAME_PATTERN.exec(memberToml)?.[1];
|
|
304
|
+
if (name &&
|
|
305
|
+
CARGO_WASM_CRATE_NAME_PATTERN.test(name) &&
|
|
306
|
+
CARGO_CDYLIB_CRATE_TYPE_PATTERN.test(memberToml) &&
|
|
307
|
+
!wasmCrates.includes(name)) {
|
|
308
|
+
wasmCrates.push(name);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return { wasmCrates, hasWasmReleaseProfile: CARGO_WASM_RELEASE_PROFILE_PATTERN.test(cargoToml) };
|
|
312
|
+
}
|
|
178
313
|
async function readZigSteps(absoluteProjectRoot, projectRoot) {
|
|
179
314
|
const buildZigPath = join(absoluteProjectRoot, 'build.zig');
|
|
180
315
|
if (!existsSync(buildZigPath)) {
|
|
@@ -4,6 +4,8 @@ export type { NxPolicyIssue };
|
|
|
4
4
|
export interface ResolvedProjectTargets {
|
|
5
5
|
targets: ReadonlySet<string>;
|
|
6
6
|
buildDependsOn?: readonly string[];
|
|
7
|
+
targetExecutors?: ReadonlyMap<string, string>;
|
|
8
|
+
targetScripts?: ReadonlyMap<string, string>;
|
|
7
9
|
}
|
|
8
10
|
export interface PackageTargetPolicyOptions {
|
|
9
11
|
resolvedTargetsByProject?: ReadonlyMap<string, ReadonlySet<string> | ResolvedProjectTargets>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"package-target-policy.d.ts","sourceRoot":"","sources":["../src/package-target-policy.ts"],"names":[],"mappings":"AAGA,OAAO,EAAyB,KAAK,IAAI,EAAc,MAAM,0BAA0B,CAAC;AAGxF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAElE,YAAY,EAAE,aAAa,EAAE,CAAC;AAE9B,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC7B,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"package-target-policy.d.ts","sourceRoot":"","sources":["../src/package-target-policy.ts"],"names":[],"mappings":"AAGA,OAAO,EAAyB,KAAK,IAAI,EAAc,MAAM,0BAA0B,CAAC;AAGxF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAElE,YAAY,EAAE,aAAa,EAAE,CAAC;AAE9B,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC7B,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,eAAe,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9C,aAAa,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC7C;AAED,MAAM,WAAW,0BAA0B;IACzC,wBAAwB,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,sBAAsB,CAAC,CAAC;CAC9F;AAED,eAAO,MAAM,yBAAyB,UAWrC,CAAC;AA0HF,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI,CAGhF;AA2JD,wBAAgB,UAAU,CACxB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,OAAO,EACnB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,MAAM,CAMR;AAklBD;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,GAAE,0BAA+B,GAAG,aAAa,EAAE,CAqBlH;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,GAAE,0BAA+B,GAAG,OAAO,CA4B1G;AAoJD,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,WAAW,EAAE,MAAM,EACnB,eAAe,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GACpC,aAAa,EAAE,CAEjB;AAED,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,WAAW,CAAC,MAAM,CAAC,EACnC,OAAO,GAAE;IAAE,eAAe,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CAAO,GACtD,OAAO,CAMT;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,0BAA+B,GAAG,aAAa,EAAE,CAqBhH;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,0BAA+B,GAAG,OAAO,CA6BxG"}
|
|
@@ -141,11 +141,17 @@ function resolvedProjectBuildDependsOn(resolvedProject) {
|
|
|
141
141
|
}
|
|
142
142
|
return resolvedProject.buildDependsOn;
|
|
143
143
|
}
|
|
144
|
+
function resolvedProjectTargetExecutor(resolvedProject, targetName) {
|
|
145
|
+
return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targetExecutors?.get(targetName) : undefined;
|
|
146
|
+
}
|
|
147
|
+
function resolvedProjectTargetScript(resolvedProject, targetName) {
|
|
148
|
+
return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targetScripts?.get(targetName) : undefined;
|
|
149
|
+
}
|
|
144
150
|
function isResolvedProjectTargets(value) {
|
|
145
151
|
return isRecord(value) && value.targets instanceof Set;
|
|
146
152
|
}
|
|
147
153
|
function targetExistsInResolvedProject(targetName, resolvedTargets) {
|
|
148
|
-
return resolvedTargets?.has(targetName) === true;
|
|
154
|
+
return resolvedProjectTargetNames(resolvedTargets)?.has(targetName) === true;
|
|
149
155
|
}
|
|
150
156
|
function isBuildOutputDependencyPattern(dependency) {
|
|
151
157
|
return BUILD_OUTPUT_DEPENDENCIES.includes(dependency);
|
|
@@ -218,6 +224,13 @@ export function nxRunAlias(projectName, targetName, continuous, command) {
|
|
|
218
224
|
const flags = continuous ? ` --outputStyle=${nxRunOutputStyle(command)}` : '';
|
|
219
225
|
return `nx run ${projectName}:${targetName}${flags}`;
|
|
220
226
|
}
|
|
227
|
+
function nxRunAliasMatches(rawAlias, expectedAlias) {
|
|
228
|
+
const expected = parseNxRunAlias(expectedAlias);
|
|
229
|
+
return (!!rawAlias &&
|
|
230
|
+
!!expected &&
|
|
231
|
+
rawAlias.projectName === expected.projectName &&
|
|
232
|
+
rawAlias.targetName === expected.targetName);
|
|
233
|
+
}
|
|
221
234
|
function nxRunOutputStyle(command) {
|
|
222
235
|
if (command && /(?:^|\s)(?:astro|vite)\s+dev(?:\s|$)/.test(command)) {
|
|
223
236
|
return 'dynamic-legacy';
|
|
@@ -523,6 +536,13 @@ function validatePackageScriptPolicy(pkg, packagePath, workspaceNames, options =
|
|
|
523
536
|
continue;
|
|
524
537
|
}
|
|
525
538
|
const alias = parseNxRunAlias(rawCommand);
|
|
539
|
+
if (isResolvedSelfRecursiveScript(scriptName, alias, projectName, options.resolvedTargets)) {
|
|
540
|
+
issues.push({
|
|
541
|
+
path: packagePath,
|
|
542
|
+
message: `${packagePath}: scripts.${scriptName} recursively delegates to its own inferred Nx target; remove the script so the resolved Nx target can run`,
|
|
543
|
+
});
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
526
546
|
const rewrite = alias
|
|
527
547
|
? { targetName: alias.targetName, continuous: isContinuousTarget(alias.targetName, '') }
|
|
528
548
|
: classifyScriptRewrite(scriptName, rawCommand);
|
|
@@ -547,7 +567,7 @@ function validatePackageScriptPolicy(pkg, packagePath, workspaceNames, options =
|
|
|
547
567
|
const command = targetOptions ? stringProperty(targetOptions, 'command') : null;
|
|
548
568
|
const rewriteCommand = 'command' in rewrite ? rewrite.command : null;
|
|
549
569
|
const expectedAlias = nxRunAlias(projectName, rewrite.targetName, rewrite.continuous, command ?? rewriteCommand);
|
|
550
|
-
if (rawCommand !== expectedAlias) {
|
|
570
|
+
if (alias ? !nxRunAliasMatches(alias, expectedAlias) : rawCommand !== expectedAlias) {
|
|
551
571
|
issues.push({
|
|
552
572
|
path: packagePath,
|
|
553
573
|
message: `${packagePath}: scripts.${scriptName} must delegate to ${expectedAlias}`,
|
|
@@ -829,6 +849,11 @@ function applyPackageScriptPolicyForPkg(pkg, _packagePath, workspaceNames, optio
|
|
|
829
849
|
continue;
|
|
830
850
|
}
|
|
831
851
|
const parsedAlias = parseNxRunAlias(rawCommand);
|
|
852
|
+
if (isResolvedSelfRecursiveScript(scriptName, parsedAlias, projectName, options.resolvedTargets)) {
|
|
853
|
+
delete scripts[scriptName];
|
|
854
|
+
changed = true;
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
832
857
|
if (parsedAlias?.projectName === projectName && parsedAlias.targetName === 'test') {
|
|
833
858
|
continue;
|
|
834
859
|
}
|
|
@@ -881,6 +906,12 @@ function applyPackageScriptPolicyForPkg(pkg, _packagePath, workspaceNames, optio
|
|
|
881
906
|
}
|
|
882
907
|
return changed;
|
|
883
908
|
}
|
|
909
|
+
function isResolvedSelfRecursiveScript(scriptName, alias, projectName, resolvedTargets) {
|
|
910
|
+
return (alias?.projectName === projectName &&
|
|
911
|
+
alias.targetName === scriptName &&
|
|
912
|
+
resolvedProjectTargetExecutor(resolvedTargets, scriptName) === 'nx:run-script' &&
|
|
913
|
+
resolvedProjectTargetScript(resolvedTargets, scriptName) === scriptName);
|
|
914
|
+
}
|
|
884
915
|
export function checkPackageTargets(pkg, packagePath, resolvedTargets) {
|
|
885
916
|
return [...validateExplicitNxTargets(pkg, packagePath, resolvedTargets)];
|
|
886
917
|
}
|
|
@@ -906,7 +937,7 @@ export function checkPackageTargetPolicy(root, options = {}) {
|
|
|
906
937
|
issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedTargets));
|
|
907
938
|
issues.push(...validateTestEntrypointPresence(root, packagePath, pkg));
|
|
908
939
|
issues.push(...validateBuildZigPolicy(root, packagePath));
|
|
909
|
-
issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets }));
|
|
940
|
+
issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }));
|
|
910
941
|
}
|
|
911
942
|
return issues;
|
|
912
943
|
}
|
|
@@ -927,7 +958,8 @@ export function applyPackageTargetPolicy(root, options = {}) {
|
|
|
927
958
|
packageChanged = removePackageColonTargets(pkg) || packageChanged;
|
|
928
959
|
packageChanged = removeRedundantNoopBuildTarget(pkg, resolvedProject) || packageChanged;
|
|
929
960
|
packageChanged =
|
|
930
|
-
applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets }) ||
|
|
961
|
+
applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }) ||
|
|
962
|
+
packageChanged;
|
|
931
963
|
if (packageChanged) {
|
|
932
964
|
writeJsonObject(packageJsonPath, pkg);
|
|
933
965
|
changed = true;
|
package/package.json
CHANGED
package/src/index.test.ts
CHANGED
|
@@ -22,6 +22,27 @@ const buildOutputDependencies = [
|
|
|
22
22
|
];
|
|
23
23
|
|
|
24
24
|
describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
25
|
+
it('names standalone package projects from package metadata', async () => {
|
|
26
|
+
const workspace = await createWorkspace();
|
|
27
|
+
try {
|
|
28
|
+
await workspace.write(
|
|
29
|
+
'specs/prototype/package.json',
|
|
30
|
+
'{"name":"standalone-package","nx":{"name":"standalone-project"}}\n',
|
|
31
|
+
);
|
|
32
|
+
await workspace.write('specs/package-fallback/package.json', '{"name":"package-fallback"}\n');
|
|
33
|
+
|
|
34
|
+
const explicit = await inferProject(workspace, 'specs/prototype/package.json');
|
|
35
|
+
const fallback = await inferProject(workspace, 'specs/package-fallback/package.json');
|
|
36
|
+
|
|
37
|
+
expect(explicit?.name).toBe('standalone-project');
|
|
38
|
+
expect(explicit?.targets).toEqual({});
|
|
39
|
+
expect(fallback?.name).toBe('package-fallback');
|
|
40
|
+
expect(fallback?.targets).toEqual({});
|
|
41
|
+
} finally {
|
|
42
|
+
await workspace.cleanup();
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
25
46
|
it('infers validation and aggregate build targets without owning TypeScript lib build', async () => {
|
|
26
47
|
const workspace = await createWorkspace();
|
|
27
48
|
try {
|
|
@@ -152,6 +173,123 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
|
|
|
152
173
|
}
|
|
153
174
|
});
|
|
154
175
|
|
|
176
|
+
it('infers cargo workspace targets including explicitly named wasm crate builds', async () => {
|
|
177
|
+
const workspace = await createWorkspace();
|
|
178
|
+
try {
|
|
179
|
+
await workspace.write('packages/ferris/package.json', '{"name":"ferris"}\n');
|
|
180
|
+
await workspace.write(
|
|
181
|
+
'packages/ferris/Cargo.toml',
|
|
182
|
+
'[workspace]\nmembers = ["crates/ferris-core", "crates/ferris-wasm"]\n\n[profile.wasm-release]\ninherits = "release"\n',
|
|
183
|
+
);
|
|
184
|
+
await workspace.write('packages/ferris/crates/ferris-core/Cargo.toml', '[package]\nname = "ferris-core"\n');
|
|
185
|
+
await workspace.write(
|
|
186
|
+
'packages/ferris/crates/ferris-wasm/Cargo.toml',
|
|
187
|
+
'[package]\nname = "ferris-wasm"\n\n[lib]\ncrate-type = ["cdylib", "rlib"]\n',
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const targets = await inferProjectTargets(workspace, 'packages/ferris/package.json');
|
|
191
|
+
|
|
192
|
+
expect(Object.keys(targets).sort()).toEqual([
|
|
193
|
+
'bench',
|
|
194
|
+
'build',
|
|
195
|
+
'cargo-lint',
|
|
196
|
+
'cargo-test',
|
|
197
|
+
'cargo-wasm',
|
|
198
|
+
'clean',
|
|
199
|
+
'lint',
|
|
200
|
+
'mutation',
|
|
201
|
+
'test',
|
|
202
|
+
]);
|
|
203
|
+
expect(targets['cargo-test']?.executor).toBe('@smoothbricks/nx-plugin:bounded-exec');
|
|
204
|
+
expect(targets['cargo-test']?.options).toMatchObject({
|
|
205
|
+
command: 'cargo test --workspace',
|
|
206
|
+
cwd: 'packages/ferris',
|
|
207
|
+
});
|
|
208
|
+
expect(targets['cargo-test']?.inputs).toEqual([
|
|
209
|
+
'{projectRoot}/**/*.rs',
|
|
210
|
+
'{projectRoot}/**/Cargo.toml',
|
|
211
|
+
'{projectRoot}/Cargo.lock',
|
|
212
|
+
'{projectRoot}/.cargo/config.toml',
|
|
213
|
+
'!{projectRoot}/target/**',
|
|
214
|
+
]);
|
|
215
|
+
expect(targets['cargo-lint']?.options).toMatchObject({
|
|
216
|
+
commands: ['cargo fmt --all --check', 'cargo clippy --workspace --all-targets -- -D warnings'],
|
|
217
|
+
});
|
|
218
|
+
expect(targets.lint?.dependsOn).toEqual(['cargo-lint']);
|
|
219
|
+
expect(targets.test?.executor).toBe('@smoothbricks/nx-plugin:bounded-exec');
|
|
220
|
+
expect(targets.test?.options).toMatchObject({
|
|
221
|
+
command: 'cargo test --workspace',
|
|
222
|
+
cwd: 'packages/ferris',
|
|
223
|
+
});
|
|
224
|
+
expect(targets.mutation?.cache).toBe(false);
|
|
225
|
+
expect(targets.mutation?.options).toMatchObject({ command: 'cargo mutants --workspace' });
|
|
226
|
+
expect(targets['cargo-wasm']?.outputs).toEqual(['{projectRoot}/dist/**/*.wasm']);
|
|
227
|
+
expect(targets['cargo-wasm']?.options).toMatchObject({
|
|
228
|
+
commands: [
|
|
229
|
+
'cargo build --profile wasm-release --target wasm32-unknown-unknown -p ferris-wasm && mkdir -p dist && cp target/wasm32-unknown-unknown/wasm-release/ferris_wasm.wasm dist/ferris_wasm.wasm',
|
|
230
|
+
],
|
|
231
|
+
});
|
|
232
|
+
expect(targets['cargo-wasm']?.configurations?.development).toEqual({
|
|
233
|
+
commands: [
|
|
234
|
+
'cargo build --target wasm32-unknown-unknown -p ferris-wasm && mkdir -p dist && cp target/wasm32-unknown-unknown/debug/ferris_wasm.wasm dist/ferris_wasm.wasm',
|
|
235
|
+
],
|
|
236
|
+
});
|
|
237
|
+
// cargo-wasm matches the *-wasm build-output pattern → aggregate build/clean exist
|
|
238
|
+
expect(targets.build?.executor).toBe('nx:noop');
|
|
239
|
+
} finally {
|
|
240
|
+
await workspace.cleanup();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('does not infer wasm targets for native N-API cdylib crates', async () => {
|
|
245
|
+
const workspace = await createWorkspace();
|
|
246
|
+
try {
|
|
247
|
+
await workspace.write('packages/cowshed/package.json', '{"name":"cowshed"}\n');
|
|
248
|
+
await workspace.write('packages/cowshed/Cargo.toml', '[workspace]\nmembers = ["crates/cowshed-napi"]\n');
|
|
249
|
+
await workspace.write(
|
|
250
|
+
'packages/cowshed/crates/cowshed-napi/Cargo.toml',
|
|
251
|
+
'[package]\nname = "cowshed-napi"\n\n[lib]\ncrate-type = ["cdylib"]\n',
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
const targets = await inferProjectTargets(workspace, 'packages/cowshed/package.json');
|
|
255
|
+
|
|
256
|
+
expect(targets['cargo-wasm']).toBeUndefined();
|
|
257
|
+
expect(targets.build).toBeUndefined();
|
|
258
|
+
expect(targets.clean).toBeUndefined();
|
|
259
|
+
expect(targets['cargo-test']).toBeDefined();
|
|
260
|
+
} finally {
|
|
261
|
+
await workspace.cleanup();
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('lets explicit nx.targets override wasm inference and skips non-workspace Cargo.toml', async () => {
|
|
266
|
+
const workspace = await createWorkspace();
|
|
267
|
+
try {
|
|
268
|
+
await workspace.write(
|
|
269
|
+
'packages/custom/package.json',
|
|
270
|
+
'{"name":"custom","nx":{"targets":{"cargo-wasm":{"options":{"command":"custom"}},"test":{}}}}\n',
|
|
271
|
+
);
|
|
272
|
+
await workspace.write('packages/custom/Cargo.toml', '[workspace]\nmembers = ["crates/x-wasm"]\n');
|
|
273
|
+
await workspace.write(
|
|
274
|
+
'packages/custom/crates/x-wasm/Cargo.toml',
|
|
275
|
+
'[package]\nname = "x-wasm"\n\n[lib]\ncrate-type = ["cdylib"]\n',
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const targets = await inferProjectTargets(workspace, 'packages/custom/package.json');
|
|
279
|
+
expect(targets['cargo-wasm']).toBeUndefined();
|
|
280
|
+
expect(targets.test).toBeUndefined();
|
|
281
|
+
expect(targets['cargo-test']).toBeDefined();
|
|
282
|
+
|
|
283
|
+
// A member crate's own Cargo.toml (no [workspace]) infers nothing.
|
|
284
|
+
await workspace.write('packages/member/package.json', '{"name":"member"}\n');
|
|
285
|
+
await workspace.write('packages/member/Cargo.toml', '[package]\nname = "member"\n');
|
|
286
|
+
const memberTargets = await inferProjectTargets(workspace, 'packages/member/package.json');
|
|
287
|
+
expect(memberTargets).toEqual({});
|
|
288
|
+
} finally {
|
|
289
|
+
await workspace.cleanup();
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
|
|
155
293
|
it('leaves build.zig without b.step declarations to smoo validation', async () => {
|
|
156
294
|
const workspace = await createWorkspace();
|
|
157
295
|
try {
|
|
@@ -192,12 +330,14 @@ interface WorkspaceFixture {
|
|
|
192
330
|
cleanup(): Promise<void>;
|
|
193
331
|
}
|
|
194
332
|
|
|
333
|
+
async function inferProject(workspace: WorkspaceFixture, packageJsonPath: string) {
|
|
334
|
+
const result = await inferTargets([packageJsonPath], undefined, workspace.context);
|
|
335
|
+
return result[0]?.[1].projects?.[dirname(packageJsonPath)];
|
|
336
|
+
}
|
|
337
|
+
|
|
195
338
|
async function inferProjectTargets(
|
|
196
339
|
workspace: WorkspaceFixture,
|
|
197
340
|
packageJsonPath: string,
|
|
198
341
|
): Promise<Record<string, TargetConfiguration>> {
|
|
199
|
-
|
|
200
|
-
const projectRoot = dirname(packageJsonPath);
|
|
201
|
-
|
|
202
|
-
return result[0]?.[1].projects?.[projectRoot]?.targets ?? {};
|
|
342
|
+
return (await inferProject(workspace, packageJsonPath))?.targets ?? {};
|
|
203
343
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,44 @@ const ZIG_STEP_PATTERN = /\bb\.step\(\s*["']([^"']+)["']\s*,/g;
|
|
|
12
12
|
const VALID_ZIG_STEP_NAME = /^[A-Za-z0-9_-]+$/;
|
|
13
13
|
const BUILD_OUTPUT_TARGET_PATTERN = /-(?:js|web|html|css|ios|android|native|napi|bun|wasm)$/;
|
|
14
14
|
|
|
15
|
+
// Cargo workspace inference: a package.json sitting next to a Cargo.toml that
|
|
16
|
+
// declares [workspace] gets direct cargo-test/test targets, cargo-lint feeding
|
|
17
|
+
// the lint aggregate, mutation (cargo-mutants), bench, and — per cdylib member
|
|
18
|
+
// crate whose package name ends in `-wasm` — a cargo-wasm build producing
|
|
19
|
+
// dist/<crate>.wasm. Explicit nx.targets entries in the package.json always win
|
|
20
|
+
// over inference.
|
|
21
|
+
const CARGO_WORKSPACE_PATTERN = /^\s*\[workspace\]/m;
|
|
22
|
+
const CARGO_MEMBERS_PATTERN = /^\s*members\s*=\s*\[([^\]]*)\]/m;
|
|
23
|
+
const CARGO_MEMBER_ENTRY_PATTERN = /["']([^"']+)["']/g;
|
|
24
|
+
const CARGO_PACKAGE_NAME_PATTERN = /^\s*name\s*=\s*["']([^"']+)["']/m;
|
|
25
|
+
const CARGO_WASM_CRATE_NAME_PATTERN = /-wasm$/;
|
|
26
|
+
const CARGO_CDYLIB_CRATE_TYPE_PATTERN = /^\s*crate-type\s*=\s*\[[^\]]*["']cdylib["']/m;
|
|
27
|
+
const CARGO_WASM_RELEASE_PROFILE_PATTERN = /^\s*\[profile\.wasm-release\]/m;
|
|
28
|
+
const CARGO_INPUTS = [
|
|
29
|
+
'{projectRoot}/**/*.rs',
|
|
30
|
+
'{projectRoot}/**/Cargo.toml',
|
|
31
|
+
'{projectRoot}/Cargo.lock',
|
|
32
|
+
'{projectRoot}/.cargo/config.toml',
|
|
33
|
+
'!{projectRoot}/target/**',
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
function createCargoTestTarget(projectRoot: string): TargetConfiguration {
|
|
37
|
+
return {
|
|
38
|
+
executor: '@smoothbricks/nx-plugin:bounded-exec',
|
|
39
|
+
cache: true,
|
|
40
|
+
inputs: CARGO_INPUTS,
|
|
41
|
+
options: {
|
|
42
|
+
command: 'cargo test --workspace',
|
|
43
|
+
cwd: projectRoot,
|
|
44
|
+
timeoutMs: 600000,
|
|
45
|
+
killAfterMs: 10000,
|
|
46
|
+
},
|
|
47
|
+
configurations: {
|
|
48
|
+
production: { command: 'cargo test --workspace --release' },
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
15
53
|
export const createNodesV2: CreateNodesV2 = [
|
|
16
54
|
'**/package.json',
|
|
17
55
|
async (projectConfigurationFiles, _options, context) => {
|
|
@@ -39,8 +77,10 @@ export const createNodesV2: CreateNodesV2 = [
|
|
|
39
77
|
export default { createNodesV2 };
|
|
40
78
|
|
|
41
79
|
interface PackageJson {
|
|
80
|
+
name?: string;
|
|
42
81
|
scripts?: Record<string, unknown>;
|
|
43
82
|
nx?: {
|
|
83
|
+
name?: string;
|
|
44
84
|
targets?: Record<string, unknown>;
|
|
45
85
|
};
|
|
46
86
|
}
|
|
@@ -89,6 +129,73 @@ async function createProjectTargets(packageJsonPath: string, workspaceRoot: stri
|
|
|
89
129
|
validationTargets.push('typecheck');
|
|
90
130
|
}
|
|
91
131
|
|
|
132
|
+
const cargo = await readCargoWorkspace(absoluteProjectRoot);
|
|
133
|
+
if (cargo) {
|
|
134
|
+
const declared = packageJson.nx?.targets ?? {};
|
|
135
|
+
if (!('cargo-test' in declared)) {
|
|
136
|
+
targets['cargo-test'] = createCargoTestTarget(projectRoot);
|
|
137
|
+
}
|
|
138
|
+
if (!('cargo-lint' in declared)) {
|
|
139
|
+
targets['cargo-lint'] = {
|
|
140
|
+
executor: 'nx:run-commands',
|
|
141
|
+
cache: true,
|
|
142
|
+
inputs: CARGO_INPUTS,
|
|
143
|
+
options: {
|
|
144
|
+
commands: ['cargo fmt --all --check', 'cargo clippy --workspace --all-targets -- -D warnings'],
|
|
145
|
+
cwd: projectRoot,
|
|
146
|
+
parallel: false,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
validationTargets.push('cargo-lint');
|
|
151
|
+
if (!('test' in declared) && typeof packageJson.scripts?.test !== 'string') {
|
|
152
|
+
// Execute Cargo directly: workspace targetDefaults may replace test.dependsOn.
|
|
153
|
+
targets.test = createCargoTestTarget(projectRoot);
|
|
154
|
+
}
|
|
155
|
+
if (!('mutation' in declared)) {
|
|
156
|
+
// Mutation runs are minutes-to-hours: never cached, never part of build/lint.
|
|
157
|
+
// CI runs these per-PR via `cargo mutants --in-diff` (see mutants.toml docs).
|
|
158
|
+
targets.mutation = {
|
|
159
|
+
executor: 'nx:run-commands',
|
|
160
|
+
cache: false,
|
|
161
|
+
options: { command: 'cargo mutants --workspace', cwd: projectRoot },
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (!('bench' in declared)) {
|
|
165
|
+
targets.bench = {
|
|
166
|
+
executor: 'nx:run-commands',
|
|
167
|
+
cache: false,
|
|
168
|
+
options: { command: 'cargo bench --workspace', cwd: projectRoot },
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (cargo.wasmCrates.length > 0 && !('cargo-wasm' in declared)) {
|
|
172
|
+
const profile = cargo.hasWasmReleaseProfile ? 'wasm-release' : 'release';
|
|
173
|
+
const buildAndCopy = (crate: string, profileName: string | null) => {
|
|
174
|
+
const artifact = crate.replace(/-/g, '_');
|
|
175
|
+
const profileFlag = profileName === null ? '' : ` --profile ${profileName}`;
|
|
176
|
+
const outputDirectory = profileName ?? 'debug';
|
|
177
|
+
return `cargo build${profileFlag} --target wasm32-unknown-unknown -p ${crate} && mkdir -p dist && cp target/wasm32-unknown-unknown/${outputDirectory}/${artifact}.wasm dist/${artifact}.wasm`;
|
|
178
|
+
};
|
|
179
|
+
targets['cargo-wasm'] = {
|
|
180
|
+
executor: 'nx:run-commands',
|
|
181
|
+
cache: true,
|
|
182
|
+
inputs: CARGO_INPUTS,
|
|
183
|
+
outputs: ['{projectRoot}/dist/**/*.wasm'],
|
|
184
|
+
options: {
|
|
185
|
+
commands: cargo.wasmCrates.map((crate) => buildAndCopy(crate, profile)),
|
|
186
|
+
cwd: projectRoot,
|
|
187
|
+
parallel: false,
|
|
188
|
+
},
|
|
189
|
+
configurations: {
|
|
190
|
+
development: {
|
|
191
|
+
commands: cargo.wasmCrates.map((crate) => buildAndCopy(crate, null)),
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
hasBuildOutputTarget = true;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
92
199
|
const zigSteps = await readZigSteps(absoluteProjectRoot, projectRoot);
|
|
93
200
|
for (const step of zigSteps) {
|
|
94
201
|
const targetName = `zig-${step}`;
|
|
@@ -140,9 +247,14 @@ async function createProjectTargets(packageJsonPath: string, workspaceRoot: stri
|
|
|
140
247
|
};
|
|
141
248
|
}
|
|
142
249
|
|
|
250
|
+
const projectName = packageJson.nx?.name ?? packageJson.name;
|
|
251
|
+
if (typeof projectName !== 'string' || projectName.length === 0) {
|
|
252
|
+
throw new Error(`${packageJsonPath} must declare a non-empty package or nx project name`);
|
|
253
|
+
}
|
|
254
|
+
|
|
143
255
|
return {
|
|
144
256
|
projects: {
|
|
145
|
-
[projectRoot]: { targets },
|
|
257
|
+
[projectRoot]: { name: projectName, targets },
|
|
146
258
|
},
|
|
147
259
|
};
|
|
148
260
|
}
|
|
@@ -211,6 +323,46 @@ function hasPackageLocalBuildOutputTarget(packageJson: PackageJson): boolean {
|
|
|
211
323
|
return Object.keys(targets).some((targetName) => BUILD_OUTPUT_TARGET_PATTERN.test(targetName));
|
|
212
324
|
}
|
|
213
325
|
|
|
326
|
+
interface CargoWorkspace {
|
|
327
|
+
wasmCrates: string[];
|
|
328
|
+
hasWasmReleaseProfile: boolean;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async function readCargoWorkspace(absoluteProjectRoot: string): Promise<CargoWorkspace | null> {
|
|
332
|
+
const cargoTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
|
|
333
|
+
if (!existsSync(cargoTomlPath)) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const cargoToml = await readFile(cargoTomlPath, 'utf-8');
|
|
338
|
+
if (!CARGO_WORKSPACE_PATTERN.test(cargoToml)) {
|
|
339
|
+
// Member crates get their targets from the workspace-root package.json,
|
|
340
|
+
// never per-crate — one Nx project per Cargo workspace.
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const wasmCrates: string[] = [];
|
|
345
|
+
const membersList = CARGO_MEMBERS_PATTERN.exec(cargoToml)?.[1] ?? '';
|
|
346
|
+
for (const match of membersList.matchAll(CARGO_MEMBER_ENTRY_PATTERN)) {
|
|
347
|
+
const memberTomlPath = join(absoluteProjectRoot, match[1], 'Cargo.toml');
|
|
348
|
+
if (!existsSync(memberTomlPath)) {
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const memberToml = await readFile(memberTomlPath, 'utf-8');
|
|
352
|
+
const name = CARGO_PACKAGE_NAME_PATTERN.exec(memberToml)?.[1];
|
|
353
|
+
if (
|
|
354
|
+
name &&
|
|
355
|
+
CARGO_WASM_CRATE_NAME_PATTERN.test(name) &&
|
|
356
|
+
CARGO_CDYLIB_CRATE_TYPE_PATTERN.test(memberToml) &&
|
|
357
|
+
!wasmCrates.includes(name)
|
|
358
|
+
) {
|
|
359
|
+
wasmCrates.push(name);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return { wasmCrates, hasWasmReleaseProfile: CARGO_WASM_RELEASE_PROFILE_PATTERN.test(cargoToml) };
|
|
364
|
+
}
|
|
365
|
+
|
|
214
366
|
async function readZigSteps(absoluteProjectRoot: string, projectRoot: string): Promise<string[]> {
|
|
215
367
|
const buildZigPath = join(absoluteProjectRoot, 'build.zig');
|
|
216
368
|
|
|
@@ -262,6 +262,141 @@ describe('package target policy', () => {
|
|
|
262
262
|
}
|
|
263
263
|
});
|
|
264
264
|
|
|
265
|
+
it('removes self-recursive scripts using resolved Nx target metadata', async () => {
|
|
266
|
+
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-pkg-target-'));
|
|
267
|
+
try {
|
|
268
|
+
await writeJson(join(root, 'package.json'), {
|
|
269
|
+
name: '@scope/root',
|
|
270
|
+
private: true,
|
|
271
|
+
workspaces: ['packages/*'],
|
|
272
|
+
});
|
|
273
|
+
await writeJson(join(root, 'packages/app/package.json'), {
|
|
274
|
+
name: '@scope/app',
|
|
275
|
+
dependencies: { '@scope/lib': 'workspace:*' },
|
|
276
|
+
scripts: {
|
|
277
|
+
build: 'nx run app:build',
|
|
278
|
+
test: 'bun test',
|
|
279
|
+
},
|
|
280
|
+
nx: {
|
|
281
|
+
name: 'app',
|
|
282
|
+
targets: {
|
|
283
|
+
'tsdown-js': {
|
|
284
|
+
executor: 'nx:run-commands',
|
|
285
|
+
options: { command: 'tsdown', cwd: '{projectRoot}' },
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
await writeJson(join(root, 'packages/lib/package.json'), {
|
|
291
|
+
name: '@scope/lib',
|
|
292
|
+
});
|
|
293
|
+
const resolvedTargetsByProject = new Map<string, ResolvedProjectTargets>([
|
|
294
|
+
[
|
|
295
|
+
'app',
|
|
296
|
+
{
|
|
297
|
+
targets: new Set(['build', 'test', 'tsdown-js']),
|
|
298
|
+
targetExecutors: new Map([
|
|
299
|
+
['build', 'nx:run-script'],
|
|
300
|
+
['test', 'nx:run-script'],
|
|
301
|
+
['tsdown-js', 'nx:run-commands'],
|
|
302
|
+
]),
|
|
303
|
+
targetScripts: new Map([
|
|
304
|
+
['build', 'build'],
|
|
305
|
+
['test', 'test'],
|
|
306
|
+
]),
|
|
307
|
+
},
|
|
308
|
+
],
|
|
309
|
+
]);
|
|
310
|
+
|
|
311
|
+
const issues = checkPackageTargetPolicy(root, { resolvedTargetsByProject });
|
|
312
|
+
expect(issues.some((i) => i.message.includes('scripts.build recursively delegates'))).toBe(true);
|
|
313
|
+
expect(applyPackageTargetPolicy(root, { resolvedTargetsByProject })).toBe(true);
|
|
314
|
+
|
|
315
|
+
const app = JSON.parse(await readFile(join(root, 'packages/app/package.json'), 'utf8'));
|
|
316
|
+
expect(app.scripts.build).toBeUndefined();
|
|
317
|
+
expect(app.scripts.test).toBe('bun test');
|
|
318
|
+
} finally {
|
|
319
|
+
await rm(root, { recursive: true, force: true });
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('keeps self-alias scripts when resolved target comes from project configuration', async () => {
|
|
324
|
+
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-pkg-target-'));
|
|
325
|
+
try {
|
|
326
|
+
await writeJson(join(root, 'package.json'), {
|
|
327
|
+
name: '@scope/root',
|
|
328
|
+
private: true,
|
|
329
|
+
workspaces: ['packages/*'],
|
|
330
|
+
});
|
|
331
|
+
await writeJson(join(root, 'packages/app/package.json'), {
|
|
332
|
+
name: '@scope/app',
|
|
333
|
+
dependencies: { '@scope/lib': 'workspace:*' },
|
|
334
|
+
scripts: {
|
|
335
|
+
build: 'nx run app:build',
|
|
336
|
+
},
|
|
337
|
+
nx: { name: 'app' },
|
|
338
|
+
});
|
|
339
|
+
await writeJson(join(root, 'packages/lib/package.json'), {
|
|
340
|
+
name: '@scope/lib',
|
|
341
|
+
});
|
|
342
|
+
const resolvedTargetsByProject = new Map<string, ResolvedProjectTargets>([
|
|
343
|
+
[
|
|
344
|
+
'app',
|
|
345
|
+
{
|
|
346
|
+
targets: new Set(['build']),
|
|
347
|
+
targetExecutors: new Map([['build', 'nx:run-commands']]),
|
|
348
|
+
},
|
|
349
|
+
],
|
|
350
|
+
]);
|
|
351
|
+
|
|
352
|
+
expect(applyPackageTargetPolicy(root, { resolvedTargetsByProject })).toBe(false);
|
|
353
|
+
|
|
354
|
+
const app = JSON.parse(await readFile(join(root, 'packages/app/package.json'), 'utf8'));
|
|
355
|
+
expect(app.scripts.build).toBe('nx run app:build');
|
|
356
|
+
} finally {
|
|
357
|
+
await rm(root, { recursive: true, force: true });
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it('accepts stream output style on non-continuous nx aliases', async () => {
|
|
362
|
+
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-pkg-target-'));
|
|
363
|
+
try {
|
|
364
|
+
await writeJson(join(root, 'package.json'), {
|
|
365
|
+
name: '@scope/root',
|
|
366
|
+
private: true,
|
|
367
|
+
workspaces: ['packages/*'],
|
|
368
|
+
});
|
|
369
|
+
await writeJson(join(root, 'packages/app/package.json'), {
|
|
370
|
+
name: '@scope/app',
|
|
371
|
+
dependencies: { '@scope/lib': 'workspace:*' },
|
|
372
|
+
scripts: {
|
|
373
|
+
deploy: 'nx run app:deploy:production --outputStyle=stream',
|
|
374
|
+
},
|
|
375
|
+
nx: {
|
|
376
|
+
name: 'app',
|
|
377
|
+
targets: {
|
|
378
|
+
deploy: {
|
|
379
|
+
executor: 'nx:run-commands',
|
|
380
|
+
options: { command: 'wrangler deploy --env production', cwd: '{projectRoot}' },
|
|
381
|
+
configurations: {
|
|
382
|
+
production: { command: 'wrangler deploy --env production' },
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
await writeJson(join(root, 'packages/lib/package.json'), {
|
|
389
|
+
name: '@scope/lib',
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
const issues = checkPackageTargetPolicy(root);
|
|
393
|
+
expect(issues.some((i) => i.message.includes('scripts.deploy must delegate'))).toBe(false);
|
|
394
|
+
expect(applyPackageTargetPolicy(root)).toBe(false);
|
|
395
|
+
} finally {
|
|
396
|
+
await rm(root, { recursive: true, force: true });
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
|
|
265
400
|
it('moves env assignments into target options', async () => {
|
|
266
401
|
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-pkg-target-'));
|
|
267
402
|
try {
|
|
@@ -11,6 +11,8 @@ export type { NxPolicyIssue };
|
|
|
11
11
|
export interface ResolvedProjectTargets {
|
|
12
12
|
targets: ReadonlySet<string>;
|
|
13
13
|
buildDependsOn?: readonly string[];
|
|
14
|
+
targetExecutors?: ReadonlyMap<string, string>;
|
|
15
|
+
targetScripts?: ReadonlyMap<string, string>;
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
export interface PackageTargetPolicyOptions {
|
|
@@ -200,14 +202,31 @@ function resolvedProjectBuildDependsOn(
|
|
|
200
202
|
return resolvedProject.buildDependsOn;
|
|
201
203
|
}
|
|
202
204
|
|
|
205
|
+
function resolvedProjectTargetExecutor(
|
|
206
|
+
resolvedProject: ReadonlySet<string> | ResolvedProjectTargets | undefined,
|
|
207
|
+
targetName: string,
|
|
208
|
+
): string | undefined {
|
|
209
|
+
return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targetExecutors?.get(targetName) : undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function resolvedProjectTargetScript(
|
|
213
|
+
resolvedProject: ReadonlySet<string> | ResolvedProjectTargets | undefined,
|
|
214
|
+
targetName: string,
|
|
215
|
+
): string | undefined {
|
|
216
|
+
return isResolvedProjectTargets(resolvedProject) ? resolvedProject.targetScripts?.get(targetName) : undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
203
219
|
function isResolvedProjectTargets(
|
|
204
220
|
value: ReadonlySet<string> | ResolvedProjectTargets | undefined,
|
|
205
221
|
): value is ResolvedProjectTargets {
|
|
206
222
|
return isRecord(value) && value.targets instanceof Set;
|
|
207
223
|
}
|
|
208
224
|
|
|
209
|
-
function targetExistsInResolvedProject(
|
|
210
|
-
|
|
225
|
+
function targetExistsInResolvedProject(
|
|
226
|
+
targetName: string,
|
|
227
|
+
resolvedTargets?: ReadonlySet<string> | ResolvedProjectTargets,
|
|
228
|
+
): boolean {
|
|
229
|
+
return resolvedProjectTargetNames(resolvedTargets)?.has(targetName) === true;
|
|
211
230
|
}
|
|
212
231
|
|
|
213
232
|
function isBuildOutputDependencyPattern(dependency: string): boolean {
|
|
@@ -304,6 +323,19 @@ export function nxRunAlias(
|
|
|
304
323
|
return `nx run ${projectName}:${targetName}${flags}`;
|
|
305
324
|
}
|
|
306
325
|
|
|
326
|
+
function nxRunAliasMatches(
|
|
327
|
+
rawAlias: { projectName: string; targetName: string } | null,
|
|
328
|
+
expectedAlias: string,
|
|
329
|
+
): boolean {
|
|
330
|
+
const expected = parseNxRunAlias(expectedAlias);
|
|
331
|
+
return (
|
|
332
|
+
!!rawAlias &&
|
|
333
|
+
!!expected &&
|
|
334
|
+
rawAlias.projectName === expected.projectName &&
|
|
335
|
+
rawAlias.targetName === expected.targetName
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
307
339
|
function nxRunOutputStyle(command?: string | null): 'stream' | 'dynamic-legacy' {
|
|
308
340
|
if (command && /(?:^|\s)(?:astro|vite)\s+dev(?:\s|$)/.test(command)) {
|
|
309
341
|
return 'dynamic-legacy';
|
|
@@ -652,7 +684,7 @@ function validatePackageScriptPolicy(
|
|
|
652
684
|
pkg: Record<string, unknown>,
|
|
653
685
|
packagePath: string,
|
|
654
686
|
workspaceNames: ReadonlySet<string>,
|
|
655
|
-
options: { resolvedTargets?: ReadonlySet<string> } = {},
|
|
687
|
+
options: { resolvedTargets?: ReadonlySet<string> | ResolvedProjectTargets } = {},
|
|
656
688
|
): NxPolicyIssue[] {
|
|
657
689
|
if (!hasWorkspaceDependency(pkg, workspaceNames)) {
|
|
658
690
|
return [];
|
|
@@ -670,6 +702,13 @@ function validatePackageScriptPolicy(
|
|
|
670
702
|
continue;
|
|
671
703
|
}
|
|
672
704
|
const alias = parseNxRunAlias(rawCommand);
|
|
705
|
+
if (isResolvedSelfRecursiveScript(scriptName, alias, projectName, options.resolvedTargets)) {
|
|
706
|
+
issues.push({
|
|
707
|
+
path: packagePath,
|
|
708
|
+
message: `${packagePath}: scripts.${scriptName} recursively delegates to its own inferred Nx target; remove the script so the resolved Nx target can run`,
|
|
709
|
+
});
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
673
712
|
const rewrite = alias
|
|
674
713
|
? { targetName: alias.targetName, continuous: isContinuousTarget(alias.targetName, '') }
|
|
675
714
|
: classifyScriptRewrite(scriptName, rawCommand);
|
|
@@ -694,7 +733,7 @@ function validatePackageScriptPolicy(
|
|
|
694
733
|
const command = targetOptions ? stringProperty(targetOptions, 'command') : null;
|
|
695
734
|
const rewriteCommand = 'command' in rewrite ? rewrite.command : null;
|
|
696
735
|
const expectedAlias = nxRunAlias(projectName, rewrite.targetName, rewrite.continuous, command ?? rewriteCommand);
|
|
697
|
-
if (rawCommand !== expectedAlias) {
|
|
736
|
+
if (alias ? !nxRunAliasMatches(alias, expectedAlias) : rawCommand !== expectedAlias) {
|
|
698
737
|
issues.push({
|
|
699
738
|
path: packagePath,
|
|
700
739
|
message: `${packagePath}: scripts.${scriptName} must delegate to ${expectedAlias}`,
|
|
@@ -989,7 +1028,7 @@ function applyPackageScriptPolicyForPkg(
|
|
|
989
1028
|
pkg: Record<string, unknown>,
|
|
990
1029
|
_packagePath: string,
|
|
991
1030
|
workspaceNames: ReadonlySet<string>,
|
|
992
|
-
options: { resolvedTargets?: ReadonlySet<string> } = {},
|
|
1031
|
+
options: { resolvedTargets?: ReadonlySet<string> | ResolvedProjectTargets } = {},
|
|
993
1032
|
): boolean {
|
|
994
1033
|
if (!hasWorkspaceDependency(pkg, workspaceNames)) {
|
|
995
1034
|
return false;
|
|
@@ -1010,6 +1049,11 @@ function applyPackageScriptPolicyForPkg(
|
|
|
1010
1049
|
continue;
|
|
1011
1050
|
}
|
|
1012
1051
|
const parsedAlias = parseNxRunAlias(rawCommand);
|
|
1052
|
+
if (isResolvedSelfRecursiveScript(scriptName, parsedAlias, projectName, options.resolvedTargets)) {
|
|
1053
|
+
delete scripts[scriptName];
|
|
1054
|
+
changed = true;
|
|
1055
|
+
continue;
|
|
1056
|
+
}
|
|
1013
1057
|
if (parsedAlias?.projectName === projectName && parsedAlias.targetName === 'test') {
|
|
1014
1058
|
continue;
|
|
1015
1059
|
}
|
|
@@ -1065,6 +1109,20 @@ function applyPackageScriptPolicyForPkg(
|
|
|
1065
1109
|
return changed;
|
|
1066
1110
|
}
|
|
1067
1111
|
|
|
1112
|
+
function isResolvedSelfRecursiveScript(
|
|
1113
|
+
scriptName: string,
|
|
1114
|
+
alias: { projectName: string; targetName: string } | null,
|
|
1115
|
+
projectName: string | null,
|
|
1116
|
+
resolvedTargets?: ReadonlySet<string> | ResolvedProjectTargets,
|
|
1117
|
+
): boolean {
|
|
1118
|
+
return (
|
|
1119
|
+
alias?.projectName === projectName &&
|
|
1120
|
+
alias.targetName === scriptName &&
|
|
1121
|
+
resolvedProjectTargetExecutor(resolvedTargets, scriptName) === 'nx:run-script' &&
|
|
1122
|
+
resolvedProjectTargetScript(resolvedTargets, scriptName) === scriptName
|
|
1123
|
+
);
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1068
1126
|
export function checkPackageTargets(
|
|
1069
1127
|
pkg: Record<string, unknown>,
|
|
1070
1128
|
packagePath: string,
|
|
@@ -1103,7 +1161,7 @@ export function checkPackageTargetPolicy(root: string, options: PackageTargetPol
|
|
|
1103
1161
|
issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedTargets));
|
|
1104
1162
|
issues.push(...validateTestEntrypointPresence(root, packagePath, pkg));
|
|
1105
1163
|
issues.push(...validateBuildZigPolicy(root, packagePath));
|
|
1106
|
-
issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets }));
|
|
1164
|
+
issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }));
|
|
1107
1165
|
}
|
|
1108
1166
|
|
|
1109
1167
|
return issues;
|
|
@@ -1128,7 +1186,8 @@ export function applyPackageTargetPolicy(root: string, options: PackageTargetPol
|
|
|
1128
1186
|
packageChanged = removePackageColonTargets(pkg) || packageChanged;
|
|
1129
1187
|
packageChanged = removeRedundantNoopBuildTarget(pkg, resolvedProject) || packageChanged;
|
|
1130
1188
|
packageChanged =
|
|
1131
|
-
applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets }) ||
|
|
1189
|
+
applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }) ||
|
|
1190
|
+
packageChanged;
|
|
1132
1191
|
|
|
1133
1192
|
if (packageChanged) {
|
|
1134
1193
|
writeJsonObject(packageJsonPath, pkg);
|