@smoothbricks/nx-plugin 0.4.31 → 0.5.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.
Files changed (43) hide show
  1. package/README.md +15 -11
  2. package/dist/cargo-toolchain-policy.d.ts +6 -6
  3. package/dist/cargo-toolchain-policy.js +6 -6
  4. package/dist/cross-check-policy.d.ts +31 -14
  5. package/dist/cross-check-policy.d.ts.map +1 -1
  6. package/dist/cross-check-policy.js +43 -15
  7. package/dist/executors/cargo-zigbuild/executor.d.ts +60 -0
  8. package/dist/executors/cargo-zigbuild/executor.d.ts.map +1 -0
  9. package/dist/executors/cargo-zigbuild/executor.js +266 -0
  10. package/dist/executors/cargo-zigbuild/schema.d.ts +15 -0
  11. package/dist/executors/cargo-zigbuild/schema.d.ts.map +1 -0
  12. package/dist/index.d.ts +0 -15
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +56 -89
  15. package/dist/testing.d.ts.map +1 -1
  16. package/dist/testing.js +3 -2
  17. package/executors.json +4 -4
  18. package/managed/raw/patches/nx@23.2.1.patch +177 -9
  19. package/managed/raw/tooling/direnv/devenv.smoo.nix +46 -46
  20. package/managed/raw/tooling/git-hooks/pre-push.sh +41 -23
  21. package/package.json +1 -1
  22. package/src/cargo-toolchain-policy.ts +6 -6
  23. package/src/cross-check-policy.test.ts +90 -11
  24. package/src/cross-check-policy.ts +47 -17
  25. package/src/executors/cargo-zigbuild/executor.test.ts +129 -0
  26. package/src/executors/cargo-zigbuild/executor.ts +176 -0
  27. package/src/executors/cargo-zigbuild/schema.json +35 -0
  28. package/src/executors/cargo-zigbuild/schema.ts +14 -0
  29. package/src/index.test.ts +29 -61
  30. package/src/index.ts +60 -98
  31. package/src/nx-daemon-validity.test.ts +65 -0
  32. package/src/testing.ts +3 -2
  33. package/test-timings.json +1 -1
  34. package/dist/executors/napi-cross-toolchain/executor.d.ts +0 -42
  35. package/dist/executors/napi-cross-toolchain/executor.d.ts.map +0 -1
  36. package/dist/executors/napi-cross-toolchain/executor.js +0 -126
  37. package/dist/executors/napi-cross-toolchain/schema.d.ts +0 -4
  38. package/dist/executors/napi-cross-toolchain/schema.d.ts.map +0 -1
  39. package/src/executors/napi-cross-toolchain/executor.test.ts +0 -112
  40. package/src/executors/napi-cross-toolchain/executor.ts +0 -165
  41. package/src/executors/napi-cross-toolchain/schema.json +0 -15
  42. package/src/executors/napi-cross-toolchain/schema.ts +0 -3
  43. /package/dist/executors/{napi-cross-toolchain → cargo-zigbuild}/schema.js +0 -0
@@ -0,0 +1,176 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { realpathSync } from 'node:fs';
3
+ import { chmod, copyFile, mkdir, rename } from 'node:fs/promises';
4
+ import path from 'node:path';
5
+ import { createInterface } from 'node:readline';
6
+ import typia from 'typia';
7
+
8
+ import type { CargoZigbuildOptions } from './schema.js';
9
+
10
+ interface CargoZigbuildContext {
11
+ root: string;
12
+ }
13
+
14
+ /**
15
+ * The fields of cargo's `compiler-artifact` JSON message this executor reads.
16
+ * https://doc.rust-lang.org/cargo/reference/external-tools.html#artifact-messages
17
+ */
18
+ export interface CargoArtifactMessage {
19
+ reason: 'compiler-artifact';
20
+ manifest_path: string;
21
+ target: { kind: string[]; name: string };
22
+ filenames: string[];
23
+ executable: string | null;
24
+ }
25
+
26
+ const validateReason = typia.createValidate<{ reason: string }>();
27
+ const validateArtifact = typia.createValidate<CargoArtifactMessage>();
28
+
29
+ /** What one build produces: a named binary, or the package's cdylib. */
30
+ export type ZigbuildArtifact = { kind: 'bin'; name: string } | { kind: 'cdylib' };
31
+
32
+ export function zigbuildArtifact(options: Pick<CargoZigbuildOptions, 'bin'>): ZigbuildArtifact {
33
+ return options.bin === undefined ? { kind: 'cdylib' } : { kind: 'bin', name: options.bin };
34
+ }
35
+
36
+ /**
37
+ * `cargo-zigbuild zigbuild`, not `cargo zigbuild`. Run through cargo, the
38
+ * subcommand inherits `CARGO` naming the real cargo binary and calls it
39
+ * directly, bypassing the `cargo` on PATH; run directly, its inner `cargo` is
40
+ * the PATH one every other cargo target runs. `--locked` keeps Cargo.lock
41
+ * exactly as committed. The glibc suffix stays on `--target`: it is how
42
+ * cargo-zigbuild pins the symbol versions, and cargo itself sees the bare
43
+ * triple.
44
+ */
45
+ export function zigbuildArgs(options: CargoZigbuildOptions, manifestPath: string): string[] {
46
+ const artifact = zigbuildArtifact(options);
47
+ return [
48
+ 'zigbuild',
49
+ '--locked',
50
+ '--message-format=json-render-diagnostics',
51
+ '--target',
52
+ options.target,
53
+ '--manifest-path',
54
+ manifestPath,
55
+ '--package',
56
+ options.package,
57
+ ...(options.release === true ? ['--release'] : []),
58
+ ...(artifact.kind === 'bin' ? ['--bin', artifact.name] : ['--lib']),
59
+ ];
60
+ }
61
+
62
+ /** `x86_64-unknown-linux-gnu.2.17` → `x86_64-unknown-linux-gnu`. */
63
+ export function cargoTriple(target: string): string {
64
+ const glibcSuffix = /\.\d+\.\d+$/.exec(target);
65
+ return glibcSuffix === null ? target : target.slice(0, glibcSuffix.index);
66
+ }
67
+
68
+ /**
69
+ * The environment without any compiler or linker named for this triple.
70
+ *
71
+ * cargo-zigbuild supplies the triple's `CC_`/`CXX_`/`AR_`/`RANLIB_` and
72
+ * linker only where none is set, and cc-rs reads the hyphenated spelling
73
+ * before cargo-zigbuild's underscored one. A shell that pins its host
74
+ * compiler for the same triple — a Linux devenv shell pins
75
+ * `CC_x86_64_unknown_linux_gnu` to Nix's cc wrapper for host builds — would
76
+ * otherwise compile the artifact's C against the build host's glibc headers,
77
+ * which is exactly the build-host leak the zig toolchain exists to remove.
78
+ */
79
+ export function zigbuildEnv(env: NodeJS.ProcessEnv, target: string): NodeJS.ProcessEnv {
80
+ const triple = cargoTriple(target);
81
+ const underscored = triple.replaceAll('-', '_');
82
+ const owned = new Set([
83
+ ...['CC', 'CXX', 'AR', 'RANLIB'].flatMap((tool) => [`${tool}_${triple}`, `${tool}_${underscored}`]),
84
+ `CARGO_TARGET_${underscored.toUpperCase()}_LINKER`,
85
+ ]);
86
+ return Object.fromEntries(Object.entries(env).filter(([name]) => !owned.has(name)));
87
+ }
88
+
89
+ /**
90
+ * The artifact file one cargo JSON message names, or null when the message is
91
+ * about anything else. `manifestPath` must be canonical, as cargo reports it.
92
+ */
93
+ export function artifactFile(line: string, manifestPath: string, artifact: ZigbuildArtifact): string | null {
94
+ if (!line.startsWith('{')) return null;
95
+ const parsed: unknown = JSON.parse(line);
96
+ const header = validateReason(parsed);
97
+ if (!header.success) {
98
+ throw new Error(`cargo emitted a JSON message without a reason: ${line}`);
99
+ }
100
+ if (header.data.reason !== 'compiler-artifact') return null;
101
+ const message = validateArtifact(parsed);
102
+ if (!message.success) {
103
+ throw new Error(
104
+ `cargo compiler-artifact message does not match the artifact contract: ${JSON.stringify(message.errors)}`,
105
+ );
106
+ }
107
+ const { data } = message;
108
+ if (data.manifest_path !== manifestPath) return null;
109
+ if (artifact.kind === 'bin') {
110
+ return data.target.kind.includes('bin') && data.target.name === artifact.name ? data.executable : null;
111
+ }
112
+ if (!data.target.kind.includes('cdylib')) return null;
113
+ return data.filenames.find((file) => file.endsWith('.so')) ?? null;
114
+ }
115
+
116
+ export default async function cargoZigbuildExecutor(
117
+ options: CargoZigbuildOptions,
118
+ context: CargoZigbuildContext,
119
+ ): Promise<{ success: boolean }> {
120
+ const manifestPath = realpathSync(path.resolve(context.root, options.manifestPath));
121
+ const output = path.resolve(context.root, options.output);
122
+ const artifact = zigbuildArtifact(options);
123
+ const args = zigbuildArgs(options, manifestPath);
124
+ const command = `cargo-zigbuild ${args.join(' ')}`;
125
+
126
+ // Cargo discovers `.cargo/config.toml` from its working directory upward,
127
+ // so the crate's own directory sees every config a build of it would.
128
+ const child = spawn('cargo-zigbuild', args, {
129
+ cwd: path.dirname(manifestPath),
130
+ env: zigbuildEnv(process.env, options.target),
131
+ stdio: ['ignore', 'pipe', 'inherit'],
132
+ });
133
+ const exited = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve, reject) => {
134
+ child.once('error', reject);
135
+ child.once('close', (code, signal) => resolve({ code, signal }));
136
+ });
137
+
138
+ let built: string | null = null;
139
+ for await (const line of createInterface({ input: child.stdout })) {
140
+ built = artifactFile(line, manifestPath, artifact) ?? built;
141
+ }
142
+
143
+ let status: { code: number | null; signal: NodeJS.Signals | null };
144
+ try {
145
+ status = await exited;
146
+ } catch (error) {
147
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
148
+ console.error(
149
+ `${command}: cargo-zigbuild is not on PATH. Linux artifacts build with the linux-cross toolchain; ` +
150
+ 'on macOS run the target inside it: tooling/devenv -P linux-cross shell -- nx run <project>:<target>',
151
+ );
152
+ return { success: false };
153
+ }
154
+ throw error;
155
+ }
156
+ if (status.code !== 0) {
157
+ console.error(`${command}: exited with ${status.signal ?? `status ${status.code}`}`);
158
+ return { success: false };
159
+ }
160
+ if (built === null) {
161
+ const wanted = artifact.kind === 'bin' ? `binary ${artifact.name}` : 'cdylib';
162
+ console.error(`${command}: cargo reported no ${wanted} artifact for ${options.package} at ${manifestPath}`);
163
+ return { success: false };
164
+ }
165
+
166
+ // Copy then rename, so the output path never holds a partial file.
167
+ await mkdir(path.dirname(output), { recursive: true });
168
+ const staging = `${output}.partial`;
169
+ await copyFile(built, staging);
170
+ if (artifact.kind === 'bin') {
171
+ await chmod(staging, 0o755);
172
+ }
173
+ await rename(staging, output);
174
+ console.log(`${path.relative(context.root, built)} -> ${path.relative(context.root, output)}`);
175
+ return { success: true };
176
+ }
@@ -0,0 +1,35 @@
1
+ {
2
+ "$schema": "https://json-schema.org/schema",
3
+ "version": 2,
4
+ "title": "Cargo Zigbuild Executor",
5
+ "description": "Build one Linux binary or cdylib with devenv's zig through `cargo-zigbuild` and copy it to its output file.",
6
+ "type": "object",
7
+ "properties": {
8
+ "target": {
9
+ "type": "string",
10
+ "description": "Rust target triple, with cargo-zigbuild's glibc suffix where it pins one, e.g. x86_64-unknown-linux-gnu.2.17 or x86_64-unknown-linux-musl."
11
+ },
12
+ "manifestPath": {
13
+ "type": "string",
14
+ "description": "The crate's Cargo.toml, relative to the workspace root."
15
+ },
16
+ "package": {
17
+ "type": "string",
18
+ "description": "Cargo package to build."
19
+ },
20
+ "bin": {
21
+ "type": "string",
22
+ "description": "Build this binary and copy its executable. Absent, build the package's cdylib."
23
+ },
24
+ "output": {
25
+ "type": "string",
26
+ "description": "Destination file for the artifact, relative to the workspace root."
27
+ },
28
+ "release": {
29
+ "type": "boolean",
30
+ "description": "Compile cargo's release profile instead of dev."
31
+ }
32
+ },
33
+ "required": ["target", "manifestPath", "package", "output"],
34
+ "additionalProperties": false
35
+ }
@@ -0,0 +1,14 @@
1
+ export interface CargoZigbuildOptions {
2
+ /** Rust target triple, with cargo-zigbuild's glibc suffix where it pins one: `x86_64-unknown-linux-gnu.2.17`. */
3
+ target: string;
4
+ /** The crate's Cargo.toml, relative to the workspace root. */
5
+ manifestPath: string;
6
+ /** Cargo package to build. */
7
+ package: string;
8
+ /** Build this binary and copy its executable; absent, build the package's cdylib. */
9
+ bin?: string;
10
+ /** Destination file for the artifact, relative to the workspace root. */
11
+ output: string;
12
+ /** Compile cargo's `release` profile instead of `dev`. */
13
+ release?: boolean;
14
+ }
package/src/index.test.ts CHANGED
@@ -1918,79 +1918,47 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
1918
1918
  // nothing from it, and a platform target's dependency closure may only reach
1919
1919
  // its own family (`validatePlatformTargetDependencies`).
1920
1920
  expect(targets['napi-arm64-macos']?.dependsOn).toBeUndefined();
1921
- // A macOS triple never gets a cross toolchain: `usesNapiCross` is
1922
- // `family === 'linux' && target !== host`, so the family decides this one
1923
- // and no host can change it.
1924
- expect(targets['napi-toolchain-x64-macos']).toBeUndefined();
1925
- // Everything else about the cross regime depends on WHICH host is
1926
- // inferring — on an x64 Linux runner `napi-x64-linux` is the native build,
1927
- // with no `--use-napi-cross` and no toolchain prerequisite at all. Those
1928
- // facts are asserted against forced platforms below instead of against
1929
- // whichever machine happens to run the suite.
1930
-
1931
- // Exercise both host branches explicitly: this suite usually runs on
1932
- // Darwin, but Linux's native compiler selection is the contract at risk.
1921
+ // Every host infers the SAME Linux addon target: devenv's zig through the
1922
+ // cargo-zigbuild executor, glibc pinned by the triple suffix. There is no
1923
+ // native-versus-cross branch to diverge on, so forcing a Linux host and a
1924
+ // macOS host must yield byte-identical Linux targets, and neither may
1925
+ // carry a compiler environment or a toolchain prerequisite.
1933
1926
  const linuxX64Targets = await inferProjectTargets(
1934
1927
  workspace,
1935
1928
  'packages/cowshed/package.json',
1936
1929
  createNodesV2ForPlatform('linux', 'x64'),
1937
1930
  );
1938
- expect(linuxX64Targets['napi-x64-linux']?.options?.command).not.toContain('--use-napi-cross');
1939
- expect(linuxX64Targets['napi-x64-linux']?.options?.env).toEqual({ CC: 'cc', CXX: 'c++' });
1940
- expect(linuxX64Targets['napi-x64-linux']?.dependsOn).toBeUndefined();
1941
- expect(linuxX64Targets['napi-toolchain-x64-linux']).toBeUndefined();
1942
- expect(linuxX64Targets['napi-arm64-linux']?.options?.command).toContain('--use-napi-cross');
1943
- expect(linuxX64Targets['napi-arm64-linux']?.options?.env).toEqual({
1944
- TARGET_CC: 'clang',
1945
- TARGET_CXX: 'clang++',
1946
- });
1947
- expect(linuxX64Targets['napi-debug']?.options?.env).toEqual({ CC: 'cc', CXX: 'c++' });
1948
-
1949
1931
  const darwinArm64Targets = await inferProjectTargets(
1950
1932
  workspace,
1951
1933
  'packages/cowshed/package.json',
1952
1934
  createNodesV2ForPlatform('darwin', 'arm64'),
1953
1935
  );
1954
- expect(darwinArm64Targets['napi-x64-linux']?.options?.command).toContain('--use-napi-cross');
1955
- expect(darwinArm64Targets['napi-x64-linux']?.options?.env).toEqual({
1956
- TARGET_CC: 'clang',
1957
- TARGET_CXX: 'clang++',
1958
- });
1959
- expect(darwinArm64Targets['napi-x64-linux']?.dependsOn).toEqual(['napi-toolchain-x64-linux']);
1960
- expect(darwinArm64Targets['napi-debug']?.options?.env).toBeUndefined();
1961
- expect(darwinArm64Targets['napi-toolchain-arm64-linux']).toEqual({
1962
- executor: '@smoothbricks/nx-plugin:napi-cross-toolchain',
1963
- cache: false,
1964
- options: { triple: 'aarch64-unknown-linux-gnu' },
1936
+ expect(darwinArm64Targets['napi-x64-linux']).toMatchObject({
1937
+ executor: '@smoothbricks/nx-plugin:cargo-zigbuild',
1938
+ cache: true,
1939
+ outputs: ['{projectRoot}/dist/native/linux-x64-gnu'],
1940
+ options: {
1941
+ target: 'x86_64-unknown-linux-gnu.2.17',
1942
+ manifestPath: 'packages/cowshed/crates/cowshed-napi/Cargo.toml',
1943
+ package: 'cowshed-napi',
1944
+ output: '{projectRoot}/dist/native/linux-x64-gnu/cowshed.linux-x64-gnu.node',
1945
+ },
1946
+ configurations: { [RELEASE_CONFIGURATION]: { release: true } },
1965
1947
  });
1966
- expect(darwinArm64Targets['napi-toolchain-x64-linux']).toEqual({
1967
- executor: '@smoothbricks/nx-plugin:napi-cross-toolchain',
1968
- cache: false,
1969
- options: { triple: 'x86_64-unknown-linux-gnu' },
1948
+ expect(darwinArm64Targets['napi-arm64-linux']?.options).toMatchObject({
1949
+ target: 'aarch64-unknown-linux-gnu.2.17',
1950
+ output: '{projectRoot}/dist/native/linux-arm64-gnu/cowshed.linux-arm64-gnu.node',
1970
1951
  });
1971
- expect(darwinArm64Targets['napi-arm64-linux']?.dependsOn).toEqual(['napi-toolchain-arm64-linux']);
1972
- expect(darwinArm64Targets['napi-x64-linux']?.outputs).toEqual(['{projectRoot}/dist/native/linux-x64-gnu']);
1973
- expect(darwinArm64Targets['napi-x64-linux']?.options?.command).toBe(
1974
- 'napi build --platform --no-js --dts cowshed.linux-x64-gnu.d.ts --target x86_64-unknown-linux-gnu --use-napi-cross --manifest-path crates/cowshed-napi/Cargo.toml --package cowshed-napi --package-json-path package.json --output-dir dist/native/linux-x64-gnu',
1975
- );
1976
- expect(darwinArm64Targets['napi-x64-linux']?.configurations?.[RELEASE_CONFIGURATION]?.command).toContain(
1977
- 'napi build --release --platform',
1978
- );
1979
-
1980
- // The whole rule, stated once per forced host: a triple gets a cross
1981
- // toolchain exactly when it is a linux triple that is not this host's own.
1982
- // Asserting the pair by name is what broke on Linux, where
1983
- // napi-toolchain-x64-linux legitimately does not exist.
1984
- for (const [inferred, hostTriple] of [
1985
- [linuxX64Targets, 'x64-linux'],
1986
- [darwinArm64Targets, 'arm64-macos'],
1987
- ] as const) {
1988
- for (const suffix of ['arm64-linux', 'x64-linux', 'arm64-macos', 'x64-macos']) {
1989
- const expectsToolchain = suffix.endsWith('-linux') && suffix !== hostTriple;
1990
- expect(inferred[`napi-toolchain-${suffix}`] === undefined).toBe(!expectsToolchain);
1991
- expect(inferred[`napi-${suffix}`]?.options?.command).toContain(
1992
- expectsToolchain ? '--use-napi-cross' : '--target',
1993
- );
1952
+ for (const suffix of ['arm64-linux', 'x64-linux']) {
1953
+ expect(linuxX64Targets[`napi-${suffix}`]).toEqual(darwinArm64Targets[`napi-${suffix}`]);
1954
+ expect(darwinArm64Targets[`napi-${suffix}`]?.dependsOn).toBeUndefined();
1955
+ }
1956
+ for (const inferred of [linuxX64Targets, darwinArm64Targets]) {
1957
+ expect(Object.keys(inferred).filter((name) => name.startsWith('napi-toolchain-'))).toEqual([]);
1958
+ expect(inferred['napi-debug']?.options?.env).toBeUndefined();
1959
+ for (const suffix of ['arm64-macos', 'x64-macos']) {
1960
+ expect(inferred[`napi-${suffix}`]?.options?.command).toContain('napi build');
1961
+ expect(inferred[`napi-${suffix}`]?.options?.env).toBeUndefined();
1994
1962
  }
1995
1963
  }
1996
1964
  // The aggregate build pulls in exactly the inferring host's
package/src/index.ts CHANGED
@@ -58,12 +58,12 @@ import {
58
58
  CARGO_CROSS_LINT_TARGET,
59
59
  CARGO_FETCH_COMMAND,
60
60
  CARGO_FETCH_TARGET,
61
- CARGO_FROZEN_PREFIX,
62
61
  CARGO_LINT_CLIPPY_COMMAND,
63
62
  cargoCrossTestArchiveFile,
64
63
  cargoCrossTestArchiveTargetName,
65
64
  cargoCrossTestTargetName,
66
65
  cargoFrozen,
66
+ runsFrozenCargo,
67
67
  } from './cross-check-policy.js';
68
68
  import type { EslintFileSet } from './eslint-file-set.js';
69
69
  import { closeRunCpuPool, serveRunCpuPool } from './executors/bounded-exec/run-cpu-pool.js';
@@ -466,14 +466,7 @@ function sameNapiPlatform(left: NapiPlatform, right: NapiPlatform | null): boole
466
466
  function hostPlatformTargetNames(targetNames: Iterable<string>, hostPlatform: NapiPlatform | null): string[] {
467
467
  if (hostPlatform === null) return [];
468
468
  const suffix = `-${hostPlatform.architecture}-${hostPlatform.targetFamily}`;
469
- return [...new Set(targetNames)]
470
- .filter(
471
- // Binary targets only. A toolchain prerequisite carries the same platform
472
- // suffix but produces no artifact: the cross builds that need it depend
473
- // on it directly, so the aggregate stays a list of outputs.
474
- (name) => name.endsWith(suffix) && !name.startsWith(NAPI_TOOLCHAIN_TARGET_PREFIX),
475
- )
476
- .sort();
469
+ return [...new Set(targetNames)].filter((name) => name.endsWith(suffix)).sort();
477
470
  }
478
471
 
479
472
  /**
@@ -566,18 +559,23 @@ function targetCommandText(target: TargetConfiguration): string {
566
559
  }
567
560
  /**
568
561
  * The N-API build's inputs beyond the Cargo sources: the toolchain pin, the
569
- * package's `napi` block, which names the binary and the targets, and the napi
570
- * CLI's resolved version from the lockfile. Neither the package version nor
571
- * any other lockfile entry is part of the artifact.
562
+ * package's `napi` block, which names the binary and the targets, and the
563
+ * resolved version of the npm package that drives the build (the napi CLI on
564
+ * macOS; none on Linux, where devenv's zig and cargo-zigbuild are covered by
565
+ * the pin). Neither the package version nor any other lockfile entry is part
566
+ * of the artifact.
572
567
  *
573
- * The pin is stated here rather than inherited: `napi build` compiles with the
574
- * same rustc and links against the same SDK as any other cargo target, but its
575
- * command does not spell `cargo --frozen`, which is how every other cached
576
- * cargo target acquires its toolchain identity. Without it a `.node` linked
577
- * against one toolchain hashed equal to the same sources linked against the
578
- * next one.
568
+ * The pin is stated here rather than inherited: neither `napi build` nor the
569
+ * `cargo-zigbuild` executor spells `cargo --frozen`, which is how every other
570
+ * cached cargo target acquires its toolchain identity. Without it a `.node`
571
+ * linked against one toolchain hashed equal to the same sources linked against
572
+ * the next one.
579
573
  */
580
- function napiInputs(projectRoot: string, repoRooted: boolean): TargetConfiguration['inputs'] {
574
+ function napiInputs(
575
+ projectRoot: string,
576
+ repoRooted: boolean,
577
+ externalDependencies: readonly string[],
578
+ ): TargetConfiguration['inputs'] {
581
579
  // Runtime inputs execute from the workspace root, so the manifest is named
582
580
  // by its path from there whichever way the Cargo invocation is rooted.
583
581
  const packageJson = `./${posix.join(projectRoot, 'package.json')}`;
@@ -587,7 +585,7 @@ function napiInputs(projectRoot: string, repoRooted: boolean): TargetConfigurati
587
585
  ...(repoRooted ? ['{workspaceRoot}/scripts/*.sh'] : []),
588
586
  ...CARGO_TOOLCHAIN_PIN_INPUTS,
589
587
  runtimeInput(`bun -e 'console.log(JSON.stringify(require(${JSON.stringify(packageJson)}).napi ?? null))'`),
590
- { externalDependencies: ['@napi-rs/cli'] },
588
+ { externalDependencies: [...externalDependencies] },
591
589
  ];
592
590
  }
593
591
 
@@ -618,31 +616,14 @@ const NAPI_TARGET_CONVENTIONS: Readonly<Record<string, NapiTargetConvention>> =
618
616
  },
619
617
  };
620
618
 
621
- const NATIVE_LINUX_COMPILER_ENV = Object.freeze({ CC: 'cc', CXX: 'c++' });
622
- const CROSS_LINUX_COMPILER_ENV = Object.freeze({ TARGET_CC: 'clang', TARGET_CXX: 'clang++' });
623
-
624
- function usesNapiCross(target: NapiTargetConvention, hostPlatform: NapiPlatform | null): boolean {
625
- return target.targetFamily === 'linux' && !sameNapiPlatform(target, hostPlatform);
626
- }
627
-
628
- function napiCompilerEnv(
629
- target: NapiPlatform | null,
630
- hostPlatform: NapiPlatform | null,
631
- ): Readonly<Record<string, string>> | undefined {
632
- if (target?.targetFamily !== 'linux') return undefined;
633
- return sameNapiPlatform(target, hostPlatform) ? NATIVE_LINUX_COMPILER_ENV : CROSS_LINUX_COMPILER_ENV;
634
- }
635
-
636
- const NAPI_TOOLCHAIN_TARGET_PREFIX = 'napi-toolchain-';
637
-
638
619
  /**
639
- * Prerequisite target that extracts the cross toolchain one triple needs. The
640
- * platform suffix keeps it inside its own platform family, so it can be a
641
- * dependency of `napi-<arch>-linux` and package-local `cli-<arch>-linux`.
620
+ * The glibc symbol-version floor every Linux N-API addon links against.
621
+ * cargo-zigbuild reads it from the target triple's suffix
622
+ * (`x86_64-unknown-linux-gnu.2.17`) and links zig's stubs for exactly that
623
+ * glibc, so the floor is this literal on every host rather than whatever glibc
624
+ * the build machine carries. 2.17 is below Node ≥ 18's own 2.28 floor.
642
625
  */
643
- export function napiToolchainTargetName(convention: NapiTargetConvention): string {
644
- return `${NAPI_TOOLCHAIN_TARGET_PREFIX}${convention.architecture}-${convention.targetFamily}`;
645
- }
626
+ const LINUX_GLIBC_FLOOR = '2.17';
646
627
 
647
628
  /**
648
629
  * Cargo's two profiles. A build target's default options compile `dev`, the
@@ -1536,8 +1517,8 @@ async function createProjectTargets(
1536
1517
  // than a silent absence.
1537
1518
  //
1538
1519
  // Deliberately NOT pushed to validationTargets: joining the lint aggregate
1539
- // would make `bun run lint` — and CI's own Linux lint, already native — demand
1540
- // the 0.4 GiB cross toolchain that the devenv profile exists to keep opt-in.
1520
+ // would make `bun run lint` — and CI's own Linux lint, already native — on a
1521
+ // macOS host demand the zig toolchain that the devenv profile keeps opt-in.
1541
1522
  targets[CARGO_CROSS_LINT_TARGET] = {
1542
1523
  executor: 'nx:run-commands',
1543
1524
  cache: true,
@@ -1666,9 +1647,8 @@ async function createProjectTargets(
1666
1647
  targets[CARGO_FETCH_TARGET] = {
1667
1648
  executor: 'nx:run-commands',
1668
1649
  // The download lands in CARGO_HOME, outside the workspace: a cache hit
1669
- // would skip work a cold registry still needs — same reason
1670
- // napi-toolchain-* is uncached. Nothing here is a workspace output, so
1671
- // there is nothing for Nx to restore in its place.
1650
+ // would skip work a cold registry still needs. Nothing here is a
1651
+ // workspace output, so there is nothing for Nx to restore in its place.
1672
1652
  cache: false,
1673
1653
  options: {
1674
1654
  // One fetch per manifest, independent of the others: Cargo's own
@@ -1688,7 +1668,7 @@ async function createProjectTargets(
1688
1668
  continue;
1689
1669
  }
1690
1670
  const text = targetCommandText(target);
1691
- if (text.includes(CARGO_FROZEN_PREFIX)) {
1671
+ if (runsFrozenCargo(text)) {
1692
1672
  target.dependsOn = [CARGO_FETCH_TARGET, ...(target.dependsOn ?? [])];
1693
1673
  }
1694
1674
  }
@@ -1699,7 +1679,7 @@ async function createProjectTargets(
1699
1679
  });
1700
1680
  for (const target of Object.values(targets)) {
1701
1681
  const text = targetCommandText(target);
1702
- if (text.includes(CARGO_FROZEN_PREFIX)) {
1682
+ if (runsFrozenCargo(text)) {
1703
1683
  target.dependsOn = [rootFetch, ...(target.dependsOn ?? [])];
1704
1684
  }
1705
1685
  }
@@ -1711,7 +1691,7 @@ async function createProjectTargets(
1711
1691
  // the target already does.
1712
1692
  for (const [name, target] of Object.entries(targets)) {
1713
1693
  const text = targetCommandText(target);
1714
- if (!text.includes(CARGO_FROZEN_PREFIX)) continue;
1694
+ if (!runsFrozenCargo(text)) continue;
1715
1695
  target.outputs ??= [];
1716
1696
  if (!target.cache) continue;
1717
1697
  const cwd: unknown = target.options?.cwd;
@@ -2242,27 +2222,17 @@ function createNapiTargets(
2242
2222
  // that stale local artifacts can mask for months.
2243
2223
  const packageJsonPath = repoRooted ? posix.join(projectRoot, 'package.json') : 'package.json';
2244
2224
  const commonCommand = `--manifest-path ${config.manifestPath} --package ${config.cargoPackage} --package-json-path ${packageJsonPath}`;
2245
- const cargoInputs = napiInputs(projectRoot, repoRooted);
2225
+ const cargoInputs = napiInputs(projectRoot, repoRooted, ['@napi-rs/cli']);
2246
2226
  const cargoCwd = repoRooted ? '.' : projectRoot;
2247
2227
  // A repository-root Cargo invocation runs outside the owning npm package, so
2248
2228
  // Nx's root-only PATH cannot resolve that package's napi CLI.
2249
2229
  const napiCommand = repoRooted ? posix.join(projectRoot, 'node_modules/.bin/napi') : 'napi';
2250
2230
  const outputPath = (projectOutput: string) => (repoRooted ? posix.join(projectRoot, projectOutput) : projectOutput);
2251
- const hostPlatformTargetName =
2252
- hostPlatform === null ? null : `napi-${hostPlatform.architecture}-${hostPlatform.targetFamily}`;
2253
- let nativeHostTargetName: string | null = null;
2254
- if (hostPlatformTargetName !== null) {
2255
- for (const triple of config.targets) {
2256
- const convention = NAPI_TARGET_CONVENTIONS[triple];
2257
- const targetName = convention ? `napi-${convention.architecture}-${convention.targetFamily}` : null;
2258
- if (targetName === hostPlatformTargetName && convention && !usesNapiCross(convention, hostPlatform)) {
2259
- nativeHostTargetName = targetName;
2260
- break;
2261
- }
2262
- }
2263
- }
2231
+ const hasHostPlatformTarget = config.targets.some((triple) => {
2232
+ const convention = NAPI_TARGET_CONVENTIONS[triple];
2233
+ return convention !== undefined && sameNapiPlatform(convention, hostPlatform);
2234
+ });
2264
2235
  const targets: Record<string, TargetConfiguration> = {};
2265
- const hostCompilerEnv = napiCompilerEnv(hostPlatform, hostPlatform);
2266
2236
 
2267
2237
  // The addon the test suite loads. A plain dev build shares target/debug with
2268
2238
  // cargo-test's dependency graph, so after the tests compile this is nearly
@@ -2281,11 +2251,10 @@ function createNapiTargets(
2281
2251
  options: {
2282
2252
  cwd: cargoCwd,
2283
2253
  command: `${napiCommand} build --platform --no-js --dts ${config.binaryName}.napi.d.ts ${commonCommand} --output-dir ${outputPath('.cache/native-debug')}`,
2284
- ...(hostCompilerEnv ? { env: hostCompilerEnv } : {}),
2285
2254
  },
2286
2255
  };
2287
2256
 
2288
- if (nativeHostTargetName === null) {
2257
+ if (!hasHostPlatformTarget) {
2289
2258
  // The platform target for this host triple is the same compilation, so a
2290
2259
  // second cargo-napi invocation only buys a duplicate dependency graph
2291
2260
  // (a plain build and a `--target` build write different target/
@@ -2303,32 +2272,11 @@ function createNapiTargets(
2303
2272
  options: {
2304
2273
  cwd: cargoCwd,
2305
2274
  command: hostBuild('dev'),
2306
- ...(hostCompilerEnv ? { env: hostCompilerEnv } : {}),
2307
2275
  },
2308
2276
  configurations: { [RELEASE_CONFIGURATION]: { command: hostBuild('release') } },
2309
2277
  };
2310
2278
  }
2311
2279
 
2312
- for (const triple of config.targets) {
2313
- const convention = NAPI_TARGET_CONVENTIONS[triple];
2314
- if (!convention || !usesNapiCross(convention, hostPlatform)) {
2315
- continue;
2316
- }
2317
- // One toolchain target per triple, shared by every cross build that needs
2318
- // it: the inferred napi-<arch>-linux plus any package-local cli-<arch>-linux.
2319
- // Nx runs a shared dependency once, which is what stops those builds from
2320
- // racing inside the Bun store. See the executor for the failure it removes.
2321
- // The name carries the platform suffix so a Linux platform target's
2322
- // dependency closure stays Linux-only (package-target-policy).
2323
- targets[napiToolchainTargetName(convention)] = {
2324
- executor: '@smoothbricks/nx-plugin:napi-cross-toolchain',
2325
- // Extraction lands in ~/.napi-rs, outside the workspace: a cache hit
2326
- // would skip work a cold home directory still needs.
2327
- cache: false,
2328
- options: { triple },
2329
- };
2330
- }
2331
-
2332
2280
  for (const triple of config.targets) {
2333
2281
  const convention = NAPI_TARGET_CONVENTIONS[triple];
2334
2282
  if (!convention) {
@@ -2336,24 +2284,38 @@ function createNapiTargets(
2336
2284
  }
2337
2285
  const targetName = `napi-${convention.architecture}-${convention.targetFamily}`;
2338
2286
  const outputDirectory = `dist/native/${convention.outputName}`;
2339
- const useNapiCross = usesNapiCross(convention, hostPlatform);
2340
- const crossFlag = useNapiCross ? ' --use-napi-cross' : '';
2341
- const compilerEnv = napiCompilerEnv(convention, hostPlatform);
2287
+ if (convention.targetFamily === 'linux') {
2288
+ // Every host builds a Linux addon the same way: devenv's zig through
2289
+ // cargo-zigbuild, glibc pinned by the triple suffix. The napi CLI cannot
2290
+ // carry that suffix (it looks for the artifact under
2291
+ // target/<triple>.2.17/), so the executor names the `.node` itself.
2292
+ targets[targetName] = {
2293
+ executor: '@smoothbricks/nx-plugin:cargo-zigbuild',
2294
+ cache: true,
2295
+ inputs: napiInputs(projectRoot, repoRooted, []),
2296
+ outputs: [`{projectRoot}/${outputDirectory}`],
2297
+ options: {
2298
+ target: `${triple}.${LINUX_GLIBC_FLOOR}`,
2299
+ manifestPath: posix.join(cargoCwd, config.manifestPath),
2300
+ package: config.cargoPackage,
2301
+ output: `{projectRoot}/${outputDirectory}/${config.binaryName}.${convention.outputName}.node`,
2302
+ },
2303
+ configurations: { [RELEASE_CONFIGURATION]: { release: true } },
2304
+ };
2305
+ continue;
2306
+ }
2307
+ // macOS addons need Apple's SDK, which zig does not carry: the napi CLI
2308
+ // drives the host's own toolchain and writes the platform `.d.ts`.
2342
2309
  const platformBuild = (profile: CargoProfile): string =>
2343
- `${napiCommand} build${CARGO_PROFILE_FLAG[profile]} --platform --no-js --dts ${config.binaryName}.${convention.outputName}.d.ts --target ${triple}${crossFlag} ${commonCommand} --output-dir ${outputPath(outputDirectory)}`;
2310
+ `${napiCommand} build${CARGO_PROFILE_FLAG[profile]} --platform --no-js --dts ${config.binaryName}.${convention.outputName}.d.ts --target ${triple} ${commonCommand} --output-dir ${outputPath(outputDirectory)}`;
2344
2311
  targets[targetName] = {
2345
2312
  executor: 'nx:run-commands',
2346
2313
  cache: true,
2347
- ...(useNapiCross ? { dependsOn: [napiToolchainTargetName(convention)] } : {}),
2348
2314
  inputs: cargoInputs,
2349
2315
  outputs: [`{projectRoot}/${outputDirectory}`],
2350
2316
  options: {
2351
2317
  cwd: cargoCwd,
2352
2318
  command: platformBuild('dev'),
2353
- // Genuine Linux cross-compiles use Clang plus napi-rs's downloaded
2354
- // GNU sysroot. A native Linux target uses Nix's cc wrapper so C build
2355
- // scripts resolve the host libc headers instead.
2356
- ...(compilerEnv ? { env: compilerEnv } : {}),
2357
2319
  },
2358
2320
  configurations: { [RELEASE_CONFIGURATION]: { command: platformBuild('release') } },
2359
2321
  };