Skip to content

Commit

Permalink
fix(@angular/cli): fix ng update for Yarn Pnp
Browse files Browse the repository at this point in the history
Extend findPackageJson function to also work when using Yarn PnP

Fixes angular#26505
  • Loading branch information
dominicbachmann committed Dec 16, 2023
1 parent f7d5389 commit a293ab7
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions packages/angular/cli/src/utilities/package-tree.ts
Expand Up @@ -24,6 +24,13 @@ interface PackageJson {
'ng-add'?: {
save?: NgAddSaveDependency;
};
installConfig?: {
pnp?: boolean;
};
}

interface YarnPnp {
resolveRequest: (request: string, issuer: string) => string | null;
}

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

export function findPackageJson(workspaceDir: string, packageName: string): string | undefined {
export function findPackageJson(
workspaceDir: string,
packageName: string,
usingPnP = false,
): string | undefined {
if (usingPnP) {
if (fs.existsSync(join(workspaceDir, '.pnp.js'))) {
const pnp: YarnPnp = require(join(workspaceDir, '.pnp.js'));
const packageJsonPath = pnp.resolveRequest(`${packageName}/package.json`, workspaceDir);

return packageJsonPath ?? undefined;
} else {
throw new Error("Could not find .pnp.js of Yarn Plug'n'Play");
}
}
try {
// avoid require.resolve here, see: https://github.com/angular/angular-cli/pull/18610#issuecomment-681980185
const packageJsonPath = resolve.sync(`${packageName}/package.json`, { basedir: workspaceDir });
Expand All @@ -66,10 +87,10 @@ export async function getProjectDependencies(dir: string): Promise<Map<string, P
if (!pkg) {
throw new Error('Could not find package.json');
}

const usingPnP = !!pkg.installConfig?.pnp;
const results = new Map<string, PackageTreeNode>();
for (const [name, version] of getAllDependencies(pkg)) {
const packageJsonPath = findPackageJson(dir, name);
const packageJsonPath = findPackageJson(dir, name, usingPnP);
if (!packageJsonPath) {
continue;
}
Expand Down

0 comments on commit a293ab7

Please sign in to comment.