Skip to content

fix(@angular/cli): fix ng update for Yarn Pnp #26695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions packages/angular/cli/src/utilities/package-tree.ts
Original file line number Diff line number Diff line change
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