Skip to content

Commit

Permalink
fix: support workspaces lower than nx 13 (#1204)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli authored Dec 22, 2021
1 parent 02b5d84 commit 16e3155
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
14 changes: 12 additions & 2 deletions libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -71,8 +75,14 @@ export class NxProjectTreeProvider extends AbstractTreeProvider<NxProjectTreeIte
nxProject.project
];
if (projectDef) {
if (projectDef.root === undefined) {
getOutputChannel().appendLine(
`Project ${nxProject.project} has no root. This could be because of an error loading the workspace configuration.`
);
}

item.resourceUri = Uri.file(
join(this.cliTaskProvider.getWorkspacePath(), projectDef.root)
join(this.cliTaskProvider.getWorkspacePath(), projectDef.root ?? '')
);
}
item.contextValue = 'project';
Expand Down
38 changes: 36 additions & 2 deletions libs/vscode/nx-workspace/src/lib/get-nx-workspace-config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getNxWorkspacePackageFileUtils } from './get-nx-workspace-package';
import type {
WorkspaceJsonConfiguration,
NxJsonConfiguration,
WorkspaceJsonConfiguration,
} from '@nrwl/devkit';
import { readAndCacheJsonFile } from '@nx-console/server';
import { join } from 'path';
import { getNxWorkspacePackageFileUtils } from './get-nx-workspace-package';
import { nxVersion } from './nx-version';

export async function getNxWorkspaceConfig(
basedir: string,
Expand All @@ -12,6 +14,38 @@ export async function getNxWorkspaceConfig(
workspaceConfiguration: WorkspaceJsonConfiguration & NxJsonConfiguration;
configPath: string;
}> {
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();
Expand Down
32 changes: 32 additions & 0 deletions libs/vscode/nx-workspace/src/lib/nx-version.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 16e3155

Please sign in to comment.