Skip to content

Commit 17b9c95

Browse files
committed
fix(core): improve error handling in nx migrate registry fetching
- Always use npm for `pack` since only npm supports downloading remote packages (pnpm/yarn/bun pack the local project) - Throw on install fallback failure instead of returning empty object which caused undefined version propagation ("Fetching nx@undefined") - Log registry fetch errors at verbose level for debuggability
1 parent 1db5b32 commit 17b9c95

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

packages/nx/src/command-line/migrate/migrate.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as pc from 'picocolors';
22
import { exec, execSync, type StdioOptions } from 'child_process';
33
import { prompt } from 'enquirer';
44
import { dirname, join } from 'path';
5+
import { joinPathFragments } from '../../utils/path';
56
import {
67
clean,
78
coerce,
@@ -978,7 +979,10 @@ function createFetcher() {
978979
setCache(packageName, resolvedVersion);
979980
return getPackageMigrationsUsingRegistry(packageName, resolvedVersion);
980981
})
981-
.catch(() => {
982+
.catch((e) => {
983+
logger.verbose(
984+
`Failed to get migrations from registry for ${packageName}@${packageVersion}: ${e.message}. Falling back to install.`
985+
);
982986
logger.info(`Fetching ${packageName}@${packageVersion}`);
983987

984988
return getPackageMigrationsUsingInstall(packageName, packageVersion);
@@ -1115,7 +1119,7 @@ async function downloadPackageMigrationsFromRegistry(
11151119

11161120
const migrations = await extractFileFromTarball(
11171121
join(dir, tarballPath),
1118-
join('package', migrationsFilePath),
1122+
joinPathFragments('package', migrationsFilePath),
11191123
join(dir, migrationsFilePath)
11201124
).then((path) => readJsonFile<MigrationsJson>(path));
11211125

@@ -1148,6 +1152,10 @@ async function getPackageMigrationsUsingInstall(
11481152

11491153
await execAsync(`${pmc.add} ${packageName}@${packageVersion}`, {
11501154
cwd: dir,
1155+
env: {
1156+
...process.env,
1157+
npm_config_legacy_peer_deps: 'true',
1158+
},
11511159
});
11521160

11531161
const {
@@ -1163,11 +1171,9 @@ async function getPackageMigrationsUsingInstall(
11631171

11641172
result = { ...migrations, packageGroup, version: packageJson.version };
11651173
} catch (e) {
1166-
output.warn({
1167-
title: `Failed to fetch migrations for ${packageName}@${packageVersion}`,
1168-
bodyLines: [e.message],
1169-
});
1170-
return {};
1174+
throw new Error(
1175+
`Failed to fetch migrations for ${packageName}@${packageVersion}: ${e.message}`
1176+
);
11711177
} finally {
11721178
await cleanup();
11731179
}

packages/nx/src/utils/package-manager.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -610,19 +610,14 @@ export async function packageRegistryPack(
610610
pkg: string,
611611
version: string
612612
): Promise<{ tarballPath: string }> {
613-
let pm = detectPackageManager();
614-
if (pm === 'yarn' || pm === 'bun') {
615-
/**
616-
* `(p)npm pack` will download a tarball of the specified version,
617-
* whereas `yarn` pack creates a tarball of the active workspace, so it
618-
* does not work for getting the content of a library.
619-
*
620-
* @see https://github.com/nrwl/nx/pull/9667#discussion_r842553994
621-
*
622-
* bun doesn't currently support pack
623-
*/
624-
pm = 'npm';
625-
}
613+
/**
614+
* Only `npm pack` supports downloading a tarball of a specified remote
615+
* package. `yarn` packs the active workspace, `pnpm pack` only packs
616+
* the local project, and `bun` doesn't support pack.
617+
*
618+
* @see https://github.com/nrwl/nx/pull/9667#discussion_r842553994
619+
*/
620+
const pm = 'npm';
626621

627622
const { stdout } = await execAsync(`${pm} pack ${pkg}@${version}`, {
628623
cwd,

0 commit comments

Comments
 (0)