Skip to content

Commit c9dfc6c

Browse files
atscottalxhub
authored andcommitted
fix(vscode-extension): Show warning if multiple versions of Angular are detected in workspace
This shows a more prominent warning when multiple versions of Angular are detected in the workspace and we are forced to choose one to use for the language server. This also logs those versions with their locations and directs the user to view the output panel. fixes angular#65466
1 parent 3411638 commit c9dfc6c

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

  • vscode-ng-language-service/client/src

vscode-ng-language-service/client/src/client.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,7 @@ export class AngularLanguageClient implements vscode.Disposable {
419419
}
420420
}
421421

422-
// Pass the earliest Angular version along to the compiler for maximum compatibility.
423-
if (angularVersions.length > 0) {
424-
args.push('--angularCoreVersion', angularVersions[0].version.toString());
425-
this.outputChannel.appendLine(
426-
`Using Angular version ${angularVersions[0].version.toString()}.`,
427-
);
428-
}
422+
setAngularVersionAndShowMultipleVersionsWarning(angularVersions, args, this.outputChannel);
429423

430424
const forceStrictTemplates = config.get<boolean>('angular.forceStrictTemplates');
431425
if (forceStrictTemplates) {
@@ -674,3 +668,35 @@ async function getAngularVersionsInWorkspace(
674668
}
675669
return Array.from(angularCoreModules);
676670
}
671+
672+
function setAngularVersionAndShowMultipleVersionsWarning(
673+
angularVersions: NodeModule[],
674+
args: string[],
675+
outputChannel: vscode.OutputChannel,
676+
) {
677+
if (angularVersions.length === 0) {
678+
return;
679+
}
680+
// Pass the earliest Angular version along to the compiler for maximum compatibility.
681+
// For example, if we tell the v21 compiler that we're using v21 but there's a v13 project,
682+
// the compiler may attempt to import and use APIs from angular core that don't exist in v13.
683+
args.push('--angularCoreVersion', angularVersions[0].version.toString());
684+
outputChannel.appendLine(`Using Angular version ${angularVersions[0].version.toString()}.`);
685+
686+
let minorVersions = new Map<string, NodeModule>();
687+
for (const v of angularVersions) {
688+
minorVersions.set(`${v.version.major}.${v.version.minor}`, v);
689+
}
690+
if (minorVersions.size > 1) {
691+
vscode.window.showWarningMessage(
692+
`Multiple versions of Angular detected in the workspace. This can lead to compatibility issues for the language service. ` +
693+
`See the output panel for more details.`,
694+
);
695+
outputChannel.appendLine(`Multiple Angular versions detected in the workspace:`);
696+
for (const v of minorVersions.values()) {
697+
outputChannel.appendLine(
698+
` Angular version ${v.version.toString()} detected at ${v.resolvedPath}`,
699+
);
700+
}
701+
}
702+
}

0 commit comments

Comments
 (0)