Skip to content

fix: improve detection of Angular core version in monorepo setup #2106

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

Merged
merged 2 commits into from
Nov 8, 2024
Merged
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: 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
Loading