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
@@ -19,7 +19,6 @@ 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");
23
22
  const which_1 = tslib_1.__importDefault(require("which"));
24
23
  const json_1 = require("../generators/utils/json");
25
24
  const project_configuration_utils_1 = require("../project-graph/utils/project-configuration-utils");
@@ -195,30 +194,54 @@ function readModulePackageJsonWithoutFallbacks(moduleSpecifier, requirePaths = (
195
194
  * @returns package json contents and path
196
195
  */
197
196
  function readModulePackageJson(moduleSpecifier, requirePaths = (0, installation_directory_1.getNxRequirePaths)()) {
198
- let packageJsonPath;
199
- let packageJson;
197
+ let exported;
200
198
  try {
201
- ({ path: packageJsonPath, packageJson } =
202
- 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;
203
216
  }
204
- catch {
205
- const entryPoint = resolveWithoutCachePollution(moduleSpecifier, requirePaths);
206
- 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);
207
222
  packageJsonPath = (0, path_1.join)(moduleRootPath, 'package.json');
208
- while (!(0, fs_1.existsSync)(packageJsonPath)) {
209
- moduleRootPath = (0, path_1.dirname)(moduleRootPath);
210
- packageJsonPath = (0, path_1.join)(moduleRootPath, 'package.json');
211
- }
212
- packageJson = (0, fileutils_1.readJsonFile)(packageJsonPath);
213
- if (packageJson.name && packageJson.name !== moduleSpecifier) {
214
- throw new Error(`Found module ${packageJson.name} while trying to locate ${moduleSpecifier}/package.json`);
215
- }
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`);
216
227
  }
217
228
  return {
218
229
  packageJson,
219
230
  path: packageJsonPath,
220
231
  };
221
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
+ }
222
245
  /**
223
246
  * Resolve `request` via Node's CJS resolver while neutralising both ways
224
247
  * `require.resolve(req, { paths })` can lie about the `paths` argument:
@@ -260,10 +283,7 @@ function resolveWithoutCachePollution(request, requirePaths) {
260
283
  * This is used by both sync and async installation functions.
261
284
  */
262
285
  function preparePackageInstallation(pkg, requiredVersion, packageManager) {
263
- const { dir: tempDir, cleanup } = (0, package_manager_1.createTempNpmDirectory)?.() ?? {
264
- dir: (0, tmp_1.dirSync)().name,
265
- cleanup: () => { },
266
- };
286
+ const { dir: tempDir, cleanup } = (0, package_manager_1.createTempNpmDirectory)();
267
287
  console.log(`Fetching ${pkg}...`);
268
288
  const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
269
289
  generatePackageManagerFiles(tempDir, packageManager);
@@ -113,6 +113,21 @@ export declare function copyPackageManagerConfigurationFiles(root: string, desti
113
113
  * files under the Nx installation directory instead.
114
114
  */
115
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;
116
131
  /**
117
132
  * Creates a temporary directory where you can run package manager commands safely.
118
133
  *
@@ -13,6 +13,8 @@ exports.modifyYarnRcToFitNewDirectory = modifyYarnRcToFitNewDirectory;
13
13
  exports.modifyPnpmWorkspaceYamlToFitNewDirectory = modifyPnpmWorkspaceYamlToFitNewDirectory;
14
14
  exports.copyPackageManagerConfigurationFiles = copyPackageManagerConfigurationFiles;
15
15
  exports.getPackageManagerConfigRoot = getPackageManagerConfigRoot;
16
+ exports.createTempInstallDirectory = createTempInstallDirectory;
17
+ exports.removeStaleTempInstallDirectories = removeStaleTempInstallDirectories;
16
18
  exports.createTempNpmDirectory = createTempNpmDirectory;
17
19
  exports.resolvePackageVersionUsingRegistry = resolvePackageVersionUsingRegistry;
18
20
  exports.resolvePackageVersionUsingInstallation = resolvePackageVersionUsingInstallation;
@@ -38,6 +40,8 @@ const catalog_1 = require("./catalog");
38
40
  const fileutils_1 = require("./fileutils");
39
41
  const installation_directory_1 = require("./installation-directory");
40
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");
41
45
  const package_json_1 = require("./package-json");
42
46
  const redact_url_credentials_1 = require("./redact-url-credentials");
43
47
  const shell_quoting_1 = require("./shell-quoting");
@@ -575,6 +579,184 @@ function getPackageManagerConfigRoot(root = workspace_root_1.workspaceRoot) {
575
579
  return root;
576
580
  }
577
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
+ }
578
760
  /**
579
761
  * Creates a temporary directory where you can run package manager commands safely.
580
762
  *
@@ -587,7 +769,7 @@ function getPackageManagerConfigRoot(root = workspace_root_1.workspaceRoot) {
587
769
  * where no existing configuration files are available to copy.
588
770
  */
589
771
  function createTempNpmDirectory(skipCopy = false) {
590
- const dir = (0, tmp_1.dirSync)().name;
772
+ const dir = createTempInstallDirectory();
591
773
  // A package.json is needed for pnpm pack and for .npmrc to resolve
592
774
  (0, fileutils_1.writeJsonFile)(`${dir}/package.json`, {});
593
775
  if (!skipCopy) {
@@ -598,7 +780,7 @@ function createTempNpmDirectory(skipCopy = false) {
598
780
  await (0, promises_1.rm)(dir, { recursive: true, force: true });
599
781
  }
600
782
  catch {
601
- // It's okay if this fails, the OS will clean it up eventually
783
+ // The exit handler retries the removal.
602
784
  }
603
785
  };
604
786
  return { dir, cleanup };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nx",
3
- "version": "24.0.5",
3
+ "version": "24.0.6",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "description": "The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.",
@@ -150,16 +150,16 @@
150
150
  }
151
151
  },
152
152
  "optionalDependencies": {
153
- "@nx/nx-darwin-arm64": "24.0.5",
154
- "@nx/nx-darwin-x64": "24.0.5",
155
- "@nx/nx-freebsd-x64": "24.0.5",
156
- "@nx/nx-linux-arm-gnueabihf": "24.0.5",
157
- "@nx/nx-linux-arm64-gnu": "24.0.5",
158
- "@nx/nx-linux-arm64-musl": "24.0.5",
159
- "@nx/nx-linux-x64-gnu": "24.0.5",
160
- "@nx/nx-linux-x64-musl": "24.0.5",
161
- "@nx/nx-win32-arm64-msvc": "24.0.5",
162
- "@nx/nx-win32-x64-msvc": "24.0.5"
153
+ "@nx/nx-darwin-arm64": "24.0.6",
154
+ "@nx/nx-darwin-x64": "24.0.6",
155
+ "@nx/nx-freebsd-x64": "24.0.6",
156
+ "@nx/nx-linux-arm-gnueabihf": "24.0.6",
157
+ "@nx/nx-linux-arm64-gnu": "24.0.6",
158
+ "@nx/nx-linux-arm64-musl": "24.0.6",
159
+ "@nx/nx-linux-x64-gnu": "24.0.6",
160
+ "@nx/nx-linux-x64-musl": "24.0.6",
161
+ "@nx/nx-win32-arm64-msvc": "24.0.6",
162
+ "@nx/nx-win32-x64-msvc": "24.0.6"
163
163
  },
164
164
  "nx-migrations": {
165
165
  "migrations": "./migrations.json",