@smoothbricks/nx-plugin 0.3.13 → 0.4.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/README.md +61 -10
- package/dist/bin/smoo-nx-exec.d.ts +3 -0
- package/dist/bin/smoo-nx-exec.d.ts.map +1 -0
- package/dist/bin/smoo-nx-exec.js +117 -0
- 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 +136 -0
- package/dist/ensure-built.d.ts.map +1 -0
- package/dist/ensure-built.js +419 -0
- 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 +335 -87
- package/package.json +11 -3
- package/src/bin/smoo-nx-exec.ts +127 -0
- 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 +543 -0
- package/src/ensure-built.ts +602 -0
- 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 +173 -6
- package/src/index.ts +426 -89
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smoothbricks/nx-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./loader.js",
|
|
@@ -43,12 +43,20 @@
|
|
|
43
43
|
"import": "./dist/typecheck-test-policy.js",
|
|
44
44
|
"default": "./dist/typecheck-test-policy.js"
|
|
45
45
|
},
|
|
46
|
+
"./ensure-built": {
|
|
47
|
+
"types": "./dist/ensure-built.d.ts",
|
|
48
|
+
"import": "./dist/ensure-built.js",
|
|
49
|
+
"default": "./dist/ensure-built.js"
|
|
50
|
+
},
|
|
46
51
|
"./cross-check-policy": {
|
|
47
52
|
"types": "./dist/cross-check-policy.d.ts",
|
|
48
53
|
"import": "./dist/cross-check-policy.js",
|
|
49
54
|
"default": "./dist/cross-check-policy.js"
|
|
50
55
|
}
|
|
51
56
|
},
|
|
57
|
+
"bin": {
|
|
58
|
+
"smoo-nx-exec": "./dist/bin/smoo-nx-exec.js"
|
|
59
|
+
},
|
|
52
60
|
"generators": "./generators.json",
|
|
53
61
|
"executors": "./executors.json",
|
|
54
62
|
"scripts": {
|
|
@@ -92,8 +100,8 @@
|
|
|
92
100
|
"directory": "packages/nx-plugin"
|
|
93
101
|
},
|
|
94
102
|
"dependencies": {
|
|
95
|
-
"@nx/js": "
|
|
96
|
-
"nx": "
|
|
103
|
+
"@nx/js": "latest",
|
|
104
|
+
"nx": "latest",
|
|
97
105
|
"smol-toml": "^1.6.1",
|
|
98
106
|
"tslib": "^2.8.1"
|
|
99
107
|
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
import { inspect } from 'node:util';
|
|
5
|
+
|
|
6
|
+
import { describeMiss, ensureBuilt, parseTargetSelector } from '../ensure-built.js';
|
|
7
|
+
|
|
8
|
+
const USAGE = 'usage: smoo-nx-exec <project:target[:configuration]> [--workspace-root <dir>] -- <binary> [args...]';
|
|
9
|
+
|
|
10
|
+
/** Argument and environment problems all exit 2, the shell's "usage" code. */
|
|
11
|
+
function usageError(message: string): never {
|
|
12
|
+
process.stderr.write(`smoo-nx-exec: ${message}\n${USAGE}\n`);
|
|
13
|
+
process.exit(2);
|
|
14
|
+
}
|
|
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
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Split `--flag=value` into two tokens so both spellings parse identically.
|
|
30
|
+
* Only leading `--` forms are split: a value may legitimately contain `=`.
|
|
31
|
+
*/
|
|
32
|
+
function tokenize(args: readonly string[]): string[] {
|
|
33
|
+
const tokens: string[] = [];
|
|
34
|
+
for (const arg of args) {
|
|
35
|
+
const equals = arg.startsWith('--') ? arg.indexOf('=') : -1;
|
|
36
|
+
if (equals === -1) {
|
|
37
|
+
tokens.push(arg);
|
|
38
|
+
} else {
|
|
39
|
+
tokens.push(arg.slice(0, equals), arg.slice(equals + 1));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return tokens;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function findWorkspaceRoot(from: string): string {
|
|
46
|
+
let directory = from;
|
|
47
|
+
for (;;) {
|
|
48
|
+
if (existsSync(join(directory, 'nx.json'))) {
|
|
49
|
+
return directory;
|
|
50
|
+
}
|
|
51
|
+
const parent = dirname(directory);
|
|
52
|
+
if (parent === directory) {
|
|
53
|
+
return usageError(`no nx.json at or above ${from}; pass --workspace-root`);
|
|
54
|
+
}
|
|
55
|
+
directory = parent;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const argv = process.argv.slice(2);
|
|
60
|
+
const separator = argv.indexOf('--');
|
|
61
|
+
if (separator === -1) {
|
|
62
|
+
usageError('missing `--` before the binary to exec');
|
|
63
|
+
}
|
|
64
|
+
const command = argv.slice(separator + 1);
|
|
65
|
+
if (command.length === 0) {
|
|
66
|
+
usageError('no binary given after `--`');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let target: string | undefined;
|
|
70
|
+
let workspaceRootArg: string | undefined;
|
|
71
|
+
const tokens = tokenize(argv.slice(0, separator));
|
|
72
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
73
|
+
const token = tokens[index];
|
|
74
|
+
if (token === '--workspace-root') {
|
|
75
|
+
index += 1;
|
|
76
|
+
workspaceRootArg = tokens[index] ?? usageError('--workspace-root needs a directory');
|
|
77
|
+
} else if (token.startsWith('-')) {
|
|
78
|
+
usageError(`unknown flag ${token}`);
|
|
79
|
+
} else if (target !== undefined) {
|
|
80
|
+
usageError(`more than one target given: ${target} and ${token}`);
|
|
81
|
+
} else {
|
|
82
|
+
target = token;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (target === undefined) {
|
|
86
|
+
usageError('no project:target given');
|
|
87
|
+
}
|
|
88
|
+
if (parseTargetSelector(target) === null) {
|
|
89
|
+
usageError(`'${target}' is not a project:target[:configuration] selector`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// `execve` does no PATH lookup, so a bare name would fail as a missing file in
|
|
93
|
+
// the current directory. Say which mistake was made instead.
|
|
94
|
+
if (!command[0].includes('/')) {
|
|
95
|
+
usageError(`'${command[0]}' must be a path to the binary, not a name to look up on PATH`);
|
|
96
|
+
}
|
|
97
|
+
// Resolved against the directory the user is standing in, not the workspace
|
|
98
|
+
// root: this is their command line, and the exec'd process inherits their cwd.
|
|
99
|
+
const binary = isAbsolute(command[0]) ? command[0] : resolve(process.cwd(), command[0]);
|
|
100
|
+
|
|
101
|
+
const workspaceRoot =
|
|
102
|
+
workspaceRootArg === undefined ? findWorkspaceRoot(process.cwd()) : resolve(process.cwd(), workspaceRootArg);
|
|
103
|
+
|
|
104
|
+
const result = await ensureBuilt({ target, cwd: workspaceRoot }).catch((error: unknown) => {
|
|
105
|
+
process.stderr.write(`smoo-nx-exec: ${describeError(error)}\n`);
|
|
106
|
+
process.exit(1);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (result.disposition !== 'hit' && process.env.NX_VERBOSE_LOGGING === 'true') {
|
|
110
|
+
process.stderr.write(`smoo-nx-exec: ran ${target} because ${describeMiss(result.reason)}\n`);
|
|
111
|
+
}
|
|
112
|
+
if (result.disposition === 'failed') {
|
|
113
|
+
if (result.signal !== null) {
|
|
114
|
+
// Re-raise rather than translate, so a build killed by SIGINT leaves this
|
|
115
|
+
// process looking killed by SIGINT to whatever is watching it.
|
|
116
|
+
process.kill(process.pid, result.signal);
|
|
117
|
+
}
|
|
118
|
+
process.exit(result.exitCode);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (process.execve === undefined) {
|
|
122
|
+
throw new Error('smoo-nx-exec needs process.execve, which requires a POSIX host on Node 24+ or Bun');
|
|
123
|
+
}
|
|
124
|
+
// `execve`, not spawn-and-wait: the built binary replaces this process, so it
|
|
125
|
+
// owns the terminal, the signals and the exit status directly, with no wrapper
|
|
126
|
+
// left behind to forward them.
|
|
127
|
+
process.execve(binary, [binary, ...command.slice(1)], process.env);
|
|
@@ -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
|
|