diff --git a/apps/vscode/src/main.ts b/apps/vscode/src/main.ts index ac2ab0f112..ecd8fa75de 100644 --- a/apps/vscode/src/main.ts +++ b/apps/vscode/src/main.ts @@ -25,6 +25,7 @@ import { teardownTelemetry, watchFile, directoryExists, + fileExists, } from '@nx-console/server'; import { GlobalConfigurationStore, @@ -118,7 +119,7 @@ export async function activate(c: ExtensionContext) { workspace.workspaceFolders && workspace.workspaceFolders[0].uri.fsPath; if (vscodeWorkspacePath) { - scanForWorkspace(vscodeWorkspacePath); + await scanForWorkspace(vscodeWorkspacePath); } context.subscriptions.push( @@ -179,9 +180,11 @@ function manuallySelectWorkspaceDefinition() { } } -function scanForWorkspace(vscodeWorkspacePath: string) { +async function scanForWorkspace(vscodeWorkspacePath: string) { let currentDirectory = vscodeWorkspacePath; + const { root } = parse(vscodeWorkspacePath); + const workspacePath = WorkspaceConfigurationStore.instance.get( 'nxWorkspacePath', '' @@ -191,10 +194,25 @@ function scanForWorkspace(vscodeWorkspacePath: string) { currentDirectory = workspacePath; } - setWorkspace(currentDirectory); + while (currentDirectory !== root) { + if (await fileExists(join(currentDirectory, 'angular.json'))) { + return setWorkspace(currentDirectory); + } + if (await fileExists(join(currentDirectory, 'workspace.json'))) { + return setWorkspace(currentDirectory); + } + if (await fileExists(join(currentDirectory, 'nx.json'))) { + return setWorkspace(currentDirectory); + } + currentDirectory = dirname(currentDirectory); + } } async function setWorkspace(workspacePath: string) { + if (workspacePath.match(/(workspace|angular)\.json$/)) { + workspacePath = dirname(workspacePath); + } + WorkspaceConfigurationStore.instance.set('nxWorkspacePath', workspacePath); const { verifyWorkspace } = await import('@nx-console/vscode/nx-workspace'); diff --git a/libs/typescript-plugin/src/lib/typescript-plugin.ts b/libs/typescript-plugin/src/lib/typescript-plugin.ts index 35be63393a..569dbbd20d 100644 --- a/libs/typescript-plugin/src/lib/typescript-plugin.ts +++ b/libs/typescript-plugin/src/lib/typescript-plugin.ts @@ -116,11 +116,18 @@ async function configurePlugin(workspaceRoot: string, api: any) { async function getExternalFiles( workspaceRoot: string ): Promise<{ mainFile: string; directory: string }[]> { - const baseTsConfig = ( - await readAndCacheJsonFile(TSCONFIG_BASE, workspaceRoot) - ).json; + let tsconfig = (await readAndCacheJsonFile(TSCONFIG_BASE, workspaceRoot)) + .json; + + if (!('compilerOptions' in tsconfig)) { + tsconfig = (await readAndCacheJsonFile('tsconfig.json', workspaceRoot)) + .json; + if (!('compilerOptions' in tsconfig)) { + return []; + } + } - const paths = baseTsConfig.compilerOptions.paths; + const paths = tsconfig.compilerOptions.paths ?? {}; const externals: { mainFile: string; directory: string }[] = [];