nx 24.0.5 → 24.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) 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/adapter/ngcli-adapter.js +7 -20
  5. package/dist/src/command-line/configure-ai-agents/configure-ai-agents.js +0 -1
  6. package/dist/src/command-line/generate/generate.js +12 -23
  7. package/dist/src/command-line/graph/graph.js +1 -7
  8. package/dist/src/command-line/import/utils/prepare-source-repo.js +0 -3
  9. package/dist/src/command-line/init/implementation/angular/standalone-workspace.js +2 -2
  10. package/dist/src/command-line/init/init-v2.js +2 -2
  11. package/dist/src/command-line/migrate/migrate.js +4 -6
  12. package/dist/src/command-line/migrate/prompt-files.js +5 -5
  13. package/dist/src/command-line/migrate/run/orchestrator.js +2 -3
  14. package/dist/src/command-line/migrate/run/worker.js +7 -5
  15. package/dist/src/command-line/migrate/run-migration-process.js +3 -2
  16. package/dist/src/command-line/nx-cloud/start-nx-agents/command-object.js +1 -1
  17. package/dist/src/command-line/release/changelog.js +8 -39
  18. package/dist/src/command-line/release/config/use-legacy-versioning.js +1 -1
  19. package/dist/src/command-line/release/utils/diff.js +0 -14
  20. package/dist/src/command-line/release/utils/git.js +1 -1
  21. package/dist/src/command-line/reset/reset.js +13 -1
  22. package/dist/src/command-line/run/executor-utils.js +1 -1
  23. package/dist/src/command-line/show/show-target/utils.js +1 -1
  24. package/dist/src/command-line/watch/watch.js +1 -1
  25. package/dist/src/commands-runner/get-command-projects.js +0 -3
  26. package/dist/src/config/misc-interfaces.d.ts +0 -1
  27. package/dist/src/core/graph/main.js +1 -1
  28. package/dist/src/core/graph/styles.css +1 -1
  29. package/dist/src/core/graph/styles.js +1 -1
  30. package/dist/src/daemon/client/daemon-environment.d.ts +5 -3
  31. package/dist/src/daemon/client/daemon-environment.js +15 -3
  32. package/dist/src/devkit-internals.js +1 -1
  33. package/dist/src/executors/run-commands/running-tasks.d.ts +1 -0
  34. package/dist/src/executors/run-commands/running-tasks.js +69 -17
  35. package/dist/src/executors/run-script/run-script.impl.js +11 -3
  36. package/dist/src/hasher/hash-task.js +4 -4
  37. package/dist/src/hasher/task-hasher.js +2 -2
  38. package/dist/src/native/index.js +31 -3
  39. package/dist/src/native/nx.wasm32-wasi.debug.wasm +0 -0
  40. package/dist/src/native/nx.wasm32-wasi.wasm +0 -0
  41. package/dist/src/native/transform-objects.js +5 -1
  42. package/dist/src/nx-cloud/update-manager.d.ts +6 -1
  43. package/dist/src/nx-cloud/update-manager.js +39 -12
  44. package/dist/src/nx-cloud/utilities/environment.js +0 -17
  45. package/dist/src/plugins/js/lock-file/bun-parser.js +0 -2
  46. package/dist/src/plugins/js/lock-file/npm-parser.js +1 -2
  47. package/dist/src/plugins/js/package-json/create-package-json.d.ts +1 -1
  48. package/dist/src/plugins/js/package-json/create-package-json.js +31 -13
  49. package/dist/src/plugins/js/project-graph/build-dependencies/target-project-locator.js +7 -1
  50. package/dist/src/plugins/js/utils/get-workspace-packages-from-graph.js +1 -1
  51. package/dist/src/plugins/js/utils/register.js +1 -1
  52. package/dist/src/project-graph/affected/affected-tasks.js +1 -1
  53. package/dist/src/tasks-runner/run-command.js +6 -7
  54. package/dist/src/tasks-runner/running-tasks/batch-process.d.ts +3 -2
  55. package/dist/src/tasks-runner/running-tasks/batch-process.js +7 -3
  56. package/dist/src/tasks-runner/task-orchestrator.js +1 -1
  57. package/dist/src/tasks-runner/utils.js +1 -12
  58. package/dist/src/ultracache/overrides.js +1 -1
  59. package/dist/src/utils/cache-directory.d.ts +3 -0
  60. package/dist/src/utils/cache-directory.js +105 -25
  61. package/dist/src/utils/child-process.d.ts +8 -1
  62. package/dist/src/utils/child-process.js +24 -0
  63. package/dist/src/utils/code-frames.js +0 -1
  64. package/dist/src/utils/compile-cache.d.ts +6 -8
  65. package/dist/src/utils/compile-cache.js +23 -16
  66. package/dist/src/utils/db-connection.js +6 -4
  67. package/dist/src/utils/min-release-age/behavior/pnpm.js +9 -9
  68. package/dist/src/utils/min-release-age/behavior/yarn.js +2 -2
  69. package/dist/src/utils/nx-tmp-dir.d.ts +14 -2
  70. package/dist/src/utils/nx-tmp-dir.js +26 -2
  71. package/dist/src/utils/package-json.js +40 -20
  72. package/dist/src/utils/package-manager.d.ts +15 -0
  73. package/dist/src/utils/package-manager.js +184 -2
  74. package/dist/src/utils/plugins/output.js +0 -2
  75. package/package.json +12 -12
@@ -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
  };
@@ -9,7 +9,7 @@ const native_1 = require("../native");
9
9
  const project_graph_1 = require("../project-graph/project-graph");
10
10
  const task_io_service_1 = require("../tasks-runner/task-io-service");
11
11
  const task_env_1 = require("../tasks-runner/task-env");
12
- // TODO(v25): remove custom hasher support.
12
+ // TODO(v25): remove custom hasher support (NXC-5052).
13
13
  const utils_1 = require("../tasks-runner/utils");
14
14
  const db_connection_1 = require("../utils/db-connection");
15
15
  const task_hasher_1 = require("./task-hasher");
@@ -33,7 +33,7 @@ async function hashTasksThatDoNotDependOnOutputsOfOtherTasks(hasher, projectGrap
33
33
  ? new Set((0, native_1.getUltracacheDeferredTaskIds)(ultracacheConfigurations, taskGraph))
34
34
  : null;
35
35
  const tasks = Object.values(taskGraph.tasks);
36
- // TODO(v25): remove custom hasher support.
36
+ // TODO(v25): remove custom hasher support (NXC-5052).
37
37
  const tasksWithHashers = await Promise.all(tasks.map(async (task) => {
38
38
  const customHasher = (0, utils_1.getCustomHasher)(task, projects);
39
39
  return { task, customHasher };
@@ -83,7 +83,7 @@ function readsDependencyOutputs(task, taskGraph, projectGraph, nxJson) {
83
83
  async function hashTask(hasher, projectGraph, taskGraph, task, env, taskDetails) {
84
84
  performance.mark('hashSingleTask:start');
85
85
  const projectsConfigurations = (0, project_graph_1.readProjectsConfigurationFromProjectGraph)(projectGraph);
86
- // TODO(v25): remove custom hasher support.
86
+ // TODO(v25): remove custom hasher support (NXC-5052).
87
87
  const customHasher = (0, utils_1.getCustomHasher)(task, projectsConfigurations.projects);
88
88
  const { value, details, inputs } = await (customHasher
89
89
  ? customHasher(task, {
@@ -129,7 +129,7 @@ async function hashTasks(hasher, projectGraph, taskGraph, perTaskEnvs, taskDetai
129
129
  const projectsConfigurations = (0, project_graph_1.readProjectsConfigurationFromProjectGraph)(projectGraph);
130
130
  const nxJson = (0, nx_json_1.readNxJson)();
131
131
  const tasks = (tasksToHashOverride ?? Object.values(taskGraph.tasks)).filter((task) => !task.hash);
132
- // TODO(v25): remove custom hasher support.
132
+ // TODO(v25): remove custom hasher support (NXC-5052).
133
133
  // Separate tasks with custom hashers from those without
134
134
  const tasksWithCustomHashers = [];
135
135
  const tasksWithoutCustomHashers = [];
@@ -201,7 +201,7 @@ function splitInputsIntoSelfAndDependencies(inputs, namedInputs) {
201
201
  else if ('input' in d &&
202
202
  !('fileset' in d) &&
203
203
  (('dependencies' in d && d.dependencies && !('projects' in d)) ||
204
- // Todo(@AgentEnder): Remove check in v17
204
+ // TODO(v25): remove the 'dependencies' magic string (NXC-5157)
205
205
  ('projects' in d &&
206
206
  typeof d.projects === 'string' &&
207
207
  d.projects === 'dependencies'))) {
@@ -214,7 +214,7 @@ function splitInputsIntoSelfAndDependencies(inputs, namedInputs) {
214
214
  !('fileset' in d) &&
215
215
  'projects' in d &&
216
216
  d.projects &&
217
- // Todo(@AgentEnder): Remove check in v17
217
+ // TODO(v25): remove the 'self' magic string (NXC-5157)
218
218
  !(d.projects === 'self')) {
219
219
  projectInputs.push({
220
220
  input: d.input,
@@ -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) => {
@@ -5,26 +5,9 @@ const tslib_1 = require("tslib");
5
5
  const dotenv = tslib_1.__importStar(require("dotenv"));
6
6
  const fs_1 = require("fs");
7
7
  const path_1 = require("path");
8
- const is_ci_1 = require("../../utils/is-ci");
9
8
  const workspace_root_1 = require("../../utils/workspace-root");
10
9
  // Set once
11
10
  exports.UNLIMITED_TIMEOUT = 9999999;
12
- process.env.NX_CLOUD_AGENT_TIMEOUT_MS
13
- ? Number(process.env.NX_CLOUD_AGENT_TIMEOUT_MS)
14
- : 3600000;
15
- // 60 minutes
16
- process.env.NX_CLOUD_ORCHESTRATOR_TIMEOUT_MS
17
- ? Number(process.env.NX_CLOUD_ORCHESTRATOR_TIMEOUT_MS)
18
- : 3600000;
19
- // 60 minutes
20
- process.env.NX_CLOUD_DISTRIBUTED_EXECUTION_AGENT_COUNT
21
- ? Number(process.env.NX_CLOUD_DISTRIBUTED_EXECUTION_AGENT_COUNT)
22
- : null;
23
- process.env.NX_CLOUD_NUMBER_OF_RETRIES
24
- ? Number(process.env.NX_CLOUD_NUMBER_OF_RETRIES)
25
- : (0, is_ci_1.isCI)()
26
- ? 10
27
- : 1;
28
11
  loadEnvVars();
29
12
  function parseEnv() {
30
13
  try {
@@ -34,7 +34,6 @@ let currentLockFileHash;
34
34
  let cachedParsedLockFile;
35
35
  let cachedPackageIndex;
36
36
  const keyMap = new Map();
37
- const packageVersions = new Map();
38
37
  const specParseCache = new Map();
39
38
  // Memoizes (packageName, versionSpec) -> resolved version for the current
40
39
  // lockfile. The same name/range pairs repeat across many dependency lists, so
@@ -84,7 +83,6 @@ function clearCache() {
84
83
  cachedParsedLockFile = undefined;
85
84
  cachedPackageIndex = undefined;
86
85
  keyMap.clear();
87
- packageVersions.clear();
88
86
  specParseCache.clear();
89
87
  resolvedVersionCache.clear();
90
88
  }
@@ -580,10 +580,9 @@ function getPeerDependencies(path) {
580
580
  const fullPath = `${workspace_root_1.workspaceRoot}/${path}/package.json`;
581
581
  if ((0, fs_1.existsSync)(fullPath)) {
582
582
  const content = (0, fs_1.readFileSync)(fullPath, 'utf-8');
583
- const { peerDependencies, peerDependenciesMeta } = JSON.parse(content);
583
+ const { peerDependencies } = JSON.parse(content);
584
584
  return {
585
585
  ...(peerDependencies && { peerDependencies }),
586
- ...(peerDependenciesMeta && { peerDependenciesMeta }),
587
586
  };
588
587
  }
589
588
  else {
@@ -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 {};