Skip to content

Commit af3d754

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 af3d754

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

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

+23-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,20 @@ 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+
return packageJsonPath ?? undefined;
70+
} else {
71+
throw new Error("Could not find .pnp.js of Yarn Plug'n'Play");
72+
}
73+
}
5474
try {
5575
// avoid require.resolve here, see: https://github.com/angular/angular-cli/pull/18610#issuecomment-681980185
5676
const packageJsonPath = resolve.sync(`${packageName}/package.json`, { basedir: workspaceDir });
@@ -66,10 +86,10 @@ export async function getProjectDependencies(dir: string): Promise<Map<string, P
6686
if (!pkg) {
6787
throw new Error('Could not find package.json');
6888
}
69-
89+
const usingPnP = !!pkg.installConfig?.pnp;
7090
const results = new Map<string, PackageTreeNode>();
7191
for (const [name, version] of getAllDependencies(pkg)) {
72-
const packageJsonPath = findPackageJson(dir, name);
92+
const packageJsonPath = findPackageJson(dir, name, usingPnP);
7393
if (!packageJsonPath) {
7494
continue;
7595
}

0 commit comments

Comments
 (0)