Skip to content

Commit a293ab7

Browse files
fix(@angular/cli): fix ng update for Yarn Pnp
Extend findPackageJson function to also work when using Yarn PnP Fixes angular#26505
1 parent f7d5389 commit a293ab7

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Diff for: packages/angular/cli/src/utilities/package-tree.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ interface PackageJson {
2424
'ng-add'?: {
2525
save?: NgAddSaveDependency;
2626
};
27+
installConfig?: {
28+
pnp?: boolean;
29+
};
30+
}
31+
32+
interface YarnPnp {
33+
resolveRequest: (request: string, issuer: string) => string | null;
2734
}
2835

2936
function getAllDependencies(pkg: PackageJson): Set<[string, string]> {
@@ -50,7 +57,21 @@ export async function readPackageJson(packageJsonPath: string): Promise<PackageJ
5057
}
5158
}
5259

53-
export function findPackageJson(workspaceDir: string, packageName: string): string | undefined {
60+
export function findPackageJson(
61+
workspaceDir: string,
62+
packageName: string,
63+
usingPnP = false,
64+
): string | undefined {
65+
if (usingPnP) {
66+
if (fs.existsSync(join(workspaceDir, '.pnp.js'))) {
67+
const pnp: YarnPnp = require(join(workspaceDir, '.pnp.js'));
68+
const packageJsonPath = pnp.resolveRequest(`${packageName}/package.json`, workspaceDir);
69+
70+
return packageJsonPath ?? undefined;
71+
} else {
72+
throw new Error("Could not find .pnp.js of Yarn Plug'n'Play");
73+
}
74+
}
5475
try {
5576
// avoid require.resolve here, see: https://github.com/angular/angular-cli/pull/18610#issuecomment-681980185
5677
const packageJsonPath = resolve.sync(`${packageName}/package.json`, { basedir: workspaceDir });
@@ -66,10 +87,10 @@ export async function getProjectDependencies(dir: string): Promise<Map<string, P
6687
if (!pkg) {
6788
throw new Error('Could not find package.json');
6889
}
69-
90+
const usingPnP = !!pkg.installConfig?.pnp;
7091
const results = new Map<string, PackageTreeNode>();
7192
for (const [name, version] of getAllDependencies(pkg)) {
72-
const packageJsonPath = findPackageJson(dir, name);
93+
const packageJsonPath = findPackageJson(dir, name, usingPnP);
7394
if (!packageJsonPath) {
7495
continue;
7596
}

0 commit comments

Comments
 (0)