From 16e315521dbd90de3040abc71c28826fba44af8e Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Tue, 21 Dec 2021 21:01:48 -0500 Subject: [PATCH] fix: support workspaces lower than nx 13 (#1204) --- .../src/lib/nx-project-tree-provider.ts | 14 ++++++- .../src/lib/get-nx-workspace-config.ts | 38 ++++++++++++++++++- .../vscode/nx-workspace/src/lib/nx-version.ts | 32 ++++++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 libs/vscode/nx-workspace/src/lib/nx-version.ts diff --git a/libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts b/libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts index 9dd3f68212..fceae2dede 100644 --- a/libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts +++ b/libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts @@ -1,4 +1,8 @@ -import { AbstractTreeProvider, clearJsonCache } from '@nx-console/server'; +import { + AbstractTreeProvider, + clearJsonCache, + getOutputChannel, +} from '@nx-console/server'; import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration'; import { revealNxProject } from '@nx-console/vscode/nx-workspace'; import { CliTaskProvider } from '@nx-console/vscode/tasks'; @@ -71,8 +75,14 @@ export class NxProjectTreeProvider extends AbstractTreeProvider { + const versionNumber = nxVersion(); + if (versionNumber && versionNumber < 13) { + // Versions lower than 13 throw an error when trying to read configurations of workspaces that have nx.json properties in workspace.json + let workspaceJson: WorkspaceJsonConfiguration; + if (format === 'nx') { + workspaceJson = (await readAndCacheJsonFile('workspace.json', basedir)) + .json; + } else { + workspaceJson = (await readAndCacheJsonFile('angular.json', basedir)) + .json; + } + + const nxJson: NxJsonConfiguration = ( + await readAndCacheJsonFile('nx.json', basedir) + ).json; + return { + workspaceConfiguration: { + ...workspaceJson, + ...nxJson, + projects: { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ...((nxJson as any).projects ?? {}), + ...workspaceJson.projects, + }, + }, + configPath: + format === 'nx' + ? join(basedir, 'workspace.json') + : join(basedir, 'angular.json'), + }; + } + const nxWorkspacePackage = await getNxWorkspacePackageFileUtils(); const configFile = nxWorkspacePackage.workspaceFileName(); diff --git a/libs/vscode/nx-workspace/src/lib/nx-version.ts b/libs/vscode/nx-workspace/src/lib/nx-version.ts new file mode 100644 index 0000000000..6aebe36598 --- /dev/null +++ b/libs/vscode/nx-workspace/src/lib/nx-version.ts @@ -0,0 +1,32 @@ +import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration'; + +declare function __non_webpack_require__(importPath: string): any; + +let nxWorkspacePackageJson: { version: string }; +let loadedNxWorkspacePackage = false; +export function nxVersion(): number | null { + if (!loadedNxWorkspacePackage) { + const workspacePath = WorkspaceConfigurationStore.instance.get( + 'nxWorkspacePath', + '' + ); + try { + nxWorkspacePackageJson = __non_webpack_require__( + `${workspacePath}/node_modules/@nrwl/workspace/package.json` + ); + loadedNxWorkspacePackage = true; + } catch (e) { + return null; + } + } + + if (!nxWorkspacePackageJson) { + return 0; + } + const nxPackageVersion = nxWorkspacePackageJson.version; + const majorVersion = nxPackageVersion.split('.')[0]; + if (!majorVersion) { + return 0; + } + return +majorVersion; +}