nx 24.0.4 → 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 (78) 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/affected/affected.js +1 -0
  5. package/dist/src/command-line/migrate/migrate.js +23 -7
  6. package/dist/src/command-line/migrate/prompt-files.d.ts +4 -2
  7. package/dist/src/command-line/migrate/prompt-files.js +26 -5
  8. package/dist/src/command-line/nx-cloud/start-nx-agents/command-object.js +1 -1
  9. package/dist/src/command-line/release/changelog.js +15 -12
  10. package/dist/src/command-line/reset/reset.js +13 -1
  11. package/dist/src/daemon/client/client.js +8 -7
  12. package/dist/src/daemon/client/daemon-environment.d.ts +43 -11
  13. package/dist/src/daemon/client/daemon-environment.js +139 -8
  14. package/dist/src/daemon/server/handle-client-env.d.ts +11 -1
  15. package/dist/src/daemon/server/handle-client-env.js +96 -5
  16. package/dist/src/daemon/server/handle-configure-ai-agents.js +2 -1
  17. package/dist/src/daemon/server/latest-nx.js +2 -1
  18. package/dist/src/daemon/server/server.js +1 -1
  19. package/dist/src/daemon/socket-utils.d.ts +9 -1
  20. package/dist/src/daemon/socket-utils.js +25 -0
  21. package/dist/src/executors/run-commands/running-tasks.d.ts +1 -0
  22. package/dist/src/executors/run-commands/running-tasks.js +69 -17
  23. package/dist/src/executors/run-script/run-script.impl.js +11 -3
  24. package/dist/src/native/index.d.ts +1 -1
  25. package/dist/src/native/index.js +31 -3
  26. package/dist/src/native/native-file-cache-location.js +10 -4
  27. package/dist/src/native/nx.wasm32-wasi.debug.wasm +0 -0
  28. package/dist/src/native/nx.wasm32-wasi.wasm +0 -0
  29. package/dist/src/native/transform-objects.js +5 -1
  30. package/dist/src/nx-cloud/update-manager.d.ts +6 -1
  31. package/dist/src/nx-cloud/update-manager.js +39 -12
  32. package/dist/src/plugins/js/lock-file/pnpm-parser.js +22 -34
  33. package/dist/src/plugins/js/lock-file/project-graph-pruning.d.ts +7 -0
  34. package/dist/src/plugins/js/lock-file/project-graph-pruning.js +31 -5
  35. package/dist/src/plugins/js/lock-file/utils/pnpm-normalizer.d.ts +1 -1
  36. package/dist/src/plugins/js/lock-file/utils/pnpm-normalizer.js +6 -5
  37. package/dist/src/plugins/js/package-json/create-package-json.d.ts +1 -1
  38. package/dist/src/plugins/js/package-json/create-package-json.js +53 -16
  39. package/dist/src/plugins/js/project-graph/build-dependencies/target-project-locator.js +7 -1
  40. package/dist/src/project-graph/affected/affected-tasks.js +29 -1
  41. package/dist/src/project-graph/plugins/get-plugins.d.ts +2 -0
  42. package/dist/src/project-graph/plugins/get-plugins.js +17 -2
  43. package/dist/src/project-graph/plugins/isolation/isolated-plugin.js +4 -2
  44. package/dist/src/tasks-runner/fork.js +4 -2
  45. package/dist/src/tasks-runner/run-command.d.ts +4 -1
  46. package/dist/src/tasks-runner/run-command.js +6 -4
  47. package/dist/src/tasks-runner/running-tasks/batch-process.d.ts +3 -2
  48. package/dist/src/tasks-runner/running-tasks/batch-process.js +7 -3
  49. package/dist/src/tasks-runner/task-orchestrator.d.ts +1 -0
  50. package/dist/src/tasks-runner/task-orchestrator.js +12 -3
  51. package/dist/src/tasks-runner/tasks-runner.d.ts +5 -0
  52. package/dist/src/ultracache/overrides.d.ts +1 -1
  53. package/dist/src/ultracache/overrides.js +1 -1
  54. package/dist/src/ultracache/report.d.ts +1 -1
  55. package/dist/src/ultracache/report.js +4 -1
  56. package/dist/src/utils/cache-directory.d.ts +3 -0
  57. package/dist/src/utils/cache-directory.js +105 -25
  58. package/dist/src/utils/child-process.d.ts +8 -1
  59. package/dist/src/utils/child-process.js +24 -0
  60. package/dist/src/utils/compile-cache.d.ts +6 -8
  61. package/dist/src/utils/compile-cache.js +23 -16
  62. package/dist/src/utils/min-release-age/packument.d.ts +2 -1
  63. package/dist/src/utils/min-release-age/packument.js +5 -3
  64. package/dist/src/utils/nx-tmp-dir.d.ts +14 -2
  65. package/dist/src/utils/nx-tmp-dir.js +26 -2
  66. package/dist/src/utils/owned-private-dir.d.ts +22 -0
  67. package/dist/src/utils/owned-private-dir.js +148 -0
  68. package/dist/src/utils/package-json.d.ts +8 -1
  69. package/dist/src/utils/package-json.js +77 -34
  70. package/dist/src/utils/package-manager.d.ts +25 -0
  71. package/dist/src/utils/package-manager.js +244 -9
  72. package/dist/src/utils/path.d.ts +4 -0
  73. package/dist/src/utils/path.js +8 -0
  74. package/dist/src/utils/redact-url-credentials.js +3 -2
  75. package/dist/src/utils/wait-for-socket-connection.d.ts +2 -0
  76. package/dist/src/utils/wait-for-socket-connection.js +3 -3
  77. package/package.json +11 -11
  78. package/schemas/migrations-schema.json +1 -1
@@ -172,7 +172,14 @@ export declare function installPackageToTmp(pkg: string, requiredVersion: string
172
172
  tempDir: string;
173
173
  cleanup: () => void;
174
174
  };
175
- export declare function installPackageToTmpAsync(pkg: string, requiredVersion: string, packageManager: PackageManager): Promise<{
175
+ /**
176
+ * @param options.env Base env for each install command, read as the command
177
+ * starts. Defaults to `process.env`. With it, a `yarn` that only Corepack
178
+ * provides runs through Corepack.
179
+ */
180
+ export declare function installPackageToTmpAsync(pkg: string, requiredVersion: string, packageManager: PackageManager, options?: {
181
+ env?: () => NodeJS.ProcessEnv;
182
+ }): Promise<{
176
183
  tempDir: string;
177
184
  cleanup: () => void;
178
185
  }>;
@@ -19,7 +19,7 @@ const fs_1 = require("fs");
19
19
  const node_module_1 = tslib_1.__importStar(require("node:module"));
20
20
  const path_1 = require("path");
21
21
  const execAsync = (0, util_1.promisify)(child_process_1.exec);
22
- const tmp_1 = require("tmp");
22
+ const which_1 = tslib_1.__importDefault(require("which"));
23
23
  const json_1 = require("../generators/utils/json");
24
24
  const project_configuration_utils_1 = require("../project-graph/utils/project-configuration-utils");
25
25
  const target_merging_1 = require("../project-graph/utils/project-configuration/target-merging");
@@ -194,30 +194,54 @@ function readModulePackageJsonWithoutFallbacks(moduleSpecifier, requirePaths = (
194
194
  * @returns package json contents and path
195
195
  */
196
196
  function readModulePackageJson(moduleSpecifier, requirePaths = (0, installation_directory_1.getNxRequirePaths)()) {
197
- let packageJsonPath;
198
- let packageJson;
197
+ let exported;
199
198
  try {
200
- ({ path: packageJsonPath, packageJson } =
201
- readModulePackageJsonWithoutFallbacks(moduleSpecifier, requirePaths));
199
+ exported = readModulePackageJsonWithoutFallbacks(moduleSpecifier, requirePaths);
200
+ }
201
+ catch { }
202
+ // A wildcard export can map package.json to a nested `{"type": ...}` stub,
203
+ // so a nameless manifest is kept only when the lookup step finds no other.
204
+ if (exported?.packageJson.name) {
205
+ return exported;
206
+ }
207
+ const lookupPathMatch = findPackageJsonInLookupPaths(moduleSpecifier, requirePaths);
208
+ if (lookupPathMatch) {
209
+ return {
210
+ packageJson: (0, fileutils_1.readJsonFile)(lookupPathMatch),
211
+ path: lookupPathMatch,
212
+ };
213
+ }
214
+ if (exported) {
215
+ return exported;
202
216
  }
203
- catch {
204
- const entryPoint = resolveWithoutCachePollution(moduleSpecifier, requirePaths);
205
- let moduleRootPath = (0, path_1.dirname)(entryPoint);
217
+ const entryPoint = resolveWithoutCachePollution(moduleSpecifier, requirePaths);
218
+ let moduleRootPath = (0, path_1.dirname)(entryPoint);
219
+ let packageJsonPath = (0, path_1.join)(moduleRootPath, 'package.json');
220
+ while (!(0, fs_1.existsSync)(packageJsonPath)) {
221
+ moduleRootPath = (0, path_1.dirname)(moduleRootPath);
206
222
  packageJsonPath = (0, path_1.join)(moduleRootPath, 'package.json');
207
- while (!(0, fs_1.existsSync)(packageJsonPath)) {
208
- moduleRootPath = (0, path_1.dirname)(moduleRootPath);
209
- packageJsonPath = (0, path_1.join)(moduleRootPath, 'package.json');
210
- }
211
- packageJson = (0, fileutils_1.readJsonFile)(packageJsonPath);
212
- if (packageJson.name && packageJson.name !== moduleSpecifier) {
213
- throw new Error(`Found module ${packageJson.name} while trying to locate ${moduleSpecifier}/package.json`);
214
- }
223
+ }
224
+ const packageJson = (0, fileutils_1.readJsonFile)(packageJsonPath);
225
+ if (packageJson.name && packageJson.name !== moduleSpecifier) {
226
+ throw new Error(`Found module ${packageJson.name} while trying to locate ${moduleSpecifier}/package.json`);
215
227
  }
216
228
  return {
217
229
  packageJson,
218
230
  path: packageJsonPath,
219
231
  };
220
232
  }
233
+ function findPackageJsonInLookupPaths(moduleSpecifier, requirePaths) {
234
+ for (const requirePath of requirePaths) {
235
+ const lookupPaths = (0, node_module_1.createRequire)((0, path_1.join)((0, path_1.resolve)(requirePath), 'noop.js')).resolve.paths(moduleSpecifier) ?? [];
236
+ for (const lookupPath of lookupPaths) {
237
+ const candidate = (0, path_1.join)(lookupPath, moduleSpecifier, 'package.json');
238
+ if ((0, fs_1.existsSync)(candidate)) {
239
+ // Resolving gives a symlinked package the same real path Node returns.
240
+ return resolveWithoutCachePollution(candidate, requirePaths);
241
+ }
242
+ }
243
+ }
244
+ }
221
245
  /**
222
246
  * Resolve `request` via Node's CJS resolver while neutralising both ways
223
247
  * `require.resolve(req, { paths })` can lie about the `paths` argument:
@@ -259,10 +283,7 @@ function resolveWithoutCachePollution(request, requirePaths) {
259
283
  * This is used by both sync and async installation functions.
260
284
  */
261
285
  function preparePackageInstallation(pkg, requiredVersion, packageManager) {
262
- const { dir: tempDir, cleanup } = (0, package_manager_1.createTempNpmDirectory)?.() ?? {
263
- dir: (0, tmp_1.dirSync)().name,
264
- cleanup: () => { },
265
- };
286
+ const { dir: tempDir, cleanup } = (0, package_manager_1.createTempNpmDirectory)();
266
287
  console.log(`Fetching ${pkg}...`);
267
288
  const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
268
289
  generatePackageManagerFiles(tempDir, packageManager);
@@ -288,9 +309,14 @@ function preparePackageInstallation(pkg, requiredVersion, packageManager) {
288
309
  bun: '--omit=peer',
289
310
  pnpm: '--config.auto-install-peers=false',
290
311
  };
291
- const skipPeerDependenciesEnv = packageManager === 'pnpm'
292
- ? { PNPM_CONFIG_AUTO_INSTALL_PEERS: 'false' }
293
- : {};
312
+ // Yarn Berry requires an environment variable (not a CLI flag) to disable lifecycle scripts.
313
+ // Apply this defensively for all package managers when pulling nx@latest to tmp.
314
+ const installEnv = {
315
+ YARN_ENABLE_SCRIPTS: 'false',
316
+ ...(packageManager === 'pnpm'
317
+ ? { PNPM_CONFIG_AUTO_INSTALL_PEERS: 'false' }
318
+ : {}),
319
+ };
294
320
  const installCommand = [
295
321
  pmCommands.addDev,
296
322
  `${pkg}@${requiredVersion}`,
@@ -303,13 +329,7 @@ function preparePackageInstallation(pkg, requiredVersion, packageManager) {
303
329
  cwd: tempDir,
304
330
  stdio: isVerbose ? 'inherit' : 'ignore',
305
331
  windowsHide: true,
306
- // Yarn Berry requires an environment variable (not a CLI flag) to disable lifecycle scripts.
307
- // Apply this defensively for all package managers when pulling nx@latest to tmp.
308
- env: {
309
- ...process.env,
310
- YARN_ENABLE_SCRIPTS: 'false',
311
- ...skipPeerDependenciesEnv,
312
- },
332
+ env: { ...process.env, ...installEnv },
313
333
  };
314
334
  return {
315
335
  tempDir,
@@ -317,6 +337,7 @@ function preparePackageInstallation(pkg, requiredVersion, packageManager) {
317
337
  preInstallCommand,
318
338
  installCommand,
319
339
  execOptions,
340
+ installEnv,
320
341
  };
321
342
  }
322
343
  function installPackageToTmp(pkg, requiredVersion, packageManager) {
@@ -331,14 +352,28 @@ function installPackageToTmp(pkg, requiredVersion, packageManager) {
331
352
  cleanup,
332
353
  };
333
354
  }
334
- async function installPackageToTmpAsync(pkg, requiredVersion, packageManager) {
335
- const { tempDir, cleanup, preInstallCommand, installCommand, execOptions } = preparePackageInstallation(pkg, requiredVersion, packageManager);
355
+ /**
356
+ * @param options.env Base env for each install command, read as the command
357
+ * starts. Defaults to `process.env`. With it, a `yarn` that only Corepack
358
+ * provides runs through Corepack.
359
+ */
360
+ async function installPackageToTmpAsync(pkg, requiredVersion, packageManager, options = {}) {
361
+ const { tempDir, cleanup, preInstallCommand, installCommand, execOptions, installEnv, } = preparePackageInstallation(pkg, requiredVersion, packageManager);
362
+ const run = (command) => {
363
+ if (!options.env) {
364
+ return execAsync(command, execOptions);
365
+ }
366
+ const env = { ...options.env(), ...installEnv };
367
+ return execAsync(packageManager === 'yarn'
368
+ ? yarnThroughCorepackIfNeeded(command, env)
369
+ : command, { ...execOptions, env });
370
+ };
336
371
  try {
337
372
  if (preInstallCommand) {
338
373
  // ensure package.json and repo in tmp folder is set to a proper package manager state
339
- await execAsync(preInstallCommand, execOptions);
374
+ await run(preInstallCommand);
340
375
  }
341
- await execAsync(installCommand, execOptions);
376
+ await run(installCommand);
342
377
  return {
343
378
  tempDir,
344
379
  cleanup,
@@ -350,6 +385,14 @@ async function installPackageToTmpAsync(pkg, requiredVersion, packageManager) {
350
385
  throw error;
351
386
  }
352
387
  }
388
+ function yarnThroughCorepackIfNeeded(command, env) {
389
+ const path = Object.entries(env).find(([key]) => key.toUpperCase() === 'PATH')?.[1] ??
390
+ '';
391
+ const onPath = (bin) => !!path && !!which_1.default.sync(bin, { path, nothrow: true });
392
+ return !onPath('yarn') && onPath('corepack')
393
+ ? `corepack ${command}`
394
+ : command;
395
+ }
353
396
  function getDependencyVersionFromPackageJson(treeOrPackageName, packageNameOrRoot, packageJsonPathOrObjectOrRoot, dependencyLookup) {
354
397
  if (typeof treeOrPackageName !== 'string') {
355
398
  return getDependencyVersionFromPackageJsonFromTree(treeOrPackageName, packageNameOrRoot, packageJsonPathOrObjectOrRoot, dependencyLookup);
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Package managers read an argument starting with `-` as an option, and a
3
+ * path, URL or git remote wherever a name goes. `nx migrate` passes names
4
+ * read from fetched migration metadata, so only a registry-shaped name may
5
+ * reach a package manager command line.
6
+ */
7
+ export declare function assertValidPackageName(packageName: string): void;
1
8
  export type PackageManager = 'yarn' | 'pnpm' | 'npm' | 'bun';
2
9
  export interface PackageManagerCommands {
3
10
  preInstall?: string;
@@ -106,6 +113,21 @@ export declare function copyPackageManagerConfigurationFiles(root: string, desti
106
113
  * files under the Nx installation directory instead.
107
114
  */
108
115
  export declare function getPackageManagerConfigRoot(root?: string): string;
116
+ /**
117
+ * Creates an empty owner-only directory for a throwaway package install.
118
+ *
119
+ * Package managers and Node read config and modules from every ancestor of
120
+ * the install, so it lives under `~/.nx/tmp`, else under the OS temp dir, and
121
+ * only where that root and all of its ancestors are closed to other users.
122
+ * Throws when neither root qualifies or can be written. The OS does not reap
123
+ * it, so it is removed when the process exits.
124
+ */
125
+ export declare function createTempInstallDirectory(): string;
126
+ /**
127
+ * Removes this user's throwaway install dirs under `~/.nx/tmp` and the temp
128
+ * dir fallback that are more than a day old. Best effort; creates nothing.
129
+ */
130
+ export declare function removeStaleTempInstallDirectories(): void;
109
131
  /**
110
132
  * Creates a temporary directory where you can run package manager commands safely.
111
133
  *
@@ -121,6 +143,9 @@ export declare function createTempNpmDirectory(skipCopy?: boolean): {
121
143
  dir: string;
122
144
  cleanup: () => Promise<void>;
123
145
  };
146
+ export declare class InvalidResolvedVersionError extends Error {
147
+ constructor(packageName: string, version: unknown);
148
+ }
124
149
  /**
125
150
  * Returns the resolved version for a given package and version tag using the
126
151
  * NPM registry (when using Yarn it will fall back to NPM to fetch the info).
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidResolvedVersionError = void 0;
4
+ exports.assertValidPackageName = assertValidPackageName;
3
5
  exports.detectPackageManager = detectPackageManager;
4
6
  exports.isWorkspacesEnabled = isWorkspacesEnabled;
5
7
  exports.getPackageManagerCommand = getPackageManagerCommand;
@@ -11,6 +13,8 @@ exports.modifyYarnRcToFitNewDirectory = modifyYarnRcToFitNewDirectory;
11
13
  exports.modifyPnpmWorkspaceYamlToFitNewDirectory = modifyPnpmWorkspaceYamlToFitNewDirectory;
12
14
  exports.copyPackageManagerConfigurationFiles = copyPackageManagerConfigurationFiles;
13
15
  exports.getPackageManagerConfigRoot = getPackageManagerConfigRoot;
16
+ exports.createTempInstallDirectory = createTempInstallDirectory;
17
+ exports.removeStaleTempInstallDirectories = removeStaleTempInstallDirectories;
14
18
  exports.createTempNpmDirectory = createTempNpmDirectory;
15
19
  exports.resolvePackageVersionUsingRegistry = resolvePackageVersionUsingRegistry;
16
20
  exports.resolvePackageVersionUsingInstallation = resolvePackageVersionUsingInstallation;
@@ -36,6 +40,8 @@ const catalog_1 = require("./catalog");
36
40
  const fileutils_1 = require("./fileutils");
37
41
  const installation_directory_1 = require("./installation-directory");
38
42
  const logger_1 = require("./logger");
43
+ const nx_tmp_dir_1 = require("./nx-tmp-dir");
44
+ const owned_private_dir_1 = require("./owned-private-dir");
39
45
  const package_json_1 = require("./package-json");
40
46
  const redact_url_credentials_1 = require("./redact-url-credentials");
41
47
  const shell_quoting_1 = require("./shell-quoting");
@@ -66,6 +72,19 @@ function execPackageManagerSync(pm, args, options) {
66
72
  }
67
73
  return (0, child_process_1.execFileSync)(pm, args, options);
68
74
  }
75
+ // The `@` keeps a scoped name from reading as an option, so its parts may start with `-` or `_`.
76
+ const PACKAGE_NAME = /^(?:@[a-z0-9~_-][a-z0-9-._~]*\/[a-z0-9~_-]|[a-z0-9~])[a-z0-9-._~]*$/i;
77
+ /**
78
+ * Package managers read an argument starting with `-` as an option, and a
79
+ * path, URL or git remote wherever a name goes. `nx migrate` passes names
80
+ * read from fetched migration metadata, so only a registry-shaped name may
81
+ * reach a package manager command line.
82
+ */
83
+ function assertValidPackageName(packageName) {
84
+ if (!PACKAGE_NAME.test(packageName)) {
85
+ throw new Error(`Invalid package name: "${packageName}". Expected an npm package name such as "my-package" or "@scope/my-package".`);
86
+ }
87
+ }
69
88
  /**
70
89
  * Detects the package manager from the CLI override or directory lockfiles.
71
90
  * Project directories in the current workspace can fall back to its root lockfile.
@@ -560,6 +579,184 @@ function getPackageManagerConfigRoot(root = workspace_root_1.workspaceRoot) {
560
579
  return root;
561
580
  }
562
581
  }
582
+ /**
583
+ * Creates an empty owner-only directory for a throwaway package install.
584
+ *
585
+ * Package managers and Node read config and modules from every ancestor of
586
+ * the install, so it lives under `~/.nx/tmp`, else under the OS temp dir, and
587
+ * only where that root and all of its ancestors are closed to other users.
588
+ * Throws when neither root qualifies or can be written. The OS does not reap
589
+ * it, so it is removed when the process exits.
590
+ */
591
+ function createTempInstallDirectory() {
592
+ const dir = createInPrivateInstallRoot();
593
+ if (tempInstallDirectories.size === 0) {
594
+ process.once('exit', removeTempInstallDirectories);
595
+ }
596
+ tempInstallDirectories.add(dir);
597
+ return dir;
598
+ }
599
+ /**
600
+ * Removes this user's throwaway install dirs under `~/.nx/tmp` and the temp
601
+ * dir fallback that are more than a day old. Best effort; creates nothing.
602
+ */
603
+ function removeStaleTempInstallDirectories() {
604
+ if (nx_tmp_dir_1.NX_HOME_TMP_DIR && (0, nx_tmp_dir_1.homeDirIsDistinctFromSharedTmp)()) {
605
+ const root = (0, path_1.join)(nx_tmp_dir_1.NX_HOME_TMP_DIR, 'tmp');
606
+ if ((0, owned_private_dir_1.isOwnedRealDirectory)(nx_tmp_dir_1.NX_HOME_TMP_DIR) && (0, owned_private_dir_1.isOwnedRealDirectory)(root)) {
607
+ removeStaleInstallDirectories(root);
608
+ }
609
+ }
610
+ const fallbackRoot = (0, path_1.join)((0, node_os_1.tmpdir)(), TMPDIR_INSTALL_ROOT);
611
+ if (!privateTreeRefusal((0, node_os_1.tmpdir)()) && (0, owned_private_dir_1.isOwnedRealDirectory)(fallbackRoot)) {
612
+ removeStaleInstallDirectories(fallbackRoot);
613
+ }
614
+ }
615
+ const tempInstallDirectories = new Set();
616
+ function removeTempInstallDirectories() {
617
+ for (const dir of tempInstallDirectories) {
618
+ try {
619
+ (0, fs_1.rmSync)(dir, { recursive: true, force: true });
620
+ }
621
+ catch { }
622
+ }
623
+ }
624
+ const STALE_INSTALL_DIRECTORY_MS = 24 * 60 * 60 * 1000;
625
+ /**
626
+ * Removes install dirs left behind by processes that never reached their exit
627
+ * handler. A day outlasts any realistic install or `nx migrate` run, so a
628
+ * concurrent process's install survives.
629
+ */
630
+ function removeStaleInstallDirectories(root) {
631
+ let entries;
632
+ try {
633
+ entries = (0, fs_1.readdirSync)(root);
634
+ }
635
+ catch {
636
+ return;
637
+ }
638
+ const cutoff = Date.now() - STALE_INSTALL_DIRECTORY_MS;
639
+ for (const entry of entries) {
640
+ const path = (0, path_1.join)(root, entry);
641
+ try {
642
+ const stats = (0, fs_1.lstatSync)(path);
643
+ if (stats.isDirectory() &&
644
+ stats.mtimeMs < cutoff &&
645
+ (typeof process.getuid !== 'function' || stats.uid === process.getuid())) {
646
+ (0, fs_1.rmSync)(path, { recursive: true, force: true });
647
+ }
648
+ }
649
+ catch { }
650
+ }
651
+ }
652
+ const TMPDIR_INSTALL_ROOT = 'nx-installs';
653
+ /**
654
+ * An existing `~/.nx/tmp` can still refuse writes, for example under an agent
655
+ * sandbox, so a failed create there moves on to the OS temp dir.
656
+ */
657
+ function createInPrivateInstallRoot() {
658
+ let homeRefusal = homeInstallRootRefusal();
659
+ if (!homeRefusal) {
660
+ const root = (0, path_1.join)(nx_tmp_dir_1.NX_HOME_TMP_DIR, 'tmp');
661
+ try {
662
+ return createInstallDirectory(root);
663
+ }
664
+ catch (e) {
665
+ homeRefusal = `${root} is not writable${e?.code ? ` (${e.code})` : ''}`;
666
+ }
667
+ }
668
+ const tmp = (0, node_os_1.tmpdir)();
669
+ const fallbackRoot = (0, path_1.join)(tmp, TMPDIR_INSTALL_ROOT);
670
+ let tmpRefusal = privateTreeRefusal(tmp);
671
+ if (!tmpRefusal) {
672
+ const result = (0, owned_private_dir_1.ensureOwnedPrivateDir)(fallbackRoot);
673
+ if (result.status === 'ok') {
674
+ return createInstallDirectory(fallbackRoot);
675
+ }
676
+ tmpRefusal = (0, owned_private_dir_1.describeRefusal)(result.refusal);
677
+ }
678
+ if (process.env.NX_ALLOW_INSECURE_DIRECTORIES === 'true') {
679
+ console.warn(`NX_ALLOW_INSECURE_DIRECTORIES is set, so Nx is installing packages in ${tmp} although ${tmpRefusal}.`);
680
+ return (0, tmp_1.dirSync)({ tmpdir: tmp, prefix: 'nx-install-' }).name;
681
+ }
682
+ throw new PrivateInstallRootError(homeRefusal, tmpRefusal);
683
+ }
684
+ function createInstallDirectory(root) {
685
+ removeStaleInstallDirectories(root);
686
+ return (0, tmp_1.dirSync)({ tmpdir: root }).name;
687
+ }
688
+ function homeInstallRootRefusal() {
689
+ if (!nx_tmp_dir_1.NX_HOME_TMP_DIR || !(0, nx_tmp_dir_1.homeDirIsDistinctFromSharedTmp)()) {
690
+ return 'there is no home directory separate from the shared temp directory';
691
+ }
692
+ for (const dir of [nx_tmp_dir_1.NX_HOME_TMP_DIR, (0, path_1.join)(nx_tmp_dir_1.NX_HOME_TMP_DIR, 'tmp')]) {
693
+ const result = (0, owned_private_dir_1.ensureOwnedPrivateDir)(dir);
694
+ if (result.status === 'refused') {
695
+ return (0, owned_private_dir_1.describeRefusal)(result.refusal);
696
+ }
697
+ }
698
+ return privateTreeRefusal((0, path_1.join)(nx_tmp_dir_1.NX_HOME_TMP_DIR, 'tmp'));
699
+ }
700
+ /**
701
+ * Why `dir` or one of its ancestors could be written by another user, if it
702
+ * could. Each must be a directory owned by us or root without group or other
703
+ * write, sticky or not, and `dir` itself must be ours and not a symlink. Both
704
+ * the path as written and its canonical form are checked, so a symlinked
705
+ * ancestor and the directory it leads to must each qualify.
706
+ */
707
+ function privateTreeRefusal(dir) {
708
+ if (typeof process.getuid !== 'function') {
709
+ return (0, owned_private_dir_1.isOwnedRealDirectory)(dir)
710
+ ? undefined
711
+ : `${dir} is not a real directory`;
712
+ }
713
+ for (const leaf of [(0, path_1.resolve)(dir), (0, owned_private_dir_1.canonicalDir)(dir)]) {
714
+ for (let p = leaf;; p = (0, path_1.dirname)(p)) {
715
+ const refusal = privatePathRefusal(p, p === leaf);
716
+ if (refusal) {
717
+ return refusal;
718
+ }
719
+ if ((0, path_1.dirname)(p) === p) {
720
+ break;
721
+ }
722
+ }
723
+ }
724
+ return undefined;
725
+ }
726
+ function privatePathRefusal(path, isLeaf) {
727
+ let stats;
728
+ try {
729
+ stats = (0, fs_1.lstatSync)(path);
730
+ }
731
+ catch (e) {
732
+ return `${path} could not be inspected${e?.code ? ` (${e.code})` : ''}`;
733
+ }
734
+ const uid = process.getuid();
735
+ const trustedOwner = stats.uid === uid || (!isLeaf && stats.uid === 0);
736
+ if (stats.isSymbolicLink() && !isLeaf) {
737
+ return trustedOwner ? undefined : `${path} is owned by uid ${stats.uid}`;
738
+ }
739
+ if (!stats.isDirectory()) {
740
+ return stats.isSymbolicLink()
741
+ ? `${path} is a symlink, not a real directory`
742
+ : `${path} is not a directory`;
743
+ }
744
+ if (stats.mode & 0o022) {
745
+ const mode = (stats.mode & 0o7777).toString(8).padStart(4, '0');
746
+ return `${path} is writable by other users (mode ${mode})`;
747
+ }
748
+ return trustedOwner ? undefined : `${path} is owned by uid ${stats.uid}`;
749
+ }
750
+ class PrivateInstallRootError extends Error {
751
+ constructor(homeRefusal, tmpRefusal) {
752
+ super('Nx cannot install packages because it found no directory closed to other users to install into.\n' +
753
+ ` ~/.nx/tmp: ${homeRefusal}.\n` +
754
+ ` The OS temp directory: ${tmpRefusal}.\n` +
755
+ 'Package managers and Node read config and modules from every parent of the install directory, so Nx will not install where another user could write to it or to any of its parents.\n' +
756
+ 'Point TMPDIR at a directory only you can write to, or set NX_ALLOW_INSECURE_DIRECTORIES=true to install in the OS temp directory anyway.');
757
+ this.name = 'PrivateInstallRootError';
758
+ }
759
+ }
563
760
  /**
564
761
  * Creates a temporary directory where you can run package manager commands safely.
565
762
  *
@@ -572,7 +769,7 @@ function getPackageManagerConfigRoot(root = workspace_root_1.workspaceRoot) {
572
769
  * where no existing configuration files are available to copy.
573
770
  */
574
771
  function createTempNpmDirectory(skipCopy = false) {
575
- const dir = (0, tmp_1.dirSync)().name;
772
+ const dir = createTempInstallDirectory();
576
773
  // A package.json is needed for pnpm pack and for .npmrc to resolve
577
774
  (0, fileutils_1.writeJsonFile)(`${dir}/package.json`, {});
578
775
  if (!skipCopy) {
@@ -583,11 +780,27 @@ function createTempNpmDirectory(skipCopy = false) {
583
780
  await (0, promises_1.rm)(dir, { recursive: true, force: true });
584
781
  }
585
782
  catch {
586
- // It's okay if this fails, the OS will clean it up eventually
783
+ // The exit handler retries the removal.
587
784
  }
588
785
  };
589
786
  return { dir, cleanup };
590
787
  }
788
+ class InvalidResolvedVersionError extends Error {
789
+ constructor(packageName, version) {
790
+ super(`${packageName} reports "${version}" as its version, which is not a valid version.`);
791
+ this.name = 'InvalidResolvedVersionError';
792
+ }
793
+ }
794
+ exports.InvalidResolvedVersionError = InvalidResolvedVersionError;
795
+ // A manifest's `version` is publisher-written and need not match the key it is
796
+ // listed under. Callers reuse it after `<package>@`, where a non-version is a
797
+ // spec of its own (git URL, tarball, path).
798
+ function assertResolvedVersion(packageName, version) {
799
+ if (!(0, semver_1.valid)(version)) {
800
+ throw new InvalidResolvedVersionError(packageName, version);
801
+ }
802
+ return version;
803
+ }
591
804
  /**
592
805
  * Returns the resolved version for a given package and version tag using the
593
806
  * NPM registry (when using Yarn it will fall back to NPM to fetch the info).
@@ -603,7 +816,7 @@ async function resolvePackageVersionUsingRegistry(packageName, version) {
603
816
  }
604
817
  const lines = result.split('\n');
605
818
  if (lines.length === 1) {
606
- return lines[0];
819
+ return assertResolvedVersion(packageName, lines[0]);
607
820
  }
608
821
  /**
609
822
  * The output contains multiple lines ordered by release date, so the last
@@ -617,9 +830,12 @@ async function resolvePackageVersionUsingRegistry(packageName, version) {
617
830
  .sort()
618
831
  .pop()
619
832
  .replace(/'/g, '');
620
- return finalResolvedVersion;
833
+ return assertResolvedVersion(packageName, finalResolvedVersion);
621
834
  }
622
835
  catch (e) {
836
+ if (e instanceof InvalidResolvedVersionError) {
837
+ throw e;
838
+ }
623
839
  // npm masks a URL credential only in the password position, so a bare token
624
840
  // in the registry URL survives into the error kept as the cause.
625
841
  throw new Error(`Unable to resolve version ${packageName}@${version}.`, {
@@ -645,6 +861,7 @@ function redactErrorCause(error) {
645
861
  * package.json.
646
862
  */
647
863
  async function resolvePackageVersionUsingInstallation(packageName, version) {
864
+ assertValidPackageName(packageName);
648
865
  const { dir, cleanup } = createTempNpmDirectory();
649
866
  try {
650
867
  let resolvedVersion = version;
@@ -656,12 +873,21 @@ async function resolvePackageVersionUsingInstallation(packageName, version) {
656
873
  }
657
874
  }
658
875
  const pmc = getPackageManagerCommand();
659
- await execAsync(`${pmc.add} ${packageName}@${resolvedVersion}`, {
876
+ const addCommand = [
877
+ pmc.add,
878
+ (0, shell_quoting_1.quoteShellArg)(`${packageName}@${resolvedVersion}`),
879
+ pmc.ignoreScriptsFlag,
880
+ ]
881
+ .filter(Boolean)
882
+ .join(' ');
883
+ await execAsync(addCommand, {
660
884
  cwd: dir,
661
885
  windowsHide: true,
886
+ // Yarn Berry has no flag to skip lifecycle scripts.
887
+ env: { ...process.env, YARN_ENABLE_SCRIPTS: 'false' },
662
888
  });
663
889
  const { packageJson } = (0, package_json_1.readModulePackageJson)(packageName, [dir]);
664
- return packageJson.version;
890
+ return assertResolvedVersion(packageName, packageJson.version);
665
891
  }
666
892
  finally {
667
893
  await cleanup();
@@ -729,11 +955,16 @@ function getWorkspaceRegistryUrlForDisplay(pkg) {
729
955
  if (!URL.canParse(value)) {
730
956
  return null;
731
957
  }
732
- const protocol = new URL(value).protocol;
733
- if (protocol !== 'http:' && protocol !== 'https:') {
958
+ const url = new URL(value);
959
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
734
960
  return null;
735
961
  }
736
- return (0, redact_url_credentials_1.redactUrlCredentials)(value);
962
+ if (!url.username && !url.password) {
963
+ return value;
964
+ }
965
+ url.username = '***';
966
+ url.password = '';
967
+ return url.href;
737
968
  }
738
969
  return null;
739
970
  }
@@ -777,6 +1008,7 @@ async function packageRegistryView(pkg, version, args,
777
1008
  // a field across every matched version, whereas `pnpm view <pkg>@<range>`
778
1009
  // collapses to the single highest match (breaks per-version field queries).
779
1010
  options) {
1011
+ assertValidPackageName(pkg);
780
1012
  const { workspacePm, workspacePmVersion, configRoot, buildEnv } = createRegistrySpawnContext(pkg);
781
1013
  const { pm, env } = resolveRegistrySpawnTarget(workspacePm, workspacePmVersion, buildEnv, options);
782
1014
  // An empty version means we want the full packument; omit the trailing `@`.
@@ -812,6 +1044,7 @@ function parseRegistryViewJson(raw) {
812
1044
  * @see https://github.com/nrwl/nx/pull/9667#discussion_r842553994
813
1045
  */
814
1046
  async function packageRegistryPack(packDestination, pkg, version, options) {
1047
+ assertValidPackageName(pkg);
815
1048
  const pm = 'npm';
816
1049
  const { configRoot, buildEnv } = createRegistrySpawnContext(pkg);
817
1050
  // Run from the config root, not the temp dir, so npm reads the workspace
@@ -825,6 +1058,8 @@ async function packageRegistryPack(packDestination, pkg, version, options) {
825
1058
  // downgrade npm's devEngines.packageManager enforcement (onFail:
826
1059
  // error) to a warning so pack still runs in a non-npm workspace
827
1060
  npm_config_force: 'true',
1061
+ // callers only read files out of the tarball
1062
+ npm_config_ignore_scripts: 'true',
828
1063
  ...(options?.bypassMinReleaseAge
829
1064
  ? { npm_config_min_release_age: '0' }
830
1065
  : {}),
@@ -12,6 +12,10 @@ export declare function joinPathFragments(...fragments: string[]): string;
12
12
  * True when `relativePath` stays inside the directory it is resolved against.
13
13
  * Absolute paths are rejected because `path.join` silently treats them as
14
14
  * relative, so they would otherwise slip past a `..` check.
15
+ *
16
+ * Callers resolve with both `path.join` and `joinPathFragments`, and only the
17
+ * latter treats `\` as a separator on every platform, so the path must be
18
+ * contained under both readings.
15
19
  */
16
20
  export declare function isContainedRelativePath(relativePath: string): boolean;
17
21
  /**
@@ -28,8 +28,16 @@ function joinPathFragments(...fragments) {
28
28
  * True when `relativePath` stays inside the directory it is resolved against.
29
29
  * Absolute paths are rejected because `path.join` silently treats them as
30
30
  * relative, so they would otherwise slip past a `..` check.
31
+ *
32
+ * Callers resolve with both `path.join` and `joinPathFragments`, and only the
33
+ * latter treats `\` as a separator on every platform, so the path must be
34
+ * contained under both readings.
31
35
  */
32
36
  function isContainedRelativePath(relativePath) {
37
+ return (isContainedNativePath(relativePath) &&
38
+ isContainedNativePath(normalizePath(relativePath)));
39
+ }
40
+ function isContainedNativePath(relativePath) {
33
41
  if (path.isAbsolute(relativePath)) {
34
42
  return false;
35
43
  }
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.redactUrlCredentials = redactUrlCredentials;
4
- // Masks the userinfo in a URL: `user`, `user:pass`, or a bare token.
4
+ // Masks the userinfo in a URL: `user`, `user:pass`, or a bare token. Like a URL
5
+ // parser, it runs to the last `@` of the authority.
5
6
  function redactUrlCredentials(text) {
6
- return text.replace(/([a-z][a-z0-9+.-]*:\/\/)[^/@\s]+@/gi, '$1***@');
7
+ return text.replace(/([a-z][a-z0-9+.-]*:\/\/)[^/?#\s]+@/gi, '$1***@');
7
8
  }
@@ -23,4 +23,6 @@ export declare function waitForSocketConnection(socketPath: string | (() => stri
23
23
  * refusal may no longer be able to resolve one at all.
24
24
  */
25
25
  onConnectError?: (error: NodeJS.ErrnoException, socketPath: string) => boolean | void;
26
+ /** How to dial. Defaults to `net.connect`. */
27
+ connect?: (socketPath: string, onConnect: () => void) => Socket;
26
28
  }): Promise<Socket | null>;