Skip to content

Commit

Permalink
fix: improve detection of Angular core version in monorepo setup (#2106)
Browse files Browse the repository at this point in the history
* fix: improve detection of Angular core version in monorepo setup

Angular may not be installed at the project root. We should look for
`package.json` roots and determine the `@angular/core` version based on
these folders.

Related to: angular/angular#58546

* fixup! fix: improve detection of Angular core version in monorepo setup

Feedback
  • Loading branch information
devversion authored Nov 8, 2024
1 parent 54ba623 commit 6692c40
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class AngularLanguageClient implements vscode.Disposable {
}

// Node module for the language server
const args = this.constructArgs();
const args = await this.constructArgs();
const prodBundle = this.context.asAbsolutePath('server');
const devBundle =
this.context.asAbsolutePath(path.join('bazel-bin', 'server', 'src', 'server.js'));
Expand Down Expand Up @@ -289,7 +289,7 @@ export class AngularLanguageClient implements vscode.Disposable {
* Construct the arguments that's used to spawn the server process.
* @param ctx vscode extension context
*/
private constructArgs(): string[] {
private async constructArgs(): Promise<string[]> {
const config = vscode.workspace.getConfiguration();
const args: string[] = ['--logToConsole'];

Expand Down Expand Up @@ -317,8 +317,8 @@ export class AngularLanguageClient implements vscode.Disposable {
}

// Sort the versions from oldest to newest.
const angularVersions = getAngularVersionsInWorkspace().sort(
(a, b) => a.version.greaterThanOrEqual(b.version) ? 1 : -1);
const angularVersions = (await getAngularVersionsInWorkspace(this.outputChannel))
.sort((a, b) => a.version.greaterThanOrEqual(b.version) ? 1 : -1);

// Only disable block syntax if we find angular/core and every one we find does not support
// block syntax
Expand Down Expand Up @@ -552,13 +552,22 @@ function extensionVersionCompatibleWithAllProjects(serverModuleLocation: string)
}

/**
* Returns true if any project in the workspace supports block syntax (v17+).
* Traverses through the currently open VSCode workspace (i.e. all open folders)
* and finds all `@angular/core` versions installed based on `package.json` files.
*/
function getAngularVersionsInWorkspace(): NodeModule[] {
async function getAngularVersionsInWorkspace(outputChannel: vscode.OutputChannel):
Promise<NodeModule[]> {
const packageJsonFiles = await vscode.workspace.findFiles(
'**/package.json',
// Skip looking inside `node_module` folders as those contain irrelevant files.
'**/node_modules/**');
const packageJsonRoots = packageJsonFiles.map(f => path.dirname(f.fsPath));
const angularCoreModules = new Set<NodeModule>();
const workspaceFolders = vscode.workspace.workspaceFolders || [];
for (const workspaceFolder of workspaceFolders) {
const angularCore = resolve('@angular/core', workspaceFolder.uri.fsPath);

outputChannel.appendLine(`package.json roots detected: ${packageJsonRoots.join(',\n ')}`);

for (const packageJsonRoot of packageJsonRoots) {
const angularCore = resolve('@angular/core', packageJsonRoot);
if (angularCore === undefined) {
continue;
}
Expand Down

0 comments on commit 6692c40

Please sign in to comment.