nx 22.7.8 → 22.7.10

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 (40) hide show
  1. package/dist/src/command-line/reset/reset.js +6 -1
  2. package/dist/src/daemon/client/client.d.ts +45 -3
  3. package/dist/src/daemon/client/client.js +138 -42
  4. package/dist/src/daemon/client/daemon-socket-messenger.js +4 -0
  5. package/dist/src/daemon/message-types/daemon-message.d.ts +33 -0
  6. package/dist/src/daemon/message-types/daemon-message.js +48 -0
  7. package/dist/src/daemon/server/server.js +22 -0
  8. package/dist/src/daemon/server/shutdown-utils.d.ts +5 -0
  9. package/dist/src/daemon/server/shutdown-utils.js +11 -4
  10. package/dist/src/daemon/socket-utils.d.ts +1 -0
  11. package/dist/src/daemon/socket-utils.js +24 -3
  12. package/dist/src/daemon/tmp-dir.d.ts +43 -3
  13. package/dist/src/daemon/tmp-dir.js +437 -15
  14. package/dist/src/devkit-internals.d.ts +1 -0
  15. package/dist/src/devkit-internals.js +4 -1
  16. package/dist/src/native/index.js +71 -28
  17. package/dist/src/native/native-file-cache-location.d.ts +17 -1
  18. package/dist/src/native/native-file-cache-location.js +86 -12
  19. package/dist/src/native/nx.wasm32-wasi.debug.wasm +0 -0
  20. package/dist/src/native/nx.wasm32-wasi.wasm +0 -0
  21. package/dist/src/project-graph/plugins/isolation/isolated-plugin.d.ts +1 -0
  22. package/dist/src/project-graph/plugins/isolation/isolated-plugin.js +18 -1
  23. package/dist/src/project-graph/plugins/isolation/messaging.d.ts +16 -3
  24. package/dist/src/project-graph/plugins/isolation/messaging.js +3 -0
  25. package/dist/src/project-graph/plugins/isolation/plugin-worker.js +58 -8
  26. package/dist/src/project-graph/project-graph.js +20 -0
  27. package/dist/src/utils/nx-tmp-dir.d.ts +18 -0
  28. package/dist/src/utils/nx-tmp-dir.js +40 -0
  29. package/dist/src/utils/owned-private-dir.d.ts +132 -0
  30. package/dist/src/utils/owned-private-dir.js +323 -0
  31. package/dist/src/utils/package-json.js +13 -2
  32. package/dist/src/utils/path.d.ts +6 -0
  33. package/dist/src/utils/path.js +15 -0
  34. package/dist/src/utils/safe-spawn.d.ts +24 -0
  35. package/dist/src/utils/safe-spawn.js +104 -0
  36. package/dist/src/utils/shell-quoting.d.ts +15 -0
  37. package/dist/src/utils/shell-quoting.js +35 -0
  38. package/dist/src/utils/wait-for-socket-connection.d.ts +11 -0
  39. package/dist/src/utils/wait-for-socket-connection.js +14 -8
  40. package/package.json +11 -11
@@ -1,15 +1,11 @@
1
1
  const { join, basename } = require('path');
2
- const {
3
- copyFileSync,
4
- existsSync,
5
- mkdirSync,
6
- renameSync,
7
- statSync,
8
- unlinkSync,
9
- } = require('fs');
2
+ const { copyFileSync, renameSync, statSync, unlinkSync } = require('fs');
3
+ const { createHash } = require('crypto');
10
4
  const Module = require('module');
11
5
  const { nxVersion } = require('../utils/versions');
12
- const { getNativeFileCacheLocation } = require('./native-file-cache-location');
6
+ const {
7
+ ensureSecureNativeFileCacheLocation,
8
+ } = require('./native-file-cache-location');
13
9
 
14
10
  const MAX_COPY_RETRIES = 3;
15
11
 
@@ -73,6 +69,14 @@ function statsOrNull(path) {
73
69
  }
74
70
  }
75
71
 
72
+ function removeIfPresent(path) {
73
+ try {
74
+ unlinkSync(path);
75
+ } catch {
76
+ // Ignore cleanup errors
77
+ }
78
+ }
79
+
76
80
  function isNoExecError(e) {
77
81
  return e.code === 'EACCES' || e.code === 'EPERM';
78
82
  }
@@ -95,20 +99,44 @@ Module._load = function (request, parent, isMain) {
95
99
  const nativeLocation = require.resolve(modulePath);
96
100
  const fileName = basename(nativeLocation);
97
101
 
98
- // we copy the file to a workspace-scoped tmp directory and prefix with nxVersion to avoid stale files being loaded
99
- const nativeFileCacheLocation = getNativeFileCacheLocation();
102
+ // null when the cache dir cannot be created *and trusted*; loading in place
103
+ // from node_modules is safer than loading out of a directory we don't own.
104
+ const nativeFileCacheLocation = ensureSecureNativeFileCacheLocation();
105
+ if (!nativeFileCacheLocation) {
106
+ return originalLoad.apply(this, [nativeLocation, parent, isMain]);
107
+ }
108
+
109
+ // nxVersion alone is not enough: in a source checkout it is the placeholder
110
+ // 0.0.1 for every worktree, so hashing the resolved binding path keeps each
111
+ // checkout in its own entry.
112
+ const cacheKey =
113
+ nxVersion +
114
+ '-' +
115
+ createHash('sha256')
116
+ .update(nativeLocation)
117
+ .digest('hex')
118
+ .substring(0, 12);
119
+
100
120
  // This is a path to copy to, not the one that gets loaded
101
121
  const tmpTmpFile = join(
102
122
  nativeFileCacheLocation,
103
- nxVersion + '-' + Math.random() + fileName
123
+ cacheKey + '-' + Math.random() + fileName
104
124
  );
105
125
  // This is the path that will get loaded
106
- const tmpFile = join(nativeFileCacheLocation, nxVersion + '-' + fileName);
107
- const expectedFileSize = statSync(nativeLocation).size;
126
+ const tmpFile = join(nativeFileCacheLocation, cacheKey + '-' + fileName);
127
+ const sourceStats = statSync(nativeLocation);
128
+ const expectedFileSize = sourceStats.size;
108
129
  const existingFileStats = statsOrNull(tmpFile);
109
130
 
110
- // If the file to be loaded already exists, just load it
111
- if (existingFileStats?.size === expectedFileSize) {
131
+ // Size alone does not detect a rebuilt binding: a small Rust edit routinely
132
+ // produces a byte-identical size. The copy is taken after the build, so a
133
+ // cache entry older than the source means the source was rebuilt since.
134
+ const isFresh =
135
+ existingFileStats?.size === expectedFileSize &&
136
+ existingFileStats.mtimeMs >= sourceStats.mtimeMs;
137
+
138
+ // If the file to be loaded already exists and is current, just load it
139
+ if (isFresh) {
112
140
  try {
113
141
  return originalLoad.apply(this, [tmpFile, parent, isMain]);
114
142
  } catch (e) {
@@ -119,20 +147,36 @@ Module._load = function (request, parent, isMain) {
119
147
  throw e;
120
148
  }
121
149
  }
122
- if (!existsSync(nativeFileCacheLocation)) {
123
- mkdirSync(nativeFileCacheLocation, { recursive: true });
124
- }
125
150
 
126
151
  // Retry copying up to 3 times, validating after each copy
152
+ let attemptsMade = 0;
153
+ let cacheError = null;
127
154
  for (let attempt = 1; attempt <= MAX_COPY_RETRIES; attempt++) {
155
+ attemptsMade = attempt;
128
156
  // First copy to a unique location for each process
129
- copyFileSync(nativeLocation, tmpTmpFile);
157
+ try {
158
+ copyFileSync(nativeLocation, tmpTmpFile);
159
+ } catch (e) {
160
+ // Permission errors won't heal on retry, and a throwing copy can still
161
+ // have left a partial file behind.
162
+ cacheError = e;
163
+ removeIfPresent(tmpTmpFile);
164
+ break;
165
+ }
130
166
 
131
167
  // Validate the copy - check file size matches expected
132
168
  const copiedFileStats = statsOrNull(tmpTmpFile);
133
169
  if (copiedFileStats?.size === expectedFileSize) {
134
170
  // Copy succeeded, rename to final location and load
135
- renameSync(tmpTmpFile, tmpFile);
171
+ try {
172
+ renameSync(tmpTmpFile, tmpFile);
173
+ } catch (e) {
174
+ // Unguarded, this throws out of `require` and native-bindings.js
175
+ // reports it as a missing optional dependency, which it is not.
176
+ cacheError = e;
177
+ removeIfPresent(tmpTmpFile);
178
+ break;
179
+ }
136
180
  try {
137
181
  return originalLoad.apply(this, [tmpFile, parent, isMain]);
138
182
  } catch (e) {
@@ -145,16 +189,15 @@ Module._load = function (request, parent, isMain) {
145
189
  }
146
190
 
147
191
  // Copy failed validation, clean up the malformed file
148
- try {
149
- unlinkSync(tmpTmpFile);
150
- } catch {
151
- // Ignore cleanup errors
152
- }
192
+ removeIfPresent(tmpTmpFile);
153
193
  }
154
194
 
155
- // All retries failed - warn and load from original location
195
+ // Name the errno: ENOSPC, EROFS, EACCES and EDQUOT are all actionable and
196
+ // otherwise indistinguishable.
156
197
  console.warn(
157
- `Warning: Failed to copy native module to cache after ${MAX_COPY_RETRIES} attempts. ` +
198
+ `Warning: Failed to copy native module to cache after ${attemptsMade} attempt${
199
+ attemptsMade === 1 ? '' : 's'
200
+ }${cacheError ? ` (${cacheError.code ?? cacheError.message})` : ''}. ` +
158
201
  `Loading from original location instead. ` +
159
202
  `This may cause file locking issues on Windows.`
160
203
  );
@@ -1 +1,17 @@
1
- export declare function getNativeFileCacheLocation(): string;
1
+ /**
2
+ * Path of the current user's native binary cache. Nothing about the constant is
3
+ * owner-only — `ensureSecureNativeFileCacheLocation` is what establishes that,
4
+ * and on Windows nothing does, since the OS temp dir is already per-account.
5
+ */
6
+ export declare const NATIVE_CACHE_ROOT: string;
7
+ export declare function getNativeFileCacheLocationToDelete(): string | null;
8
+ /**
9
+ * Create the native file cache dir, or return `null` if it cannot be created
10
+ * *securely* — in which case the caller loads the binding in place.
11
+ *
12
+ * The stable top-level container is verified as safe for private children. The
13
+ * uid directory and every directory loaded through are owner-only.
14
+ * `ensureOwnedPrivateDir` refuses a directory or symlink another local user
15
+ * planted before us.
16
+ */
17
+ export declare function ensureSecureNativeFileCacheLocation(cacheRoot?: string): string | null;
@@ -1,23 +1,97 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getNativeFileCacheLocation = getNativeFileCacheLocation;
4
- const os_1 = require("os");
3
+ exports.NATIVE_CACHE_ROOT = void 0;
4
+ exports.getNativeFileCacheLocationToDelete = getNativeFileCacheLocationToDelete;
5
+ exports.ensureSecureNativeFileCacheLocation = ensureSecureNativeFileCacheLocation;
5
6
  const path_1 = require("path");
6
- const crypto_1 = require("crypto");
7
- const workspace_root_1 = require("../utils/workspace-root");
7
+ const fs_1 = require("fs");
8
+ const nx_tmp_dir_1 = require("../utils/nx-tmp-dir");
8
9
  const versions_1 = require("../utils/versions");
9
- function getNativeFileCacheLocation() {
10
- if (process.env.NX_NATIVE_FILE_CACHE_DIRECTORY) {
11
- return process.env.NX_NATIVE_FILE_CACHE_DIRECTORY;
10
+ const owned_private_dir_1 = require("../utils/owned-private-dir");
11
+ /**
12
+ * Path of the current user's native binary cache. Nothing about the constant is
13
+ * owner-only — `ensureSecureNativeFileCacheLocation` is what establishes that,
14
+ * and on Windows nothing does, since the OS temp dir is already per-account.
15
+ */
16
+ exports.NATIVE_CACHE_ROOT = (0, path_1.join)(nx_tmp_dir_1.NX_USER_TMP_DIR, 'native-cache');
17
+ /**
18
+ * The configured cache dir, normalized. `resolve` strips a trailing slash, which
19
+ * would otherwise defeat the guards downstream: `lstat` on a path ending in `/`
20
+ * resolves a symlink rather than reporting it, and `O_NOFOLLOW` then opens the
21
+ * target. `NX_SOCKET_DIR` is normalized for the same reason.
22
+ */
23
+ function configuredCacheDir() {
24
+ const dir = process.env.NX_NATIVE_FILE_CACHE_DIRECTORY;
25
+ return dir ? (0, path_1.resolve)(dir) : undefined;
26
+ }
27
+ function getNativeFileCacheLocationToDelete() {
28
+ const configured = configuredCacheDir();
29
+ if (configured !== undefined) {
30
+ // Checked for ownership before it is handed to a recursive delete: being
31
+ // configured does not make it ours. Weaker than the load path below, which
32
+ // also enforces mode and validates the version directory.
33
+ return (0, owned_private_dir_1.isOwnedRealDirectory)(configured) ? configured : null;
12
34
  }
13
- else {
14
- const hash = (0, crypto_1.createHash)('sha256').update(workspace_root_1.workspaceRoot).update(versions_1.nxVersion);
35
+ return (0, owned_private_dir_1.isSafeSharedRoot)(nx_tmp_dir_1.NX_TMP_DIR).status === 'ok' &&
36
+ (0, owned_private_dir_1.isOwnedRealDirectory)(nx_tmp_dir_1.NX_USER_TMP_DIR) &&
37
+ (0, owned_private_dir_1.isOwnedRealDirectory)(exports.NATIVE_CACHE_ROOT)
38
+ ? (0, path_1.join)(exports.NATIVE_CACHE_ROOT, versions_1.nxVersion)
39
+ : null;
40
+ }
41
+ /**
42
+ * Create the native file cache dir, or return `null` if it cannot be created
43
+ * *securely* — in which case the caller loads the binding in place.
44
+ *
45
+ * The stable top-level container is verified as safe for private children. The
46
+ * uid directory and every directory loaded through are owner-only.
47
+ * `ensureOwnedPrivateDir` refuses a directory or symlink another local user
48
+ * planted before us.
49
+ */
50
+ function ensureSecureNativeFileCacheLocation(
51
+ // Test seam: lets a spec plant a hostile directory under a root it controls.
52
+ cacheRoot = exports.NATIVE_CACHE_ROOT) {
53
+ const configured = configuredCacheDir();
54
+ if (configured !== undefined) {
55
+ const dir = configured;
15
56
  try {
16
- hash.update((0, os_1.userInfo)().username);
57
+ // Held to the same 0700 bar as the default location: a `.node` is loaded
58
+ // out of here. Unlike NX_SOCKET_DIR it is not additionally refused for
59
+ // naming one of Nx's own roots, which is the only case that throws.
60
+ (0, fs_1.mkdirSync)((0, path_1.dirname)(dir), { recursive: true });
61
+ const established = (0, owned_private_dir_1.ensureOwnedPrivateDir)(dir);
62
+ if (established.status !== 'ok') {
63
+ // The guard's own reason rather than a fixed sentence: `not-created
64
+ // (EACCES)` and `foreign-owner` are exactly what the catch below
65
+ // promises to tell apart, and one string for all of them cannot.
66
+ throw new owned_private_dir_1.DirectoryRefusedError(established.refusal);
67
+ }
68
+ return dir;
17
69
  }
18
70
  catch (e) {
19
- // if there's no user, we only use the workspace root for the hash and move on
71
+ // Never discard a configured directory silently — the socket directory
72
+ // follows the same rule. A typo, EACCES or EROFS is otherwise
73
+ // indistinguishable from a working cache.
74
+ console.warn(`Nx could not use the configured native file cache directory ${dir} (${e?.code ?? e?.message ?? e}). Loading the native binding in place instead.`);
75
+ return null;
76
+ }
77
+ }
78
+ const userRoot = (0, path_1.dirname)(cacheRoot);
79
+ const sharedRoot = (0, path_1.dirname)(userRoot);
80
+ // Outermost first: the stable shared container must either belong to root or
81
+ // to us, and must be sticky if peers can write there. The uid directory then
82
+ // becomes the owner-only boundary for sockets and native-cache alike.
83
+ if ((0, owned_private_dir_1.ensureSafeSharedRoot)(sharedRoot).status !== 'ok') {
84
+ return null;
85
+ }
86
+ for (const root of [userRoot, cacheRoot]) {
87
+ if ((0, owned_private_dir_1.ensureOwnedPrivateDir)(root).status !== 'ok') {
88
+ return null;
20
89
  }
21
- return (0, path_1.join)((0, os_1.tmpdir)(), `nx-native-file-cache-${hash.digest('hex').substring(0, 7)}`);
22
90
  }
91
+ // Verified, not assumed: this is the directory we load a `.node` out of.
92
+ const versionDir = (0, path_1.join)(cacheRoot, versions_1.nxVersion);
93
+ if ((0, owned_private_dir_1.ensureOwnedPrivateDir)(versionDir).status !== 'ok') {
94
+ return null;
95
+ }
96
+ return versionDir;
23
97
  }
Binary file
@@ -60,3 +60,4 @@ export declare class IsolatedPlugin implements LoadedNxPlugin {
60
60
  shutdown(): void;
61
61
  private registerProcessMetrics;
62
62
  }
63
+ export declare function getPluginWorkerSocketId(): string;
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IsolatedPlugin = void 0;
4
+ exports.getPluginWorkerSocketId = getPluginWorkerSocketId;
4
5
  const child_process_1 = require("child_process");
6
+ const crypto_1 = require("crypto");
5
7
  const net_1 = require("net");
6
8
  const path = require("path");
7
9
  const logger_1 = require("../../../daemon/logger");
@@ -11,6 +13,7 @@ const installation_directory_1 = require("../../../utils/installation-directory"
11
13
  const logger_2 = require("../../../utils/logger");
12
14
  const progress_topics_1 = require("../../../utils/progress-topics");
13
15
  const wait_for_socket_connection_1 = require("../../../utils/wait-for-socket-connection");
16
+ const workspace_root_1 = require("../../../utils/workspace-root");
14
17
  const resolve_plugin_1 = require("../resolve-plugin");
15
18
  const messaging_1 = require("./messaging");
16
19
  const plugin_lifecycle_manager_1 = require("./plugin-lifecycle-manager");
@@ -335,6 +338,10 @@ class IsolatedPlugin {
335
338
  exports.IsolatedPlugin = IsolatedPlugin;
336
339
  // --- Worker Spawning Utilities ---
337
340
  global.nxPluginWorkerCount ??= 0;
341
+ function getPluginWorkerSocketId() {
342
+ const counter = global.nxPluginWorkerCount++;
343
+ return `${process.pid}-${counter.toString(36)}-${(0, crypto_1.randomBytes)(4).toString('hex')}`;
344
+ }
338
345
  async function startPluginWorker(name) {
339
346
  performance.mark(`start-plugin-worker:${name}`);
340
347
  const isWorkerTypescript = path.extname(__filename) === '.ts';
@@ -351,12 +358,22 @@ async function startPluginWorker(name) {
351
358
  }
352
359
  : {}),
353
360
  };
354
- const ipcPath = (0, socket_utils_1.getPluginOsSocketPath)([process.pid, global.nxPluginWorkerCount++, performance.now()].join('-'));
361
+ // Keep the host pid readable for diagnostics, the counter collision-free
362
+ // within this process, and entropy for a reused pid across process runs. The
363
+ // suffix is a base36 counter plus four random bytes: eleven characters after
364
+ // the pid until the counter passes 36, and fixed-length where the
365
+ // `performance.now()` stamp it replaced varied with process uptime. That
366
+ // keeps the socket path inside the 95-character budget `assertValidSocketPath`
367
+ // enforces.
368
+ const ipcPath = (0, socket_utils_1.getPluginOsSocketPath)(getPluginWorkerSocketId());
355
369
  const worker = (0, child_process_1.spawn)(process.execPath, [
356
370
  ...(isWorkerTypescript ? ['--require', 'ts-node/register'] : []),
357
371
  workerPath,
358
372
  ipcPath,
359
373
  name,
374
+ // The host's root. The worker validates against this rather than re-resolving,
375
+ // so the two agree by construction.
376
+ workspace_root_1.workspaceRoot,
360
377
  ], {
361
378
  stdio: ['ignore', 'pipe', 'pipe'],
362
379
  env,
@@ -107,10 +107,23 @@ type PluginMessageDefs = DefineMessages<{
107
107
  };
108
108
  };
109
109
  }>;
110
+ /**
111
+ * The sender's workspace root. Only the *worker* asserts on it, for host→worker
112
+ * messages; results and notifications carry the field but the host does not
113
+ * check it. The daemon channel is checked in the same one direction — the
114
+ * server validates inbound client messages and the client does not validate
115
+ * responses — so the two are alike rather than one being weaker.
116
+ * Optional and stamped centrally in `sendMessageOverSocket`, so the many
117
+ * message constructors need not set it; undefined is treated as not foreign,
118
+ * which is why this detects a shared socket directory rather than an attacker.
119
+ */
120
+ export type WorkspaceStampedMessage = {
121
+ workspaceRoot?: string;
122
+ };
110
123
  /** Union of all plugin worker message types */
111
- export type PluginWorkerMessage = AllMessages<PluginMessageDefs>;
124
+ export type PluginWorkerMessage = AllMessages<PluginMessageDefs> & WorkspaceStampedMessage;
112
125
  /** Union of all plugin worker result types */
113
- export type PluginWorkerResult = AllResults<PluginMessageDefs>;
126
+ export type PluginWorkerResult = AllResults<PluginMessageDefs> & WorkspaceStampedMessage;
114
127
  /** Result type for the load message */
115
128
  export type PluginWorkerLoadResult = ResultOf<PluginMessageDefs, 'load'>;
116
129
  /**
@@ -122,7 +135,7 @@ export type PluginWorkerEmitLogNotification = {
122
135
  type: 'emitLog';
123
136
  level: 'log' | 'warn' | 'error';
124
137
  message: string;
125
- };
138
+ } & WorkspaceStampedMessage;
126
139
  export type PluginWorkerNotification = PluginWorkerEmitLogNotification;
127
140
  export declare function isPluginWorkerNotification(message: Serializable): message is PluginWorkerNotification;
128
141
  export declare function isPluginWorkerMessage(message: Serializable): message is PluginWorkerMessage;
@@ -7,6 +7,7 @@ exports.consumeMessage = consumeMessage;
7
7
  exports.sendMessageOverSocket = sendMessageOverSocket;
8
8
  const socket_utils_1 = require("../../../daemon/socket-utils");
9
9
  const consume_messages_from_socket_1 = require("../../../utils/consume-messages-from-socket");
10
+ const workspace_root_1 = require("../../../utils/workspace-root");
10
11
  const NOTIFICATION_TYPES = [
11
12
  'emitLog',
12
13
  ];
@@ -81,6 +82,8 @@ async function consumeMessage(socket, raw, handlers) {
81
82
  * Sends a message over the socket with proper formatting.
82
83
  */
83
84
  function sendMessageOverSocket(socket, message) {
85
+ // Stamped here rather than in each constructor, mirroring the daemon.
86
+ message.workspaceRoot = workspace_root_1.workspaceRoot;
84
87
  socket.write((0, socket_utils_1.serialize)(message));
85
88
  socket.write(consume_messages_from_socket_1.MESSAGE_END_SEQ);
86
89
  }
@@ -7,6 +7,7 @@ node_perf_hooks_1.performance.mark(`plugin worker ${process.pid} code loading --
7
7
  const consume_messages_from_socket_1 = require("../../../utils/consume-messages-from-socket");
8
8
  const logger_1 = require("../../../utils/logger");
9
9
  const serializable_error_1 = require("../../../utils/serializable-error");
10
+ const daemon_message_1 = require("../../../daemon/message-types/daemon-message");
10
11
  const messaging_1 = require("./messaging");
11
12
  const worker_streaming_1 = require("./worker-streaming");
12
13
  const fs_1 = require("fs");
@@ -22,6 +23,19 @@ global.NX_PLUGIN_WORKER = true;
22
23
  let plugin;
23
24
  const socketPath = process.argv[2];
24
25
  const expectedPluginName = process.argv[3];
26
+ // The host's root, passed explicitly rather than re-resolved: a host that set
27
+ // its root at runtime would resolve a different one here and drop every
28
+ // legitimate message as foreign.
29
+ const hostWorkspaceRoot = process.argv[4];
30
+ // Positional, so inserting an argument host-side shifts all of them — and the
31
+ // symptom is silent and total: an undefined hostWorkspaceRoot makes every
32
+ // message look foreign while the host waits out its plugin timeout.
33
+ if (!socketPath || !expectedPluginName || !hostWorkspaceRoot) {
34
+ console.error(`[plugin-worker] started with an incomplete argument list ` +
35
+ `(socketPath=${socketPath}, pluginName=${expectedPluginName}, hostWorkspaceRoot=${hostWorkspaceRoot}). ` +
36
+ `This is an Nx bug — please report it at https://github.com/nrwl/nx/issues.`);
37
+ process.exit(1);
38
+ }
25
39
  const CONNECT_TIMEOUT_MS = 30_000;
26
40
  let connectErrorTimeout = setErrorTimeout(CONNECT_TIMEOUT_MS, `The plugin worker for ${expectedPluginName} is exiting as it was not connected to within ${CONNECT_TIMEOUT_MS / 1000} seconds. ` +
27
41
  'Plugin workers expect to receive a socket connection from the parent process shortly after being started. ' +
@@ -44,6 +58,16 @@ const server = (0, net_1.createServer)((socket) => {
44
58
  if (!(0, messaging_1.isPluginWorkerMessage)(message)) {
45
59
  return;
46
60
  }
61
+ // Same check the daemon applies to its own socket. Dropped rather than thrown:
62
+ // a stray foreign message must not kill a worker serving its host. The daemon
63
+ // has a response channel and surfaces it to the client instead.
64
+ try {
65
+ (0, daemon_message_1.assertNotForeignWorkspaceMessage)(message, hostWorkspaceRoot, `The Nx plugin worker "${expectedPluginName}" (pid: ${process.pid})`);
66
+ }
67
+ catch (e) {
68
+ logger_1.logger.verbose(`[plugin-worker] ignored a "${message.type}" message: ${e instanceof Error ? e.message : e}`);
69
+ return;
70
+ }
47
71
  return (0, messaging_1.consumeMessage)(socket, message, {
48
72
  load: async ({ plugin: pluginConfiguration, root, name, pluginPath, shouldRegisterTSTranspiler, }) => {
49
73
  loadErrorTimeout?.clear();
@@ -105,8 +129,24 @@ const server = (0, net_1.createServer)((socket) => {
105
129
  process.exit(0);
106
130
  });
107
131
  });
108
- server.listen(socketPath);
109
- logger_1.logger.verbose(`[plugin-worker] "${expectedPluginName}" (pid: ${process.pid}) listening on ${socketPath}`);
132
+ server.on('error', (err) => {
133
+ // Without this the host only sees "exited before the connection was
134
+ // established"; the errno distinguishes a denied bind from EADDRINUSE.
135
+ console.error(`[plugin-worker] "${expectedPluginName}" (pid: ${process.pid}) failed to listen on ${socketPath}: ${err.message}`);
136
+ process.exit(1);
137
+ });
138
+ // A worker killed without its 'end' handler leaves the socket behind. Colliding
139
+ // takes a recycled host pid landing on the same counter *and* the same 4 random
140
+ // bytes; the random component is what this unlink rests on, and on Windows it is
141
+ // the only barrier, since the endpoint is a namespace object and the mode never
142
+ // applies. A failed bind surfaces through the error handler above.
143
+ try {
144
+ (0, fs_1.unlinkSync)(socketPath);
145
+ }
146
+ catch { }
147
+ server.listen(socketPath, () => {
148
+ logger_1.logger.verbose(`[plugin-worker] "${expectedPluginName}" (pid: ${process.pid}) listening on ${socketPath}`);
149
+ });
110
150
  async function withErrorHandling(cb) {
111
151
  try {
112
152
  const result = await cb();
@@ -137,15 +177,25 @@ function setErrorTimeout(timeoutMs, errorMessage) {
137
177
  },
138
178
  };
139
179
  }
140
- const exitHandler = (exitCode) => () => {
180
+ const cleanup = () => {
141
181
  server.close();
142
182
  try {
143
183
  (0, fs_1.unlinkSync)(socketPath);
144
184
  }
145
185
  catch (e) { }
146
- process.exit(exitCode);
147
186
  };
148
- const events = ['SIGINT', 'SIGTERM', 'SIGQUIT', 'exit'];
149
- events.forEach((event) => process.once(event, exitHandler(0)));
150
- process.once('uncaughtException', exitHandler(1));
151
- process.once('unhandledRejection', exitHandler(1));
187
+ const events = ['SIGINT', 'SIGTERM', 'SIGQUIT'];
188
+ events.forEach((event) => process.once(event, () => {
189
+ cleanup();
190
+ process.exit(0);
191
+ }));
192
+ // Cleanup only: process.exit() here would override the real exit code.
193
+ process.once('exit', cleanup);
194
+ const fatalHandler = (error) => {
195
+ // Registering this handler suppresses Node's default reporting.
196
+ console.error(error);
197
+ cleanup();
198
+ process.exit(1);
199
+ };
200
+ process.once('uncaughtException', fatalHandler);
201
+ process.once('unhandledRejection', fatalHandler);
@@ -350,6 +350,26 @@ async function createProjectGraphAndSourceMapsAsync(opts = {
350
350
  (0, tmp_dir_1.markDaemonAsDisabled)(e.message);
351
351
  return buildProjectGraphAndSourceMapsWithoutDaemon();
352
352
  }
353
+ if (e.daemonPermissionError) {
354
+ // Deliberately not disabled: unlike the inotify limit above, a socket
355
+ // owned by someone else stops being there when it is removed or the
356
+ // machine reboots, and disabling until `nx reset` would outlive the
357
+ // cause and hide the fix from anyone who followed the advice.
358
+ // The first line carries the errno, and it is the one token that tells
359
+ // the two causes apart: EACCES is a socket owned by someone else (delete
360
+ // it), EPERM is a sandbox refusing the connect syscall (allow unix
361
+ // sockets under the Nx socket root). The message hedges between exactly
362
+ // those two because it cannot tell them apart, and this branch does not
363
+ // call writeDaemonLogs, so dropping the line loses the errno for good.
364
+ const [summary, ...details] = e.message.split('\n');
365
+ output_1.output.note({
366
+ title: `${summary} Continuing without the daemon.`,
367
+ // The blank line after the summary separates paragraphs when the
368
+ // message is printed as one blob; as bodyLines it is a leading gap.
369
+ bodyLines: details[0] === '' ? details.slice(1) : details,
370
+ });
371
+ return buildProjectGraphAndSourceMapsWithoutDaemon();
372
+ }
353
373
  if (e.internalDaemonError) {
354
374
  const errorLogFile = (0, tmp_dir_1.writeDaemonLogs)(e.message);
355
375
  output_1.output.warn({
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Stable root for Nx runtime artifacts that need an OS tmp location. On POSIX a
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
6
+ * and stable, so it keeps `os.tmpdir()`.
7
+ *
8
+ * Consumed by the native binding loader, so keep this file limited to local
9
+ * helpers that themselves use Node builtins only.
10
+ */
11
+ export declare const NX_TMP_DIR: string;
12
+ /**
13
+ * Owner-only runtime root for the current user. No user segment on Windows —
14
+ * `%TMP%` is already per-account, and the segment would only cost path length.
15
+ */
16
+ export declare const NX_USER_TMP_DIR: string;
17
+ /** Runtime root under the user's home, used when the shared container cannot be established. */
18
+ export declare const NX_HOME_TMP_DIR: string;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NX_HOME_TMP_DIR = exports.NX_USER_TMP_DIR = exports.NX_TMP_DIR = void 0;
4
+ const node_os_1 = require("node:os");
5
+ const node_path_1 = require("node:path");
6
+ const owned_private_dir_1 = require("./owned-private-dir");
7
+ /**
8
+ * Stable root for Nx runtime artifacts that need an OS tmp location. On POSIX a
9
+ * 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
12
+ * and stable, so it keeps `os.tmpdir()`.
13
+ *
14
+ * Consumed by the native binding loader, so keep this file limited to local
15
+ * helpers that themselves use Node builtins only.
16
+ */
17
+ exports.NX_TMP_DIR = (0, node_os_1.platform)() === 'win32' ? (0, node_path_1.join)((0, node_os_1.tmpdir)(), '.nx') : '/tmp/.nx';
18
+ /**
19
+ * Owner-only runtime root for the current user. No user segment on Windows —
20
+ * `%TMP%` is already per-account, and the segment would only cost path length.
21
+ */
22
+ exports.NX_USER_TMP_DIR = (0, node_os_1.platform)() === 'win32' ? exports.NX_TMP_DIR : (0, node_path_1.join)(exports.NX_TMP_DIR, (0, owned_private_dir_1.getUserSegment)());
23
+ /** Runtime root under the user's home, used when the shared container cannot be established. */
24
+ exports.NX_HOME_TMP_DIR = resolveHomeTmpDir();
25
+ /**
26
+ * Absolute, not merely non-empty: a relative `$HOME` would make `join` return
27
+ * `.nx`, putting sockets under the cwd and aiming `removeSocketDir`'s recursive
28
+ * delete at it. A rootless container has no `$HOME` and no passwd entry, so
29
+ * `homedir()` throws or returns empty — caught here because this runs at module
30
+ * scope and the native binding loader imports this file.
31
+ */
32
+ function resolveHomeTmpDir() {
33
+ try {
34
+ const home = (0, node_os_1.homedir)();
35
+ return home && (0, node_path_1.isAbsolute)(home) ? (0, node_path_1.join)(home, '.nx') : undefined;
36
+ }
37
+ catch {
38
+ return undefined;
39
+ }
40
+ }