@smoothbricks/nx-plugin 0.3.14 → 0.4.1
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 +36 -13
- package/dist/bin/smoo-nx-exec.d.ts +3 -0
- package/dist/bin/smoo-nx-exec.d.ts.map +1 -0
- package/dist/bin/{ensure-built.js → smoo-nx-exec.js} +17 -5
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +5 -2
- package/dist/cargo-workspace.d.ts +15 -1
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +113 -8
- package/dist/ensure-built.d.ts +3 -0
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +21 -1
- package/dist/executors/typescript-emit/executor.d.ts.map +1 -1
- package/dist/executors/typescript-emit/executor.js +20 -2
- package/dist/executors/typescript-emit/schema.d.ts +1 -0
- package/dist/executors/typescript-emit/schema.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +345 -88
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +2 -1
- package/package.json +4 -4
- package/src/bin/{ensure-built.ts → smoo-nx-exec.ts} +18 -6
- package/src/bounded-test-policy.test.ts +21 -0
- package/src/bounded-test-policy.ts +6 -2
- package/src/cargo-workspace.test.ts +142 -0
- package/src/cargo-workspace.ts +137 -8
- package/src/ensure-built.test.ts +121 -21
- package/src/ensure-built.ts +23 -1
- package/src/executors/typescript-emit/executor.test.ts +37 -1
- package/src/executors/typescript-emit/executor.ts +24 -2
- package/src/executors/typescript-emit/schema.json +8 -0
- package/src/executors/typescript-emit/schema.ts +1 -0
- package/src/index.test.ts +177 -10
- package/src/index.ts +436 -90
- package/src/package-target-policy.test.ts +13 -0
- package/src/package-target-policy.ts +2 -1
- package/dist/bin/ensure-built.d.ts +0 -3
- package/dist/bin/ensure-built.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs';
|
|
2
2
|
import { readdir, readFile } from 'node:fs/promises';
|
|
3
|
-
import { dirname, join, posix } from 'node:path';
|
|
3
|
+
import { dirname, join, posix, relative } from 'node:path';
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
5
|
import { readJsonFile, } from 'nx/src/devkit-exports.js';
|
|
6
6
|
import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
|
|
7
7
|
import { parse as parseToml } from 'smol-toml';
|
|
8
8
|
import { BOUNDED_TEST_KILL_AFTER_MS, BOUNDED_TEST_TIMEOUT_MS } from './bounded-test-policy.js';
|
|
9
|
-
import { CARGO_TEST_COMPILE_TARGET, CARGO_TEST_EXCEPTIONS_SUFFIX, CARGO_TEST_TARGET, cargoPackageTestInputs, cargoTestPackageTargetName, exceptionalTestFilter, listCargoWorkspacePackages, nextestConfigRelPath, } from './cargo-workspace.js';
|
|
9
|
+
import { attributeCargoWorkspacePackages, CARGO_TEST_COMPILE_TARGET, CARGO_TEST_EXCEPTIONS_SUFFIX, CARGO_TEST_TARGET, cargoPackageTestInputs, cargoTestPackageTargetName, exceptionalTestFilter, listCargoWorkspacePackages, nextestConfigRelPath, } from './cargo-workspace.js';
|
|
10
10
|
import { CARGO_CROSS_LINT_COMMAND, CARGO_CROSS_LINT_TARGET, CARGO_FETCH_COMMAND, CARGO_FETCH_TARGET, CARGO_FROZEN_PREFIX, CARGO_LINT_CLIPPY_COMMAND, cargoFrozen, } from './cross-check-policy.js';
|
|
11
11
|
import { PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
|
|
12
12
|
export { CARGO_TEST_COMPILE_TARGET };
|
|
@@ -76,6 +76,16 @@ const CARGO_INPUTS = [
|
|
|
76
76
|
'!{projectRoot}/**/target/**',
|
|
77
77
|
];
|
|
78
78
|
const CARGO_OUTPUT_INPUTS = [...CARGO_INPUTS, '{projectRoot}/package.json', '{workspaceRoot}/bun.lock'];
|
|
79
|
+
const REPO_ROOT_CARGO_OUTPUT_INPUTS = [
|
|
80
|
+
'{workspaceRoot}/**/*.rs',
|
|
81
|
+
'{workspaceRoot}/**/Cargo.toml',
|
|
82
|
+
'{workspaceRoot}/Cargo.lock',
|
|
83
|
+
'{workspaceRoot}/**/.cargo/config.toml',
|
|
84
|
+
'{workspaceRoot}/scripts/*.sh',
|
|
85
|
+
'!{workspaceRoot}/**/target/**',
|
|
86
|
+
'{projectRoot}/package.json',
|
|
87
|
+
'{workspaceRoot}/bun.lock',
|
|
88
|
+
];
|
|
79
89
|
const NAPI_INPUTS = CARGO_OUTPUT_INPUTS;
|
|
80
90
|
const NAPI_TARGET_CONVENTIONS = {
|
|
81
91
|
'aarch64-apple-darwin': {
|
|
@@ -118,21 +128,25 @@ const NAPI_TOOLCHAIN_TARGET_PREFIX = 'napi-toolchain-';
|
|
|
118
128
|
export function napiToolchainTargetName(convention) {
|
|
119
129
|
return `${NAPI_TOOLCHAIN_TARGET_PREFIX}${convention.architecture}-${convention.targetFamily}`;
|
|
120
130
|
}
|
|
121
|
-
function createCargoWasmTarget(projectRoot, config) {
|
|
122
|
-
const cargoTargetDirectory =
|
|
131
|
+
function createCargoWasmTarget(projectRoot, config, repoRooted) {
|
|
132
|
+
const cargoTargetDirectory = repoRooted
|
|
133
|
+
? 'target/cargo-wasm'
|
|
134
|
+
: posix.join(posix.dirname(config.manifestPath), 'target/cargo-wasm');
|
|
123
135
|
const wasmInput = `${cargoTargetDirectory}/wasm32-unknown-unknown/release/${config.libraryName}.wasm`;
|
|
136
|
+
const outputDirectory = repoRooted ? posix.join(projectRoot, config.outputDirectory) : config.outputDirectory;
|
|
137
|
+
const cargoSelection = repoRooted ? `-p ${config.cargoPackage}` : `--manifest-path ${config.manifestPath}`;
|
|
124
138
|
return {
|
|
125
139
|
executor: 'nx:run-commands',
|
|
126
140
|
cache: true,
|
|
127
141
|
dependsOn: ['^build'],
|
|
128
|
-
inputs: CARGO_OUTPUT_INPUTS,
|
|
142
|
+
inputs: repoRooted ? REPO_ROOT_CARGO_OUTPUT_INPUTS : CARGO_OUTPUT_INPUTS,
|
|
129
143
|
outputs: [`{projectRoot}/${config.outputDirectory}`],
|
|
130
144
|
options: {
|
|
131
145
|
commands: [
|
|
132
|
-
cargoFrozen(`build --release --target wasm32-unknown-unknown --target-dir ${cargoTargetDirectory}
|
|
133
|
-
...config.targets.map(({ bindgenTarget, outputName }) => `wasm-bindgen --target ${bindgenTarget} --out-dir ${
|
|
146
|
+
cargoFrozen(`build --release --target wasm32-unknown-unknown --target-dir ${cargoTargetDirectory} ${cargoSelection}`),
|
|
147
|
+
...config.targets.map(({ bindgenTarget, outputName }) => `wasm-bindgen --target ${bindgenTarget} --out-dir ${outputDirectory}/${outputName} ${wasmInput}`),
|
|
134
148
|
],
|
|
135
|
-
cwd: projectRoot,
|
|
149
|
+
cwd: repoRooted ? '.' : projectRoot,
|
|
136
150
|
parallel: false,
|
|
137
151
|
},
|
|
138
152
|
};
|
|
@@ -205,11 +219,16 @@ const PLUGIN_NEXTEST_CONFIG = fileURLToPath(new URL('../nextest.toml', import.me
|
|
|
205
219
|
* depending on how the hash happened to fall.
|
|
206
220
|
*
|
|
207
221
|
* The two filtersets are exact complements, so their union is the crate whatever
|
|
208
|
-
* either one matches
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
222
|
+
* either one matches. Every piece uses `--no-tests=pass`: a valid workspace
|
|
223
|
+
* member may expose no nextest tests, a small suite can leave a declared hash
|
|
224
|
+
* shard empty, and platform-specific exceptions may not exist on this host.
|
|
225
|
+
* Manifest discovery proves the package exists and generates the selector from
|
|
226
|
+
* its Cargo name, so accepting an empty run cannot hide a misspelled package.
|
|
227
|
+
*
|
|
228
|
+
* The trade is explicit: a generated target cannot distinguish an intentionally
|
|
229
|
+
* test-less crate from one whose entire suite stopped matching, so both pass.
|
|
230
|
+
* Detecting that regression requires a separate coverage policy rather than
|
|
231
|
+
* making valid empty crates and shards fail execution.
|
|
213
232
|
*
|
|
214
233
|
* Pieces chain rather than fan out, like the crates do: cargo flocks one
|
|
215
234
|
* `target/`, and chaining keeps even the pinned group from overlapping the
|
|
@@ -237,7 +256,7 @@ async function addPerPackageCargoTestTargets(targets, projectRoot, workspaceRoot
|
|
|
237
256
|
inputs,
|
|
238
257
|
dependsOn: [previous],
|
|
239
258
|
options: {
|
|
240
|
-
command: cargoFrozen(`nextest run --workspace -E '${selector}'${extra} --user-config-file none --config-file ${configFile}`),
|
|
259
|
+
command: cargoFrozen(`nextest run --workspace -E '${selector}'${extra} --no-tests=pass --user-config-file none --config-file ${configFile}`),
|
|
241
260
|
cwd: projectRoot,
|
|
242
261
|
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
243
262
|
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
@@ -252,7 +271,7 @@ async function addPerPackageCargoTestTargets(targets, projectRoot, workspaceRoot
|
|
|
252
271
|
addTarget(sharded ? `shard${index}` : undefined, shardable, sharded ? ` --partition hash:${index}/${pkg.testShards}` : '');
|
|
253
272
|
}
|
|
254
273
|
if (pin !== null) {
|
|
255
|
-
addTarget(CARGO_TEST_EXCEPTIONS_SUFFIX, `package(${pkg.name}) and (${pin})`, '
|
|
274
|
+
addTarget(CARGO_TEST_EXCEPTIONS_SUFFIX, `package(${pkg.name}) and (${pin})`, '');
|
|
256
275
|
}
|
|
257
276
|
}
|
|
258
277
|
return packageTargetNames;
|
|
@@ -261,17 +280,22 @@ function createNodesHandler(hostPlatform) {
|
|
|
261
280
|
return async (projectConfigurationFiles, _options, context) => {
|
|
262
281
|
const results = [];
|
|
263
282
|
const errors = [];
|
|
283
|
+
const repoRootCargoWorkspace = await resolveRepoRootCargoWorkspace(projectConfigurationFiles, context.workspaceRoot);
|
|
264
284
|
await Promise.all(projectConfigurationFiles.map(async (packageJsonPath) => {
|
|
265
285
|
try {
|
|
266
|
-
if (isManagedPackageJsonSource(packageJsonPath)) {
|
|
286
|
+
if (isManagedPackageJsonSource(packageJsonPath) || isBuildOutputPackageJson(packageJsonPath)) {
|
|
267
287
|
// smoo monorepo managed raw/templates are source copies, not projects.
|
|
268
288
|
// Dogfood trees symlink live paths here; discovering both doubles names.
|
|
289
|
+
// A package.json a build wrote (napi's platform dirs, dist, cargo's
|
|
290
|
+
// target, a package cache) is an artifact, never a project: inferring
|
|
291
|
+
// one races every task that rebuilds the graph against the build that
|
|
292
|
+
// is writing it, and a name collision drops the real project.
|
|
269
293
|
results.push([packageJsonPath, {}]);
|
|
270
294
|
return;
|
|
271
295
|
}
|
|
272
296
|
results.push([
|
|
273
297
|
packageJsonPath,
|
|
274
|
-
await createProjectTargets(packageJsonPath, context.workspaceRoot, hostPlatform),
|
|
298
|
+
await createProjectTargets(packageJsonPath, context.workspaceRoot, hostPlatform, repoRootCargoWorkspace),
|
|
275
299
|
]);
|
|
276
300
|
}
|
|
277
301
|
catch (error) {
|
|
@@ -295,25 +319,63 @@ function isManagedPackageJsonSource(packageJsonPath) {
|
|
|
295
319
|
const normalized = packageJsonPath.replaceAll('\\', '/');
|
|
296
320
|
return normalized.includes('/managed/raw/') || normalized.includes('/managed/templates/');
|
|
297
321
|
}
|
|
322
|
+
/** Directories only builds write into; a package.json found under one is an output. */
|
|
323
|
+
const BUILD_OUTPUT_SEGMENTS = ['/node_modules/', '/dist/', '/target/', '/.cache/', '/.nx/'];
|
|
324
|
+
function isBuildOutputPackageJson(packageJsonPath) {
|
|
325
|
+
const normalized = `/${packageJsonPath.replaceAll('\\', '/')}`;
|
|
326
|
+
return BUILD_OUTPUT_SEGMENTS.some((segment) => normalized.includes(segment));
|
|
327
|
+
}
|
|
298
328
|
export default { createNodesV2 };
|
|
299
|
-
|
|
329
|
+
function packageBinOutputs(packageJson, packageJsonPath) {
|
|
330
|
+
if (packageJson.bin === undefined) {
|
|
331
|
+
return [];
|
|
332
|
+
}
|
|
333
|
+
if (typeof packageJson.bin === 'string') {
|
|
334
|
+
if (packageJson.bin.length === 0) {
|
|
335
|
+
throw new Error(`${packageJsonPath}: bin must not be empty`);
|
|
336
|
+
}
|
|
337
|
+
return [packageJson.bin];
|
|
338
|
+
}
|
|
339
|
+
if (!isRecord(packageJson.bin)) {
|
|
340
|
+
throw new Error(`${packageJsonPath}: bin must be a string or an object of executable paths`);
|
|
341
|
+
}
|
|
342
|
+
const outputs = new Set();
|
|
343
|
+
for (const [name, output] of Object.entries(packageJson.bin)) {
|
|
344
|
+
if (typeof output !== 'string' || output.length === 0) {
|
|
345
|
+
throw new Error(`${packageJsonPath}: bin.${name} must be a non-empty executable path`);
|
|
346
|
+
}
|
|
347
|
+
outputs.add(output);
|
|
348
|
+
}
|
|
349
|
+
return [...outputs];
|
|
350
|
+
}
|
|
351
|
+
async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform, repoRootCargoWorkspace) {
|
|
300
352
|
const projectRoot = dirname(packageJsonPath);
|
|
301
353
|
const absoluteProjectRoot = join(workspaceRoot, projectRoot);
|
|
302
354
|
const packageJson = await readPackageJson(join(workspaceRoot, packageJsonPath));
|
|
355
|
+
const projectName = packageJson.nx?.name ?? packageJson.name;
|
|
356
|
+
if (typeof projectName !== 'string' || projectName.length === 0) {
|
|
357
|
+
throw new Error(`${packageJsonPath} must declare a non-empty package or nx project name`);
|
|
358
|
+
}
|
|
303
359
|
const targets = {};
|
|
304
360
|
const validationTargets = [];
|
|
305
361
|
const libTsconfigPath = join(absoluteProjectRoot, 'tsconfig.lib.json');
|
|
306
362
|
const hasLibTsconfig = existsSync(libTsconfigPath);
|
|
307
363
|
const cargoTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
|
|
308
364
|
const isCargoWorkspace = existsSync(cargoTomlPath) && CARGO_WORKSPACE_PATTERN.test(await readFile(cargoTomlPath, 'utf-8'));
|
|
309
|
-
const
|
|
365
|
+
const repoRootPackagePlans = repoRootCargoWorkspace?.packages.filter((plan) => plan.package.projectRoot === projectRoot) ?? [];
|
|
366
|
+
const isRepoRootWorkspaceRoot = repoRootCargoWorkspace !== null && projectRoot === '.';
|
|
367
|
+
const isRepoRootCargoProject = isRepoRootWorkspaceRoot || repoRootPackagePlans.length > 0;
|
|
368
|
+
const repoRootPackages = repoRootPackagePlans.map((plan) => plan.package);
|
|
369
|
+
const isRepoRootedCargoProject = isRepoRootCargoProject && (!isCargoWorkspace || isRepoRootWorkspaceRoot);
|
|
370
|
+
const napiConfig = resolveNapiConfig(packageJson, packageJsonPath, absoluteProjectRoot, isCargoWorkspace, repoRootPackages);
|
|
310
371
|
const declaredTargets = packageJson.nx?.targets ?? {};
|
|
311
|
-
const cargoWasmConfig = await resolveCargoWasmConfig(absoluteProjectRoot, packageJsonPath);
|
|
372
|
+
const cargoWasmConfig = await resolveCargoWasmConfig(absoluteProjectRoot, packageJsonPath, workspaceRoot, repoRootPackages);
|
|
312
373
|
const inferCargoWasm = cargoWasmConfig !== null && !('cargo-wasm' in declaredTargets);
|
|
313
374
|
const packageLocalBuildOutputs = classifyPackageLocalBuildOutputs(packageJson);
|
|
314
375
|
const hasOrdinaryBuildOutputTarget = hasLibTsconfig || napiConfig !== null || cargoWasmConfig !== null || packageLocalBuildOutputs.ordinary;
|
|
315
376
|
const hasAnyBuildOutputTarget = hasOrdinaryBuildOutputTarget || packageLocalBuildOutputs.platform;
|
|
316
377
|
if (hasLibTsconfig) {
|
|
378
|
+
const executableOutputs = packageBinOutputs(packageJson, packageJsonPath);
|
|
317
379
|
// Dependency packages may publish bundled entries that raw tsc does not produce.
|
|
318
380
|
// Build every JavaScript output lane before resolving package exports, without pulling
|
|
319
381
|
// unrelated Wasm, N-API, native, or web outputs onto the compiler critical path.
|
|
@@ -329,6 +391,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
329
391
|
outputs: inferTypescriptOutputs(libTsconfigPath, packageJsonPath),
|
|
330
392
|
dependsOn: ['^*-js', ...(cargoWasmConfig ? ['cargo-wasm'] : [])],
|
|
331
393
|
options: {
|
|
394
|
+
...(executableOutputs.length > 0 ? { executableOutputs } : {}),
|
|
332
395
|
tsConfig: 'tsconfig.lib.json',
|
|
333
396
|
cwd: projectRoot,
|
|
334
397
|
},
|
|
@@ -383,10 +446,10 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
383
446
|
validationTargets.push('typecheck');
|
|
384
447
|
}
|
|
385
448
|
if (inferCargoWasm && cargoWasmConfig) {
|
|
386
|
-
targets['cargo-wasm'] = createCargoWasmTarget(projectRoot, cargoWasmConfig);
|
|
449
|
+
targets['cargo-wasm'] = createCargoWasmTarget(projectRoot, cargoWasmConfig, isRepoRootedCargoProject);
|
|
387
450
|
}
|
|
388
451
|
if (napiConfig) {
|
|
389
|
-
Object.assign(targets, createNapiTargets(projectRoot, napiConfig, hostPlatform));
|
|
452
|
+
Object.assign(targets, createNapiTargets(projectRoot, napiConfig, hostPlatform, isRepoRootedCargoProject));
|
|
390
453
|
if (!('napi-test' in (packageJson.nx?.targets ?? {})) &&
|
|
391
454
|
existsSync(join(absoluteProjectRoot, 'src/native.test.ts'))) {
|
|
392
455
|
targets['napi-test'] = createNapiTestTarget(projectRoot, existsSync(join(absoluteProjectRoot, 'bunfig.napi-test.toml')));
|
|
@@ -438,20 +501,30 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
438
501
|
// all-or-nothing, because for those the package decides whether the target
|
|
439
502
|
// EXISTS — a packaging decision for output families, the bounded-test policy's
|
|
440
503
|
// rewrite for `test` — so there is no inferred base to partially override.
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
const
|
|
504
|
+
const ownsCargoWorkspaceTargets = isCargoWorkspace || isRepoRootWorkspaceRoot;
|
|
505
|
+
if (ownsCargoWorkspaceTargets) {
|
|
506
|
+
const cargoWorkspaceRoot = isRepoRootWorkspaceRoot ? '.' : projectRoot;
|
|
507
|
+
targets[CARGO_TEST_COMPILE_TARGET] = createCargoTestCompileTarget(cargoWorkspaceRoot);
|
|
508
|
+
const packageTargetNames = isRepoRootWorkspaceRoot
|
|
509
|
+
? await addRepoRootCargoTestTargets(targets, projectName, projectRoot, workspaceRoot, repoRootCargoWorkspace)
|
|
510
|
+
: await addPerPackageCargoTestTargets(targets, projectRoot, workspaceRoot, absoluteProjectRoot);
|
|
511
|
+
const aggregateDependencies = isRepoRootWorkspaceRoot
|
|
512
|
+
? repoRootCargoWorkspace.packages.flatMap((plan) => plan.pieces.map((piece) => cargoTargetDependency(projectName, {
|
|
513
|
+
projectName: plan.package.projectName,
|
|
514
|
+
targetName: piece.targetName,
|
|
515
|
+
})))
|
|
516
|
+
: packageTargetNames;
|
|
444
517
|
targets[CARGO_TEST_TARGET] =
|
|
445
|
-
|
|
446
|
-
? { executor: 'nx:noop', cache: true, dependsOn:
|
|
447
|
-
: createCargoTestTarget(
|
|
518
|
+
aggregateDependencies.length > 0
|
|
519
|
+
? { executor: 'nx:noop', cache: true, dependsOn: aggregateDependencies }
|
|
520
|
+
: createCargoTestTarget(cargoWorkspaceRoot);
|
|
448
521
|
targets['cargo-lint'] = {
|
|
449
522
|
executor: 'nx:run-commands',
|
|
450
523
|
cache: true,
|
|
451
524
|
inputs: CARGO_INPUTS,
|
|
452
525
|
options: {
|
|
453
526
|
commands: ['cargo fmt --all --check', CARGO_LINT_CLIPPY_COMMAND],
|
|
454
|
-
cwd:
|
|
527
|
+
cwd: cargoWorkspaceRoot,
|
|
455
528
|
parallel: false,
|
|
456
529
|
},
|
|
457
530
|
};
|
|
@@ -464,7 +537,9 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
464
537
|
// governs targets that PRODUCE artifacts, where "does this package ship this
|
|
465
538
|
// artifact" is a packaging decision no crate manifest can answer. This target
|
|
466
539
|
// emits nothing — clippy type-checks and discards — so it belongs with
|
|
467
|
-
// cargo-lint and cargo-test on the inferred validation side.
|
|
540
|
+
// cargo-lint and cargo-test on the inferred validation side. Repository-root
|
|
541
|
+
// workspaces own it once at the root because `--workspace` already checks
|
|
542
|
+
// every member.
|
|
468
543
|
//
|
|
469
544
|
// The discriminator for "has cross-platform Rust" is deliberately the same
|
|
470
545
|
// `[workspace]` Cargo.toml that already grants cargo-lint, NOT a scan of
|
|
@@ -487,7 +562,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
487
562
|
inputs: CARGO_INPUTS,
|
|
488
563
|
options: {
|
|
489
564
|
command: CARGO_CROSS_LINT_COMMAND,
|
|
490
|
-
cwd:
|
|
565
|
+
cwd: cargoWorkspaceRoot,
|
|
491
566
|
},
|
|
492
567
|
};
|
|
493
568
|
if (!targets.test && !('test' in declaredTargets) && typeof packageJson.scripts?.test !== 'string') {
|
|
@@ -502,12 +577,12 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
502
577
|
targets.mutation = {
|
|
503
578
|
executor: 'nx:run-commands',
|
|
504
579
|
cache: false,
|
|
505
|
-
options: { command: cargoFrozen('mutants --workspace'), cwd:
|
|
580
|
+
options: { command: cargoFrozen('mutants --workspace'), cwd: cargoWorkspaceRoot },
|
|
506
581
|
};
|
|
507
582
|
targets.bench = {
|
|
508
583
|
executor: 'nx:run-commands',
|
|
509
584
|
cache: false,
|
|
510
|
-
options: { command: cargoFrozen('bench --workspace'), cwd:
|
|
585
|
+
options: { command: cargoFrozen('bench --workspace'), cwd: cargoWorkspaceRoot },
|
|
511
586
|
};
|
|
512
587
|
// Target-dir GC. Cargo never removes superseded artifacts, so a busy
|
|
513
588
|
// workspace grows tens of GB of stale variants no fingerprint references;
|
|
@@ -517,9 +592,24 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
517
592
|
targets['cargo-sweep'] = {
|
|
518
593
|
executor: 'nx:run-commands',
|
|
519
594
|
cache: false,
|
|
520
|
-
options: { command: 'cargo sweep --time 7', cwd:
|
|
595
|
+
options: { command: 'cargo sweep --time 7', cwd: cargoWorkspaceRoot },
|
|
521
596
|
};
|
|
522
597
|
}
|
|
598
|
+
if (!isCargoWorkspace && !isRepoRootWorkspaceRoot && repoRootPackagePlans.length > 0 && repoRootCargoWorkspace) {
|
|
599
|
+
const packageTargetNames = await addRepoRootCargoTestTargets(targets, projectName, projectRoot, workspaceRoot, repoRootCargoWorkspace);
|
|
600
|
+
targets[CARGO_TEST_TARGET] = {
|
|
601
|
+
executor: 'nx:noop',
|
|
602
|
+
cache: true,
|
|
603
|
+
dependsOn: packageTargetNames,
|
|
604
|
+
};
|
|
605
|
+
if (!targets.test && !('test' in declaredTargets) && typeof packageJson.scripts?.test !== 'string') {
|
|
606
|
+
targets.test = {
|
|
607
|
+
executor: 'nx:noop',
|
|
608
|
+
cache: true,
|
|
609
|
+
dependsOn: [CARGO_TEST_TARGET],
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
}
|
|
523
613
|
// Every `cargoFrozen` command needs a registry cache holding the whole locked
|
|
524
614
|
// graph, and offline cargo checks that at RESOLUTION — so a dev-dependency no
|
|
525
615
|
// build ever downloaded takes down `cargo --frozen build` just as surely as
|
|
@@ -534,6 +624,8 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
534
624
|
// frozen target added later is covered without anyone remembering to. The edge
|
|
535
625
|
// goes on EVERY frozen target, not just the head of the serialization chain,
|
|
536
626
|
// so re-routing that chain cannot strip a surviving target's precondition.
|
|
627
|
+
// Package-rooted workspaces fetch beside their package.json; members of a
|
|
628
|
+
// repository-root workspace all depend on the root project's one fetch.
|
|
537
629
|
//
|
|
538
630
|
// A package that REPLACES one of these `dependsOn` lists drops the edge with
|
|
539
631
|
// it (see the merge rules above) and gets its cold-cache failure back; `"..."`
|
|
@@ -542,9 +634,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
542
634
|
if (isCargoWorkspace) {
|
|
543
635
|
frozenCargoManifests.add('Cargo.toml');
|
|
544
636
|
}
|
|
545
|
-
if (inferCargoWasm && cargoWasmConfig) {
|
|
546
|
-
// A wasm-bindgen crate can sit in a nested Cargo workspace the project root
|
|
547
|
-
// is not a member of, so its graph is a second fetch, not the same one.
|
|
637
|
+
if (inferCargoWasm && cargoWasmConfig && !isRepoRootedCargoProject) {
|
|
548
638
|
frozenCargoManifests.add(cargoWasmConfig.manifestPath);
|
|
549
639
|
}
|
|
550
640
|
if (frozenCargoManifests.size > 0) {
|
|
@@ -561,7 +651,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
561
651
|
.map((manifestPath) => manifestPath === 'Cargo.toml'
|
|
562
652
|
? CARGO_FETCH_COMMAND
|
|
563
653
|
: `${CARGO_FETCH_COMMAND} --manifest-path ${manifestPath}`),
|
|
564
|
-
cwd: projectRoot,
|
|
654
|
+
cwd: isRepoRootWorkspaceRoot ? '.' : projectRoot,
|
|
565
655
|
// Two fetches into one CARGO_HOME contend on its package-cache flock;
|
|
566
656
|
// serializing here spends the wait in Nx instead of inside cargo.
|
|
567
657
|
parallel: false,
|
|
@@ -577,32 +667,66 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
577
667
|
typeof command === 'string' ? command : '',
|
|
578
668
|
...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
|
|
579
669
|
].join('\n');
|
|
580
|
-
if (
|
|
581
|
-
|
|
670
|
+
if (text.includes(CARGO_FROZEN_PREFIX)) {
|
|
671
|
+
target.dependsOn = [CARGO_FETCH_TARGET, ...(target.dependsOn ?? [])];
|
|
582
672
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
else if (isRepoRootedCargoProject && repoRootCargoWorkspace) {
|
|
676
|
+
const rootFetch = cargoTargetDependency(projectName, {
|
|
677
|
+
projectName: repoRootCargoWorkspace.rootProjectName,
|
|
678
|
+
targetName: CARGO_FETCH_TARGET,
|
|
679
|
+
});
|
|
680
|
+
for (const target of Object.values(targets)) {
|
|
681
|
+
const command = target.options?.command;
|
|
682
|
+
const commands = target.options?.commands;
|
|
683
|
+
const text = [
|
|
684
|
+
typeof command === 'string' ? command : '',
|
|
685
|
+
...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
|
|
686
|
+
].join('\n');
|
|
687
|
+
if (text.includes(CARGO_FROZEN_PREFIX)) {
|
|
688
|
+
target.dependsOn = [rootFetch, ...(target.dependsOn ?? [])];
|
|
586
689
|
}
|
|
587
690
|
}
|
|
588
691
|
}
|
|
589
|
-
// Cargo flocks the
|
|
590
|
-
//
|
|
692
|
+
// Cargo flocks the workspace's default target/. Keep every writer out of the
|
|
693
|
+
// bounded test window by placing N-API debug builds on the same serialized
|
|
694
|
+
// chain as the root compile and per-crate runners.
|
|
695
|
+
const cargoTestCompileDependency = targets[CARGO_TEST_COMPILE_TARGET]
|
|
696
|
+
? CARGO_TEST_COMPILE_TARGET
|
|
697
|
+
: isRepoRootCargoProject && repoRootCargoWorkspace
|
|
698
|
+
? cargoTargetDependency(projectName, {
|
|
699
|
+
projectName: repoRootCargoWorkspace.rootProjectName,
|
|
700
|
+
targetName: CARGO_TEST_COMPILE_TARGET,
|
|
701
|
+
})
|
|
702
|
+
: null;
|
|
591
703
|
const napiDebug = targets['napi-debug'];
|
|
592
|
-
if (napiDebug &&
|
|
704
|
+
if (napiDebug && cargoTestCompileDependency) {
|
|
593
705
|
const dependsOn = napiDebug.dependsOn ?? [];
|
|
594
|
-
if (!dependsOn
|
|
595
|
-
napiDebug.dependsOn = [...dependsOn,
|
|
706
|
+
if (!hasCargoTargetDependency(dependsOn, cargoTestCompileDependency)) {
|
|
707
|
+
napiDebug.dependsOn = [...dependsOn, cargoTestCompileDependency];
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (napiDebug && isRepoRootedCargoProject) {
|
|
711
|
+
const firstCargoTest = repoRootPackagePlans.flatMap((plan) => plan.pieces)[0];
|
|
712
|
+
const firstTarget = firstCargoTest ? targets[firstCargoTest.targetName] : undefined;
|
|
713
|
+
if (firstCargoTest && firstTarget) {
|
|
714
|
+
const previous = cargoTargetDependency(projectName, firstCargoTest.previous);
|
|
715
|
+
const debugDependencies = napiDebug.dependsOn ?? [];
|
|
716
|
+
if (!hasCargoTargetDependency(debugDependencies, previous)) {
|
|
717
|
+
napiDebug.dependsOn = [...debugDependencies, previous];
|
|
718
|
+
}
|
|
719
|
+
firstTarget.dependsOn = (firstTarget.dependsOn ?? []).map((dependency) => hasCargoTargetDependency([dependency], previous) ? 'napi-debug' : dependency);
|
|
596
720
|
}
|
|
597
721
|
}
|
|
598
|
-
if (napiDebug) {
|
|
722
|
+
else if (napiDebug) {
|
|
599
723
|
for (const [name, target] of Object.entries(targets)) {
|
|
600
724
|
if (!name.startsWith('cargo-test-') || name === CARGO_TEST_COMPILE_TARGET) {
|
|
601
725
|
continue;
|
|
602
726
|
}
|
|
603
727
|
const dependsOn = target.dependsOn ?? [];
|
|
604
728
|
if (dependsOn.includes(CARGO_TEST_COMPILE_TARGET) && !dependsOn.includes('napi-debug')) {
|
|
605
|
-
target.dependsOn = dependsOn.map((
|
|
729
|
+
target.dependsOn = dependsOn.map((dependency) => dependency === CARGO_TEST_COMPILE_TARGET ? 'napi-debug' : dependency);
|
|
606
730
|
}
|
|
607
731
|
}
|
|
608
732
|
const cargoTest = targets[CARGO_TEST_TARGET];
|
|
@@ -616,8 +740,8 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
616
740
|
}
|
|
617
741
|
}
|
|
618
742
|
// `build` lists cargo-test-compile beside the host's platform binaries, and
|
|
619
|
-
// every one of those is a `napi build` — another cargo writer on the
|
|
620
|
-
// default `target/`, which cargo flocks.
|
|
743
|
+
// every one of those is a `napi build` — another cargo writer on the
|
|
744
|
+
// workspace's default `target/`, which cargo flocks.
|
|
621
745
|
//
|
|
622
746
|
// Only `cargo-napi` gets the ordering edge. A platform-suffixed target may NOT
|
|
623
747
|
// have one: `validatePlatformTargetDependencies` (package-target-policy.ts:674)
|
|
@@ -636,10 +760,10 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
636
760
|
// targets, so the wait is not charged against a test budget.
|
|
637
761
|
const cargoWriterNames = hostPlatformTargetNames([...Object.keys(declaredTargets), ...Object.keys(targets)], hostPlatform);
|
|
638
762
|
const cargoNapi = targets['cargo-napi'];
|
|
639
|
-
if (cargoNapi &&
|
|
763
|
+
if (cargoNapi && cargoTestCompileDependency) {
|
|
640
764
|
const dependsOn = cargoNapi.dependsOn ?? [];
|
|
641
|
-
if (!dependsOn
|
|
642
|
-
cargoNapi.dependsOn = [...dependsOn,
|
|
765
|
+
if (!hasCargoTargetDependency(dependsOn, cargoTestCompileDependency)) {
|
|
766
|
+
cargoNapi.dependsOn = [...dependsOn, cargoTestCompileDependency];
|
|
643
767
|
}
|
|
644
768
|
}
|
|
645
769
|
// No `outputs`, deliberately: this aggregate runs no command, so every file
|
|
@@ -666,7 +790,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
666
790
|
// cacheable, while every RUNNER is a bounded-exec target reached only
|
|
667
791
|
// through the `test` aggregate. That split is what keeps a cold cargo
|
|
668
792
|
// workspace's compile time out of the bounded test window.
|
|
669
|
-
...(
|
|
793
|
+
...(cargoTestCompileDependency ? [cargoTestCompileDependency] : []),
|
|
670
794
|
...buildOutputTargetNames([...Object.keys(declaredTargets), ...Object.keys(targets)]),
|
|
671
795
|
...cargoWriterNames,
|
|
672
796
|
]),
|
|
@@ -696,10 +820,6 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
|
|
|
696
820
|
dependsOn: validationTargets,
|
|
697
821
|
};
|
|
698
822
|
}
|
|
699
|
-
const projectName = packageJson.nx?.name ?? packageJson.name;
|
|
700
|
-
if (typeof projectName !== 'string' || projectName.length === 0) {
|
|
701
|
-
throw new Error(`${packageJsonPath} must declare a non-empty package or nx project name`);
|
|
702
|
-
}
|
|
703
823
|
return {
|
|
704
824
|
projects: {
|
|
705
825
|
[projectRoot]: { name: projectName, targets },
|
|
@@ -714,23 +834,26 @@ const WASM_BINDGEN_OUTPUT_NAMES = {
|
|
|
714
834
|
nodejs: 'node',
|
|
715
835
|
web: 'web',
|
|
716
836
|
};
|
|
717
|
-
async function resolveCargoWasmConfig(absoluteProjectRoot, packageJsonPath) {
|
|
718
|
-
const manifestPaths =
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
const
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
if (
|
|
726
|
-
|
|
837
|
+
async function resolveCargoWasmConfig(absoluteProjectRoot, packageJsonPath, workspaceRoot, repoRootPackages) {
|
|
838
|
+
const manifestPaths = repoRootPackages.map((pkg) => posix.join(pkg.dir, 'Cargo.toml'));
|
|
839
|
+
if (manifestPaths.length === 0) {
|
|
840
|
+
manifestPaths.push('Cargo.toml');
|
|
841
|
+
const cratesDirectory = join(absoluteProjectRoot, 'crates');
|
|
842
|
+
if (existsSync(cratesDirectory)) {
|
|
843
|
+
const crateEntries = await readdir(cratesDirectory, { withFileTypes: true });
|
|
844
|
+
for (const entry of crateEntries.sort((left, right) => left.name.localeCompare(right.name))) {
|
|
845
|
+
if (entry.isDirectory()) {
|
|
846
|
+
const manifestPath = posix.join('crates', entry.name, 'Cargo.toml');
|
|
847
|
+
if (existsSync(join(absoluteProjectRoot, manifestPath))) {
|
|
848
|
+
manifestPaths.push(manifestPath);
|
|
849
|
+
}
|
|
727
850
|
}
|
|
728
851
|
}
|
|
729
852
|
}
|
|
730
853
|
}
|
|
731
854
|
const resolved = [];
|
|
732
855
|
for (const manifestPath of manifestPaths) {
|
|
733
|
-
const absoluteManifestPath = join(absoluteProjectRoot, manifestPath);
|
|
856
|
+
const absoluteManifestPath = repoRootPackages.length > 0 ? join(workspaceRoot, manifestPath) : join(absoluteProjectRoot, manifestPath);
|
|
734
857
|
if (!existsSync(absoluteManifestPath)) {
|
|
735
858
|
continue;
|
|
736
859
|
}
|
|
@@ -780,7 +903,13 @@ async function resolveCargoWasmConfig(absoluteProjectRoot, packageJsonPath) {
|
|
|
780
903
|
outputDirectory.startsWith('/')) {
|
|
781
904
|
throw new Error(`${packageJsonPath}: ${manifestPath} smoothbricks.wasm-bindgen.out-dir must stay inside the project`);
|
|
782
905
|
}
|
|
783
|
-
resolved.push({
|
|
906
|
+
resolved.push({
|
|
907
|
+
cargoPackage: packageName,
|
|
908
|
+
libraryName: rawLibraryName,
|
|
909
|
+
manifestPath,
|
|
910
|
+
outputDirectory,
|
|
911
|
+
targets,
|
|
912
|
+
});
|
|
784
913
|
}
|
|
785
914
|
if (resolved.length > 1) {
|
|
786
915
|
throw new Error(`${packageJsonPath}: only one Cargo crate may declare smoothbricks.wasm-bindgen metadata`);
|
|
@@ -800,12 +929,12 @@ function inferTypescriptOutputs(tsconfigPath, packageJsonPath) {
|
|
|
800
929
|
}
|
|
801
930
|
return [`{projectRoot}/${normalized}`];
|
|
802
931
|
}
|
|
803
|
-
function resolveNapiConfig(packageJson, packageJsonPath, absoluteProjectRoot, isCargoWorkspace) {
|
|
932
|
+
function resolveNapiConfig(packageJson, packageJsonPath, absoluteProjectRoot, isCargoWorkspace, repoRootPackages) {
|
|
804
933
|
if (!packageJson.napi) {
|
|
805
934
|
return null;
|
|
806
935
|
}
|
|
807
|
-
if (!isCargoWorkspace) {
|
|
808
|
-
throw new Error(`${packageJsonPath}: napi target inference requires a Cargo workspace
|
|
936
|
+
if (!isCargoWorkspace && repoRootPackages.length === 0) {
|
|
937
|
+
throw new Error(`${packageJsonPath}: napi target inference requires a Cargo workspace containing this project`);
|
|
809
938
|
}
|
|
810
939
|
const binaryName = packageJson.napi.binaryName;
|
|
811
940
|
if (typeof binaryName !== 'string' || !/^[A-Za-z0-9_-]+$/.test(binaryName)) {
|
|
@@ -824,14 +953,30 @@ function resolveNapiConfig(packageJson, packageJsonPath, absoluteProjectRoot, is
|
|
|
824
953
|
}
|
|
825
954
|
}
|
|
826
955
|
const cargoPackage = `${binaryName}-napi`;
|
|
827
|
-
const
|
|
828
|
-
|
|
956
|
+
const rootMember = repoRootPackages.find((pkg) => pkg.name === cargoPackage);
|
|
957
|
+
const manifestPath = rootMember ? posix.join(rootMember.dir, 'Cargo.toml') : `crates/${cargoPackage}/Cargo.toml`;
|
|
958
|
+
const absoluteManifestPath = rootMember
|
|
959
|
+
? join(absoluteProjectRoot, relative(dirname(packageJsonPath), manifestPath))
|
|
960
|
+
: join(absoluteProjectRoot, manifestPath);
|
|
961
|
+
if (!existsSync(absoluteManifestPath)) {
|
|
829
962
|
throw new Error(`${packageJsonPath}: inferred N-API crate is missing at ${manifestPath}`);
|
|
830
963
|
}
|
|
831
964
|
return { binaryName, cargoPackage, manifestPath, targets };
|
|
832
965
|
}
|
|
833
|
-
function createNapiTargets(projectRoot, config, hostPlatform) {
|
|
834
|
-
|
|
966
|
+
function createNapiTargets(projectRoot, config, hostPlatform, repoRooted) {
|
|
967
|
+
// napi derives the addon filename ([name]) from the nearest package.json `napi`
|
|
968
|
+
// field starting at the invocation cwd. A repository-root invocation runs where
|
|
969
|
+
// no such field exists, so without an explicit path every repo-rooted build
|
|
970
|
+
// emits index.*.node while the loader resolves binaryName.*.node — a mismatch
|
|
971
|
+
// that stale local artifacts can mask for months.
|
|
972
|
+
const packageJsonPath = repoRooted ? posix.join(projectRoot, 'package.json') : 'package.json';
|
|
973
|
+
const commonCommand = `--manifest-path ${config.manifestPath} --package ${config.cargoPackage} --package-json-path ${packageJsonPath}`;
|
|
974
|
+
const cargoInputs = repoRooted ? REPO_ROOT_CARGO_OUTPUT_INPUTS : NAPI_INPUTS;
|
|
975
|
+
const cargoCwd = repoRooted ? '.' : projectRoot;
|
|
976
|
+
// A repository-root Cargo invocation runs outside the owning npm package, so
|
|
977
|
+
// Nx's root-only PATH cannot resolve that package's napi CLI.
|
|
978
|
+
const napiCommand = repoRooted ? posix.join(projectRoot, 'node_modules/.bin/napi') : 'napi';
|
|
979
|
+
const outputPath = (projectOutput) => (repoRooted ? posix.join(projectRoot, projectOutput) : projectOutput);
|
|
835
980
|
const hostPlatformTargetName = hostPlatform === null ? null : `napi-${hostPlatform.architecture}-${hostPlatform.targetFamily}`;
|
|
836
981
|
let nativeHostTargetName = null;
|
|
837
982
|
if (hostPlatformTargetName !== null) {
|
|
@@ -857,11 +1002,11 @@ function createNapiTargets(projectRoot, config, hostPlatform) {
|
|
|
857
1002
|
executor: 'nx:run-commands',
|
|
858
1003
|
cache: true,
|
|
859
1004
|
dependsOn: ['^build'],
|
|
860
|
-
inputs:
|
|
1005
|
+
inputs: cargoInputs,
|
|
861
1006
|
outputs: ['{projectRoot}/.cache/native-debug'],
|
|
862
1007
|
options: {
|
|
863
|
-
cwd:
|
|
864
|
-
command:
|
|
1008
|
+
cwd: cargoCwd,
|
|
1009
|
+
command: `${napiCommand} build --platform --no-js --dts ${config.binaryName}.napi.d.ts ${commonCommand} --output-dir ${outputPath('.cache/native-debug')}`,
|
|
865
1010
|
...(hostCompilerEnv ? { env: hostCompilerEnv } : {}),
|
|
866
1011
|
},
|
|
867
1012
|
};
|
|
@@ -874,11 +1019,11 @@ function createNapiTargets(projectRoot, config, hostPlatform) {
|
|
|
874
1019
|
executor: 'nx:run-commands',
|
|
875
1020
|
cache: true,
|
|
876
1021
|
dependsOn: ['^build'],
|
|
877
|
-
inputs:
|
|
1022
|
+
inputs: cargoInputs,
|
|
878
1023
|
outputs: ['{projectRoot}/dist/native/host'],
|
|
879
1024
|
options: {
|
|
880
|
-
cwd:
|
|
881
|
-
command:
|
|
1025
|
+
cwd: cargoCwd,
|
|
1026
|
+
command: `${napiCommand} build --release --platform --no-js --dts ${config.binaryName}.napi.d.ts ${commonCommand} --output-dir ${outputPath('dist/native/host')}`,
|
|
882
1027
|
...(hostCompilerEnv ? { env: hostCompilerEnv } : {}),
|
|
883
1028
|
},
|
|
884
1029
|
};
|
|
@@ -916,11 +1061,11 @@ function createNapiTargets(projectRoot, config, hostPlatform) {
|
|
|
916
1061
|
executor: 'nx:run-commands',
|
|
917
1062
|
cache: true,
|
|
918
1063
|
...(useNapiCross ? { dependsOn: [napiToolchainTargetName(convention)] } : {}),
|
|
919
|
-
inputs:
|
|
1064
|
+
inputs: cargoInputs,
|
|
920
1065
|
outputs: [`{projectRoot}/${outputDirectory}`],
|
|
921
1066
|
options: {
|
|
922
|
-
cwd:
|
|
923
|
-
command:
|
|
1067
|
+
cwd: cargoCwd,
|
|
1068
|
+
command: `${napiCommand} build --release --platform --no-js --dts ${config.binaryName}.${convention.outputName}.d.ts --target ${triple}${crossFlag} ${commonCommand} --output-dir ${outputPath(outputDirectory)}`,
|
|
924
1069
|
// Genuine Linux cross-compiles use Clang plus napi-rs's downloaded
|
|
925
1070
|
// GNU sysroot. A native Linux target uses Nix's cc wrapper so C build
|
|
926
1071
|
// scripts resolve the host libc headers instead.
|
|
@@ -963,6 +1108,7 @@ async function readPackageJson(packageJsonPath) {
|
|
|
963
1108
|
}
|
|
964
1109
|
const rawNx = isRecord(parsed.nx) ? parsed.nx : undefined;
|
|
965
1110
|
return {
|
|
1111
|
+
...('bin' in parsed ? { bin: parsed.bin } : {}),
|
|
966
1112
|
...(typeof parsed.name === 'string' ? { name: parsed.name } : {}),
|
|
967
1113
|
...(isRecord(parsed.napi) ? { napi: parsed.napi } : {}),
|
|
968
1114
|
...(isRecord(parsed.scripts) ? { scripts: parsed.scripts } : {}),
|
|
@@ -1025,3 +1171,114 @@ function classifyPackageLocalBuildOutputs(packageJson) {
|
|
|
1025
1171
|
platform: targetNames.some((targetName) => PLATFORM_TARGET_GLOBS.some((glob) => targetName.endsWith(glob.slice(1)))),
|
|
1026
1172
|
};
|
|
1027
1173
|
}
|
|
1174
|
+
function cargoTargetDependency(currentProjectName, target) {
|
|
1175
|
+
return target.projectName === currentProjectName
|
|
1176
|
+
? target.targetName
|
|
1177
|
+
: { projects: [target.projectName], target: target.targetName };
|
|
1178
|
+
}
|
|
1179
|
+
async function resolveRepoRootCargoWorkspace(packageJsonPaths, workspaceRoot) {
|
|
1180
|
+
const cargoTomlPath = join(workspaceRoot, 'Cargo.toml');
|
|
1181
|
+
if (!existsSync(cargoTomlPath) || !CARGO_WORKSPACE_PATTERN.test(await readFile(cargoTomlPath, 'utf-8'))) {
|
|
1182
|
+
return null;
|
|
1183
|
+
}
|
|
1184
|
+
const projects = [];
|
|
1185
|
+
for (const packageJsonPath of packageJsonPaths) {
|
|
1186
|
+
if (isManagedPackageJsonSource(packageJsonPath) || isBuildOutputPackageJson(packageJsonPath)) {
|
|
1187
|
+
continue;
|
|
1188
|
+
}
|
|
1189
|
+
try {
|
|
1190
|
+
const packageJson = await readPackageJson(join(workspaceRoot, packageJsonPath));
|
|
1191
|
+
const name = packageJson.nx?.name ?? packageJson.name;
|
|
1192
|
+
if (typeof name === 'string' && name.length > 0) {
|
|
1193
|
+
projects.push({ name, root: dirname(packageJsonPath) });
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
catch {
|
|
1197
|
+
// createProjectTargets records the path-specific parse failure below.
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
const rootProject = projects.find((project) => project.root === '.');
|
|
1201
|
+
if (!rootProject) {
|
|
1202
|
+
return null;
|
|
1203
|
+
}
|
|
1204
|
+
const attributed = attributeCargoWorkspacePackages(listCargoWorkspacePackages(workspaceRoot), projects);
|
|
1205
|
+
const packages = [];
|
|
1206
|
+
let previous = {
|
|
1207
|
+
projectName: rootProject.name,
|
|
1208
|
+
targetName: CARGO_TEST_COMPILE_TARGET,
|
|
1209
|
+
};
|
|
1210
|
+
const exceptional = exceptionalTestFilter(PLUGIN_NEXTEST_CONFIG);
|
|
1211
|
+
for (const pkg of attributed) {
|
|
1212
|
+
const pieces = [];
|
|
1213
|
+
const sharded = pkg.testShards > 1;
|
|
1214
|
+
const pin = sharded && exceptional !== null ? exceptional : null;
|
|
1215
|
+
const shardable = pin === null ? `package(${pkg.name})` : `package(${pkg.name}) and not (${pin})`;
|
|
1216
|
+
for (let index = 1; index <= pkg.testShards; index += 1) {
|
|
1217
|
+
const targetName = cargoTestPackageTargetName(pkg.name, sharded ? `shard${index}` : undefined);
|
|
1218
|
+
pieces.push({
|
|
1219
|
+
extra: sharded ? ` --partition hash:${index}/${pkg.testShards}` : '',
|
|
1220
|
+
previous,
|
|
1221
|
+
selector: shardable,
|
|
1222
|
+
targetName,
|
|
1223
|
+
});
|
|
1224
|
+
previous = { projectName: pkg.projectName, targetName };
|
|
1225
|
+
}
|
|
1226
|
+
if (pin !== null) {
|
|
1227
|
+
const targetName = cargoTestPackageTargetName(pkg.name, CARGO_TEST_EXCEPTIONS_SUFFIX);
|
|
1228
|
+
pieces.push({
|
|
1229
|
+
extra: '',
|
|
1230
|
+
previous,
|
|
1231
|
+
selector: `package(${pkg.name}) and (${pin})`,
|
|
1232
|
+
targetName,
|
|
1233
|
+
});
|
|
1234
|
+
previous = { projectName: pkg.projectName, targetName };
|
|
1235
|
+
}
|
|
1236
|
+
packages.push({ package: pkg, pieces });
|
|
1237
|
+
}
|
|
1238
|
+
return { packages, rootProjectName: rootProject.name };
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Root mode keeps `--workspace` and adds `-p` only to make the owning crate
|
|
1242
|
+
* explicit in the command Nx attributes to its project. Cargo treats
|
|
1243
|
+
* `--workspace -p X` as the full workspace selection, so `-p` is inert for
|
|
1244
|
+
* feature resolution; the nextest filterset is what narrows the tests that run.
|
|
1245
|
+
* Never drop `--workspace`: `-p X` alone re-resolves features for one package
|
|
1246
|
+
* and defeats the compile target's shared artifacts.
|
|
1247
|
+
*/
|
|
1248
|
+
async function addRepoRootCargoTestTargets(targets, currentProjectName, projectRoot, workspaceRoot, workspace) {
|
|
1249
|
+
const targetNames = [];
|
|
1250
|
+
const configFile = nextestConfigRelPath(workspaceRoot, '.', PLUGIN_NEXTEST_CONFIG);
|
|
1251
|
+
for (const plan of workspace.packages) {
|
|
1252
|
+
if (plan.package.projectRoot !== projectRoot) {
|
|
1253
|
+
continue;
|
|
1254
|
+
}
|
|
1255
|
+
const inputs = await cargoPackageTestInputs(workspaceRoot, plan.package.dir, '{workspaceRoot}');
|
|
1256
|
+
for (const piece of plan.pieces) {
|
|
1257
|
+
targetNames.push(piece.targetName);
|
|
1258
|
+
targets[piece.targetName] = {
|
|
1259
|
+
executor: '@smoothbricks/nx-plugin:bounded-exec',
|
|
1260
|
+
cache: true,
|
|
1261
|
+
inputs,
|
|
1262
|
+
dependsOn: [cargoTargetDependency(currentProjectName, piece.previous)],
|
|
1263
|
+
options: {
|
|
1264
|
+
command: cargoFrozen(`nextest run --workspace -p ${plan.package.name} -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none --config-file ${configFile}`),
|
|
1265
|
+
cwd: '.',
|
|
1266
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
1267
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
1268
|
+
},
|
|
1269
|
+
};
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
return targetNames;
|
|
1273
|
+
}
|
|
1274
|
+
function hasCargoTargetDependency(dependencies, expected) {
|
|
1275
|
+
if (typeof expected === 'string') {
|
|
1276
|
+
return dependencies.includes(expected);
|
|
1277
|
+
}
|
|
1278
|
+
return dependencies.some((dependency) => typeof dependency !== 'string' &&
|
|
1279
|
+
dependency.target === expected.target &&
|
|
1280
|
+
Array.isArray(dependency.projects) &&
|
|
1281
|
+
Array.isArray(expected.projects) &&
|
|
1282
|
+
dependency.projects.length === expected.projects.length &&
|
|
1283
|
+
dependency.projects.every((project, index) => project === expected.projects?.[index]));
|
|
1284
|
+
}
|