From 9697d7ea663e3e1e29f269fdbe5001bafc98479b Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 24 Feb 2025 11:08:54 -0800 Subject: [PATCH] Always try sending along project root paths If the user opens a ts file outside of any workspace, currently we may not send along a project root path to TS. This can result in extra projects being loaded Instead we should tell TS about the current workspace we have opened --- .../src/tsServer/bufferSyncSupport.ts | 15 ++++++++++++++- .../src/typescriptServiceClient.ts | 5 +++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/extensions/typescript-language-features/src/tsServer/bufferSyncSupport.ts b/extensions/typescript-language-features/src/tsServer/bufferSyncSupport.ts index 56d2896fa339f..7b47be8d32f48 100644 --- a/extensions/typescript-language-features/src/tsServer/bufferSyncSupport.ts +++ b/extensions/typescript-language-features/src/tsServer/bufferSyncSupport.ts @@ -191,7 +191,20 @@ class SyncedBuffer { } private getProjectRootPath(resource: vscode.Uri): string | undefined { - const workspaceRoot = this.client.getWorkspaceRootForResource(resource); + let workspaceRoot = this.client.getWorkspaceRootForResource(resource); + + // If we didn't find a real workspace, we still want to try sending along a workspace folder + // to prevent TS from loading projects from outside of any workspace. + // Just pick the highest level one on the same FS even though the file is outside of it + if (!workspaceRoot && vscode.workspace.workspaceFolders) { + for (const root of Array.from(vscode.workspace.workspaceFolders).sort((a, b) => a.uri.path.length - b.uri.path.length)) { + if (root.uri.scheme === resource.scheme && root.uri.authority === resource.authority) { + workspaceRoot = root.uri; + break; + } + } + } + if (workspaceRoot) { const tsRoot = this.client.toTsFilePath(workspaceRoot); return tsRoot?.startsWith(inMemoryResourcePrefix) ? undefined : tsRoot; diff --git a/extensions/typescript-language-features/src/typescriptServiceClient.ts b/extensions/typescript-language-features/src/typescriptServiceClient.ts index d9f47c1b85a57..4201d6da29b81 100644 --- a/extensions/typescript-language-features/src/typescriptServiceClient.ts +++ b/extensions/typescript-language-features/src/typescriptServiceClient.ts @@ -836,9 +836,10 @@ export default class TypeScriptServiceClient extends Disposable implements IType } } - for (const root of roots.sort((a, b) => a.uri.fsPath.length - b.uri.fsPath.length)) { + // Find the highest level workspace folder that contains the file + for (const root of roots.sort((a, b) => a.uri.path.length - b.uri.path.length)) { if (root.uri.scheme === resource.scheme && root.uri.authority === resource.authority) { - if (resource.fsPath.startsWith(root.uri.fsPath + path.sep)) { + if (resource.path.startsWith(root.uri.path + '/')) { return root.uri; } }