@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
|
@@ -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,EAIL,KAAK,aAAa,EACnB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAChG,YAAY,EAAE,aAAa,EAAE,CAAC;AAE9B,MAAM,WAAW,sBAAsB;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC7B,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,kBAAkB,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;IAC5D,eAAe,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9C,aAAa,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACvE,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;AA0HD,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI,CAGhF;AA6LD,wBAAgB,UAAU,CACxB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,OAAO,EACnB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,MAAM,CAMR;
|
|
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,EAIL,KAAK,aAAa,EACnB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAChG,YAAY,EAAE,aAAa,EAAE,CAAC;AAE9B,MAAM,WAAW,sBAAsB;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC7B,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,kBAAkB,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;IAC5D,eAAe,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9C,aAAa,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACvE,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;AA0HD,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,GAAG,IAAI,CAGhF;AA6LD,wBAAgB,UAAU,CACxB,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,OAAO,EACnB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,GACtB,MAAM,CAMR;AAgoBD;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,GAAE,0BAA+B,GAAG,aAAa,EAAE,CAsBlH;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,GAAE,0BAA+B,GAAG,OAAO,CA8B1G;AAwJD,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,WAAW,EAAE,MAAM,EACnB,eAAe,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,EACrC,YAAY,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GACjC,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,CAAC;IAAC,YAAY,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CAAO,GAC1F,OAAO,CAMT;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,0BAA+B,GAAG,aAAa,EAAE,CAoBhH;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,0BAA+B,GAAG,OAAO,CA8BxG"}
|
|
@@ -551,7 +551,8 @@ function validateTargetDependencies(target, label, resolvedTargets, projectNames
|
|
|
551
551
|
if (typeof dependency !== 'string') {
|
|
552
552
|
continue;
|
|
553
553
|
}
|
|
554
|
-
|
|
554
|
+
// Nx expands "..." to the inferred target's existing dependencies before merging explicit entries.
|
|
555
|
+
if (dependency === '...' || isCrossProjectTargetDependency(dependency, projectNames)) {
|
|
555
556
|
continue;
|
|
556
557
|
}
|
|
557
558
|
if (dependency.includes(':')) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smoothbricks/nx-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./loader.js",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
57
|
"bin": {
|
|
58
|
-
"
|
|
58
|
+
"smoo-nx-exec": "./dist/bin/smoo-nx-exec.js"
|
|
59
59
|
},
|
|
60
60
|
"generators": "./generators.json",
|
|
61
61
|
"executors": "./executors.json",
|
|
@@ -100,8 +100,8 @@
|
|
|
100
100
|
"directory": "packages/nx-plugin"
|
|
101
101
|
},
|
|
102
102
|
"dependencies": {
|
|
103
|
-
"@nx/js": "
|
|
104
|
-
"nx": "
|
|
103
|
+
"@nx/js": "latest",
|
|
104
|
+
"nx": "latest",
|
|
105
105
|
"smol-toml": "^1.6.1",
|
|
106
106
|
"tslib": "^2.8.1"
|
|
107
107
|
}
|
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { existsSync } from 'node:fs';
|
|
3
3
|
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
import { inspect } from 'node:util';
|
|
4
5
|
|
|
5
6
|
import { describeMiss, ensureBuilt, parseTargetSelector } from '../ensure-built.js';
|
|
6
7
|
|
|
7
|
-
const USAGE =
|
|
8
|
-
'usage: smoothbricks-ensure-built <project:target[:configuration]> [--workspace-root <dir>] -- <binary> [args...]';
|
|
8
|
+
const USAGE = 'usage: smoo-nx-exec <project:target[:configuration]> [--workspace-root <dir>] -- <binary> [args...]';
|
|
9
9
|
|
|
10
10
|
/** Argument and environment problems all exit 2, the shell's "usage" code. */
|
|
11
11
|
function usageError(message: string): never {
|
|
12
|
-
process.stderr.write(`
|
|
12
|
+
process.stderr.write(`smoo-nx-exec: ${message}\n${USAGE}\n`);
|
|
13
13
|
process.exit(2);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
function describeError(error: unknown): string {
|
|
17
|
+
if (error instanceof Error) {
|
|
18
|
+
return error.message;
|
|
19
|
+
}
|
|
20
|
+
if (typeof error === 'object' && error !== null && 'message' in error && typeof error.message === 'string') {
|
|
21
|
+
return error.message;
|
|
22
|
+
}
|
|
23
|
+
return typeof error === 'string'
|
|
24
|
+
? error
|
|
25
|
+
: inspect(error, { breakLength: Number.POSITIVE_INFINITY, colors: false, depth: 5 });
|
|
26
|
+
}
|
|
27
|
+
|
|
16
28
|
/**
|
|
17
29
|
* Split `--flag=value` into two tokens so both spellings parse identically.
|
|
18
30
|
* Only leading `--` forms are split: a value may legitimately contain `=`.
|
|
@@ -90,12 +102,12 @@ const workspaceRoot =
|
|
|
90
102
|
workspaceRootArg === undefined ? findWorkspaceRoot(process.cwd()) : resolve(process.cwd(), workspaceRootArg);
|
|
91
103
|
|
|
92
104
|
const result = await ensureBuilt({ target, cwd: workspaceRoot }).catch((error: unknown) => {
|
|
93
|
-
process.stderr.write(`
|
|
105
|
+
process.stderr.write(`smoo-nx-exec: ${describeError(error)}\n`);
|
|
94
106
|
process.exit(1);
|
|
95
107
|
});
|
|
96
108
|
|
|
97
109
|
if (result.disposition !== 'hit' && process.env.NX_VERBOSE_LOGGING === 'true') {
|
|
98
|
-
process.stderr.write(`
|
|
110
|
+
process.stderr.write(`smoo-nx-exec: ran ${target} because ${describeMiss(result.reason)}\n`);
|
|
99
111
|
}
|
|
100
112
|
if (result.disposition === 'failed') {
|
|
101
113
|
if (result.signal !== null) {
|
|
@@ -107,7 +119,7 @@ if (result.disposition === 'failed') {
|
|
|
107
119
|
}
|
|
108
120
|
|
|
109
121
|
if (process.execve === undefined) {
|
|
110
|
-
throw new Error('
|
|
122
|
+
throw new Error('smoo-nx-exec needs process.execve, which requires a POSIX host on Node 24+ or Bun');
|
|
111
123
|
}
|
|
112
124
|
// `execve`, not spawn-and-wait: the built binary replaces this process, so it
|
|
113
125
|
// owns the terminal, the signals and the exit status directly, with no wrapper
|
|
@@ -159,6 +159,27 @@ describe('bounded test target policy', () => {
|
|
|
159
159
|
expect(checkBoundedTestTargetPolicy(packageJson, { projectName: 'example', projectJson })).toBe(false);
|
|
160
160
|
});
|
|
161
161
|
|
|
162
|
+
it('accepts custom positive timeouts for bounded execution targets', () => {
|
|
163
|
+
const packageJson: BoundedTestPolicyPackageJson = {
|
|
164
|
+
scripts: { test: boundedTestScriptAlias('example') },
|
|
165
|
+
nx: {
|
|
166
|
+
targets: {
|
|
167
|
+
test: {
|
|
168
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
169
|
+
options: {
|
|
170
|
+
command: 'bun x vitest run',
|
|
171
|
+
cwd: '{projectRoot}',
|
|
172
|
+
timeoutMs: 300_000,
|
|
173
|
+
killAfterMs: 15_000,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
expect(checkBoundedTestTargetPolicy(packageJson, { projectName: 'example' })).toBe(true);
|
|
181
|
+
});
|
|
182
|
+
|
|
162
183
|
it('accepts an executorless aggregate whose resolved execution target is bounded', () => {
|
|
163
184
|
const packageJson: BoundedTestPolicyPackageJson = {
|
|
164
185
|
nx: { targets: { test: { dependsOn: ['napi-test'] } } },
|
|
@@ -385,6 +385,10 @@ function isNoopAggregateTarget(target: Record<string, unknown>): boolean {
|
|
|
385
385
|
return Array.isArray(target.dependsOn) && target.dependsOn.length > 0;
|
|
386
386
|
}
|
|
387
387
|
|
|
388
|
+
function isPositiveSafeInteger(value: unknown): value is number {
|
|
389
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
|
|
390
|
+
}
|
|
391
|
+
|
|
388
392
|
function isBoundedExecutionTarget(executor: unknown, rawOptions: unknown, allowedCwds: ReadonlySet<string>): boolean {
|
|
389
393
|
if (executor !== BOUNDED_TEST_EXECUTOR || !isRecord(rawOptions)) {
|
|
390
394
|
return false;
|
|
@@ -397,8 +401,8 @@ function isBoundedExecutionTarget(executor: unknown, rawOptions: unknown, allowe
|
|
|
397
401
|
command === ensureBunTestTimeoutFlag(command) &&
|
|
398
402
|
typeof rawOptions.cwd === 'string' &&
|
|
399
403
|
allowedCwds.has(rawOptions.cwd) &&
|
|
400
|
-
rawOptions.timeoutMs
|
|
401
|
-
rawOptions.killAfterMs
|
|
404
|
+
isPositiveSafeInteger(rawOptions.timeoutMs) &&
|
|
405
|
+
isPositiveSafeInteger(rawOptions.killAfterMs)
|
|
402
406
|
);
|
|
403
407
|
}
|
|
404
408
|
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
attributeCargoWorkspacePackages,
|
|
8
|
+
cargoPackageTestInputs,
|
|
9
|
+
listCargoWorkspacePackages,
|
|
10
|
+
} from './cargo-workspace.js';
|
|
11
|
+
|
|
12
|
+
describe('Cargo workspace layouts', () => {
|
|
13
|
+
it('keeps package-rooted workspace discovery and inputs project-relative', async () => {
|
|
14
|
+
const root = await mkdtemp(join(tmpdir(), 'nx-plugin-cargo-package-root-'));
|
|
15
|
+
try {
|
|
16
|
+
await write(root, 'Cargo.toml', '[workspace]\nmembers = ["crates/ferris-core"]\n');
|
|
17
|
+
await write(root, 'Cargo.lock', 'version = 4\n');
|
|
18
|
+
await write(root, 'crates/ferris-core/Cargo.toml', '[package]\nname = "ferris-core"\n');
|
|
19
|
+
|
|
20
|
+
expect(listCargoWorkspacePackages(root)).toEqual([
|
|
21
|
+
{ name: 'ferris-core', dir: 'crates/ferris-core', testShards: 1 },
|
|
22
|
+
]);
|
|
23
|
+
expect(await cargoPackageTestInputs(root, 'crates/ferris-core')).toEqual([
|
|
24
|
+
'{projectRoot}/Cargo.toml',
|
|
25
|
+
'{projectRoot}/Cargo.lock',
|
|
26
|
+
'{projectRoot}/crates/ferris-core/**/*.rs',
|
|
27
|
+
'{projectRoot}/crates/ferris-core/Cargo.toml',
|
|
28
|
+
'{projectRoot}/**/.cargo/config.toml',
|
|
29
|
+
'{projectRoot}/scripts/*.sh',
|
|
30
|
+
'!{projectRoot}/**/target/**',
|
|
31
|
+
]);
|
|
32
|
+
} finally {
|
|
33
|
+
await rm(root, { recursive: true, force: true });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('expands repo-root member globs and attributes every crate to its containing Nx project', async () => {
|
|
38
|
+
const root = await mkdtemp(join(tmpdir(), 'nx-plugin-cargo-repo-root-'));
|
|
39
|
+
try {
|
|
40
|
+
await write(
|
|
41
|
+
root,
|
|
42
|
+
'Cargo.toml',
|
|
43
|
+
[
|
|
44
|
+
'[workspace]',
|
|
45
|
+
'members = ["packages/containium", "packages/*/crates/*", "vendor/*/crates/*"]',
|
|
46
|
+
'exclude = ["packages/*/crates/excluded-*", "vendor/ignored"]',
|
|
47
|
+
'',
|
|
48
|
+
'[workspace.dependencies]',
|
|
49
|
+
'runtime-core = { path = "packages/runtime/crates/runtime-core" }',
|
|
50
|
+
'',
|
|
51
|
+
].join('\n'),
|
|
52
|
+
);
|
|
53
|
+
await write(root, 'Cargo.lock', 'version = 4\n');
|
|
54
|
+
await write(root, 'packages/containium/Cargo.toml', '[package]\nname = "containium-cli"\n');
|
|
55
|
+
await write(root, 'packages/runtime/crates/runtime-core/Cargo.toml', '[package]\nname = "runtime-core"\n');
|
|
56
|
+
await write(
|
|
57
|
+
root,
|
|
58
|
+
'packages/host/crates/host-runtime/Cargo.toml',
|
|
59
|
+
'[package]\nname = "host-runtime"\n\n[dependencies]\nruntime-core = { workspace = true }\n',
|
|
60
|
+
);
|
|
61
|
+
await write(root, 'packages/host/crates/excluded-fixture/Cargo.toml', '[package]\nname = "excluded-fixture"\n');
|
|
62
|
+
await write(
|
|
63
|
+
root,
|
|
64
|
+
'vendor/ignored/crates/excluded-by-parent/Cargo.toml',
|
|
65
|
+
'[package]\nname = "excluded-by-parent"\n',
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const packages = listCargoWorkspacePackages(root);
|
|
69
|
+
expect(packages).toEqual([
|
|
70
|
+
{ name: 'containium-cli', dir: 'packages/containium', testShards: 1 },
|
|
71
|
+
{ name: 'host-runtime', dir: 'packages/host/crates/host-runtime', testShards: 1 },
|
|
72
|
+
{ name: 'runtime-core', dir: 'packages/runtime/crates/runtime-core', testShards: 1 },
|
|
73
|
+
]);
|
|
74
|
+
expect(
|
|
75
|
+
attributeCargoWorkspacePackages(packages, [
|
|
76
|
+
{ name: '@axe.sc/codebase', root: '.' },
|
|
77
|
+
{ name: 'runtime', root: 'packages/runtime' },
|
|
78
|
+
{ name: 'host', root: 'packages/host' },
|
|
79
|
+
{ name: 'containium', root: 'packages/containium' },
|
|
80
|
+
]),
|
|
81
|
+
).toEqual([
|
|
82
|
+
{
|
|
83
|
+
name: 'containium-cli',
|
|
84
|
+
dir: 'packages/containium',
|
|
85
|
+
testShards: 1,
|
|
86
|
+
projectName: 'containium',
|
|
87
|
+
projectRoot: 'packages/containium',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: 'host-runtime',
|
|
91
|
+
dir: 'packages/host/crates/host-runtime',
|
|
92
|
+
testShards: 1,
|
|
93
|
+
projectName: 'host',
|
|
94
|
+
projectRoot: 'packages/host',
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: 'runtime-core',
|
|
98
|
+
dir: 'packages/runtime/crates/runtime-core',
|
|
99
|
+
testShards: 1,
|
|
100
|
+
projectName: 'runtime',
|
|
101
|
+
projectRoot: 'packages/runtime',
|
|
102
|
+
},
|
|
103
|
+
]);
|
|
104
|
+
expect(await cargoPackageTestInputs(root, 'packages/host/crates/host-runtime', '{workspaceRoot}')).toEqual([
|
|
105
|
+
'{workspaceRoot}/Cargo.toml',
|
|
106
|
+
'{workspaceRoot}/Cargo.lock',
|
|
107
|
+
'{workspaceRoot}/packages/host/crates/host-runtime/**/*.rs',
|
|
108
|
+
'{workspaceRoot}/packages/host/crates/host-runtime/Cargo.toml',
|
|
109
|
+
'{workspaceRoot}/packages/runtime/crates/runtime-core/**/*.rs',
|
|
110
|
+
'{workspaceRoot}/packages/runtime/crates/runtime-core/Cargo.toml',
|
|
111
|
+
'{workspaceRoot}/**/.cargo/config.toml',
|
|
112
|
+
'{workspaceRoot}/scripts/*.sh',
|
|
113
|
+
'!{workspaceRoot}/**/target/**',
|
|
114
|
+
]);
|
|
115
|
+
} finally {
|
|
116
|
+
await rm(root, { recursive: true, force: true });
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('reports member patterns it cannot expand instead of silently omitting crates', async () => {
|
|
121
|
+
const root = await mkdtemp(join(tmpdir(), 'nx-plugin-cargo-invalid-glob-'));
|
|
122
|
+
try {
|
|
123
|
+
await write(root, 'Cargo.toml', '[workspace]\nmembers = ["packages/**/crates/*"]\n');
|
|
124
|
+
expect(() => listCargoWorkspacePackages(root)).toThrow(
|
|
125
|
+
'Cargo workspace member uses an unsupported glob pattern: packages/**/crates/*',
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
await write(root, 'Cargo.toml', '[workspace]\nmembers = ["packages/*/crates/*"]\n');
|
|
129
|
+
expect(() => listCargoWorkspacePackages(root)).toThrow(
|
|
130
|
+
'Cargo workspace member glob matched no directories: packages/*/crates/*',
|
|
131
|
+
);
|
|
132
|
+
} finally {
|
|
133
|
+
await rm(root, { recursive: true, force: true });
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
async function write(root: string, path: string, contents: string): Promise<void> {
|
|
139
|
+
const absolutePath = join(root, path);
|
|
140
|
+
await mkdir(dirname(absolutePath), { recursive: true });
|
|
141
|
+
await writeFile(absolutePath, contents);
|
|
142
|
+
}
|
package/src/cargo-workspace.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
2
2
|
import { readFile } from 'node:fs/promises';
|
|
3
3
|
import { join, normalize, posix, relative, sep } from 'node:path';
|
|
4
4
|
import { parse as parseToml } from 'smol-toml';
|
|
@@ -13,6 +13,16 @@ export interface CargoWorkspacePackage {
|
|
|
13
13
|
testShards: number;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export interface CargoWorkspaceProject {
|
|
17
|
+
name: string;
|
|
18
|
+
root: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AttributedCargoWorkspacePackage extends CargoWorkspacePackage {
|
|
22
|
+
projectName: string;
|
|
23
|
+
projectRoot: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
16
26
|
export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWorkspacePackage[] {
|
|
17
27
|
const workspaceTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
|
|
18
28
|
if (!existsSync(workspaceTomlPath)) {
|
|
@@ -22,12 +32,30 @@ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWo
|
|
|
22
32
|
if (!isRecord(parsed) || !isRecord(parsed.workspace) || !Array.isArray(parsed.workspace.members)) {
|
|
23
33
|
return [];
|
|
24
34
|
}
|
|
25
|
-
const
|
|
35
|
+
const excludedDirs = new Set<string>();
|
|
36
|
+
if (Array.isArray(parsed.workspace.exclude)) {
|
|
37
|
+
for (const excluded of parsed.workspace.exclude) {
|
|
38
|
+
if (typeof excluded === 'string' && excluded.length > 0) {
|
|
39
|
+
for (const excludedDir of expandCargoMemberDirs(absoluteProjectRoot, excluded, false)) {
|
|
40
|
+
excludedDirs.add(excludedDir);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const packageDirs = new Set<string>();
|
|
26
46
|
for (const member of parsed.workspace.members) {
|
|
27
47
|
if (typeof member !== 'string' || member.length === 0) {
|
|
28
48
|
continue;
|
|
29
49
|
}
|
|
30
|
-
const
|
|
50
|
+
for (const memberDir of expandCargoMemberDirs(absoluteProjectRoot, member)) {
|
|
51
|
+
if (!isExcludedCargoMember(memberDir, excludedDirs)) {
|
|
52
|
+
packageDirs.add(memberDir);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const packages: CargoWorkspacePackage[] = [];
|
|
57
|
+
for (const memberDir of packageDirs) {
|
|
58
|
+
const crateTomlPath = join(absoluteProjectRoot, memberDir, 'Cargo.toml');
|
|
31
59
|
if (!existsSync(crateTomlPath)) {
|
|
32
60
|
continue;
|
|
33
61
|
}
|
|
@@ -37,7 +65,7 @@ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWo
|
|
|
37
65
|
}
|
|
38
66
|
packages.push({
|
|
39
67
|
name: crateParsed.package.name,
|
|
40
|
-
dir:
|
|
68
|
+
dir: memberDir,
|
|
41
69
|
testShards: readTestShards(crateParsed.package, crateTomlPath),
|
|
42
70
|
});
|
|
43
71
|
}
|
|
@@ -45,7 +73,108 @@ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWo
|
|
|
45
73
|
return packages;
|
|
46
74
|
}
|
|
47
75
|
|
|
48
|
-
|
|
76
|
+
function isExcludedCargoMember(memberDir: string, excludedDirs: ReadonlySet<string>): boolean {
|
|
77
|
+
for (const excludedDir of excludedDirs) {
|
|
78
|
+
if (memberDir === excludedDir || memberDir.startsWith(`${excludedDir}/`)) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Cargo accepts glob patterns at any depth in `workspace.members` and
|
|
87
|
+
* `workspace.exclude`. Walk one path segment at a time so patterns with
|
|
88
|
+
* wildcards in multiple segments cannot silently erase every crate from the
|
|
89
|
+
* Nx graph.
|
|
90
|
+
*/
|
|
91
|
+
function expandCargoMemberDirs(absoluteProjectRoot: string, member: string, requireGlobMatch = true): string[] {
|
|
92
|
+
const normalized = member.split('\\').join('/').replace(/\/+$/, '');
|
|
93
|
+
const segments = normalized.split('/');
|
|
94
|
+
if (
|
|
95
|
+
normalized.length === 0 ||
|
|
96
|
+
normalized.startsWith('/') ||
|
|
97
|
+
segments.some((segment) => segment.length === 0 || segment === '.' || segment === '..')
|
|
98
|
+
) {
|
|
99
|
+
throw new Error(`Cargo workspace member must be a non-empty relative path: ${member}`);
|
|
100
|
+
}
|
|
101
|
+
if (segments.some((segment) => segment === '**' || /[[\]{}!]/.test(segment))) {
|
|
102
|
+
throw new Error(`Cargo workspace member uses an unsupported glob pattern: ${member}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const hasGlob = segments.some((segment) => segment.includes('*') || segment.includes('?'));
|
|
106
|
+
let candidates = [''];
|
|
107
|
+
for (const segment of segments) {
|
|
108
|
+
if (!segment.includes('*') && !segment.includes('?')) {
|
|
109
|
+
candidates = candidates.map((candidate) => posix.join(candidate, segment));
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const matcher = cargoGlobSegmentMatcher(segment);
|
|
113
|
+
const expanded: string[] = [];
|
|
114
|
+
for (const candidate of candidates) {
|
|
115
|
+
const absoluteParent = join(absoluteProjectRoot, candidate);
|
|
116
|
+
if (!existsSync(absoluteParent)) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
for (const entry of readdirSync(absoluteParent, { withFileTypes: true })) {
|
|
120
|
+
if (entry.isDirectory() && matcher.test(entry.name)) {
|
|
121
|
+
expanded.push(posix.join(candidate, entry.name));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
candidates = expanded;
|
|
126
|
+
}
|
|
127
|
+
if (requireGlobMatch && hasGlob && candidates.length === 0) {
|
|
128
|
+
throw new Error(`Cargo workspace member glob matched no directories: ${member}`);
|
|
129
|
+
}
|
|
130
|
+
return candidates.sort((left, right) => left.localeCompare(right));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function cargoGlobSegmentMatcher(segment: string): RegExp {
|
|
134
|
+
let source = '^';
|
|
135
|
+
for (const character of segment) {
|
|
136
|
+
if (character === '*') {
|
|
137
|
+
source += '.*';
|
|
138
|
+
} else if (character === '?') {
|
|
139
|
+
source += '.';
|
|
140
|
+
} else {
|
|
141
|
+
source += character.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return new RegExp(`${source}$`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Assign each workspace crate to the most specific Nx project that contains
|
|
149
|
+
* its directory. The repository root is the fallback owner; a nested package
|
|
150
|
+
* wins by path depth so projects under `packages/*` own their own crates.
|
|
151
|
+
*/
|
|
152
|
+
export function attributeCargoWorkspacePackages(
|
|
153
|
+
packages: readonly CargoWorkspacePackage[],
|
|
154
|
+
projects: readonly CargoWorkspaceProject[],
|
|
155
|
+
): AttributedCargoWorkspacePackage[] {
|
|
156
|
+
const bySpecificity = [...projects].sort((left, right) => {
|
|
157
|
+
const leftDepth = left.root === '.' ? 0 : left.root.split('/').length;
|
|
158
|
+
const rightDepth = right.root === '.' ? 0 : right.root.split('/').length;
|
|
159
|
+
return rightDepth - leftDepth || left.name.localeCompare(right.name);
|
|
160
|
+
});
|
|
161
|
+
const attributed: AttributedCargoWorkspacePackage[] = [];
|
|
162
|
+
for (const pkg of packages) {
|
|
163
|
+
const owner = bySpecificity.find(
|
|
164
|
+
(project) => project.root === '.' || pkg.dir === project.root || pkg.dir.startsWith(`${project.root}/`),
|
|
165
|
+
);
|
|
166
|
+
if (owner) {
|
|
167
|
+
attributed.push({ ...pkg, projectName: owner.name, projectRoot: owner.root });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return attributed;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function cargoPackageTestInputs(
|
|
174
|
+
absoluteProjectRoot: string,
|
|
175
|
+
memberDir: string,
|
|
176
|
+
inputRoot = '{projectRoot}',
|
|
177
|
+
): Promise<string[]> {
|
|
49
178
|
const workspacePathDeps = await workspacePathDependencies(absoluteProjectRoot);
|
|
50
179
|
const dirs = new Set<string>([memberDir.split('\\').join('/')]);
|
|
51
180
|
const crateParsed: unknown = parseToml(await readFile(join(absoluteProjectRoot, memberDir, 'Cargo.toml'), 'utf-8'));
|
|
@@ -63,11 +192,11 @@ export async function cargoPackageTestInputs(absoluteProjectRoot: string, member
|
|
|
63
192
|
}
|
|
64
193
|
}
|
|
65
194
|
}
|
|
66
|
-
const inputs: string[] = [
|
|
195
|
+
const inputs: string[] = [`${inputRoot}/Cargo.toml`, `${inputRoot}/Cargo.lock`];
|
|
67
196
|
for (const dir of [...dirs].sort()) {
|
|
68
|
-
inputs.push(
|
|
197
|
+
inputs.push(`${inputRoot}/${dir}/**/*.rs`, `${inputRoot}/${dir}/Cargo.toml`);
|
|
69
198
|
}
|
|
70
|
-
inputs.push(
|
|
199
|
+
inputs.push(`${inputRoot}/**/.cargo/config.toml`, `${inputRoot}/scripts/*.sh`, `!${inputRoot}/**/target/**`);
|
|
71
200
|
return inputs;
|
|
72
201
|
}
|
|
73
202
|
|