nx 24.0.3 → 24.0.4

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 (55) hide show
  1. package/dist/release/changelog-renderer/index.js +12 -5
  2. package/dist/src/ai/clone-ai-config-repo.d.ts +2 -1
  3. package/dist/src/ai/clone-ai-config-repo.js +48 -11
  4. package/dist/src/ai/set-up-ai-agents/set-up-ai-agents.js +25 -7
  5. package/dist/src/analytics/analytics.js +7 -2
  6. package/dist/src/command-line/ai/ai-output.js +3 -3
  7. package/dist/src/command-line/configure-ai-agents/configure-ai-agents.js +2 -2
  8. package/dist/src/command-line/graph/graph.d.ts +10 -0
  9. package/dist/src/command-line/graph/graph.js +61 -2
  10. package/dist/src/command-line/import/import.js +18 -30
  11. package/dist/src/command-line/import/utils/merge-remote-source.d.ts +1 -0
  12. package/dist/src/command-line/import/utils/merge-remote-source.js +10 -1
  13. package/dist/src/command-line/init/implementation/angular/legacy-angular-versions.js +17 -5
  14. package/dist/src/command-line/init/init-v1.js +2 -2
  15. package/dist/src/command-line/init/init-v2.js +4 -4
  16. package/dist/src/command-line/migrate/migrate.js +6 -7
  17. package/dist/src/command-line/nx-cloud/record/record.js +3 -2
  18. package/dist/src/command-line/nx-commands.js +3 -1
  19. package/dist/src/command-line/release/changelog/version-plan-filtering.js +4 -1
  20. package/dist/src/command-line/release/changelog.js +2 -1
  21. package/dist/src/command-line/release/config/version-plans.js +29 -29
  22. package/dist/src/command-line/release/utils/git.d.ts +2 -0
  23. package/dist/src/command-line/release/utils/git.js +49 -26
  24. package/dist/src/command-line/release/utils/markdown.js +4 -1
  25. package/dist/src/command-line/release/utils/remote-release-clients/github.js +18 -7
  26. package/dist/src/command-line/release/utils/remote-release-clients/gitlab.js +3 -1
  27. package/dist/src/command-line/release/version/derive-specifier-from-conventional-commits.js +1 -1
  28. package/dist/src/command-line/release/version.js +2 -1
  29. package/dist/src/core/graph/styles.js +1 -1
  30. package/dist/src/daemon/server/latest-nx.js +2 -2
  31. package/dist/src/hasher/native-task-hasher-impl.js +5 -2
  32. package/dist/src/native/index.d.ts +11 -1
  33. package/dist/src/native/nx.wasm32-wasi.debug.wasm +0 -0
  34. package/dist/src/native/nx.wasm32-wasi.wasm +0 -0
  35. package/dist/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.js +5 -6
  36. package/dist/src/nx-cloud/utilities/get-cloud-options.d.ts +1 -1
  37. package/dist/src/nx-cloud/utilities/get-cloud-options.js +5 -2
  38. package/dist/src/nx-cloud/utilities/is-workspace-claimed.js +1 -1
  39. package/dist/src/nx-cloud/utilities/url-shorten.d.ts +1 -1
  40. package/dist/src/nx-cloud/utilities/url-shorten.js +4 -4
  41. package/dist/src/plugins/js/lock-file/yarn-parser.js +49 -2
  42. package/dist/src/tasks-runner/cache.js +24 -8
  43. package/dist/src/tasks-runner/default-tasks-runner.js +21 -13
  44. package/dist/src/tasks-runner/life-cycles/performance-report.js +2 -2
  45. package/dist/src/utils/git-utils.js +3 -3
  46. package/dist/src/utils/git-utils.tree-filter.js +102 -24
  47. package/dist/src/utils/package-manager.d.ts +7 -2
  48. package/dist/src/utils/package-manager.js +22 -22
  49. package/dist/src/utils/provenance.d.ts +14 -1
  50. package/dist/src/utils/provenance.js +44 -13
  51. package/dist/src/utils/redact-url-credentials.d.ts +1 -0
  52. package/dist/src/utils/redact-url-credentials.js +7 -0
  53. package/dist/src/utils/workspace-id.d.ts +4 -3
  54. package/dist/src/utils/workspace-id.js +4 -3
  55. package/package.json +11 -11
@@ -45,8 +45,7 @@ function getNxInitDate() {
45
45
  return new Date().toISOString();
46
46
  }
47
47
  }
48
- async function createNxCloudWorkspaceV1(workspaceName, installationSource, nxInitDate) {
49
- const apiUrl = (0, get_cloud_options_1.getCloudUrl)();
48
+ async function createNxCloudWorkspaceV1(apiUrl, workspaceName, installationSource, nxInitDate) {
50
49
  const response = await (0, http_client_1.httpRequest)(`${apiUrl}/nx-cloud/create-org-and-workspace`, {
51
50
  method: 'POST',
52
51
  data: {
@@ -60,8 +59,7 @@ async function createNxCloudWorkspaceV1(workspaceName, installationSource, nxIni
60
59
  }
61
60
  return response.data;
62
61
  }
63
- async function createNxCloudWorkspaceV2(workspaceName, installationSource, nxInitDate) {
64
- const apiUrl = (0, get_cloud_options_1.getCloudUrl)();
62
+ async function createNxCloudWorkspaceV2(apiUrl, workspaceName, installationSource, nxInitDate) {
65
63
  const response = await (0, http_client_1.httpRequest)(`${apiUrl}/nx-cloud/v2/create-org-and-workspace`, {
66
64
  method: 'POST',
67
65
  data: {
@@ -131,12 +129,13 @@ async function connectToNxCloud(tree, schema, nxJson = (0, nx_json_1.readNxJson)
131
129
  (schema.installationSource === 'nx-connect' ||
132
130
  schema.installationSource === 'nx-console'))
133
131
  return null;
132
+ const apiUrl = (0, get_cloud_options_1.getCloudUrl)((0, path_1.join)(tree.root, schema.directory ?? ''));
134
133
  try {
135
- responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(getRootPackageName(tree, schema.directory), schema.installationSource, getNxInitDate());
134
+ responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(apiUrl, getRootPackageName(tree, schema.directory), schema.installationSource, getNxInitDate());
136
135
  }
137
136
  catch (e) {
138
137
  if (e.response?.status === 404) {
139
- responseFromCreateNxCloudWorkspaceV1 = await createNxCloudWorkspaceV1(getRootPackageName(tree, schema.directory), schema.installationSource, getNxInitDate());
138
+ responseFromCreateNxCloudWorkspaceV1 = await createNxCloudWorkspaceV1(apiUrl, getRootPackageName(tree, schema.directory), schema.installationSource, getNxInitDate());
140
139
  }
141
140
  else {
142
141
  throw e;
@@ -1,5 +1,5 @@
1
1
  import { CloudTaskRunnerOptions } from '../nx-cloud-tasks-runner-shell';
2
2
  export declare function getCloudOptions(directory?: string): CloudTaskRunnerOptions;
3
- export declare function getCloudUrl(): string;
3
+ export declare function getCloudUrl(directory?: string): string;
4
4
  export declare function removeTrailingSlash(apiUrl: string): string;
5
5
  export declare function isNxCloudId(token: string): boolean;
@@ -15,8 +15,11 @@ function getCloudOptions(directory = workspace_root_1.workspaceRoot) {
15
15
  // TODO: The default is not always cloud? But it's not handled at the moment
16
16
  return getRunnerOptions('default', nxJson, {}, true);
17
17
  }
18
- function getCloudUrl() {
19
- return removeTrailingSlash(process.env.NX_CLOUD_API || process.env.NRWL_API || `https://cloud.nx.app`);
18
+ function getCloudUrl(directory = workspace_root_1.workspaceRoot) {
19
+ return removeTrailingSlash(process.env.NX_CLOUD_API ||
20
+ process.env.NRWL_API ||
21
+ getCloudOptions(directory).url ||
22
+ `https://cloud.nx.app`);
20
23
  }
21
24
  function removeTrailingSlash(apiUrl) {
22
25
  return apiUrl[apiUrl.length - 1] === '/' ? apiUrl.slice(0, -1) : apiUrl;
@@ -6,8 +6,8 @@ const http_client_1 = require("../../utils/http-client");
6
6
  async function isWorkspaceClaimed(accessToken) {
7
7
  if (!accessToken)
8
8
  return false;
9
- const apiUrl = (0, get_cloud_options_1.getCloudUrl)();
10
9
  try {
10
+ const apiUrl = (0, get_cloud_options_1.getCloudUrl)();
11
11
  const requestData = (0, get_cloud_options_1.isNxCloudId)(accessToken)
12
12
  ? { nxCloudId: accessToken }
13
13
  : { nxCloudAccessToken: accessToken };
@@ -1,2 +1,2 @@
1
1
  export declare function createNxCloudOnboardingURL(onboardingSource: string, accessToken?: string, meta?: string, forceManual?: boolean, forceGithub?: boolean, directory?: string, signal?: AbortSignal): Promise<string>;
2
- export declare function getURLifShortenFailed(usesGithub: boolean, githubSlug: string | null, apiUrl: string, source: string, accessToken?: string): string;
2
+ export declare function getURLifShortenFailed(usesGithub: boolean, githubSlug: string | null, apiUrl: string, source: string): string;
@@ -12,7 +12,7 @@ async function createNxCloudOnboardingURL(onboardingSource, accessToken, meta, f
12
12
  // the event loop open, since these requests have no timeout.
13
13
  signal) {
14
14
  const remoteInfo = (0, git_utils_1.getVcsRemoteInfo)(directory);
15
- const apiUrl = (0, get_cloud_options_1.getCloudUrl)();
15
+ const apiUrl = (0, get_cloud_options_1.getCloudUrl)(directory);
16
16
  const installationSupportsGitHub = await getInstallationSupportsGitHub(apiUrl, signal);
17
17
  let usesGithub = false;
18
18
  if (forceGithub) {
@@ -47,7 +47,7 @@ signal) {
47
47
  catch (e) {
48
48
  logger_1.logger.verbose(`Failed to shorten Nx Cloud URL.
49
49
  ${e}`);
50
- return getURLifShortenFailed(usesGithub, usesGithub ? remoteInfo?.slug : null, apiUrl, source, accessToken);
50
+ return getURLifShortenFailed(usesGithub, usesGithub ? remoteInfo?.slug : null, apiUrl, source);
51
51
  }
52
52
  }
53
53
  function getSource(installationSource) {
@@ -61,7 +61,7 @@ function getSource(installationSource) {
61
61
  return installationSource;
62
62
  }
63
63
  }
64
- function getURLifShortenFailed(usesGithub, githubSlug, apiUrl, source, accessToken) {
64
+ function getURLifShortenFailed(usesGithub, githubSlug, apiUrl, source) {
65
65
  if (usesGithub) {
66
66
  if (githubSlug) {
67
67
  return `${apiUrl}/setup/connect-workspace/github/connect?name=${encodeURIComponent(githubSlug)}&source=${source}`;
@@ -70,7 +70,7 @@ function getURLifShortenFailed(usesGithub, githubSlug, apiUrl, source, accessTok
70
70
  return `${apiUrl}/setup/connect-workspace/github/select?source=${source}`;
71
71
  }
72
72
  }
73
- return `${apiUrl}/setup/connect-workspace/manual?accessToken=${accessToken}&source=${source}`;
73
+ return `${apiUrl}/setup/connect-workspace/manual?source=${source}`;
74
74
  }
75
75
  async function getInstallationSupportsGitHub(apiUrl, signal) {
76
76
  try {
@@ -58,9 +58,23 @@ function getNodes(dependencies, packageJson, isBerry) {
58
58
  ...packageJson.peerDependencies,
59
59
  ...packageJson.optionalDependencies,
60
60
  };
61
+ // @patch: descriptors are not their own nodes. Their checksum and resolution
62
+ // are folded into the matching package node so a patch changes its hash.
63
+ const patchesByPackage = new Map();
61
64
  Object.entries(dependencies).forEach(([keys, snapshot]) => {
62
- // ignore workspace projects & patches
63
- if (snapshot.linkType === 'soft' || keys.includes('@patch:')) {
65
+ if (snapshot.linkType === 'soft') {
66
+ return;
67
+ }
68
+ if (keys.includes('@patch:')) {
69
+ for (const packageName of getPackageNameKeyPairs(keys).keys()) {
70
+ const patches = patchesByPackage.get(packageName);
71
+ if (patches) {
72
+ patches.push(snapshot);
73
+ }
74
+ else {
75
+ patchesByPackage.set(packageName, [snapshot]);
76
+ }
77
+ }
64
78
  return;
65
79
  }
66
80
  const nameKeyPairs = getPackageNameKeyPairs(keys);
@@ -103,6 +117,7 @@ function getNodes(dependencies, packageJson, isBerry) {
103
117
  });
104
118
  });
105
119
  });
120
+ foldPatchIntoExternalNodeHashes(nodes, patchesByPackage);
106
121
  const externalNodes = {};
107
122
  for (const [packageName, versionMap] of nodes.entries()) {
108
123
  // If there's only one version of a package, treat it as hoisted
@@ -120,6 +135,38 @@ function getNodes(dependencies, packageJson, isBerry) {
120
135
  }
121
136
  return { nodes: externalNodes, keyMap };
122
137
  }
138
+ function foldPatchIntoExternalNodeHashes(nodes, patchesByPackage) {
139
+ for (const [packageName, patches] of patchesByPackage) {
140
+ const versionMap = nodes.get(packageName);
141
+ if (!versionMap) {
142
+ continue;
143
+ }
144
+ for (const [versionKey, node] of versionMap) {
145
+ const seen = new Set();
146
+ const parts = [];
147
+ const matching = patches
148
+ .filter((patch) => !!patch.version &&
149
+ (patch.version === versionKey ||
150
+ patch.version === node.data.version))
151
+ .sort((a, b) => patchSortKey(a).localeCompare(patchSortKey(b)));
152
+ for (const patch of matching) {
153
+ const patchParts = [patch.checksum, patch.resolution].filter((part) => !!part);
154
+ const id = patchParts.join('\0');
155
+ if (!id || seen.has(id)) {
156
+ continue;
157
+ }
158
+ seen.add(id);
159
+ parts.push(...patchParts);
160
+ }
161
+ if (parts.length > 0) {
162
+ node.data.hash = (0, file_hasher_1.hashArray)([node.data.hash, ...parts]);
163
+ }
164
+ }
165
+ }
166
+ }
167
+ function patchSortKey(snapshot) {
168
+ return `${snapshot.resolution ?? ''}\0${snapshot.checksum ?? ''}`;
169
+ }
123
170
  function findHoistedNode(packageName, versionMap, combinedDeps) {
124
171
  const hoistedVersion = getHoistedVersion(packageName);
125
172
  if (hoistedVersion) {
@@ -107,8 +107,7 @@ class DbCache {
107
107
  // didn't find it locally but we have a remote cache
108
108
  // attempt remote cache
109
109
  const res = await this.remoteCache.retrieve(task.hash, this.cache.cacheDirectory);
110
- if (res) {
111
- this.applyRemoteCacheResults(task.hash, res, task.outputs);
110
+ if (res && this.applyRemoteCacheResults(task, res)) {
112
111
  return {
113
112
  ...res,
114
113
  terminalOutput: res.terminalOutput ?? '',
@@ -159,8 +158,7 @@ class DbCache {
159
158
  if (remoteMisses.length > 0) {
160
159
  await Promise.all(remoteMisses.map(async (task) => {
161
160
  const res = await this.remoteCache.retrieve(task.hash, this.cache.cacheDirectory);
162
- if (res) {
163
- this.applyRemoteCacheResults(task.hash, res, task.outputs);
161
+ if (res && this.applyRemoteCacheResults(task, res)) {
164
162
  results.set(task.hash, {
165
163
  ...res,
166
164
  terminalOutput: res.terminalOutput ?? '',
@@ -174,15 +172,33 @@ class DbCache {
174
172
  getUsedCacheSpace() {
175
173
  return this.cache.getCacheSize();
176
174
  }
177
- applyRemoteCacheResults(hash, res, outputs) {
178
- return this.cache.applyRemoteCacheResults(hash, res, outputs);
175
+ applyRemoteCacheResults(task, res) {
176
+ const applied = this.cache.applyRemoteCacheResults(task.hash, res, task.outputs);
177
+ if (!applied) {
178
+ output_1.output.warn({
179
+ title: `Ignoring the remote cache result for ${task.id}`,
180
+ bodyLines: [
181
+ 'Its outputs contain a symlink that points outside the output it is in.',
182
+ ],
183
+ });
184
+ }
185
+ return applied;
179
186
  }
180
187
  async put(task, terminalOutput, outputs, code) {
181
188
  return tryAndRetry(async () => {
182
- const { expandedOutputs, files } = this.cache.put(task.hash, terminalOutput, outputs, code);
189
+ const { expandedOutputs, files, outputsBelowALink } = this.cache.put(task.hash, terminalOutput, outputs, code);
183
190
  // Notify TaskIOService of actual output files
184
191
  (0, task_io_service_1.getTaskIOService)().notifyTaskOutputs(task.id, expandedOutputs);
185
- if (this.remoteCache) {
192
+ if (outputsBelowALink.length > 0) {
193
+ output_1.output.warn({
194
+ title: `Not caching ${task.id}`,
195
+ bodyLines: [
196
+ 'These outputs are inside a symlinked directory, so they cannot be cached:',
197
+ ...outputsBelowALink.map((o) => `- ${o}`),
198
+ ],
199
+ });
200
+ }
201
+ else if (this.remoteCache) {
186
202
  await this.remoteCache.store(task.hash, this.cache.cacheDirectory, terminalOutput, code);
187
203
  }
188
204
  return files;
@@ -6,27 +6,35 @@ const performance_life_cycle_1 = require("./life-cycles/performance-life-cycle")
6
6
  const cache_directory_1 = require("../utils/cache-directory");
7
7
  const promises_1 = require("fs/promises");
8
8
  const path_1 = require("path");
9
+ async function readRegularFile(path) {
10
+ const stats = await (0, promises_1.lstat)(path).catch(() => null);
11
+ return stats?.isFile() ? (0, promises_1.readFile)(path, 'utf-8').catch(() => null) : null;
12
+ }
9
13
  class RemoteCacheV2 {
10
14
  static async fromCacheV1(cache) {
11
15
  await (0, promises_1.mkdir)((0, path_1.join)(cache_directory_1.cacheDir, 'terminalOutputs'), { recursive: true });
12
16
  return {
13
17
  retrieve: async (hash, cacheDirectory) => {
14
- const res = await cache.retrieve(hash, cacheDirectory);
15
- if (res) {
16
- const [terminalOutput, oldTerminalOutput, code] = await Promise.all([
17
- (0, promises_1.readFile)((0, path_1.join)(cacheDirectory, hash, 'terminalOutputs'), 'utf-8').catch(() => null),
18
- (0, promises_1.readFile)((0, path_1.join)(cache_directory_1.cacheDir, 'terminalOutputs', hash), 'utf-8').catch(() => null),
19
- (0, promises_1.readFile)((0, path_1.join)(cacheDirectory, hash, 'code'), 'utf-8').then((s) => +s),
20
- ]);
21
- return {
22
- outputsPath: (0, path_1.join)(cacheDirectory, hash, 'outputs'),
23
- terminalOutput: terminalOutput ?? oldTerminalOutput,
24
- code,
25
- };
18
+ if (!(await cache.retrieve(hash, cacheDirectory))) {
19
+ return null;
26
20
  }
27
- else {
21
+ const entry = (0, path_1.join)(cacheDirectory, hash);
22
+ const [outputs, terminalOutputs, code] = await Promise.all(['outputs', 'terminalOutputs', 'code'].map((name) => (0, promises_1.lstat)((0, path_1.join)(entry, name)).catch(() => null)));
23
+ if ((outputs && !outputs.isDirectory()) ||
24
+ (terminalOutputs && !terminalOutputs.isFile()) ||
25
+ !code?.isFile()) {
28
26
  return null;
29
27
  }
28
+ const [terminalOutput, oldTerminalOutput, exitCode] = await Promise.all([
29
+ readRegularFile((0, path_1.join)(entry, 'terminalOutputs')),
30
+ readRegularFile((0, path_1.join)(cache_directory_1.cacheDir, 'terminalOutputs', hash)),
31
+ (0, promises_1.readFile)((0, path_1.join)(entry, 'code'), 'utf-8'),
32
+ ]);
33
+ return {
34
+ outputsPath: (0, path_1.join)(entry, 'outputs'),
35
+ terminalOutput: terminalOutput ?? oldTerminalOutput,
36
+ code: +exitCode,
37
+ };
30
38
  },
31
39
  store: async (hash, cacheDirectory, terminalOutput, code) => {
32
40
  // The new db cache places the outputs directly into the cacheDirectory + hash.
@@ -62,8 +62,8 @@ async function prefetchRemoteCacheOnboardingUrl(nxJson) {
62
62
  }, ONBOARDING_URL_TIMEOUT).unref();
63
63
  }),
64
64
  ]);
65
- // createNxCloudOnboardingURL never throws - on an unreachable API it returns a
66
- // paste-a-token URL (accessToken=undefined). Only accept a real short link.
65
+ // On an unreachable API createNxCloudOnboardingURL returns a paste-a-token
66
+ // URL. Only accept a real short link.
67
67
  if (url?.startsWith(`${(0, get_cloud_options_1.getCloudUrl)()}/connect/`)) {
68
68
  remoteCacheLink = url;
69
69
  }
@@ -114,7 +114,7 @@ class GitRepository {
114
114
  ]);
115
115
  }
116
116
  async fetch(remote, ref) {
117
- return await this.execGit(['fetch', remote, ...(ref ? [ref] : [])]);
117
+ return await this.execGit(['fetch', '--', remote, ...(ref ? [ref] : [])]);
118
118
  }
119
119
  async checkout(branch, opts) {
120
120
  return await this.execGit([
@@ -128,7 +128,7 @@ class GitRepository {
128
128
  return await this.execGit(['mv', '--', path, destination]);
129
129
  }
130
130
  async push(ref, remoteName) {
131
- return await this.execGit(['push', '-u', '-f', remoteName, ref]);
131
+ return await this.execGit(['push', '-u', '-f', '--', remoteName, ref]);
132
132
  }
133
133
  async commit(message) {
134
134
  return await this.execGit(['commit', '-am', message]);
@@ -140,7 +140,7 @@ class GitRepository {
140
140
  return await this.execGit(['remote', 'rm', name]);
141
141
  }
142
142
  async addGitRemote(name, url) {
143
- return await this.execGit(['remote', 'add', name, url]);
143
+ return await this.execGit(['remote', 'add', '--no-tags', name, url]);
144
144
  }
145
145
  async hasFilterRepoInstalled() {
146
146
  try {
@@ -8,36 +8,114 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
8
  * Example:
9
9
  * NX_IMPORT_SOURCE=<source> NX_IMPORT_DESTINATION=<destination> git filter-branch --tree-filter 'node git-utils.tree-filter.js' --prune-empty -- --all
10
10
  */
11
- const { execSync } = require('child_process');
12
- const { existsSync, mkdirSync, renameSync, rmSync } = require('fs');
13
- // NOTE: The path passed to `git filter-branch` is POSIX, so we need to use the `posix` module.
14
- const { posix } = require('path');
11
+ const { execFileSync } = require('child_process');
12
+ const { lstatSync, mkdirSync, mkdtempSync, readdirSync, renameSync, rmdirSync, rmSync, } = require('fs');
13
+ // Paths are raw bytes so names that are not valid UTF-8 move unchanged.
14
+ const SLASH = 0x2f;
15
+ function split(path, separator) {
16
+ const parts = [];
17
+ let start = 0;
18
+ for (let i = 0; i <= path.length; i++) {
19
+ if (i === path.length || path[i] === separator) {
20
+ if (i > start)
21
+ parts.push(path.subarray(start, i));
22
+ start = i + 1;
23
+ }
24
+ }
25
+ return parts;
26
+ }
27
+ function joinPath(parts) {
28
+ const joined = [];
29
+ for (const part of parts) {
30
+ if (joined.length)
31
+ joined.push(Buffer.from('/'));
32
+ joined.push(part);
33
+ }
34
+ return Buffer.concat(joined);
35
+ }
36
+ // Creates the missing directories of `parts` and refuses to pass through
37
+ // anything that is not a real directory.
38
+ function ensureRealDir(parts) {
39
+ for (let i = 1; i <= parts.length; i++) {
40
+ const current = joinPath(parts.slice(0, i));
41
+ let stats;
42
+ try {
43
+ stats = lstatSync(current);
44
+ }
45
+ catch (error) {
46
+ if (error.code !== 'ENOENT')
47
+ throw error;
48
+ mkdirSync(current);
49
+ continue;
50
+ }
51
+ if (!stats.isDirectory()) {
52
+ throw new Error(`${current.toString()} is not a directory`);
53
+ }
54
+ }
55
+ }
56
+ function removeEmptyDirs(path) {
57
+ if (!lstatSync(path).isDirectory())
58
+ return;
59
+ for (const entry of readdirSync(path, { encoding: 'buffer' })) {
60
+ removeEmptyDirs(joinPath([path, entry]));
61
+ }
62
+ try {
63
+ rmdirSync(path);
64
+ }
65
+ catch (error) {
66
+ if (error.code !== 'ENOTEMPTY' && error.code !== 'EEXIST')
67
+ throw error;
68
+ }
69
+ }
15
70
  try {
16
71
  // NOTE: Using env vars because Windows PowerShell has its own handling of quotes (") messes up quotes in args, even if escaped.
72
+ // NOTE: The paths passed to `git filter-branch` are POSIX.
17
73
  const src = process.env.NX_IMPORT_SOURCE;
18
74
  const dest = process.env.NX_IMPORT_DESTINATION;
19
- const files = execSync(`git ls-files -z ${src}`, {
20
- windowsHide: true,
21
- })
22
- .toString()
23
- .trim()
24
- .split('\x00')
25
- .map((s) => s.trim())
26
- .filter(Boolean);
27
- for (const file of files) {
28
- if (src === '' || file.startsWith(src)) {
29
- // If source and destination are the same, then keep the file as is.
30
- if (src === dest)
31
- continue;
32
- const destFile = posix.join(dest, file.replace(src, ''));
33
- const dir = posix.dirname(destFile);
34
- if (!existsSync(dir))
35
- mkdirSync(dir, { recursive: true });
36
- renameSync(file, destFile);
75
+ if (src !== dest) {
76
+ const srcParts = split(Buffer.from(src), SLASH);
77
+ const files = split(execFileSync('git', ['ls-files', '-z', '--', ...(src ? [src] : [])], {
78
+ windowsHide: true,
79
+ maxBuffer: 1024 * 1024 * 1024,
80
+ }), 0);
81
+ // Move everything into a fresh directory first, so the destination is
82
+ // created in a tree that no longer holds any imported entries.
83
+ const staging = Buffer.from(mkdtempSync('.nx-import-'));
84
+ const leftovers = [];
85
+ for (const file of files) {
86
+ const parts = split(file, SLASH);
87
+ const inSource = srcParts.every((part, i) => parts[i]?.equals(part));
88
+ ensureRealDir(parts.slice(0, -1));
89
+ if (inSource && parts.length === srcParts.length) {
90
+ // The source path is itself a file, so it is the only entry and
91
+ // takes the staging directory's place.
92
+ rmdirSync(staging);
93
+ renameSync(file, staging);
94
+ }
95
+ else if (inSource) {
96
+ const target = [staging, ...parts.slice(srcParts.length)];
97
+ ensureRealDir(target.slice(0, -1));
98
+ renameSync(file, joinPath(target));
99
+ }
100
+ else {
101
+ rmSync(file);
102
+ }
103
+ if (parts.length > 1)
104
+ leftovers.push(parts[0].toString('hex'));
105
+ }
106
+ for (const leftover of new Set(leftovers)) {
107
+ removeEmptyDirs(Buffer.from(leftover, 'hex'));
108
+ }
109
+ const destParts = split(Buffer.from(dest), SLASH);
110
+ if (destParts.length) {
111
+ ensureRealDir(destParts.slice(0, -1));
112
+ renameSync(staging, joinPath(destParts));
37
113
  }
38
114
  else {
39
- // If not matching the source we are filtering, remove it.
40
- rmSync(file);
115
+ for (const entry of readdirSync(staging, { encoding: 'buffer' })) {
116
+ renameSync(joinPath([staging, entry]), entry);
117
+ }
118
+ rmdirSync(staging);
41
119
  }
42
120
  }
43
121
  }
@@ -17,8 +17,8 @@ export interface PackageManagerCommands {
17
17
  * @deprecated Use `publishArgv` instead, which needs no shell off Windows.
18
18
  * This will be removed in Nx 25.
19
19
  */
20
- publish: (packageRoot: string, registry: string, registryConfigKey: string, tag: string) => string;
21
- publishArgv: (packageRoot: string, registry: string, registryConfigKey: string, tag: string) => {
20
+ publish: (packageRoot: string, registry: string | undefined, registryConfigKey: string, tag: string) => string;
21
+ publishArgv: (packageRoot: string, registry: string | undefined, registryConfigKey: string, tag: string) => {
22
22
  command: string;
23
23
  args: string[];
24
24
  };
@@ -101,6 +101,11 @@ export declare function modifyYarnRcToFitNewDirectory(contents: string): string;
101
101
  */
102
102
  export declare function modifyPnpmWorkspaceYamlToFitNewDirectory(contents: string): string;
103
103
  export declare function copyPackageManagerConfigurationFiles(root: string, destination: string): void;
104
+ /**
105
+ * A non-JS workspace has no root package.json and keeps its package manager
106
+ * files under the Nx installation directory instead.
107
+ */
108
+ export declare function getPackageManagerConfigRoot(root?: string): string;
104
109
  /**
105
110
  * Creates a temporary directory where you can run package manager commands safely.
106
111
  *
@@ -10,6 +10,7 @@ exports.modifyYarnRcYmlToFitNewDirectory = modifyYarnRcYmlToFitNewDirectory;
10
10
  exports.modifyYarnRcToFitNewDirectory = modifyYarnRcToFitNewDirectory;
11
11
  exports.modifyPnpmWorkspaceYamlToFitNewDirectory = modifyPnpmWorkspaceYamlToFitNewDirectory;
12
12
  exports.copyPackageManagerConfigurationFiles = copyPackageManagerConfigurationFiles;
13
+ exports.getPackageManagerConfigRoot = getPackageManagerConfigRoot;
13
14
  exports.createTempNpmDirectory = createTempNpmDirectory;
14
15
  exports.resolvePackageVersionUsingRegistry = resolvePackageVersionUsingRegistry;
15
16
  exports.resolvePackageVersionUsingInstallation = resolvePackageVersionUsingInstallation;
@@ -36,6 +37,7 @@ const fileutils_1 = require("./fileutils");
36
37
  const installation_directory_1 = require("./installation-directory");
37
38
  const logger_1 = require("./logger");
38
39
  const package_json_1 = require("./package-json");
40
+ const redact_url_credentials_1 = require("./redact-url-credentials");
39
41
  const shell_quoting_1 = require("./shell-quoting");
40
42
  const workspace_root_1 = require("./workspace-root");
41
43
  const execAsync = (0, util_1.promisify)(child_process_1.exec);
@@ -194,7 +196,7 @@ function getPackageManagerCommand(packageManager = detectPackageManager(), root
194
196
  'publish',
195
197
  packageRoot,
196
198
  '--json',
197
- `--${registryConfigKey}=${registry}`,
199
+ ...(registry ? [`--${registryConfigKey}=${registry}`] : []),
198
200
  `--tag=${tag}`,
199
201
  ],
200
202
  });
@@ -249,11 +251,15 @@ function getPackageManagerCommand(packageManager = detectPackageManager(), root
249
251
  'publish',
250
252
  packageRoot,
251
253
  '--json',
252
- `--${configForm
253
- ? `config.${registryConfigKey}`
254
- : scopedForm
255
- ? registryConfigKey
256
- : 'registry'}=${registry}`,
254
+ ...(registry
255
+ ? [
256
+ `--${configForm
257
+ ? `config.${registryConfigKey}`
258
+ : scopedForm
259
+ ? registryConfigKey
260
+ : 'registry'}=${registry}`,
261
+ ]
262
+ : []),
257
263
  `--tag=${tag}`,
258
264
  '--no-git-checks',
259
265
  ],
@@ -291,7 +297,7 @@ function getPackageManagerCommand(packageManager = detectPackageManager(), root
291
297
  'publish',
292
298
  packageRoot,
293
299
  '--json',
294
- `--${registryConfigKey}=${registry}`,
300
+ ...(registry ? [`--${registryConfigKey}=${registry}`] : []),
295
301
  `--tag=${tag}`,
296
302
  ],
297
303
  });
@@ -320,7 +326,7 @@ function getPackageManagerCommand(packageManager = detectPackageManager(), root
320
326
  'publish',
321
327
  `--cwd=${packageRoot}`,
322
328
  '--json',
323
- `--registry=${registry}`,
329
+ ...(registry ? [`--registry=${registry}`] : []),
324
330
  `--tag=${tag}`,
325
331
  ],
326
332
  });
@@ -539,21 +545,19 @@ function copyPackageManagerConfigurationFiles(root, destination) {
539
545
  * A non-JS workspace has no root package.json and keeps its package manager
540
546
  * files under the Nx installation directory instead.
541
547
  */
542
- function getPackageManagerConfigRoot() {
543
- if ((0, fs_1.existsSync)((0, path_1.join)(workspace_root_1.workspaceRoot, 'package.json'))) {
544
- return workspace_root_1.workspaceRoot;
548
+ function getPackageManagerConfigRoot(root = workspace_root_1.workspaceRoot) {
549
+ if ((0, fs_1.existsSync)((0, path_1.join)(root, 'package.json'))) {
550
+ return root;
545
551
  }
546
- const installationPath = (0, installation_directory_1.getNxInstallationPath)(workspace_root_1.workspaceRoot);
552
+ const installationPath = (0, installation_directory_1.getNxInstallationPath)(root);
547
553
  // The installation directory can be missing or not a directory, and spawning
548
554
  // with such a cwd fails outright (ENOENT/ENOTDIR).
549
555
  try {
550
- return (0, fs_1.statSync)(installationPath).isDirectory()
551
- ? installationPath
552
- : workspace_root_1.workspaceRoot;
556
+ return (0, fs_1.statSync)(installationPath).isDirectory() ? installationPath : root;
553
557
  }
554
558
  catch (e) {
555
559
  logger_1.logger.verbose(`Failed to stat the Nx installation directory at "${installationPath}".`, e);
556
- return workspace_root_1.workspaceRoot;
560
+ return root;
557
561
  }
558
562
  }
559
563
  /**
@@ -623,17 +627,13 @@ async function resolvePackageVersionUsingRegistry(packageName, version) {
623
627
  });
624
628
  }
625
629
  }
626
- // Masks the userinfo in a URL: `user`, `user:pass`, or a bare token.
627
- function redactUrlCredentials(text) {
628
- return text.replace(/([a-z][a-z0-9+.-]*:\/\/)[^/@\s]+@/gi, '$1***@');
629
- }
630
630
  function redactErrorCause(error) {
631
631
  if (error && typeof error === 'object') {
632
632
  const e = error;
633
633
  // An exec error carries the command and both output streams as fields.
634
634
  for (const field of ['message', 'stack', 'stderr', 'stdout', 'cmd']) {
635
635
  if (typeof e[field] === 'string') {
636
- e[field] = redactUrlCredentials(e[field]);
636
+ e[field] = (0, redact_url_credentials_1.redactUrlCredentials)(e[field]);
637
637
  }
638
638
  }
639
639
  }
@@ -733,7 +733,7 @@ function getWorkspaceRegistryUrlForDisplay(pkg) {
733
733
  if (protocol !== 'http:' && protocol !== 'https:') {
734
734
  return null;
735
735
  }
736
- return redactUrlCredentials(value);
736
+ return (0, redact_url_credentials_1.redactUrlCredentials)(value);
737
737
  }
738
738
  return null;
739
739
  }
@@ -1,4 +1,17 @@
1
- export declare function ensurePackageHasProvenance(packageName: string, packageVersion: string): Promise<void>;
1
+ /**
2
+ * Resolves `spec` to the version the workspace's package manager would
3
+ * install under its minimum-release-age policy, verifies that version's
4
+ * provenance, and returns it. Install the returned version, not `spec`.
5
+ */
6
+ export declare function resolveVersionWithProvenance(packageName: string, spec: string, root?: string): Promise<string>;
7
+ /**
8
+ * Verifies that the given npm package has provenance attestations
9
+ * generated by the GitHub Actions workflow at .github/workflows/publish.yml
10
+ * in the nrwl/nx repository, and resolves to the exact version it verified.
11
+ *
12
+ * Will throw if the package does not have valid provenance.
13
+ */
14
+ export declare function ensurePackageHasProvenance(packageName: string, packageVersion: string): Promise<string>;
2
15
  export declare class ProvenanceError extends Error {
3
16
  constructor(packageName: string, packageVersion: string, error?: string);
4
17
  }