nx 24.0.5 → 24.0.6

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 (34) hide show
  1. package/dist/bin/nx.js +2 -8
  2. package/dist/bin/workspace-env.d.ts +7 -0
  3. package/dist/bin/workspace-env.js +20 -0
  4. package/dist/src/command-line/migrate/migrate.js +3 -2
  5. package/dist/src/command-line/nx-cloud/start-nx-agents/command-object.js +1 -1
  6. package/dist/src/command-line/reset/reset.js +13 -1
  7. package/dist/src/daemon/client/daemon-environment.d.ts +5 -3
  8. package/dist/src/daemon/client/daemon-environment.js +15 -3
  9. package/dist/src/executors/run-commands/running-tasks.d.ts +1 -0
  10. package/dist/src/executors/run-commands/running-tasks.js +69 -17
  11. package/dist/src/executors/run-script/run-script.impl.js +11 -3
  12. package/dist/src/native/index.js +31 -3
  13. package/dist/src/native/nx.wasm32-wasi.debug.wasm +0 -0
  14. package/dist/src/native/nx.wasm32-wasi.wasm +0 -0
  15. package/dist/src/native/transform-objects.js +5 -1
  16. package/dist/src/nx-cloud/update-manager.d.ts +6 -1
  17. package/dist/src/nx-cloud/update-manager.js +39 -12
  18. package/dist/src/plugins/js/package-json/create-package-json.d.ts +1 -1
  19. package/dist/src/plugins/js/package-json/create-package-json.js +31 -13
  20. package/dist/src/plugins/js/project-graph/build-dependencies/target-project-locator.js +7 -1
  21. package/dist/src/tasks-runner/running-tasks/batch-process.d.ts +3 -2
  22. package/dist/src/tasks-runner/running-tasks/batch-process.js +7 -3
  23. package/dist/src/utils/cache-directory.d.ts +3 -0
  24. package/dist/src/utils/cache-directory.js +105 -25
  25. package/dist/src/utils/child-process.d.ts +8 -1
  26. package/dist/src/utils/child-process.js +24 -0
  27. package/dist/src/utils/compile-cache.d.ts +6 -8
  28. package/dist/src/utils/compile-cache.js +23 -16
  29. package/dist/src/utils/nx-tmp-dir.d.ts +14 -2
  30. package/dist/src/utils/nx-tmp-dir.js +26 -2
  31. package/dist/src/utils/package-json.js +40 -20
  32. package/dist/src/utils/package-manager.d.ts +15 -0
  33. package/dist/src/utils/package-manager.js +184 -2
  34. package/package.json +11 -11
@@ -14,6 +14,7 @@ const path_1 = require("path");
14
14
  const task_hasher_1 = require("../../../hasher/task-hasher");
15
15
  const output_1 = require("../../../utils/output");
16
16
  const project_graph_pruning_1 = require("../lock-file/project-graph-pruning");
17
+ const logger_1 = require("../../../utils/logger");
17
18
  /**
18
19
  * Creates a package.json in the output directory for support to install dependencies within containers.
19
20
  *
@@ -28,7 +29,7 @@ function createPackageJson(projectName, graph, options = {}, fileMap = null) {
28
29
  const npmDeps = findProjectsNpmDependencies(projectNode, graph, options.target, rootPackageJson, {
29
30
  helperDependencies: options.helperDependencies,
30
31
  isProduction: options.isProduction,
31
- }, fileMap);
32
+ }, fileMap, root);
32
33
  // default package.json if one does not exist
33
34
  let packageJson = {
34
35
  name: projectName,
@@ -211,7 +212,7 @@ function createPackageJson(projectName, graph, options = {}, fileMap = null) {
211
212
  // endregion Overrides/Resolutions
212
213
  return packageJson;
213
214
  }
214
- function findProjectsNpmDependencies(projectNode, graph, target, rootPackageJson, options, fileMap) {
215
+ function findProjectsNpmDependencies(projectNode, graph, target, rootPackageJson, options, fileMap, root = workspace_root_1.workspaceRoot) {
215
216
  if (fileMap == null) {
216
217
  fileMap = (0, nx_deps_cache_1.readFileMapCache)()?.fileMap?.projectFileMap || {};
217
218
  }
@@ -231,7 +232,7 @@ function findProjectsNpmDependencies(projectNode, graph, target, rootPackageJson
231
232
  }
232
233
  seen.add(node.name);
233
234
  npmDeps.dependencies[node.data.packageName] = node.data.version;
234
- recursivelyCollectPeerDependencies(node.name, graph, npmDeps, seen);
235
+ recursivelyCollectPeerDependencies(node.name, graph, npmDeps, seen, root);
235
236
  });
236
237
  // if it's production, we want to ignore all found devDependencies
237
238
  const ignoredDependencies = options.isProduction && rootPackageJson.devDependencies
@@ -240,10 +241,10 @@ function findProjectsNpmDependencies(projectNode, graph, target, rootPackageJson
240
241
  ...Object.keys(rootPackageJson.devDependencies),
241
242
  ]
242
243
  : options.ignoredDependencies || [];
243
- findAllNpmDeps(fileMap, projectNode, graph, npmDeps, seen, ignoredDependencies, dependencyInputs, selfInputs, false);
244
+ findAllNpmDeps(fileMap, projectNode, graph, npmDeps, seen, root, ignoredDependencies, dependencyInputs, selfInputs, false);
244
245
  return npmDeps;
245
246
  }
246
- function findAllNpmDeps(projectFileMap, projectNode, graph, npmDeps, seen, ignoredDependencies, dependencyPatterns, rootPatterns, isTransitiveDependency = false) {
247
+ function findAllNpmDeps(projectFileMap, projectNode, graph, npmDeps, seen, root, ignoredDependencies, dependencyPatterns, rootPatterns, isTransitiveDependency = false) {
247
248
  if (seen.has(projectNode.name))
248
249
  return;
249
250
  seen.add(projectNode.name);
@@ -271,10 +272,10 @@ function findAllNpmDeps(projectFileMap, projectNode, graph, npmDeps, seen, ignor
271
272
  continue;
272
273
  }
273
274
  npmDeps.dependencies[node.data.packageName] = node.data.version;
274
- recursivelyCollectPeerDependencies(node.name, graph, npmDeps, seen);
275
+ recursivelyCollectPeerDependencies(node.name, graph, npmDeps, seen, root);
275
276
  }
276
277
  else if (graph.nodes[dep]) {
277
- findAllNpmDeps(projectFileMap, graph.nodes[dep], graph, npmDeps, seen, ignoredDependencies, dependencyPatterns, undefined, true);
278
+ findAllNpmDeps(projectFileMap, graph.nodes[dep], graph, npmDeps, seen, root, ignoredDependencies, dependencyPatterns, undefined, true);
278
279
  }
279
280
  }
280
281
  }
@@ -294,14 +295,14 @@ function findHelperDependencyNode(graph, projectName, dep) {
294
295
  ? graph.externalNodes[projectDependency.target]
295
296
  : (0, project_graph_pruning_1.findNodeMatchingVersion)(graph, packageName, '*');
296
297
  }
297
- function recursivelyCollectPeerDependencies(projectName, graph, npmDeps, seen) {
298
+ function recursivelyCollectPeerDependencies(projectName, graph, npmDeps, seen, root) {
298
299
  const npmPackage = graph.externalNodes[projectName];
299
300
  if (!npmPackage) {
300
301
  return npmDeps;
301
302
  }
302
303
  const packageName = npmPackage.data.packageName;
303
304
  try {
304
- const packageJson = require(`${packageName}/package.json`);
305
+ const packageJson = readDependencyPackageJson(packageName, root);
305
306
  if (!packageJson.peerDependencies) {
306
307
  return npmDeps;
307
308
  }
@@ -310,22 +311,39 @@ function recursivelyCollectPeerDependencies(projectName, graph, npmDeps, seen) {
310
311
  .map((dependency) => graph.externalNodes[dependency])
311
312
  .filter(Boolean)
312
313
  .forEach((node) => {
314
+ const isOptional = packageJson.peerDependenciesMeta?.[node.data.packageName]?.optional;
313
315
  if (!seen.has(node.name)) {
314
316
  seen.add(node.name);
315
317
  npmDeps.peerDependencies[node.data.packageName] = node.data.version;
316
- if (packageJson.peerDependenciesMeta &&
317
- packageJson.peerDependenciesMeta[node.data.packageName] &&
318
- packageJson.peerDependenciesMeta[node.data.packageName].optional) {
318
+ if (isOptional) {
319
319
  npmDeps.peerDependenciesMeta[node.data.packageName] = {
320
320
  optional: true,
321
321
  };
322
322
  }
323
- recursivelyCollectPeerDependencies(node.name, graph, npmDeps, seen);
323
+ recursivelyCollectPeerDependencies(node.name, graph, npmDeps, seen, root);
324
+ }
325
+ else if (!isOptional) {
326
+ // A peer stays optional only while every host declaring it says so.
327
+ delete npmDeps.peerDependenciesMeta[node.data.packageName];
324
328
  }
325
329
  });
326
330
  return npmDeps;
327
331
  }
328
332
  catch (e) {
333
+ logger_1.logger.verbose(`Could not read the package.json of ${packageName}, so its peer dependencies were not collected: ${e.message}`);
329
334
  return npmDeps;
330
335
  }
331
336
  }
337
+ // Watch builds collect peers on every compilation, so read each manifest once.
338
+ const dependencyPackageJsonCache = new Map();
339
+ function readDependencyPackageJson(packageName, root) {
340
+ const cacheKey = `${root}\0${packageName}`;
341
+ let packageJson = dependencyPackageJsonCache.get(cacheKey);
342
+ if (!packageJson) {
343
+ // nx's own location first: under pnpm it reaches the hidden hoisted
344
+ // .pnpm/node_modules, which the workspace root does not.
345
+ ({ packageJson } = (0, package_json_1.readModulePackageJson)(packageName, [__dirname, root]));
346
+ dependencyPackageJsonCache.set(cacheKey, packageJson);
347
+ }
348
+ return packageJson;
349
+ }
@@ -19,9 +19,15 @@ const typescript_1 = require("../../utils/typescript");
19
19
  const defaultNpmResolutionCache = new Map();
20
20
  const defaultPackageJsonResolutionCache = new Map();
21
21
  const experimentalNodeModules = new Set(['node:sqlite']);
22
+ // Node's isBuiltin is false for Bun's runtime modules (`bun`, `bun:*`).
23
+ function isBunModuleImport(importExpr) {
24
+ return importExpr === 'bun' || importExpr.startsWith('bun:');
25
+ }
22
26
  function isBuiltinModuleImport(importExpr) {
23
27
  const packageName = (0, get_package_name_from_import_path_1.getPackageNameFromImportPath)(importExpr);
24
- return (0, node_module_1.isBuiltin)(packageName) || experimentalNodeModules.has(packageName);
28
+ return ((0, node_module_1.isBuiltin)(packageName) ||
29
+ experimentalNodeModules.has(packageName) ||
30
+ isBunModuleImport(importExpr));
25
31
  }
26
32
  // TypeScript matches the `${configDir}` template case-insensitively and only as a
27
33
  // prefix (commandLineParser.ts `startsWithConfigDirTemplate`).
@@ -77,6 +77,7 @@ export declare class BatchProcess {
77
77
  * summary is still pointing at.
78
78
  */
79
79
  private readonly captureNonce;
80
+ private readonly stdioClosed;
80
81
  constructor(childProcess: ChildProcess, executorName: string,
81
82
  /**
82
83
  * Whether the worker's output is written to the terminal as it streams.
@@ -128,8 +129,8 @@ export declare class BatchProcess {
128
129
  * batch as one fold, so output no task claimed is not lost.
129
130
  *
130
131
  * Not valid to read before `flushCapturedOutput`; that flush ends the stream,
131
- * so a chunk arriving afterwards - a worker's stdout can deliver past the
132
- * exit event `getResults()` settles on - is dropped rather than appended.
132
+ * so a chunk arriving afterwards - a worker can still write after the
133
+ * results message `getResults()` settles on - is dropped rather than appended.
133
134
  */
134
135
  getCapturedOutputPath(): string | undefined;
135
136
  /** Releases the capture file. Safe to call more than once. */
@@ -6,6 +6,7 @@ const crypto_1 = require("crypto");
6
6
  const path_1 = require("path");
7
7
  const native_1 = require("../../native");
8
8
  const cache_1 = require("../cache");
9
+ const child_process_1 = require("../../utils/child-process");
9
10
  const exit_codes_1 = require("../../utils/exit-codes");
10
11
  const output_1 = require("../../utils/output");
11
12
  const batch_messages_1 = require("../batch/batch-messages");
@@ -68,6 +69,7 @@ class BatchProcess {
68
69
  * summary is still pointing at.
69
70
  */
70
71
  this.captureNonce = (0, crypto_1.randomBytes)(4).toString('hex');
72
+ this.stdioClosed = (0, child_process_1.whenStdioClosed)(this.childProcess);
71
73
  this.childProcess.on('message', (message) => {
72
74
  switch (message.type) {
73
75
  case batch_messages_1.BatchMessageType.CompleteTask: {
@@ -294,8 +296,8 @@ class BatchProcess {
294
296
  * batch as one fold, so output no task claimed is not lost.
295
297
  *
296
298
  * Not valid to read before `flushCapturedOutput`; that flush ends the stream,
297
- * so a chunk arriving afterwards - a worker's stdout can deliver past the
298
- * exit event `getResults()` settles on - is dropped rather than appended.
299
+ * so a chunk arriving afterwards - a worker can still write after the
300
+ * results message `getResults()` settles on - is dropped rather than appended.
299
301
  */
300
302
  getCapturedOutputPath() {
301
303
  return this.capturedOutputPath;
@@ -341,7 +343,9 @@ class BatchProcess {
341
343
  new Promise((_, rej) => {
342
344
  this.onExit((code) => {
343
345
  if (code !== 0) {
344
- rej(new Error(`"${this.executorName}" exited unexpectedly with code: ${code}`));
346
+ // A rejection makes the caller flush the capture, which drops
347
+ // whatever the crashed worker wrote that has not been read yet.
348
+ this.stdioClosed.then(() => rej(new Error(`"${this.executorName}" exited unexpectedly with code: ${code}`)));
345
349
  }
346
350
  });
347
351
  }),
@@ -1,3 +1,6 @@
1
+ export declare class UntrustedDataDirectoryError extends Error {
2
+ constructor(kind: string, dir: string, reason: string);
3
+ }
1
4
  /**
2
5
  * `readNxJson` rather than a raw read, so a `cacheDirectory` reached through
3
6
  * `extends` counts. It throws on malformed JSON, which must not take the
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.workspaceDataDirectory = exports.cacheDir = void 0;
3
+ exports.workspaceDataDirectory = exports.cacheDir = exports.UntrustedDataDirectoryError = void 0;
4
4
  exports.readCacheDirectoryProperty = readCacheDirectoryProperty;
5
5
  exports.resetSharedRootCacheForTesting = resetSharedRootCacheForTesting;
6
6
  exports.resolveSharedDataLocation = resolveSharedDataLocation;
@@ -16,6 +16,102 @@ const nx_tmp_dir_1 = require("./nx-tmp-dir");
16
16
  const owned_private_dir_1 = require("./owned-private-dir");
17
17
  const workspace_id_1 = require("./workspace-id");
18
18
  const workspace_root_1 = require("./workspace-root");
19
+ const S_ISVTX = 0o1000;
20
+ const trustedConfiguredDirs = new Set();
21
+ class UntrustedDataDirectoryError extends Error {
22
+ constructor(kind, dir, reason) {
23
+ super(`The configured Nx ${kind} directory ${dir} is not safe to use: ${reason}. ` +
24
+ `Nx restores task outputs and reads cached configuration from this directory, so it must only be modifiable by you, root, or the owner of the workspace. ` +
25
+ `Point it at a directory you own instead, or set NX_ALLOW_INSECURE_DIRECTORIES=true to use it anyway.`);
26
+ this.name = 'UntrustedDataDirectoryError';
27
+ }
28
+ }
29
+ exports.UntrustedDataDirectoryError = UntrustedDataDirectoryError;
30
+ /**
31
+ * Creates a configured data directory and refuses it if a user other than us,
32
+ * root, or the workspace owner could replace its contents. Paths inside the
33
+ * workspace are trusted like the workspace. Group write is an explicit grant.
34
+ */
35
+ function ensureTrustedConfiguredDir(dir, root, kind) {
36
+ const key = `${dir}\0${root}`;
37
+ if (typeof process.getuid !== 'function' || trustedConfiguredDirs.has(key)) {
38
+ return dir;
39
+ }
40
+ // Claim a missing directory under a shared parent before another user can.
41
+ if (!isWithin((0, path_1.resolve)(dir), (0, path_1.resolve)(root)) && nearestExistingIsShared(dir)) {
42
+ try {
43
+ (0, fs_1.mkdirSync)(dir, { recursive: true, mode: 0o775 });
44
+ }
45
+ catch { }
46
+ }
47
+ const trusted = new Set([process.getuid(), 0]);
48
+ try {
49
+ trusted.add((0, fs_1.statSync)(root).uid);
50
+ }
51
+ catch { }
52
+ // The path as written catches a symlink a peer could repoint; the canonical
53
+ // path catches the directories the symlinks lead to.
54
+ for (const [leaf, stop] of [
55
+ [(0, path_1.resolve)(dir), (0, path_1.resolve)(root)],
56
+ [(0, owned_private_dir_1.canonicalDir)(dir), (0, owned_private_dir_1.canonicalDir)(root)],
57
+ ]) {
58
+ if (isWithin(leaf, stop)) {
59
+ continue;
60
+ }
61
+ for (let p = leaf; p !== stop; p = (0, path_1.dirname)(p)) {
62
+ const reason = untrustedReason(p, p === leaf, trusted);
63
+ if (reason) {
64
+ if (process.env.NX_ALLOW_INSECURE_DIRECTORIES !== 'true') {
65
+ throw new UntrustedDataDirectoryError(kind, dir, reason);
66
+ }
67
+ console.warn(`NX_ALLOW_INSECURE_DIRECTORIES is set, so Nx is using the ${kind} directory ${dir} although ${reason}.`);
68
+ trustedConfiguredDirs.add(key);
69
+ return dir;
70
+ }
71
+ if ((0, path_1.dirname)(p) === p) {
72
+ break;
73
+ }
74
+ }
75
+ }
76
+ trustedConfiguredDirs.add(key);
77
+ return dir;
78
+ }
79
+ function nearestExistingIsShared(dir) {
80
+ for (let p = (0, path_1.resolve)(dir);; p = (0, path_1.dirname)(p)) {
81
+ try {
82
+ return ((0, fs_1.statSync)(p).mode & 0o002) !== 0;
83
+ }
84
+ catch { }
85
+ if ((0, path_1.dirname)(p) === p) {
86
+ return false;
87
+ }
88
+ }
89
+ }
90
+ function isWithin(path, root) {
91
+ const rel = (0, path_1.relative)(root, path);
92
+ return !rel.startsWith('..') && !(0, path_1.isAbsolute)(rel);
93
+ }
94
+ function untrustedReason(path, isLeaf, trusted) {
95
+ let stats;
96
+ try {
97
+ stats = (0, fs_1.lstatSync)(path);
98
+ }
99
+ catch {
100
+ return undefined;
101
+ }
102
+ if (!trusted.has(stats.uid)) {
103
+ return `${path} is owned by uid ${stats.uid}`;
104
+ }
105
+ if (!stats.isDirectory() || !(stats.mode & 0o002)) {
106
+ return undefined;
107
+ }
108
+ if (isLeaf) {
109
+ return `${path} is writable by other users`;
110
+ }
111
+ return stats.mode & S_ISVTX
112
+ ? undefined
113
+ : `${path} is writable by other users and not sticky`;
114
+ }
19
115
  function absolutePath(root, path) {
20
116
  if ((0, path_1.isAbsolute)(path)) {
21
117
  return path;
@@ -30,7 +126,7 @@ function cacheDirectory(root, cacheDirectory) {
30
126
  cacheDirectory = cacheDirFromEnv;
31
127
  }
32
128
  if (cacheDirectory) {
33
- return absolutePath(root, cacheDirectory);
129
+ return ensureTrustedConfiguredDir(absolutePath(root, cacheDirectory), root, 'cache');
34
130
  }
35
131
  else {
36
132
  return defaultCacheDirectory(root);
@@ -81,30 +177,12 @@ const SHARED_SEGMENT = {
81
177
  'workspace-data': 'databases',
82
178
  };
83
179
  const SHARED_DATA_KINDS = Object.keys(SHARED_SEGMENT);
84
- /**
85
- * Whether `~/.nx` is somewhere other than the shared container.
86
- *
87
- * `homedir()` honours `$HOME`, so with `HOME=/tmp` the two are one path -- and
88
- * that container is deliberately world-writable (`1777`) so peers can each
89
- * create their own subtree under it. Build outputs and the graph DB get
90
- * replayed into the workspace and supply target commands, so they cannot live
91
- * anywhere a peer can replace them.
92
- *
93
- * `daemon/tmp-dir.ts` makes the same check for the socket tier. It cannot be
94
- * shared from there: that module imports this one.
95
- */
96
- function homeDirIsDistinctFromSharedTmp() {
97
- if (!nx_tmp_dir_1.NX_HOME_TMP_DIR) {
98
- return false;
99
- }
100
- const home = (0, owned_private_dir_1.canonicalDir)(nx_tmp_dir_1.NX_HOME_TMP_DIR);
101
- return ![nx_tmp_dir_1.NX_TMP_DIR, nx_tmp_dir_1.NX_USER_TMP_DIR].some((shared) => (0, owned_private_dir_1.canonicalDir)(shared) === home);
102
- }
103
180
  /** Memoizes the verdict below, per repository. */
104
181
  const sharedRootUsable = new Map();
105
182
  /** The establish verdict is cached per process; tests need it cleared. */
106
183
  function resetSharedRootCacheForTesting() {
107
184
  sharedRootUsable.clear();
185
+ trustedConfiguredDirs.clear();
108
186
  workspaceIds.clear();
109
187
  resolvedLocations.clear();
110
188
  }
@@ -116,7 +194,7 @@ function resetSharedRootCacheForTesting() {
116
194
  * it never wrote. One answer serves both.
117
195
  */
118
196
  function establishUserRoot(workspaceId) {
119
- if (!nx_tmp_dir_1.NX_HOME_TMP_DIR || !homeDirIsDistinctFromSharedTmp()) {
197
+ if (!nx_tmp_dir_1.NX_HOME_TMP_DIR || !(0, nx_tmp_dir_1.homeDirIsDistinctFromSharedTmp)()) {
120
198
  return { kind: 'not-a-directory', dir: nx_tmp_dir_1.NX_HOME_TMP_DIR ?? '~/.nx' };
121
199
  }
122
200
  const repoRoot = (0, path_1.join)(nx_tmp_dir_1.NX_HOME_TMP_DIR, sharedDirName(workspaceId));
@@ -331,7 +409,9 @@ function cacheDirectoryForWorkspace(root) {
331
409
  */
332
410
  exports.workspaceDataDirectory = workspaceDataDirectoryForWorkspace(workspace_root_1.workspaceRoot);
333
411
  function workspaceDataDirectoryForWorkspace(workspaceRoot) {
334
- return absolutePath(workspaceRoot, process.env.NX_WORKSPACE_DATA_DIRECTORY ??
335
- process.env.NX_PROJECT_GRAPH_CACHE_DIRECTORY ??
336
- defaultWorkspaceDataDirectory(workspaceRoot));
412
+ const configured = process.env.NX_WORKSPACE_DATA_DIRECTORY ??
413
+ process.env.NX_PROJECT_GRAPH_CACHE_DIRECTORY;
414
+ return configured !== undefined
415
+ ? ensureTrustedConfiguredDir(absolutePath(workspaceRoot, configured), workspaceRoot, 'workspace data')
416
+ : defaultWorkspaceDataDirectory(workspaceRoot);
337
417
  }
@@ -1,4 +1,4 @@
1
- import { type ExecOptions, type ExecSyncOptions, type SpawnSyncOptions } from 'child_process';
1
+ import { type ChildProcess as NodeChildProcess, type ExecOptions, type ExecSyncOptions, type SpawnSyncOptions } from 'child_process';
2
2
  import { PackageManagerCommands } from './package-manager';
3
3
  import { ChildProcess } from '../native';
4
4
  export declare function getRunNxBaseCommand(packageManagerCommand?: PackageManagerCommands, cwd?: string): string;
@@ -41,6 +41,13 @@ export declare function runNxAsync(cmd: string, options?: ExecOptions & {
41
41
  silent?: boolean;
42
42
  packageManagerCommand?: PackageManagerCommands;
43
43
  }): Promise<void>;
44
+ export declare function hasExited(child: NodeChildProcess): boolean;
45
+ /**
46
+ * Settles once `child`'s stdio has closed, which Node reports after 'exit'.
47
+ * Call it at spawn time: a 'close' emitted before the call is missed. Gives up
48
+ * 1s after 'exit', since a descendant can keep the pipes open indefinitely.
49
+ */
50
+ export declare function whenStdioClosed(child: NodeChildProcess): Promise<void>;
44
51
  export declare class PseudoTtyProcess {
45
52
  private childProcess;
46
53
  isAlive: boolean;
@@ -7,6 +7,8 @@ exports.readInstalledNxBin = readInstalledNxBin;
7
7
  exports.runNxArgvSync = runNxArgvSync;
8
8
  exports.runNxSync = runNxSync;
9
9
  exports.runNxAsync = runNxAsync;
10
+ exports.hasExited = hasExited;
11
+ exports.whenStdioClosed = whenStdioClosed;
10
12
  const child_process_1 = require("child_process");
11
13
  const fs_1 = require("fs");
12
14
  const path_1 = require("path");
@@ -160,6 +162,28 @@ async function runNxAsync(cmd, options) {
160
162
  }
161
163
  });
162
164
  }
165
+ function hasExited(child) {
166
+ return child.exitCode !== null || child.signalCode !== null;
167
+ }
168
+ /**
169
+ * Settles once `child`'s stdio has closed, which Node reports after 'exit'.
170
+ * Call it at spawn time: a 'close' emitted before the call is missed. Gives up
171
+ * 1s after 'exit', since a descendant can keep the pipes open indefinitely.
172
+ */
173
+ function whenStdioClosed(child) {
174
+ return new Promise((resolve) => {
175
+ let timeout;
176
+ child.once('close', () => {
177
+ clearTimeout(timeout);
178
+ resolve();
179
+ });
180
+ child.once('exit', () => {
181
+ // Node runs expired timers before it reads the pipes, so a blocked event
182
+ // loop would otherwise give up on output that is already there.
183
+ timeout = setTimeout(() => setImmediate(resolve), 1000);
184
+ });
185
+ });
186
+ }
163
187
  class PseudoTtyProcess {
164
188
  constructor(childProcess) {
165
189
  this.childProcess = childProcess;
@@ -1,15 +1,13 @@
1
1
  /**
2
2
  * Enables V8's on-disk bytecode cache for the current process.
3
3
  *
4
- * Calls `module.enableCompileCache()` with no arguments and lets Node pick
5
- * the location, which means Node's standard env vars work transparently:
6
- * - `NODE_COMPILE_CACHE=<dir>` — override the cache directory.
4
+ * The cache lives in the current user's owner-only Nx tmp directory, because
5
+ * Node's default location is a shared `os.tmpdir()` path whose contents are
6
+ * loaded regardless of who created them. If that directory cannot be
7
+ * established securely, the cache stays off. Node's env vars still apply:
8
+ * - `NODE_COMPILE_CACHE=<dir>` — the user's chosen directory wins.
7
9
  * - `NODE_DISABLE_COMPILE_CACHE=1` — disable entirely.
8
10
  *
9
- * The default location lives under the OS temp dir keyed by V8 version, so
10
- * the cache is shared across workspaces and self-invalidates on Node
11
- * upgrades — no nx-specific cleanup needed.
12
- *
13
11
  * Called at the entry point of every long-lived nx process (main CLI,
14
12
  * daemon, plugin workers) via `enable-compile-cache.ts`, which side-effects
15
13
  * this on import.
@@ -21,4 +19,4 @@
21
19
  * Errors are swallowed — the compile cache is a pure performance
22
20
  * optimization and must never break the CLI.
23
21
  */
24
- export declare function enableCompileCache(...override: [unknown?]): boolean;
22
+ export declare function enableCompileCache(impl?: unknown, cacheDir?: string): boolean;
@@ -3,18 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.enableCompileCache = enableCompileCache;
4
4
  const tslib_1 = require("tslib");
5
5
  const nodeModule = tslib_1.__importStar(require("node:module"));
6
+ const node_path_1 = require("node:path");
7
+ const nx_tmp_dir_1 = require("./nx-tmp-dir");
8
+ const owned_private_dir_1 = require("./owned-private-dir");
9
+ const COMPILE_CACHE_DIR = (0, node_path_1.join)(nx_tmp_dir_1.NX_USER_TMP_DIR, 'compile-cache');
6
10
  /**
7
11
  * Enables V8's on-disk bytecode cache for the current process.
8
12
  *
9
- * Calls `module.enableCompileCache()` with no arguments and lets Node pick
10
- * the location, which means Node's standard env vars work transparently:
11
- * - `NODE_COMPILE_CACHE=<dir>` — override the cache directory.
13
+ * The cache lives in the current user's owner-only Nx tmp directory, because
14
+ * Node's default location is a shared `os.tmpdir()` path whose contents are
15
+ * loaded regardless of who created them. If that directory cannot be
16
+ * established securely, the cache stays off. Node's env vars still apply:
17
+ * - `NODE_COMPILE_CACHE=<dir>` — the user's chosen directory wins.
12
18
  * - `NODE_DISABLE_COMPILE_CACHE=1` — disable entirely.
13
19
  *
14
- * The default location lives under the OS temp dir keyed by V8 version, so
15
- * the cache is shared across workspaces and self-invalidates on Node
16
- * upgrades — no nx-specific cleanup needed.
17
- *
18
20
  * Called at the entry point of every long-lived nx process (main CLI,
19
21
  * daemon, plugin workers) via `enable-compile-cache.ts`, which side-effects
20
22
  * this on import.
@@ -27,20 +29,25 @@ const nodeModule = tslib_1.__importStar(require("node:module"));
27
29
  * optimization and must never break the CLI.
28
30
  */
29
31
  function enableCompileCache(
30
- // Test seam: production callers omit this. `unknown` (rather than
31
- // `EnableCompileCacheFn | undefined`) lets tests pass a non-function to
32
- // simulate pre-22.8 Node where `module.enableCompileCache` is missing. We
33
- // read `arguments.length` so callers can *explicitly* pass `undefined`.
34
- ...override) {
32
+ // Test seams: production callers omit both. A non-function `impl` simulates
33
+ // pre-22.8 Node, where `module.enableCompileCache` is missing.
34
+ impl = nodeModule.enableCompileCache, cacheDir = COMPILE_CACHE_DIR) {
35
35
  if (process.env.NX_COMPILE_CACHE === 'false')
36
36
  return false;
37
- const impl = override.length === 0
38
- ? nodeModule.enableCompileCache
39
- : override[0];
40
37
  if (typeof impl !== 'function')
41
38
  return false;
42
39
  try {
43
- impl();
40
+ if (process.env.NODE_COMPILE_CACHE) {
41
+ impl();
42
+ return true;
43
+ }
44
+ const userRoot = (0, node_path_1.dirname)(cacheDir);
45
+ if ((0, owned_private_dir_1.ensureSafeSharedRoot)((0, node_path_1.dirname)(userRoot)).status !== 'ok' ||
46
+ (0, owned_private_dir_1.ensureOwnedPrivateDir)(userRoot).status !== 'ok' ||
47
+ (0, owned_private_dir_1.ensureOwnedPrivateDir)(cacheDir).status !== 'ok') {
48
+ return false;
49
+ }
50
+ impl(cacheDir);
44
51
  return true;
45
52
  }
46
53
  catch {
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * Stable root for Nx runtime artifacts that need an OS tmp location. On POSIX a
3
3
  * literal `/tmp`, not `os.tmpdir()`, which honours `$TMPDIR` — per-user on
4
- * macOS, rewritten by sandboxes, stripped from the daemon env, so client and
5
- * daemon would disagree. Windows has no `/tmp`, and `%TMP%` is already per-user
4
+ * macOS, rewritten by sandboxes, fixed in the daemon at spawn and never
5
+ * reflected from later clients, so client and daemon would disagree. Windows has no `/tmp`, and `%TMP%` is already per-user
6
6
  * and stable, so it keeps `os.tmpdir()`.
7
7
  *
8
8
  * Consumed by the native binding loader, so keep this file limited to local
@@ -21,3 +21,15 @@ export declare const NX_TMP_DIR: string;
21
21
  export declare const NX_USER_TMP_DIR: string;
22
22
  /** Runtime root under the user's home, used when the shared container cannot be established. */
23
23
  export declare const NX_HOME_TMP_DIR: string;
24
+ /**
25
+ * Whether the home directory lies outside the shared temp dir.
26
+ *
27
+ * `homedir()` honours `$HOME`, so `HOME=/tmp` or `HOME=/tmp/foo` puts `~/.nx`
28
+ * at or under a directory every user can write to (`1777`). Peers can plant
29
+ * files in its ancestors and, at `HOME=/tmp`, own the `.nx` container itself.
30
+ * Nothing that Nx later executes, installs into, or replays into the workspace
31
+ * can live there.
32
+ *
33
+ * `native/utils/socket_path.rs` makes only the same-path check, for sockets.
34
+ */
35
+ export declare function homeDirIsDistinctFromSharedTmp(): boolean;
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NX_HOME_TMP_DIR = exports.NX_USER_TMP_DIR = exports.NX_TMP_DIR = exports.NX_TMP_DIR_POSIX = void 0;
4
+ exports.homeDirIsDistinctFromSharedTmp = homeDirIsDistinctFromSharedTmp;
4
5
  const node_os_1 = require("node:os");
5
6
  const node_path_1 = require("node:path");
6
7
  const owned_private_dir_1 = require("./owned-private-dir");
7
8
  /**
8
9
  * Stable root for Nx runtime artifacts that need an OS tmp location. On POSIX a
9
10
  * literal `/tmp`, not `os.tmpdir()`, which honours `$TMPDIR` — per-user on
10
- * macOS, rewritten by sandboxes, stripped from the daemon env, so client and
11
- * daemon would disagree. Windows has no `/tmp`, and `%TMP%` is already per-user
11
+ * macOS, rewritten by sandboxes, fixed in the daemon at spawn and never
12
+ * reflected from later clients, so client and daemon would disagree. Windows has no `/tmp`, and `%TMP%` is already per-user
12
13
  * and stable, so it keeps `os.tmpdir()`.
13
14
  *
14
15
  * Consumed by the native binding loader, so keep this file limited to local
@@ -43,3 +44,26 @@ function resolveHomeTmpDir() {
43
44
  return undefined;
44
45
  }
45
46
  }
47
+ /**
48
+ * Whether the home directory lies outside the shared temp dir.
49
+ *
50
+ * `homedir()` honours `$HOME`, so `HOME=/tmp` or `HOME=/tmp/foo` puts `~/.nx`
51
+ * at or under a directory every user can write to (`1777`). Peers can plant
52
+ * files in its ancestors and, at `HOME=/tmp`, own the `.nx` container itself.
53
+ * Nothing that Nx later executes, installs into, or replays into the workspace
54
+ * can live there.
55
+ *
56
+ * `native/utils/socket_path.rs` makes only the same-path check, for sockets.
57
+ */
58
+ function homeDirIsDistinctFromSharedTmp() {
59
+ if (!exports.NX_HOME_TMP_DIR) {
60
+ return false;
61
+ }
62
+ const home = (0, owned_private_dir_1.canonicalDir)((0, node_path_1.dirname)(exports.NX_HOME_TMP_DIR));
63
+ const fromSharedTmp = (0, node_path_1.relative)((0, owned_private_dir_1.canonicalDir)((0, node_path_1.dirname)(exports.NX_TMP_DIR)), home);
64
+ const atOrUnderSharedTmp = fromSharedTmp === '' ||
65
+ (fromSharedTmp !== '..' &&
66
+ !fromSharedTmp.startsWith('..' + node_path_1.sep) &&
67
+ !(0, node_path_1.isAbsolute)(fromSharedTmp));
68
+ return !atOrUnderSharedTmp;
69
+ }