Skip to content

Commit

Permalink
feat: move all workspace retrieval to the language server (#1402)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli authored Nov 4, 2022
1 parent 47966d5 commit 650a44e
Show file tree
Hide file tree
Showing 69 changed files with 378 additions and 224 deletions.
8 changes: 4 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
"enforceBuildableLibDependency": true,
"allow": [],
"depConstraints": [
{
"sourceTag": "*",
"onlyDependOnLibsWithTags": ["*"]
},
{
"sourceTag": "type:vscode",
"onlyDependOnLibsWithTags": ["type:shared", "type:vscode"]
Expand All @@ -29,6 +25,10 @@
"sourceTag": "type:shared",
"onlyDependOnLibsWithTags": ["type:shared"],
"bannedExternalImports": ["vscode"]
},
{
"sourceTag": "type:vscode-ui",
"onlyDependOnLibsWithTags": ["type:shared", "type:vscode-ui"]
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
runs-on: ubuntu-latest
name: Nx Cloud - Agent E2E
timeout-minutes: 15
env:
NX_VERBOSE_LOGGING: 'false'
steps:
- name: Set nx run variable
run: echo "NX_RUN_GROUP=run-group-e2e-${{ github.run_id}}-${{ github.run_attempt }}" >> $GITHUB_ENV
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/nx-run/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ runs:
shell: bash
- name: Workspace Lint
run: |
yarn exec nx-cloud record -- yarn exec nx workspace-lint
yarn exec nx-cloud record -- yarn exec nx workspace-lint
yarn exec nx-cloud record -- yarn exec nx format:check
shell: bash
env:
Expand All @@ -26,6 +26,7 @@ runs:
- name: E2E
env:
NX_RUN_GROUP: run-group-e2e-${{ github.run_id}}-${{ github.run_attempt }}
NX_VERBOSE_LOGGING: 'false'
run: yarn exec nx affected --target=e2e --parallel=3 --configuration=ci
shell: bash
- name: Stopping nx-cloud ci agents
Expand Down
5 changes: 4 additions & 1 deletion apps/nxls/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
"options": {},
"configurations": {
"ci": {
"commands": ["yarn tsc -p ./apps/nxls/tsconfig.app.json --noEmit"]
"commands": [
"yarn tsc -p ./apps/nxls/tsconfig.app.json --noEmit",
"node ./apps/nxls/esbuild.js"
]
},
"production": {
"command": "node ./apps/nxls/esbuild.js"
Expand Down
33 changes: 21 additions & 12 deletions apps/nxls/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './global-polyfills';
import { getCompletionItems } from '@nx-console/language-server/capabilities/code-completion';
import { getDocumentLinks } from '@nx-console/language-server/capabilities/document-links';
import {
NxChangeWorkspace,
NxWorkspaceRefreshNotification,
NxWorkspaceRequest,
} from '@nx-console/language-server/types';
Expand All @@ -22,7 +23,7 @@ import {
getProjectJsonSchema,
getWorkspaceJsonSchema,
} from '@nx-console/shared/json-schema';
import { nxWorkspace } from '@nx-console/shared/workspace';
import { nxWorkspace } from '@nx-console/language-server/workspace';
import {
ClientCapabilities,
CompletionList,
Expand Down Expand Up @@ -68,17 +69,18 @@ connection.onInitialize(async (params) => {
const { workspacePath } = params.initializationOptions ?? {};
try {
WORKING_PATH =
workspacePath ??
params.rootPath ??
URI.parse(params.rootUri ?? '').fsPath;
workspacePath ||
params.rootPath ||
URI.parse(params.rootUri ?? '').fsPath ||
params.workspaceFolders?.[0]?.uri;

if (!WORKING_PATH) {
throw 'Unable to determine workspace path';
}

CLIENT_CAPABILITIES = params.capabilities;

await configureSchemas(WORKING_PATH, CLIENT_CAPABILITIES);
configureSchemas(WORKING_PATH, CLIENT_CAPABILITIES);
} catch (e) {
lspLogger.log('Unable to get Nx info: ' + e.toString());
}
Expand Down Expand Up @@ -182,25 +184,32 @@ connection.onShutdown(() => {
jsonDocumentMapper.dispose();
});

connection.onRequest(NxWorkspaceRequest, async () => {
connection.onRequest(NxWorkspaceRequest, async ({ reset }) => {
if (!WORKING_PATH) {
return new ResponseError(1000, 'Unable to get Nx info: no workspace path');
}

const workspace = await nxWorkspace(WORKING_PATH, lspLogger);

return workspace.workspace;
return nxWorkspace(WORKING_PATH, lspLogger, reset);
});

connection.onNotification(NxWorkspaceRefreshNotification, async () => {
if (!WORKING_PATH) {
return new ResponseError(1000, 'Unable to get Nx info: no workspace path');
return new ResponseError(1001, 'Unable to get Nx info: no workspace path');
}

await nxWorkspace(WORKING_PATH, lspLogger, true);
await configureSchemas(WORKING_PATH, CLIENT_CAPABILITIES);
await reconfigure(WORKING_PATH);
});

connection.onNotification(NxChangeWorkspace, async (workspacePath) => {
WORKING_PATH = workspacePath;
await reconfigure(WORKING_PATH);
});

async function reconfigure(workingPath: string) {
await nxWorkspace(workingPath, lspLogger, true);
await configureSchemas(workingPath, CLIENT_CAPABILITIES);
}

async function configureSchemas(
workingPath: string | undefined,
capabilities: ClientCapabilities | undefined
Expand Down
2 changes: 1 addition & 1 deletion apps/vscode-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"dependsOn": ["^build"]
}
},
"implicitDependencies": ["vscode", "nxls"],
"implicitDependencies": ["vscode"],
"tags": []
}
23 changes: 23 additions & 0 deletions apps/vscode-e2e/specs/language-server/language-server.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { openWorkspace } from '../utils';

describe('Nx Console should have the language server running', () => {
before(async () => {
await openWorkspace('nx');
});

it('should start the ls', async () => {
const workbench = await browser.getWorkbench();
const bottomBar = await workbench.getBottomBar();
await bottomBar.maximize();
const outputView = await bottomBar.openOutputView();
await outputView.wait();
await outputView.selectChannel('Nx Console Client');
const output = await outputView.getText();
console.log(output);
expect(
output.some((element) =>
element.toLowerCase().includes('createProjectGraphAsync'.toLowerCase())
)
).toBeTruthy();
});
});
2 changes: 2 additions & 0 deletions apps/vscode-e2e/wdio.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export const config: Options.Testrunner = {
'wdio:vscodeOptions': {
// point to the root directory of your project
vscodeArgs: { 'disable-extensions': true },
userSettings: { 'NxConsoleClient.trace.server': 'info' },
verboseLogging: true,
extensionPath: join(__dirname, '../..', 'dist/apps/vscode'),
},

Expand Down
2 changes: 1 addition & 1 deletion apps/vscode-ui-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
}
}
},
"tags": [],
"tags": ["type:vscode-ui"],
"implicitDependencies": ["vscode-ui-feature-task-execution-form"]
}
2 changes: 1 addition & 1 deletion apps/vscode-ui/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@
"outputs": ["coverage/apps/vscode-ui"]
}
},
"tags": []
"tags": ["type:vscode-ui"]
}
2 changes: 1 addition & 1 deletion apps/vscode/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,5 @@
"outputs": ["dist/apps/vscode"]
}
},
"tags": []
"tags": ["type:vscode"]
}
10 changes: 3 additions & 7 deletions apps/vscode/src/commands/refresh-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NxWorkspaceRefreshNotification } from '@nx-console/language-server/types';
import { sendNotification } from '@nx-console/vscode/lsp-client';
import { getWorkspacePath, outputLogger } from '@nx-console/vscode/utils';
import { getNxWorkspace } from '@nx-console/vscode/nx-workspace';
import { outputLogger } from '@nx-console/vscode/utils';
import { debounceTime, Subject } from 'rxjs';
import { commands } from 'vscode';

Expand All @@ -9,12 +10,7 @@ export const REFRESH_WORKSPACE = 'nxConsole.refreshWorkspace';
const refresh = new Subject();

refresh.pipe(debounceTime(150)).subscribe(async () => {
const { nxWorkspace } = await import('@nx-console/shared/workspace');
const { daemonEnabled, workspaceType } = await nxWorkspace(
getWorkspacePath(),
outputLogger,
true
);
const { daemonEnabled, workspaceType } = await getNxWorkspace();

if (!daemonEnabled && workspaceType === 'nx') {
outputLogger.log('Nx Daemon is not enabled.');
Expand Down
17 changes: 8 additions & 9 deletions apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
REFRESH_WORKSPACE,
} from './commands/refresh-workspace';
import {
getNxWorkspace,
stopDaemon,
WorkspaceCodeLensProvider,
} from '@nx-console/vscode/nx-workspace';
Expand Down Expand Up @@ -131,8 +132,7 @@ export async function activate(c: ExtensionContext) {
revealWebViewPanelCommand,
manuallySelectWorkspaceDefinitionCommand,
refreshWorkspace(),
projectGraph(),
await configureLspClient(context)
projectGraph()
);

// registers itself as a CodeLensProvider and watches config to dispose/re-register
Expand Down Expand Up @@ -226,6 +226,8 @@ async function setWorkspace(workspacePath: string) {

WorkspaceConfigurationStore.instance.set('nxWorkspacePath', workspacePath);

const lspContext = configureLspClient(context);

// Set the NX_WORKSPACE_ROOT_PATH as soon as possible so that the nx utils can get this.
process.env.NX_WORKSPACE_ROOT_PATH = workspacePath;

Expand Down Expand Up @@ -258,7 +260,8 @@ async function setWorkspace(workspacePath: string) {
context.subscriptions.push(
nxCommandsTreeView,
nxProjectTreeView,
nxHelpAndFeedbackTreeView
nxHelpAndFeedbackTreeView,
lspContext
);
} else {
WorkspaceConfigurationStore.instance.set('nxWorkspacePath', workspacePath);
Expand Down Expand Up @@ -300,9 +303,7 @@ async function setWorkspace(workspacePath: string) {
}

async function setApplicationAndLibraryContext(workspacePath: string) {
const { nxWorkspace } = await import('@nx-console/shared/workspace');

const { workspaceLayout } = await nxWorkspace(workspacePath, outputLogger);
const { workspaceLayout } = await getNxWorkspace();

commands.executeCommand('setContext', 'nxAppsDir', [
join(workspacePath, workspaceLayout.appsDir),
Expand Down Expand Up @@ -346,9 +347,7 @@ async function registerWorkspaceFileWatcher(
workspaceFileWatcher.dispose();
}

const { nxWorkspace } = await import('@nx-console/shared/workspace');

const { workspaceLayout } = await nxWorkspace(workspacePath, outputLogger);
const { workspaceLayout } = await getNxWorkspace();
const workspacePackageDirs = new Set<string>();
workspacePackageDirs.add(workspaceLayout.appsDir);
workspacePackageDirs.add(workspaceLayout.libsDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import {
configureJsonLanguageService,
getJsonLanguageService,
} from '@nx-console/language-server/utils';
import { configureJsonLanguageService } from '@nx-console/language-server/utils';
import {
CompletionType,
EnhancedJsonSchema,
} from '@nx-console/shared/json-schema';
import * as workspace from '@nx-console/shared/workspace';
import * as workspace from '@nx-console/language-server/workspace';
import { vol } from 'memfs';
import {
ClientCapabilities,
Position,
TextDocument,
} from 'vscode-json-languageservice';
import { getCompletionItems } from './get-completion-items';
import { NxWorkspace } from '@nx-console/shared/types';

jest.mock(
'@nx-console/shared/workspace',
'@nx-console/language-server/workspace',
(): Partial<typeof workspace> => ({
nxWorkspace: jest.fn(() =>
Promise.resolve<workspace.NxWorkspace>({
Promise.resolve<NxWorkspace>({
configurationFilePath: '/workspace.json',
isLerna: false,
validWorkspaceJson: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nxWorkspace } from '@nx-console/shared/workspace';
import { nxWorkspace } from '@nx-console/language-server/workspace';
import {
ASTNode,
CompletionItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nxWorkspace } from '@nx-console/shared/workspace';
import { nxWorkspace } from '@nx-console/language-server/workspace';
import {
ASTNode,
CompletionItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nxWorkspace } from '@nx-console/shared/workspace';
import { nxWorkspace } from '@nx-console/language-server/workspace';
import {
ASTNode,
CompletionItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArrayNode } from '@nx-console/language-server/utils';
import { nxWorkspace } from '@nx-console/shared/workspace';
import { nxWorkspace } from '@nx-console/language-server/workspace';
import {
ASTNode,
CompletionItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArrayNode } from '@nx-console/language-server/utils';
import { nxWorkspace } from '@nx-console/shared/workspace';
import { nxWorkspace } from '@nx-console/language-server/workspace';
import {
ASTNode,
CompletionItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
isStringNode,
lspLogger,
} from '@nx-console/language-server/utils';
import { nxWorkspace } from '@nx-console/shared/workspace';
import { nxWorkspace } from '@nx-console/language-server/workspace';
import { join } from 'path';
import {
ASTNode,
Expand Down
12 changes: 8 additions & 4 deletions libs/language-server/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { RequestType, NotificationType } from 'vscode-languageserver/node';
import type { NxWorkspaceConfiguration } from '@nx-console/shared/workspace';
import { NotificationType, RequestType } from 'vscode-languageserver/node';
import { NxWorkspace } from '@nx-console/shared/types';

export const NxChangeWorkspace: NotificationType<string> = new NotificationType(
'nx/changeWorkspace'
);

export const NxWorkspaceRefreshNotification: NotificationType<void> =
new NotificationType('nx/refreshWorkspace');

export const NxWorkspaceRequest: RequestType<
void,
NxWorkspaceConfiguration,
{ reset: boolean },
NxWorkspace,
unknown
> = new RequestType('nx/workspace');
5 changes: 4 additions & 1 deletion libs/language-server/utils/src/lib/lsp-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export function setLspLogger(connection: Connection) {

export const lspLogger: Logger = {
log(message: string, ...args: any[]) {
log?.(message, ...args);
log?.(
`[Nxls] - ${new Date(Date.now()).toISOString()} - ${message}\n`,
...args
);
},
};
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions libs/language-server/workspace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# language-server-workspace

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test language-server-workspace` to execute the unit tests via [Jest](https://jestjs.io).

## Running lint

Run `nx lint language-server-workspace` to execute the lint via [ESLint](https://eslint.org/).
Loading

0 comments on commit 650a44e

Please sign in to comment.