@smoothbricks/nx-plugin 0.4.21 → 0.4.23
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 +95 -18
- package/dist/bin/smoo-nx-cargo-hash.js +32 -6
- package/dist/bin/smoo-nx-exec.js +35 -11
- package/dist/cargo-closure-input.d.ts +63 -0
- package/dist/cargo-closure-input.d.ts.map +1 -0
- package/dist/cargo-closure-input.js +197 -0
- package/dist/cargo-source-hash.d.ts +46 -2
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +238 -114
- package/dist/cross-check-policy.d.ts +14 -0
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +15 -1
- package/dist/ensure-built.d.ts +3 -2
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +33 -56
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +112 -76
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +12 -15
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +10 -0
- package/dist/managed-files/publish-workflow.d.ts.map +1 -1
- package/dist/managed-files/publish-workflow.js +12 -6
- package/dist/manifest-hash.d.ts.map +1 -1
- package/dist/manifest-hash.js +24 -30
- package/dist/package-target-policy.js +1 -1
- package/dist/platform-targets.d.ts +7 -0
- package/dist/platform-targets.d.ts.map +1 -1
- package/dist/platform-targets.js +7 -0
- package/dist/workspace-config-policy.d.ts +1 -1
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +1 -1
- package/managed/raw/envrc +38 -1
- package/managed/raw/tooling/direnv/developer-links.ts +201 -0
- package/managed/raw/tooling/direnv/devenv.smoo.nix +75 -51
- package/managed/raw/tooling/direnv/secret-references.ts +3 -1
- package/managed/raw/tooling/direnv/setup-environment.ts +562 -78
- package/managed/raw/tooling/direnv/shared-caches.sh +53 -0
- package/managed/raw/tooling/git-hooks/pre-push.sh +22 -1
- package/managed/templates/github/workflows/managed-files.yml +4 -3
- package/nextest.toml +5 -5
- package/package.json +2 -2
- package/src/__tests__/fixture-nx-env.ts +31 -0
- package/src/__tests__/in-process-nx-index.ts +9 -0
- package/src/bin/smoo-nx-cargo-hash.ts +29 -9
- package/src/bin/smoo-nx-exec.ts +40 -11
- package/src/cargo-closure-input.test.ts +184 -0
- package/src/cargo-closure-input.ts +247 -0
- package/src/cargo-source-hash.test.ts +104 -10
- package/src/cargo-source-hash.ts +290 -37
- package/src/cross-check-policy.ts +15 -3
- package/src/ensure-built.test.ts +65 -5
- package/src/ensure-built.ts +40 -85
- package/src/index.test.ts +94 -53
- package/src/index.ts +164 -84
- package/src/managed-files/ci-workflow.ts +12 -15
- package/src/managed-files/files.ts +10 -0
- package/src/managed-files/generator.test.ts +29 -0
- package/src/managed-files/publish-workflow.ts +17 -6
- package/src/manifest-hash.test.ts +21 -1
- package/src/manifest-hash.ts +26 -29
- package/src/package-target-policy.ts +1 -1
- package/src/platform-targets.ts +8 -0
- package/src/workspace-config-policy.ts +6 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Managed by `smoo monorepo`. Sourced by devenv.smoo.nix's enterShell prologue
|
|
2
|
+
# from the workspace root, naming the machine's shared caches root:
|
|
3
|
+
#
|
|
4
|
+
# . "$DEVENV_ROOT/shared-caches.sh" /private/cowshed/caches
|
|
5
|
+
#
|
|
6
|
+
# Where the Go and ttsc caches live. Every checkout on a cowshed host — the
|
|
7
|
+
# host's own and every sandboxed workspace — reaches them through the one
|
|
8
|
+
# literal path under the caches root. Keying the choice on $HOME split them:
|
|
9
|
+
# a sandbox's HOME is private to the workspace, so each sandbox fell back to a
|
|
10
|
+
# cold per-checkout cache while the host filled another one. Both Go caches
|
|
11
|
+
# are content-addressed, so sharing them needs no patch — unlike Rust, Go keys
|
|
12
|
+
# on content and flags rather than on where the files live. Without a caches
|
|
13
|
+
# root, ttsc stays in-tree for the CI cache action while Go keeps its normal
|
|
14
|
+
# per-user defaults. An inherited value always wins so CI can place any of
|
|
15
|
+
# these caches itself. GOFLAGS carries -trimpath because it is Go's stable way
|
|
16
|
+
# to keep absolute build paths out of the artifact, and unlike Rust's
|
|
17
|
+
# trim-paths it costs no cache reuse.
|
|
18
|
+
#
|
|
19
|
+
# ttsc's plugin builds use the Go it bundles rather than ours, so the
|
|
20
|
+
# repository's own Go work and ttsc's plugin builds are two toolchains by
|
|
21
|
+
# construction. Placing both caches in the shared root is what keeps that from
|
|
22
|
+
# costing a rebuild per workspace.
|
|
23
|
+
#
|
|
24
|
+
# TTSC_GO_CACHE_DIR and GOCACHE stay separate for ownership, not correctness:
|
|
25
|
+
# both caches are content-addressed, so one directory would compile the same
|
|
26
|
+
# bytes to the same entries. What sharing loses is an owner. ttsc reclaims a Go
|
|
27
|
+
# build cache only when it resolved that directory itself — `ttsc clean` takes
|
|
28
|
+
# the cache whose source is TTSC_GO_CACHE_DIR and never one whose source is a
|
|
29
|
+
# user GOCACHE, which it treats as someone else's property. Pointing ttsc at
|
|
30
|
+
# GOCACHE therefore produced a directory ttsc filled and no repository verb
|
|
31
|
+
# could empty, measured at 35G. A dedicated directory gives ttsc's half back to
|
|
32
|
+
# `ttsc clean` and leaves GOCACHE holding only Go work this repository does
|
|
33
|
+
# itself.
|
|
34
|
+
#
|
|
35
|
+
# That buys ownership, not automatic GC: ttsc prunes opportunistically only
|
|
36
|
+
# when it owns the whole cache root, which requires TTSC_CACHE_DIR unset, and a
|
|
37
|
+
# pinned shared root is the point of this file. The trade is deliberate — one
|
|
38
|
+
# warm cache every workspace shares, reclaimed by an explicit verb, over
|
|
39
|
+
# per-workspace caches that self-trim. Go's own build cache trimming belongs to
|
|
40
|
+
# the go command and still applies to both.
|
|
41
|
+
smoo_caches_root="$1"
|
|
42
|
+
if [ -d "$smoo_caches_root" ]; then
|
|
43
|
+
export TTSC_CACHE_DIR="${TTSC_CACHE_DIR:-$smoo_caches_root/ttsc}"
|
|
44
|
+
export GOCACHE="${GOCACHE:-$smoo_caches_root/go/build}"
|
|
45
|
+
export GOMODCACHE="${GOMODCACHE:-$smoo_caches_root/go/mod}"
|
|
46
|
+
mkdir -p "$GOCACHE" "$GOMODCACHE"
|
|
47
|
+
else
|
|
48
|
+
export TTSC_CACHE_DIR="${TTSC_CACHE_DIR:-$PWD/.cache/ttsc}"
|
|
49
|
+
fi
|
|
50
|
+
export TTSC_GO_CACHE_DIR="${TTSC_GO_CACHE_DIR:-$TTSC_CACHE_DIR/go-build}"
|
|
51
|
+
mkdir -p "$TTSC_CACHE_DIR" "$TTSC_GO_CACHE_DIR"
|
|
52
|
+
export GOFLAGS="${GOFLAGS:--trimpath}"
|
|
53
|
+
unset smoo_caches_root
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
#
|
|
2
|
+
# Two gates, in order.
|
|
3
|
+
#
|
|
4
|
+
# 1. On every OS, the public-tree denylist: every commit this push publishes
|
|
5
|
+
# is refused if its tree matches a pattern from this clone's local
|
|
6
|
+
# `smoothbricks.publicDenylist` git config (or SMOOTHBRICKS_PUBLIC_DENYLIST).
|
|
7
|
+
# The patterns live outside the repository so the guard never publishes what
|
|
8
|
+
# it guards; with none configured the check passes silently.
|
|
9
|
+
#
|
|
10
|
+
# 2. The macOS-only Linux cross-compile gate. It is a cache PROBE and nothing
|
|
3
11
|
# else: it reads the nx cache and never compiles. A hit means a real
|
|
4
12
|
# `cargo clippy --target x86_64-unknown-linux-gnu` already passed for exactly
|
|
5
13
|
# this tree, so the push is safe with no toolchain present. Anything else
|
|
@@ -32,6 +40,19 @@ if [ -z "${TTSC_TSGO_BINARY:-}" ] && [ -x "$PWD/node_modules/@typescript/native/
|
|
|
32
40
|
export TTSC_TSGO_BINARY="$PWD/node_modules/@typescript/native/bin/tsc"
|
|
33
41
|
fi
|
|
34
42
|
|
|
43
|
+
# git hands a pre-push hook one `<local ref> <local sha> <remote ref> <remote sha>`
|
|
44
|
+
# line per ref it updates; an all-zero local sha is a deletion, which publishes
|
|
45
|
+
# no tree.
|
|
46
|
+
pushed=()
|
|
47
|
+
while read -r _local_ref local_sha _remote_ref _remote_sha; do
|
|
48
|
+
case "$local_sha" in
|
|
49
|
+
*[!0]*) pushed+=("$local_sha") ;;
|
|
50
|
+
esac
|
|
51
|
+
done
|
|
52
|
+
if [ "${#pushed[@]}" -gt 0 ]; then
|
|
53
|
+
smoo monorepo check-public-denylist "${pushed[@]}" || exit 1
|
|
54
|
+
fi
|
|
55
|
+
|
|
35
56
|
case "$(uname -s)" in
|
|
36
57
|
Darwin) ;;
|
|
37
58
|
*) exit 0 ;;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# Managed by @smoothbricks/cli (smoo monorepo update) - do not edit by hand.
|
|
2
2
|
#
|
|
3
3
|
# Self-healing loop for smoo-managed files: ci.yml runs the authoritative
|
|
4
|
-
# `smoo monorepo check` on every
|
|
5
|
-
#
|
|
6
|
-
#
|
|
4
|
+
# `smoo monorepo check --warn` on every event (devenv is already up there, so
|
|
5
|
+
# the check is ~free; package policies fail the job, drift only warns) and, on
|
|
6
|
+
# a default-branch push, dispatches THIS workflow only when real drift exists -
|
|
7
|
+
# typically after a @smoothbricks/cli pin bump. It runs
|
|
7
8
|
# `smoo monorepo update` and pushes the result to ONE persistent review branch
|
|
8
9
|
# (chore/managed-files) with ONE PR. The PR is NEVER auto-merged: a template
|
|
9
10
|
# change is upstream code that deserves a human look before it lands. Re-runs
|
package/nextest.toml
CHANGED
|
@@ -48,9 +48,10 @@ slow-timeout = { period = "180s", terminate-after = 1 }
|
|
|
48
48
|
#
|
|
49
49
|
# That spec sets the house rule for real-substrate timing: "CI asserts with a 3×
|
|
50
50
|
# multiplier to absorb runner noise". `apfs_integration.rs` measured its lifecycle
|
|
51
|
-
# at 14.8s
|
|
52
|
-
# model CI
|
|
53
|
-
# construction: the test was not slow, the bound contradicted the spec.
|
|
51
|
+
# at 14.8s on an IDLE host when it still ran a second image format, so under the
|
|
52
|
+
# repo's own noise model CI needed 44.4s. A 30s deadline was therefore unreachable
|
|
53
|
+
# by construction: the test was not slow, the bound contradicted the spec. The
|
|
54
|
+
# one-format lifecycle measures 6.6-10.6s idle, so the bound below still covers it.
|
|
54
55
|
#
|
|
55
56
|
# Nor is the cost reducible. The same spec's "Attach floor verdict: not
|
|
56
57
|
# flag-reducible" records that `-noverify` saves ~15ms (noise), `-noautofsck`
|
|
@@ -63,8 +64,7 @@ slow-timeout = { period = "180s", terminate-after = 1 }
|
|
|
63
64
|
#
|
|
64
65
|
# The alternative was splitting the lifecycle into phase-scoped tests so each fits
|
|
65
66
|
# 30s. It loses: every split pays that attach constant again, so the suite's total
|
|
66
|
-
# wall time rises to buy a smaller per-test bound
|
|
67
|
-
# worth it because it bought failure ATTRIBUTION; splitting phases buys only a
|
|
67
|
+
# wall time rises to buy a smaller per-test bound; splitting phases buys only a
|
|
68
68
|
# number.
|
|
69
69
|
#
|
|
70
70
|
# 60s is 3× the worst idle measurement (44.4s) rounded up, not a guess. It stays
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smoothbricks/nx-plugin",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.23",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./loader.js",
|
|
@@ -196,7 +196,7 @@
|
|
|
196
196
|
"yaml": "^2.9.0"
|
|
197
197
|
},
|
|
198
198
|
"devDependencies": {
|
|
199
|
-
"@smoothbricks/lmao": "0.
|
|
199
|
+
"@smoothbricks/lmao": "0.5.1",
|
|
200
200
|
"@smoothbricks/validation": "0.1.10",
|
|
201
201
|
"fast-check": "^4.4.0"
|
|
202
202
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The environment of an `nx` a test runs against a fixture workspace.
|
|
5
|
+
*
|
|
6
|
+
* Every inherited `NX_*` variable is dropped first. Run as
|
|
7
|
+
* `nx run nx-plugin:test`, the test process is itself an Nx task, and what the
|
|
8
|
+
* outer Nx exported to it (NX_SKIP_NX_CACHE under `--skip-nx-cache`, the
|
|
9
|
+
* NX_TASK_* identity, output capture) would reconfigure the fixture's Nx: a
|
|
10
|
+
* skipped cache turns every second run into a miss.
|
|
11
|
+
*
|
|
12
|
+
* FORCE_COLOR, which an outer Nx also exports, is kept set, next to the
|
|
13
|
+
* NO_COLOR a shell may export. The fixture's runtime inputs inherit both, and
|
|
14
|
+
* must hash the same under them.
|
|
15
|
+
*/
|
|
16
|
+
export function fixtureNxEnv(workspace: string): Record<string, string> {
|
|
17
|
+
const env: Record<string, string> = {};
|
|
18
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
19
|
+
if (value !== undefined && !key.startsWith('NX_')) env[key] = value;
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
...env,
|
|
23
|
+
FORCE_COLOR: 'true',
|
|
24
|
+
NO_COLOR: '1',
|
|
25
|
+
NX_WORKSPACE_ROOT_PATH: workspace,
|
|
26
|
+
NX_DAEMON: 'false',
|
|
27
|
+
NX_ISOLATE_PLUGINS: 'false',
|
|
28
|
+
NX_WORKSPACE_DATA_DIRECTORY: join(workspace, '.nx/workspace-data'),
|
|
29
|
+
NX_CACHE_DIRECTORY: join(workspace, '.nx/cache'),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-process plugin tests infer targets for temporary fixture workspaces. Nx's
|
|
3
|
+
* daemon client answers file-index queries for the workspace this process
|
|
4
|
+
* started in, never for the fixture root a query names, so a daemon-backed
|
|
5
|
+
* glob would silently describe the wrong tree. With the daemon off, Nx builds
|
|
6
|
+
* the index in process for the root each query names, as a CI run does.
|
|
7
|
+
* Child Nx processes the tests spawn set NX_DAEMON themselves.
|
|
8
|
+
*/
|
|
9
|
+
process.env.NX_DAEMON = 'false';
|
|
@@ -1,18 +1,38 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { resolve } from 'node:path';
|
|
3
|
-
import { hashCargoPathInputs } from '../cargo-source-hash.js';
|
|
3
|
+
import { type CargoPathInputsOptions, hashCargoPathInputs } from '../cargo-source-hash.js';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
/** Flags first, in any order and at most once each, then at most one manifest; anything else is refused. */
|
|
6
|
+
function parseArguments(args: readonly string[]): { manifest: string; options: CargoPathInputsOptions } | undefined {
|
|
7
|
+
const options: CargoPathInputsOptions = {};
|
|
8
|
+
let index = 0;
|
|
9
|
+
for (; index < args.length; index += 1) {
|
|
10
|
+
const arg = args[index];
|
|
11
|
+
if (arg === '--include-workspace' && options.includeWorkspace === undefined) {
|
|
12
|
+
options.includeWorkspace = true;
|
|
13
|
+
} else if (arg === '--closure' && options.closure === undefined) {
|
|
14
|
+
const directory = args[index + 1];
|
|
15
|
+
if (directory === undefined || directory.startsWith('-')) return undefined;
|
|
16
|
+
options.closure = directory;
|
|
17
|
+
index += 1;
|
|
18
|
+
} else if (arg?.startsWith('-')) {
|
|
19
|
+
return undefined;
|
|
20
|
+
} else {
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const rest = args.slice(index);
|
|
25
|
+
if (rest.length > 1) return undefined;
|
|
26
|
+
return { manifest: rest[0] ?? 'Cargo.toml', options };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const parsed = parseArguments(process.argv.slice(2));
|
|
30
|
+
if (parsed === undefined) {
|
|
31
|
+
process.stderr.write('usage: smoo-nx-cargo-hash [--include-workspace] [--closure <directory>] [Cargo.toml]\n');
|
|
10
32
|
process.exit(2);
|
|
11
33
|
}
|
|
12
34
|
try {
|
|
13
|
-
process.stdout.write(
|
|
14
|
-
`${await hashCargoPathInputs(resolve(manifest ?? 'Cargo.toml'), process.cwd(), { includeWorkspace })}\n`,
|
|
15
|
-
);
|
|
35
|
+
process.stdout.write(`${await hashCargoPathInputs(resolve(parsed.manifest), process.cwd(), parsed.options)}\n`);
|
|
16
36
|
} catch (error) {
|
|
17
37
|
process.stderr.write(`smoo-nx-cargo-hash: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
18
38
|
process.stderr.write(
|
package/src/bin/smoo-nx-exec.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { inspect } from 'node:util';
|
|
|
5
5
|
|
|
6
6
|
import { describeMiss, ensureBuilt, parseTargetSelector } from '../ensure-built.js';
|
|
7
7
|
|
|
8
|
-
const USAGE = 'usage: smoo-nx-exec <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 {
|
|
@@ -58,17 +58,17 @@ function findWorkspaceRoot(from: string): string {
|
|
|
58
58
|
|
|
59
59
|
const argv = process.argv.slice(2);
|
|
60
60
|
const separator = argv.indexOf('--');
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const command = argv.slice(separator + 1);
|
|
65
|
-
if (command.length === 0) {
|
|
61
|
+
// Without `--` there is nothing to exec: build what is stale and exit, so a
|
|
62
|
+
// caller that only needs the target current pays the probe, not an Nx CLI
|
|
63
|
+
// start that replays every cached task's log.
|
|
64
|
+
const command = separator === -1 ? [] : argv.slice(separator + 1);
|
|
65
|
+
if (separator !== -1 && command.length === 0) {
|
|
66
66
|
usageError('no binary given after `--`');
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
let target: string | undefined;
|
|
70
70
|
let workspaceRootArg: string | undefined;
|
|
71
|
-
const tokens = tokenize(argv.slice(0, separator));
|
|
71
|
+
const tokens = tokenize(separator === -1 ? argv : argv.slice(0, separator));
|
|
72
72
|
for (let index = 0; index < tokens.length; index += 1) {
|
|
73
73
|
const token = tokens[index];
|
|
74
74
|
if (token === '--workspace-root') {
|
|
@@ -91,12 +91,9 @@ if (parseTargetSelector(target) === null) {
|
|
|
91
91
|
|
|
92
92
|
// `execve` does no PATH lookup, so a bare name would fail as a missing file in
|
|
93
93
|
// the current directory. Say which mistake was made instead.
|
|
94
|
-
if (!command[0].includes('/')) {
|
|
94
|
+
if (command.length > 0 && !command[0].includes('/')) {
|
|
95
95
|
usageError(`'${command[0]}' must be a path to the binary, not a name to look up on PATH`);
|
|
96
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
97
|
|
|
101
98
|
const workspaceRoot =
|
|
102
99
|
workspaceRootArg === undefined ? findWorkspaceRoot(process.cwd()) : resolve(process.cwd(), workspaceRootArg);
|
|
@@ -109,6 +106,15 @@ const result = await ensureBuilt({ target, cwd: workspaceRoot }).catch((error: u
|
|
|
109
106
|
if (result.disposition !== 'hit' && process.env.NX_VERBOSE_LOGGING === 'true') {
|
|
110
107
|
process.stderr.write(`smoo-nx-exec: ran ${target} because ${describeMiss(result.reason)}\n`);
|
|
111
108
|
}
|
|
109
|
+
if (result.disposition === 'failed' || command.length === 0) {
|
|
110
|
+
// This process ends here rather than becoming the binary, and `process.exit`
|
|
111
|
+
// drops whatever a pipe has not taken yet: stdio pipes are asynchronous on
|
|
112
|
+
// macOS, and the tail of a build log is the part that explains it. Under
|
|
113
|
+
// Node an empty write completes once everything queued before it has.
|
|
114
|
+
await Promise.all(
|
|
115
|
+
[process.stdout, process.stderr].map((stream) => new Promise<void>((settle) => stream.write('', () => settle()))),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
112
118
|
if (result.disposition === 'failed') {
|
|
113
119
|
if (result.signal !== null) {
|
|
114
120
|
// Re-raise rather than translate, so a build killed by SIGINT leaves this
|
|
@@ -117,10 +123,33 @@ if (result.disposition === 'failed') {
|
|
|
117
123
|
}
|
|
118
124
|
process.exit(result.exitCode);
|
|
119
125
|
}
|
|
126
|
+
if (command.length === 0) {
|
|
127
|
+
process.exit(0);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Resolved against the directory the user is standing in, not the workspace
|
|
131
|
+
// root: this is their command line, and the exec'd process inherits their cwd.
|
|
132
|
+
const binary = isAbsolute(command[0]) ? command[0] : resolve(process.cwd(), command[0]);
|
|
120
133
|
|
|
121
134
|
if (process.execve === undefined) {
|
|
122
135
|
throw new Error('smoo-nx-exec needs process.execve, which requires a POSIX host on Node 24+ or Bun');
|
|
123
136
|
}
|
|
137
|
+
// Node puts a stdio pipe into O_NONBLOCK on macOS, and that flag belongs to the
|
|
138
|
+
// open file description, so it outlives execve. A binary that writes as if its
|
|
139
|
+
// stdout blocked would then lose everything past the first full pipe buffer
|
|
140
|
+
// (EAGAIN) whenever its reader falls behind: a `<cli> list | jq` read
|
|
141
|
+
// exactly 65536 bytes. Hand the binary the blocking stdio every program expects.
|
|
142
|
+
for (const stream of [process.stdin, process.stdout, process.stderr]) {
|
|
143
|
+
const handle: unknown = Reflect.get(stream, '_handle');
|
|
144
|
+
if (
|
|
145
|
+
typeof handle === 'object' &&
|
|
146
|
+
handle !== null &&
|
|
147
|
+
'setBlocking' in handle &&
|
|
148
|
+
typeof handle.setBlocking === 'function'
|
|
149
|
+
) {
|
|
150
|
+
handle.setBlocking(true);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
124
153
|
// `execve`, not spawn-and-wait: the built binary replaces this process, so it
|
|
125
154
|
// owns the terminal, the signals and the exit status directly, with no wrapper
|
|
126
155
|
// left behind to forward them.
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { expect, it } from 'bun:test';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { mkdir, mkdtemp, readFile, realpath, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
|
|
7
|
+
import { resetWorkspaceContext } from 'nx/src/utils/workspace-context.js';
|
|
8
|
+
import { fixtureNxEnv } from './__tests__/fixture-nx-env.js';
|
|
9
|
+
import { CARGO_CLOSURE_INPUT } from './cargo-closure-input.js';
|
|
10
|
+
import { createNodesV2 } from './index.js';
|
|
11
|
+
|
|
12
|
+
const repositoryRoot = join(import.meta.dir, '../../..');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* An Nx workspace that is also the Cargo workspace. `app` reaches `leaf`
|
|
16
|
+
* through `bridge`, and `external` — outside the Nx workspace — through
|
|
17
|
+
* `bridge` too. `unrelated` is a member nothing depends on. `app` records
|
|
18
|
+
* every real compile in `executions.log`, so a cache hit is an unchanged log.
|
|
19
|
+
*/
|
|
20
|
+
async function closureFixture(root: string, gitignore = 'node_modules/\ntarget/\ndist/\n.nx/\n') {
|
|
21
|
+
const workspace = join(root, 'workspace');
|
|
22
|
+
const executions = join(root, 'executions.log');
|
|
23
|
+
const output = join(workspace, 'packages/app/dist/result.txt');
|
|
24
|
+
const files: Record<string, string> = {
|
|
25
|
+
'workspace/.gitignore': gitignore,
|
|
26
|
+
'workspace/package.json':
|
|
27
|
+
'{"name":"cargo-closure-fixture","private":true,"workspaces":["packages/*","crates/*"]}\n',
|
|
28
|
+
'workspace/nx.json': JSON.stringify({
|
|
29
|
+
plugins: ['@smoothbricks/nx-plugin'],
|
|
30
|
+
namedInputs: { externalRustCrates: [] },
|
|
31
|
+
}),
|
|
32
|
+
'workspace/Cargo.toml':
|
|
33
|
+
'[workspace]\nmembers=["packages/app","crates/bridge","crates/leaf","crates/unrelated"]\nresolver="2"\n',
|
|
34
|
+
'workspace/packages/app/package.json': JSON.stringify({
|
|
35
|
+
name: 'app',
|
|
36
|
+
private: true,
|
|
37
|
+
nx: {
|
|
38
|
+
targets: {
|
|
39
|
+
compile: {
|
|
40
|
+
executor: 'nx:run-commands',
|
|
41
|
+
cache: true,
|
|
42
|
+
inputs: ['{projectRoot}/compile.ts', CARGO_CLOSURE_INPUT],
|
|
43
|
+
outputs: ['{projectRoot}/dist'],
|
|
44
|
+
options: { command: 'bun packages/app/compile.ts' },
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
'workspace/packages/app/compile.ts': `import { execFileSync } from 'node:child_process';
|
|
50
|
+
import { appendFile, mkdir, writeFile } from 'node:fs/promises';
|
|
51
|
+
const result = execFileSync('cargo', ['run', '--quiet', '--locked', '--offline', '--manifest-path', 'packages/app/Cargo.toml'], { encoding: 'utf8' });
|
|
52
|
+
await mkdir(${JSON.stringify(dirname(output))}, { recursive: true });
|
|
53
|
+
await writeFile(${JSON.stringify(output)}, result);
|
|
54
|
+
await appendFile(${JSON.stringify(executions)}, result);
|
|
55
|
+
`,
|
|
56
|
+
'workspace/packages/app/Cargo.toml':
|
|
57
|
+
'[package]\nname="app"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nbridge={path="../../crates/bridge"}\n',
|
|
58
|
+
'workspace/packages/app/src/main.rs': 'fn main() { println!("{}", bridge::answer()); }\n',
|
|
59
|
+
'workspace/crates/bridge/Cargo.toml':
|
|
60
|
+
'[package]\nname="bridge"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nleaf={path="../leaf"}\nexternal={path="../../../external"}\n',
|
|
61
|
+
'workspace/crates/bridge/src/lib.rs': 'pub fn answer() -> u8 { leaf::answer() + external::answer() }\n',
|
|
62
|
+
'workspace/crates/leaf/package.json': '{"name":"leaf","private":true}\n',
|
|
63
|
+
'workspace/crates/leaf/Cargo.toml': '[package]\nname="leaf"\nversion="0.1.0"\nedition="2021"\n',
|
|
64
|
+
'workspace/crates/leaf/src/lib.rs': 'pub fn answer() -> u8 { 1 }\n',
|
|
65
|
+
'workspace/crates/unrelated/Cargo.toml': '[package]\nname="unrelated"\nversion="0.1.0"\nedition="2021"\n',
|
|
66
|
+
'workspace/crates/unrelated/src/lib.rs': 'pub fn unrelated() -> u8 { 3 }\n',
|
|
67
|
+
'external/Cargo.toml': '[package]\nname="external"\nversion="0.1.0"\nedition="2021"\n[workspace]\n',
|
|
68
|
+
'external/src/lib.rs': 'pub fn answer() -> u8 { 10 }\n',
|
|
69
|
+
};
|
|
70
|
+
for (const [path, text] of Object.entries(files)) {
|
|
71
|
+
await mkdir(dirname(join(root, path)), { recursive: true });
|
|
72
|
+
await writeFile(join(root, path), text);
|
|
73
|
+
}
|
|
74
|
+
execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
|
|
75
|
+
await symlink(join(repositoryRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
|
|
76
|
+
execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', join(workspace, 'Cargo.toml')], {
|
|
77
|
+
stdio: 'pipe',
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/** `nx run app:compile` in a fresh process, as CI runs it; the built plugin is the one Nx loads. */
|
|
81
|
+
async function compile(): Promise<void> {
|
|
82
|
+
const child = Bun.spawn(['bun', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:compile'], {
|
|
83
|
+
cwd: workspace,
|
|
84
|
+
env: fixtureNxEnv(workspace),
|
|
85
|
+
stdout: 'pipe',
|
|
86
|
+
stderr: 'pipe',
|
|
87
|
+
});
|
|
88
|
+
const [exitCode, stdout, stderr] = await Promise.all([
|
|
89
|
+
child.exited,
|
|
90
|
+
new Response(child.stdout).text(),
|
|
91
|
+
new Response(child.stderr).text(),
|
|
92
|
+
]);
|
|
93
|
+
expect(exitCode, stdout + stderr).toBe(0);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Rewrite a fixture file as an edit Nx can see. Nx keeps each file's hash
|
|
97
|
+
* beside its mtime in whole seconds and reuses the hash until that second
|
|
98
|
+
* changes, so a rewrite inside the second the file was last hashed in is
|
|
99
|
+
* invisible to every fileset input. A fast host runs this whole fixture in
|
|
100
|
+
* about a second; the edit is dated at least a second past the old mtime.
|
|
101
|
+
*/
|
|
102
|
+
async function edit(path: string, text: string): Promise<void> {
|
|
103
|
+
const file = join(root, path);
|
|
104
|
+
const { mtimeMs } = await stat(file);
|
|
105
|
+
await writeFile(file, text);
|
|
106
|
+
const edited = new Date(Math.max(Date.now(), mtimeMs + 1000));
|
|
107
|
+
await utimes(file, edited, edited);
|
|
108
|
+
}
|
|
109
|
+
const log = () => readFile(executions, 'utf8');
|
|
110
|
+
return { workspace, compile, edit, log };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
it('keys a custom target on exactly its Cargo closure, inside the workspace and out', async () => {
|
|
114
|
+
const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-')));
|
|
115
|
+
try {
|
|
116
|
+
const { compile, edit, log } = await closureFixture(root);
|
|
117
|
+
await compile();
|
|
118
|
+
expect(await log()).toBe('11\n');
|
|
119
|
+
await compile();
|
|
120
|
+
expect(await log()).toBe('11\n');
|
|
121
|
+
|
|
122
|
+
// Reached only through bridge: no path of app's own names it.
|
|
123
|
+
await edit('workspace/crates/leaf/src/lib.rs', 'pub fn answer() -> u8 { 2 }\n');
|
|
124
|
+
await compile();
|
|
125
|
+
expect(await log()).toBe('11\n12\n');
|
|
126
|
+
|
|
127
|
+
// A workspace member outside the closure is not an input.
|
|
128
|
+
await edit('workspace/crates/unrelated/src/lib.rs', 'pub fn unrelated() -> u8 { 4 }\n');
|
|
129
|
+
await compile();
|
|
130
|
+
expect(await log()).toBe('11\n12\n');
|
|
131
|
+
|
|
132
|
+
// Outside the Nx workspace, where no fileset reaches.
|
|
133
|
+
await edit('external/src/lib.rs', 'pub fn answer() -> u8 { 20 }\n');
|
|
134
|
+
await compile();
|
|
135
|
+
expect(await log()).toBe('11\n12\n22\n');
|
|
136
|
+
} finally {
|
|
137
|
+
await rm(root, { recursive: true, force: true });
|
|
138
|
+
}
|
|
139
|
+
}, 120_000);
|
|
140
|
+
|
|
141
|
+
it('hashes a closure member in an ignored directory, which no fileset can see', async () => {
|
|
142
|
+
const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-ignored-')));
|
|
143
|
+
try {
|
|
144
|
+
const { compile, edit, log } = await closureFixture(root, 'node_modules/\ntarget/\ndist/\n.nx/\ncrates/leaf/\n');
|
|
145
|
+
await compile();
|
|
146
|
+
expect(await log()).toBe('11\n');
|
|
147
|
+
await edit('workspace/crates/leaf/src/lib.rs', 'pub fn answer() -> u8 { 2 }\n');
|
|
148
|
+
await compile();
|
|
149
|
+
expect(await log()).toBe('11\n12\n');
|
|
150
|
+
} finally {
|
|
151
|
+
await rm(root, { recursive: true, force: true });
|
|
152
|
+
}
|
|
153
|
+
}, 120_000);
|
|
154
|
+
|
|
155
|
+
it('leaves a closure inside the workspace to Nx: no process runs when its tasks are hashed', async () => {
|
|
156
|
+
const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-inferred-')));
|
|
157
|
+
try {
|
|
158
|
+
const { workspace } = await closureFixture(root);
|
|
159
|
+
resetWorkspaceContext();
|
|
160
|
+
const [, infer] = createNodesV2;
|
|
161
|
+
const inferred = await infer(['package.json', 'packages/app/package.json', 'crates/leaf/package.json'], undefined, {
|
|
162
|
+
workspaceRoot: workspace,
|
|
163
|
+
nxJsonConfiguration: { namedInputs: { externalRustCrates: [] } },
|
|
164
|
+
});
|
|
165
|
+
const closureOf = (projectRoot: string) =>
|
|
166
|
+
inferred.find(([file]) => dirname(file) === projectRoot)?.[1].projects?.[projectRoot]?.namedInputs?.[
|
|
167
|
+
CARGO_CLOSURE_INPUT
|
|
168
|
+
];
|
|
169
|
+
const runtimeCommands = (projectRoot: string) =>
|
|
170
|
+
(closureOf(projectRoot) ?? []).flatMap((input) =>
|
|
171
|
+
typeof input === 'object' && 'runtime' in input ? [input.runtime] : [],
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
expect(closureOf('crates/leaf')).toContain('{workspaceRoot}/Cargo.lock');
|
|
175
|
+
expect(runtimeCommands('crates/leaf')).toEqual([]);
|
|
176
|
+
// app reaches `external`, the one member only a process can hash, and
|
|
177
|
+
// only it: the in-workspace members stay filesets.
|
|
178
|
+
expect(runtimeCommands('packages/app')).toEqual([
|
|
179
|
+
'node node_modules/@smoothbricks/nx-plugin/dist/bin/smoo-nx-cargo-hash.js --closure packages/app Cargo.toml',
|
|
180
|
+
]);
|
|
181
|
+
} finally {
|
|
182
|
+
await rm(root, { recursive: true, force: true });
|
|
183
|
+
}
|
|
184
|
+
}, 120_000);
|