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
package/dist/bin/nx.js CHANGED
@@ -17,6 +17,7 @@ const output_1 = require("../src/utils/output");
17
17
  const installation_directory_1 = require("../src/utils/installation-directory");
18
18
  const semver_1 = require("semver");
19
19
  const strip_indents_1 = require("../src/utils/strip-indents");
20
+ const workspace_env_1 = require("./workspace-env");
20
21
  const child_process_1 = require("child_process");
21
22
  const module_1 = require("module");
22
23
  const path_1 = require("path");
@@ -63,14 +64,7 @@ async function main() {
63
64
  // --version doesn't need any env / daemon / analytics state — skip dotenv
64
65
  // loading (and the heavy modules it would pull in).
65
66
  if (workspace && process.argv[2] !== '--version') {
66
- const { workspaceDataDirectoryForWorkspace } = await import('../src/utils/cache-directory.js');
67
- process.report.reportOnFatalError = true;
68
- process.report.directory = workspaceDataDirectoryForWorkspace(workspace.dir);
69
- const { loadRootEnvFiles } = await import('../src/utils/dotenv.js');
70
- perf_hooks_1.performance.mark('loading dotenv files:start');
71
- loadRootEnvFiles(workspace.dir);
72
- perf_hooks_1.performance.mark('loading dotenv files:end');
73
- perf_hooks_1.performance.measure('loading dotenv files', 'loading dotenv files:start', 'loading dotenv files:end');
67
+ await (0, workspace_env_1.loadWorkspaceEnv)(workspace.dir);
74
68
  }
75
69
  // new is a special case because there is no local workspace to load
76
70
  if (process.argv[2] === 'new' ||
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Loads the workspace's root `.env` files, then sends Node's fatal error
3
+ * reports to its workspace data directory. The `.env` files load first because
4
+ * importing `cache-directory` resolves and checks the configured directories,
5
+ * which read `NX_*` variables those files can set.
6
+ */
7
+ export declare function loadWorkspaceEnv(workspaceDir: string): Promise<void>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadWorkspaceEnv = loadWorkspaceEnv;
4
+ const perf_hooks_1 = require("perf_hooks");
5
+ /**
6
+ * Loads the workspace's root `.env` files, then sends Node's fatal error
7
+ * reports to its workspace data directory. The `.env` files load first because
8
+ * importing `cache-directory` resolves and checks the configured directories,
9
+ * which read `NX_*` variables those files can set.
10
+ */
11
+ async function loadWorkspaceEnv(workspaceDir) {
12
+ const { loadRootEnvFiles } = await import('../src/utils/dotenv.js');
13
+ perf_hooks_1.performance.mark('loading dotenv files:start');
14
+ loadRootEnvFiles(workspaceDir);
15
+ perf_hooks_1.performance.mark('loading dotenv files:end');
16
+ perf_hooks_1.performance.measure('loading dotenv files', 'loading dotenv files:start', 'loading dotenv files:end');
17
+ const { workspaceDataDirectoryForWorkspace } = await import('../src/utils/cache-directory.js');
18
+ process.report.reportOnFatalError = true;
19
+ process.report.directory = workspaceDataDirectoryForWorkspace(workspaceDir);
20
+ }
@@ -2912,11 +2912,12 @@ async function nxCliPath(nxWorkspaceRoot) {
2912
2912
  const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
2913
2913
  const root = nxWorkspaceRoot ?? workspace_root_1.workspaceRoot;
2914
2914
  const version = await (0, provenance_1.resolveVersionWithProvenance)('nx', process.env.NX_MIGRATE_CLI_VERSION || 'latest', root);
2915
+ // Outside the try, so a missing private install root is reported rather than
2916
+ // hidden behind the fallback to the workspace's nx.
2917
+ const tmpDir = (0, package_manager_1.createTempInstallDirectory)();
2915
2918
  try {
2916
2919
  const packageManager = (0, package_manager_1.detectPackageManager)();
2917
2920
  const pmc = (0, package_manager_1.getPackageManagerCommand)(packageManager);
2918
- const { dirSync } = require('tmp');
2919
- const tmpDir = dirSync().name;
2920
2921
  (0, fileutils_1.writeJsonFile)((0, path_1.join)(tmpDir, 'package.json'), {
2921
2922
  dependencies: {
2922
2923
  nx: version,
@@ -5,7 +5,7 @@ const handle_import_1 = require("../../../utils/handle-import");
5
5
  const shared_options_1 = require("../../yargs-utils/shared-options");
6
6
  exports.yargsStartNxAgentsCommand = {
7
7
  command: 'start-nx-agents [options]',
8
- describe: 'Provisions Nx Agents for distributed task execution, reading configuration from `.nx/ci-config.yaml`. This command is an alias for [`nx-cloud start-nx-agents`](/docs/reference/nx-cloud-cli#nx-cloud-start-workers).',
8
+ describe: 'Starts workers for distributed task execution, reading configuration from `.nx/pipelines/ci.yaml`. This command is an alias for [`nx-cloud start-workers`](/docs/reference/nx-cloud-cli#nx-cloud-start-workers).',
9
9
  builder: (yargs) => (0, shared_options_1.withVerbose)(yargs)
10
10
  .help(false)
11
11
  .showHelpOnFail(false)
@@ -15,6 +15,7 @@ const nx_cloud_utils_1 = require("../../utils/nx-cloud-utils");
15
15
  const configuration_1 = require("../../config/configuration");
16
16
  const db_connection_1 = require("../../utils/db-connection");
17
17
  const update_manager_1 = require("../../nx-cloud/update-manager");
18
+ const package_manager_1 = require("../../utils/package-manager");
18
19
  // Wait at max 5 seconds before giving up on a failing operation.
19
20
  const INCREMENTAL_BACKOFF_MAX_DURATION = 5000;
20
21
  // If an operation fails, wait 100ms before first retry.
@@ -64,6 +65,14 @@ async function resetHandler(args) {
64
65
  },
65
66
  });
66
67
  }
68
+ if (all) {
69
+ await runResetStep(errors, {
70
+ start: 'Removing stale temporary package installs',
71
+ success: 'Removed stale temporary package installs',
72
+ failure: 'Failed to remove stale temporary package installs.',
73
+ run: async () => (0, package_manager_1.removeStaleTempInstallDirectories)(),
74
+ });
75
+ }
67
76
  if ((cloudEnabled && all) || args.onlyCloud) {
68
77
  await runResetStep(errors, {
69
78
  start: 'Cleaning up the Nx Cloud client',
@@ -124,7 +133,10 @@ async function resetCloudClient() {
124
133
  }
125
134
  function removeInstalledNxCloudClient() {
126
135
  return incrementalBackoff(INCREMENTAL_BACKOFF_FIRST_DELAY, INCREMENTAL_BACKOFF_MAX_DURATION, () => {
127
- (0, node_fs_1.rmSync)((0, update_manager_1.getBundleInstallDefaultLocation)(), { recursive: true, force: true });
136
+ const location = (0, update_manager_1.getBundleInstallDefaultLocation)();
137
+ if (location) {
138
+ (0, node_fs_1.rmSync)(location, { recursive: true, force: true });
139
+ }
128
140
  });
129
141
  }
130
142
  function cleanupCacheEntries() {
@@ -29,9 +29,11 @@ export declare function getDaemonClientEnvGeneration(): number;
29
29
  * the pin, a root without markers under an ancestor that has them
30
30
  * resolves to the ancestor and the daemon publishes its socket under the
31
31
  * wrong workspace.
32
- * - NX_MAX_MESSAGE_SIZE: excluded from reflection (see above), so the value
33
- * here holds for the daemon's whole lifetime. Changing it therefore needs a
34
- * daemon restart (`nx reset`).
32
+ * - NX_MAX_MESSAGE_SIZE and NX_ALLOW_INSECURE_DIRECTORIES: excluded from
33
+ * reflection (see above), so the value here holds for the daemon's whole
34
+ * lifetime. Changing either therefore needs a daemon restart (`nx reset`).
35
+ * - TMPDIR: without it, os.tmpdir() in the daemon and every process it
36
+ * starts falls back to the shared /tmp instead of the user's own.
35
37
  */
36
38
  export declare function getDaemonSpawnEnv(): NodeJS.ProcessEnv;
37
39
  /**
@@ -68,6 +68,9 @@ const DAEMON_ENV_VARS_EXCLUSIONS = new Set([
68
68
  // to the next client's connection. Pinned at daemon spawn instead; see
69
69
  // getDaemonSpawnEnv.
70
70
  'NX_MAX_MESSAGE_SIZE',
71
+ // Read when the daemon resolves its cache directories at startup. Pinned at
72
+ // spawn instead, so one client's opt-out never reaches later clients.
73
+ 'NX_ALLOW_INSECURE_DIRECTORIES',
71
74
  // Nx UI/logging vars (don't affect graph structure)
72
75
  'NX_TUI',
73
76
  'NX_TUI_AUTO_EXIT',
@@ -311,9 +314,11 @@ function getDaemonClientEnvGeneration() {
311
314
  * the pin, a root without markers under an ancestor that has them
312
315
  * resolves to the ancestor and the daemon publishes its socket under the
313
316
  * wrong workspace.
314
- * - NX_MAX_MESSAGE_SIZE: excluded from reflection (see above), so the value
315
- * here holds for the daemon's whole lifetime. Changing it therefore needs a
316
- * daemon restart (`nx reset`).
317
+ * - NX_MAX_MESSAGE_SIZE and NX_ALLOW_INSECURE_DIRECTORIES: excluded from
318
+ * reflection (see above), so the value here holds for the daemon's whole
319
+ * lifetime. Changing either therefore needs a daemon restart (`nx reset`).
320
+ * - TMPDIR: without it, os.tmpdir() in the daemon and every process it
321
+ * starts falls back to the shared /tmp instead of the user's own.
317
322
  */
318
323
  function getDaemonSpawnEnv() {
319
324
  const env = withoutBerryBinFoldersOnPath(getDaemonEnv());
@@ -326,6 +331,13 @@ function getDaemonSpawnEnv() {
326
331
  if (process.env.NX_MAX_MESSAGE_SIZE !== undefined) {
327
332
  env.NX_MAX_MESSAGE_SIZE = process.env.NX_MAX_MESSAGE_SIZE;
328
333
  }
334
+ if (process.env.TMPDIR !== undefined) {
335
+ env.TMPDIR = process.env.TMPDIR;
336
+ }
337
+ if (process.env.NX_ALLOW_INSECURE_DIRECTORIES !== undefined) {
338
+ env.NX_ALLOW_INSECURE_DIRECTORIES =
339
+ process.env.NX_ALLOW_INSECURE_DIRECTORIES;
340
+ }
329
341
  return env;
330
342
  }
331
343
  /**
@@ -36,6 +36,7 @@ export declare class SeriallyRunningTasks implements RunningTask {
36
36
  private code;
37
37
  private error;
38
38
  private outputCallbacks;
39
+ private killSignal;
39
40
  private exited;
40
41
  private exitTerminalOutput;
41
42
  constructor(options: NormalizedRunCommandsOptions, context: ExecutorContext, tuiEnabled: boolean, task: Pick<Task, 'id' | 'ultracache'>, baseEnv?: NodeJS.ProcessEnv);
@@ -14,6 +14,7 @@ const pseudo_terminal_1 = require("../../tasks-runner/pseudo-terminal");
14
14
  const task_env_1 = require("../../tasks-runner/task-env");
15
15
  const tokens_1 = require("../../tasks-runner/tokens");
16
16
  const task_io_service_1 = require("../../tasks-runner/task-io-service");
17
+ const child_process_2 = require("../../utils/child-process");
17
18
  const exit_codes_1 = require("../../utils/exit-codes");
18
19
  const output_1 = require("../../utils/output");
19
20
  /**
@@ -110,6 +111,12 @@ class ParallelRunningTasks {
110
111
  }
111
112
  });
112
113
  childProcess.onExit((code, terminalOutput) => {
114
+ // A failed command reports only once its output is read, so
115
+ // readyWhen text a sibling prints meanwhile must not hide it.
116
+ if (code === 0 &&
117
+ this.childProcesses.some((p) => p !== childProcess && p.exitedWithFailure)) {
118
+ return;
119
+ }
113
120
  res({
114
121
  childProcess,
115
122
  result: { code, terminalOutput },
@@ -137,9 +144,14 @@ class ParallelRunningTasks {
137
144
  }
138
145
  else {
139
146
  const runningProcesses = new Set();
140
- let hasFailure = false;
141
- let failureDetails = null;
147
+ let failedProcess = null;
142
148
  const terminalOutputs = new Map();
149
+ const failFast = (childProcess) => {
150
+ if (failedProcess)
151
+ return;
152
+ failedProcess = childProcess;
153
+ this.terminateRemainingProcesses(runningProcesses, childProcess);
154
+ };
143
155
  await Promise.allSettled(this.childProcesses.map(async (childProcess) => {
144
156
  runningProcesses.add(childProcess);
145
157
  childProcess.onOutput((terminalOutput) => {
@@ -147,20 +159,20 @@ class ParallelRunningTasks {
147
159
  cb(terminalOutput);
148
160
  }
149
161
  });
162
+ // Results wait for the output to drain, which a descendant holding
163
+ // the pipes can stretch to a second.
164
+ childProcess.onFailedExit(() => failFast(childProcess));
150
165
  const { code, terminalOutput } = await childProcess.getResults();
151
166
  terminalOutputs.set(childProcess, terminalOutput);
152
- if (code !== 0 && !hasFailure) {
153
- hasFailure = true;
154
- failureDetails = { childProcess, code, terminalOutput };
155
- // Immediately terminate all other running processes
156
- this.terminateRemainingProcesses(runningProcesses, childProcess);
167
+ if (code !== 0) {
168
+ failFast(childProcess);
157
169
  }
158
170
  runningProcesses.delete(childProcess);
159
171
  }));
160
172
  let terminalOutput = Array.from(terminalOutputs.values()).join('\r\n');
161
- if (hasFailure && failureDetails) {
173
+ if (failedProcess) {
162
174
  // Add failure message
163
- const warning = commandExitedNonZeroWarning(failureDetails.childProcess.command);
175
+ const warning = commandExitedNonZeroWarning(failedProcess.command);
164
176
  terminalOutput += warning;
165
177
  streamChunk(warning, this.streamOutput, process.stderr);
166
178
  this.emitExit(1, terminalOutput);
@@ -189,6 +201,9 @@ class SeriallyRunningTasks {
189
201
  this.exitCallbacks = [];
190
202
  this.code = 0;
191
203
  this.outputCallbacks = [];
204
+ // A command can be killed after it exits, while its output drains. The
205
+ // signal then reaches no process, so this is what stops the next command.
206
+ this.killSignal = null;
192
207
  // Memoized exit so late getResults()/onExit() callers don't hang.
193
208
  this.exited = false;
194
209
  this.exitTerminalOutput = '';
@@ -253,10 +268,15 @@ class SeriallyRunningTasks {
253
268
  throw new Error('Not implemented');
254
269
  }
255
270
  kill(signal) {
271
+ this.killSignal = signal ?? 'SIGTERM';
256
272
  return this.currentProcess?.kill(signal);
257
273
  }
258
274
  async run(options, context) {
259
275
  for (const c of options.commands) {
276
+ if (this.killSignal) {
277
+ this.code = (0, exit_codes_1.signalToCode)(this.killSignal);
278
+ break;
279
+ }
260
280
  const childProcess = await this.createProcess(c, options.color, calculateCwd(options.cwd, context), options.processEnv ?? options.env ?? {}, this.task, options.usePty, options.streamOutput, options.tty, options.envFile, this.baseEnv);
261
281
  this.currentProcess = childProcess;
262
282
  childProcess.onOutput((output, isCommandHeader) => {
@@ -349,7 +369,10 @@ class RunningNodeProcess {
349
369
  this.terminalOutputChunks = [];
350
370
  this.exitCallbacks = [];
351
371
  this.outputCallbacks = [];
372
+ this.failedExitCallbacks = [];
352
373
  this.killPromise = null;
374
+ // Set at 'exit', while the output may still be unread.
375
+ this.exitedWithFailure = false;
353
376
  // Memoized exit so late getResults()/onExit() callers don't hang.
354
377
  this.exited = false;
355
378
  this.exitCode = 0;
@@ -367,6 +390,7 @@ class RunningNodeProcess {
367
390
  windowsHide: true,
368
391
  stdio: ['inherit', 'pipe', 'pipe'],
369
392
  });
393
+ this.stdioClosed = (0, child_process_2.whenStdioClosed)(this.childProcess);
370
394
  this.closedPromise = new Promise((resolve) => {
371
395
  this.childProcess.on('close', resolve);
372
396
  });
@@ -402,6 +426,10 @@ class RunningNodeProcess {
402
426
  }
403
427
  this.exitCallbacks.push(cb);
404
428
  }
429
+ /** Runs at a non-zero 'exit', before the rest of the output is read. */
430
+ onFailedExit(cb) {
431
+ this.failedExitCallbacks.push(cb);
432
+ }
405
433
  send(message) {
406
434
  this.childProcess.send(message);
407
435
  }
@@ -424,10 +452,15 @@ class RunningNodeProcess {
424
452
  return this.killPromise;
425
453
  if (!this.childProcess.pid)
426
454
  return Promise.resolve();
455
+ // An exited child's pid is free for reuse, so signalling it could reach an
456
+ // unrelated process.
457
+ const signalled = (0, child_process_2.hasExited)(this.childProcess)
458
+ ? Promise.resolve()
459
+ : (0, native_1.killProcessTreeGraceful)(this.childProcess.pid, signal);
427
460
  // Cache the promise so concurrent callers (e.g. cleanup() after a
428
461
  // fire-and-forget kill from cleanUpUnneededContinuousTasks) await the
429
462
  // same in-progress operation instead of no-oping.
430
- this.killPromise = (0, native_1.killProcessTreeGraceful)(this.childProcess.pid, signal).then(() =>
463
+ this.killPromise = signalled.then(() =>
431
464
  // Wait for stdio drain and close events before the orchestrator
432
465
  // tears down the event loop via process.exit().
433
466
  Promise.race([
@@ -451,9 +484,10 @@ class RunningNodeProcess {
451
484
  addListeners(commandConfig) {
452
485
  this.childProcess.stdout.setEncoding('utf8');
453
486
  this.childProcess.stderr.setEncoding('utf8');
487
+ const exitedWithFailure = () => this.exitedWithFailure;
454
488
  // Separate matchers, so a ready string never spans stdout and stderr
455
- const stdoutReady = readyWhenMatcher(this.readyWhenStatus);
456
- const stderrReady = readyWhenMatcher(this.readyWhenStatus);
489
+ const stdoutReady = readyWhenMatcher(this.readyWhenStatus, exitedWithFailure);
490
+ const stderrReady = readyWhenMatcher(this.readyWhenStatus, exitedWithFailure);
457
491
  this.childProcess.stdout.on('data', (data) => {
458
492
  const output = addColorAndPrefix(data, commandConfig);
459
493
  this.terminalOutputChunks.push(output);
@@ -485,11 +519,19 @@ class RunningNodeProcess {
485
519
  untrackRunningNodeProcess(this);
486
520
  this.emitExit(1, terminalOutput);
487
521
  });
488
- this.childProcess.on('exit', (code, signal) => {
522
+ this.childProcess.on('exit', async (code, signal) => {
489
523
  if (code === null) {
490
524
  code = (0, exit_codes_1.signalToCode)(signal);
491
525
  }
492
526
  untrackRunningNodeProcess(this);
527
+ this.exitedWithFailure = code !== 0;
528
+ if (this.exitedWithFailure) {
529
+ for (const cb of this.failedExitCallbacks) {
530
+ cb();
531
+ }
532
+ }
533
+ // The last stdout and stderr chunks can still be unread at 'exit'.
534
+ await this.stdioClosed;
493
535
  if (code === 0 &&
494
536
  this.readyWhenStatus.length &&
495
537
  !isReady(this.readyWhenStatus)) {
@@ -591,23 +633,33 @@ function isReady(readyWhenStatus) {
591
633
  // The output as printed, kept across chunks, matches strings that hold escape
592
634
  // sequences. LogMatcher, absent from the WASM binding, matches text with
593
635
  // escapes inside it.
594
- function readyWhenMatcher(readyWhenStatus) {
636
+ function readyWhenMatcher(readyWhenStatus, exitedWithFailure) {
595
637
  const matchers = readyWhenStatus.map((status) => ({
596
638
  status,
597
639
  matcher: native_1.IS_WASM ? null : new native_1.LogMatcher([status.stringToMatch]),
598
640
  tail: '',
641
+ matched: false,
599
642
  }));
600
643
  return (chunk) => {
644
+ // The status is shared, so only a chunk with this command's own match
645
+ // reports ready. Once ready, only a failed command can change the result.
646
+ if (isReady(readyWhenStatus) && !exitedWithFailure())
647
+ return false;
648
+ let matchedNow = false;
601
649
  for (const entry of matchers) {
602
650
  const { status, matcher } = entry;
603
- if (status.found)
651
+ if (entry.matched)
604
652
  continue;
605
653
  const printed = entry.tail + chunk;
606
- status.found =
654
+ entry.matched =
607
655
  printed.includes(status.stringToMatch) || !!matcher?.feed(chunk);
608
656
  entry.tail = printed.slice(Math.max(0, printed.length - status.stringToMatch.length + 1));
657
+ if (entry.matched) {
658
+ status.found = true;
659
+ matchedNow = true;
660
+ }
609
661
  }
610
- return isReady(readyWhenStatus);
662
+ return matchedNow && isReady(readyWhenStatus);
611
663
  };
612
664
  }
613
665
  function loadEnvVarsFile(path, env = {}) {
@@ -6,6 +6,7 @@ const child_process_1 = require("child_process");
6
6
  const path = tslib_1.__importStar(require("path"));
7
7
  const native_1 = require("../../native");
8
8
  const pseudo_terminal_1 = require("../../tasks-runner/pseudo-terminal");
9
+ const child_process_2 = require("../../utils/child-process");
9
10
  const package_manager_1 = require("../../utils/package-manager");
10
11
  async function default_1(options, context) {
11
12
  const pm = (0, package_manager_1.getPackageManagerCommand)();
@@ -40,13 +41,16 @@ function nodeProcess(command, cwd, env) {
40
41
  env,
41
42
  windowsHide: true,
42
43
  });
44
+ const stdioClosed = (0, child_process_2.whenStdioClosed)(cp);
43
45
  // Forward stdout/stderr to parent process
44
46
  cp.stdout.pipe(process.stdout);
45
47
  cp.stderr.pipe(process.stderr);
46
48
  cp.on('error', (error) => {
47
49
  rej(error);
48
50
  });
49
- cp.on('exit', (code) => {
51
+ cp.on('exit', async (code) => {
52
+ // The last stdout and stderr chunks can still be unread at 'exit'.
53
+ await stdioClosed;
50
54
  if (code === 0) {
51
55
  res();
52
56
  }
@@ -55,9 +59,13 @@ function nodeProcess(command, cwd, env) {
55
59
  }
56
60
  });
57
61
  const exitHandler = (signal) => {
58
- if (cp && cp.pid && !cp.killed) {
62
+ // An exited child's pid is free for reuse, and its exit code decides
63
+ // the result, also when the kill is what made it exit.
64
+ if (cp && cp.pid && !cp.killed && !(0, child_process_2.hasExited)(cp)) {
59
65
  (0, native_1.killProcessTreeGraceful)(cp.pid, signal).finally(() => {
60
- res();
66
+ if (!(0, child_process_2.hasExited)(cp)) {
67
+ res();
68
+ }
61
69
  });
62
70
  }
63
71
  };
@@ -1,5 +1,12 @@
1
1
  const { join, basename } = require('path');
2
- const { copyFileSync, renameSync, statSync, unlinkSync } = require('fs');
2
+ const {
3
+ chmodSync,
4
+ copyFileSync,
5
+ lstatSync,
6
+ renameSync,
7
+ statSync,
8
+ unlinkSync,
9
+ } = require('fs');
3
10
  const { createHash } = require('crypto');
4
11
  const Module = require('module');
5
12
  const { nxVersion } = require('../utils/versions');
@@ -69,6 +76,25 @@ function statsOrNull(path) {
69
76
  }
70
77
  }
71
78
 
79
+ function lstatsOrNull(path) {
80
+ try {
81
+ return lstatSync(path);
82
+ } catch {
83
+ return null;
84
+ }
85
+ }
86
+
87
+ // A trusted entry is one only the current user could have written.
88
+ function isTrustedCacheEntry(stats) {
89
+ if (!stats?.isFile()) {
90
+ return false;
91
+ }
92
+ if (typeof process.getuid !== 'function') {
93
+ return true;
94
+ }
95
+ return stats.uid === process.getuid() && (stats.mode & 0o022) === 0;
96
+ }
97
+
72
98
  function removeIfPresent(path) {
73
99
  try {
74
100
  unlinkSync(path);
@@ -122,13 +148,14 @@ Module._load = function (request, parent, isMain) {
122
148
  const tmpFile = join(nativeFileCacheLocation, cacheKey + '-' + fileName);
123
149
  const sourceStats = statSync(nativeLocation);
124
150
  const expectedFileSize = sourceStats.size;
125
- const existingFileStats = statsOrNull(tmpFile);
151
+ const existingFileStats = lstatsOrNull(tmpFile);
126
152
 
127
153
  // Size alone does not detect a rebuilt binding: a small Rust edit routinely
128
154
  // produces a byte-identical size. The copy is taken after the build, so a
129
155
  // cache entry older than the source means the source was rebuilt since.
130
156
  const isFresh =
131
- existingFileStats?.size === expectedFileSize &&
157
+ isTrustedCacheEntry(existingFileStats) &&
158
+ existingFileStats.size === expectedFileSize &&
132
159
  existingFileStats.mtimeMs >= sourceStats.mtimeMs;
133
160
 
134
161
  const loadFromCache = () => {
@@ -159,6 +186,7 @@ Module._load = function (request, parent, isMain) {
159
186
  // First copy to a unique location for each process
160
187
  try {
161
188
  copyFileSync(nativeLocation, tmpTmpFile);
189
+ chmodSync(tmpTmpFile, sourceStats.mode & 0o755);
162
190
  } catch (e) {
163
191
  // Permission errors won't heal on retry, and a throwing copy can still
164
192
  // have left a partial file behind.
Binary file
@@ -57,7 +57,11 @@ function toRustProjectGraph(graph) {
57
57
  executor: targetConfig.executor,
58
58
  inputs: targetConfig.inputs,
59
59
  outputs: targetConfig.outputs,
60
- options: JSON.stringify(targetConfig.options),
60
+ options: JSON.stringify(targetConfig.executor === 'nx:run-script' &&
61
+ typeof targetConfig.metadata?.scriptContent === 'string' &&
62
+ targetConfig.metadata.scriptContent !== ''
63
+ ? [targetConfig.options, targetConfig.metadata.scriptContent]
64
+ : targetConfig.options),
61
65
  configurations: JSON.stringify(targetConfig.configurations),
62
66
  parallelism: targetConfig.parallelism,
63
67
  };
@@ -37,6 +37,11 @@ export declare function verifyOrUpdateNxCloudClient(options?: {
37
37
  nxCloudClient: NxCloudClient;
38
38
  version: string;
39
39
  } | null>;
40
- export declare function getBundleInstallDefaultLocation(): string;
40
+ /**
41
+ * Null outside a workspace when there is no home directory apart from the
42
+ * shared temp dir, since the bundle would then be `require`d from, and resolve
43
+ * its own imports through, directories other users can write.
44
+ */
45
+ export declare function getBundleInstallDefaultLocation(): string | null;
41
46
  export declare function downloadAndExtractClientBundle(httpClient: HttpClient, version: string, url: string): Promise<CloudBundleInstall>;
42
47
  export {};
@@ -9,12 +9,13 @@ const fs_1 = require("fs");
9
9
  const zlib_1 = require("zlib");
10
10
  const stream_1 = require("stream");
11
11
  const path_1 = require("path");
12
- const os_1 = require("os");
13
12
  const nx_cloud_http_client_1 = require("./utilities/nx-cloud-http-client");
14
13
  const http_client_1 = require("../utils/http-client");
15
14
  const debug_logger_1 = require("./debug-logger");
16
15
  const tar = tslib_1.__importStar(require("tar-stream"));
17
16
  const cache_directory_1 = require("../utils/cache-directory");
17
+ const nx_tmp_dir_1 = require("../utils/nx-tmp-dir");
18
+ const owned_private_dir_1 = require("../utils/owned-private-dir");
18
19
  const is_ci_1 = require("../utils/is-ci");
19
20
  const crypto_1 = require("crypto");
20
21
  const native_1 = require("../native");
@@ -93,17 +94,24 @@ async function verifyOrUpdateNxCloudClient(options) {
93
94
  nxCloudClient: require(currentBundle.fullPath),
94
95
  };
95
96
  }
97
+ /**
98
+ * Null outside a workspace when there is no home directory apart from the
99
+ * shared temp dir, since the bundle would then be `require`d from, and resolve
100
+ * its own imports through, directories other users can write.
101
+ */
96
102
  function getBundleInstallDefaultLocation() {
97
- // When not in an Nx workspace (no nx.json), avoid creating a .nx folder
98
- // in the current directory. Instead, use a temp directory unique to the
99
- // NX_CLOUD_API URL so different cloud instances don't conflict.
103
+ // Outside a workspace, keep the bundle out of the current directory and key
104
+ // it by NX_CLOUD_API so different cloud instances don't conflict.
100
105
  if (!(0, fs_1.existsSync)((0, path_1.join)(workspace_root_1.workspaceRoot, 'nx.json'))) {
106
+ if (!nx_tmp_dir_1.NX_HOME_TMP_DIR || !(0, nx_tmp_dir_1.homeDirIsDistinctFromSharedTmp)()) {
107
+ return null;
108
+ }
101
109
  const apiUrl = process.env.NX_CLOUD_API || 'https://cloud.nx.app';
102
110
  const apiHash = (0, crypto_1.createHash)('sha256')
103
111
  .update(apiUrl)
104
112
  .digest('hex')
105
113
  .slice(0, 16);
106
- return (0, path_1.join)((0, os_1.tmpdir)(), 'nx-cloud-client', apiHash);
114
+ return (0, path_1.join)(nx_tmp_dir_1.NX_HOME_TMP_DIR, 'cloud-client', apiHash);
107
115
  }
108
116
  const legacyPath = (0, path_1.join)(workspace_root_1.workspaceRoot, 'node_modules', '.cache', 'nx', 'cloud');
109
117
  // this legacy path is used when the nx-cloud package is installed.
@@ -116,29 +124,48 @@ function getBundleInstallDefaultLocation() {
116
124
  return (0, path_1.join)((0, is_ci_1.isCI)() ? (0, cache_directory_1.cacheDirectoryForWorkspace)(workspace_root_1.workspaceRoot) : cache_directory_1.cacheDir, 'cloud');
117
125
  }
118
126
  const runnerBundleInstallDirectory = getBundleInstallDefaultLocation();
127
+ const inWorkspace = (0, fs_1.existsSync)((0, path_1.join)(workspace_root_1.workspaceRoot, 'nx.json'));
119
128
  // Control files live outside the install directory. Released nx versions load
120
129
  // any directory there as a bundle, so they would `require` a state directory
121
130
  // placed inside it.
122
- const stateDirectory = (0, fs_1.existsSync)((0, path_1.join)(workspace_root_1.workspaceRoot, 'nx.json'))
131
+ const stateDirectory = inWorkspace
123
132
  ? (0, path_1.join)(cache_directory_1.workspaceDataDirectory, 'nx-cloud')
124
- : `${runnerBundleInstallDirectory}-state`;
133
+ : runnerBundleInstallDirectory && `${runnerBundleInstallDirectory}-state`;
125
134
  function ensureStateDirectory() {
126
135
  (0, fs_1.mkdirSync)(stateDirectory, { recursive: true });
127
136
  }
128
- const downloadLockFilePath = (0, path_1.join)(stateDirectory, 'download.lock');
137
+ function ensureInstallDirectories() {
138
+ if (inWorkspace) {
139
+ (0, fs_1.mkdirSync)(runnerBundleInstallDirectory, { recursive: true });
140
+ return;
141
+ }
142
+ if (!runnerBundleInstallDirectory) {
143
+ throw new Error('The Nx Cloud client cannot be installed outside a workspace without a home directory separate from the shared temp directory.');
144
+ }
145
+ for (const dir of [
146
+ nx_tmp_dir_1.NX_HOME_TMP_DIR,
147
+ (0, path_1.dirname)(runnerBundleInstallDirectory),
148
+ runnerBundleInstallDirectory,
149
+ stateDirectory,
150
+ ]) {
151
+ const result = (0, owned_private_dir_1.ensureOwnedPrivateDir)(dir);
152
+ if (result.status === 'refused') {
153
+ throw new owned_private_dir_1.DirectoryRefusedError(result.refusal);
154
+ }
155
+ }
156
+ }
157
+ const downloadLockFilePath = stateDirectory && (0, path_1.join)(stateDirectory, 'download.lock');
129
158
  // The record lives beside the lockfile rather than inside it. Windows
130
159
  // byte-range locks are mandatory and handle-scoped, so writing to a file this
131
160
  // process holds an exclusive lock on fails with ERROR_LOCK_VIOLATION. It also
132
161
  // matches the convention elsewhere in nx: project-graph.lock and run.json.lock
133
162
  // are never written to.
134
- const downloadRecordFilePath = (0, path_1.join)(stateDirectory, 'download.record');
163
+ const downloadRecordFilePath = stateDirectory && (0, path_1.join)(stateDirectory, 'download.record');
135
164
  // A version names a directory that is created and later deleted, so a value
136
165
  // from the server must not be able to escape the install directory.
137
166
  const VALID_BUNDLE_VERSION = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
138
167
  function getLatestInstalledRunnerBundle() {
139
- if (!(0, fs_1.existsSync)(runnerBundleInstallDirectory)) {
140
- (0, fs_1.mkdirSync)(runnerBundleInstallDirectory, { recursive: true });
141
- }
168
+ ensureInstallDirectories();
142
169
  try {
143
170
  const installedBundles = (0, fs_1.readdirSync)(runnerBundleInstallDirectory)
144
171
  .filter((potentialDirectory) => {
@@ -25,5 +25,5 @@ export declare function findProjectsNpmDependencies(projectNode: ProjectGraphPro
25
25
  helperDependencies?: string[];
26
26
  ignoredDependencies?: string[];
27
27
  isProduction?: boolean;
28
- }, fileMap?: ProjectFileMap): NpmDeps;
28
+ }, fileMap?: ProjectFileMap, root?: string): NpmDeps;
29
29
  export {};