From 87dd2d0203b5c376909bd93996c89b7a0679cb42 Mon Sep 17 00:00:00 2001 From: Emily M Klassen <760204+forivall@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:30:03 -0700 Subject: [PATCH 1/5] fix(nxls): misc fixes to get nxls to work with helix --- apps/nxls/.babelrc | 10 ++++++++++ apps/nxls/src/main.ts | 11 ++++++----- libs/shared/file-system/src/lib/directory-exists.ts | 3 ++- libs/shared/npm/src/lib/workspace-dependencies.ts | 6 +++++- 4 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 apps/nxls/.babelrc diff --git a/apps/nxls/.babelrc b/apps/nxls/.babelrc new file mode 100644 index 0000000000..fd4cbcdef1 --- /dev/null +++ b/apps/nxls/.babelrc @@ -0,0 +1,10 @@ +{ + "presets": [ + [ + "@nx/js/babel", + { + "useBuiltIns": "usage" + } + ] + ] +} diff --git a/apps/nxls/src/main.ts b/apps/nxls/src/main.ts index e53cf49beb..4e1a519222 100644 --- a/apps/nxls/src/main.ts +++ b/apps/nxls/src/main.ts @@ -109,6 +109,8 @@ import { URI } from 'vscode-uri'; import { ensureOnlyJsonRpcStdout } from './ensureOnlyJsonRpcStdout'; import { NativeWatcher } from '@nx-console/shared-watcher'; +const connection = createConnection(ProposedFeatures.all); + process.on('unhandledRejection', (e: any) => { connection.console.error(formatError(`Unhandled exception`, e)); }); @@ -124,8 +126,6 @@ let unregisterFileWatcher: () => Promise = async () => { }; let reconfigureAttempts = 0; -const connection = createConnection(ProposedFeatures.all); - // Create a text document manager. const documents = new TextDocuments(TextDocument); @@ -138,12 +138,13 @@ connection.onInitialize(async (params) => { lspLogger.log('Initializing Nx Language Server'); const { workspacePath } = params.initializationOptions ?? {}; + const extractFsPath = (p: string | undefined) => p && URI.parse(p).fsPath; try { WORKING_PATH = - workspacePath || - params.workspaceFolders?.[0]?.uri || + extractFsPath(workspacePath) || + extractFsPath(params.workspaceFolders?.[0]?.uri) || params.rootPath || - URI.parse(params.rootUri ?? '').fsPath; + extractFsPath(params.rootUri ?? ''); if (!WORKING_PATH) { throw 'Unable to determine workspace path'; diff --git a/libs/shared/file-system/src/lib/directory-exists.ts b/libs/shared/file-system/src/lib/directory-exists.ts index eff690f7eb..a43ec90e05 100644 --- a/libs/shared/file-system/src/lib/directory-exists.ts +++ b/libs/shared/file-system/src/lib/directory-exists.ts @@ -1,8 +1,9 @@ +import { URI } from 'vscode-uri'; import { stat } from 'fs/promises'; export async function directoryExists(filePath: string): Promise { try { - return (await stat(filePath)).isDirectory(); + return (await stat(URI.parse(filePath).fsPath)).isDirectory(); } catch { return false; } diff --git a/libs/shared/npm/src/lib/workspace-dependencies.ts b/libs/shared/npm/src/lib/workspace-dependencies.ts index 93830bb130..e6847d2fe6 100644 --- a/libs/shared/npm/src/lib/workspace-dependencies.ts +++ b/libs/shared/npm/src/lib/workspace-dependencies.ts @@ -2,6 +2,7 @@ import { gte, NxVersion } from '@nx-console/nx-version'; import { directoryExists } from '@nx-console/shared-file-system'; import type { Logger } from '@nx-console/shared-utils'; import { stat } from 'fs/promises'; +import Module from 'module'; import type { ProjectGraphProjectNode } from 'nx/src/devkit-exports'; import { platform } from 'os'; import { join } from 'path'; @@ -80,13 +81,16 @@ export async function workspaceDependencyPath( export function importWorkspaceDependency( importPath: string, logger?: Logger, + workspacePath: string = __dirname, ): Promise { if (platform() === 'win32') { importPath = importPath.replace(/\\/g, '/'); } + const workspaceRequire = Module.createRequire(workspacePath); + // eslint-disable-next-line @typescript-eslint/no-var-requires - const imported = require(importPath); + const imported = workspaceRequire(importPath); logger?.log(`Using local Nx package at ${importPath}`); From a61db77380d9bb9d9169570c9514aa76000f741d Mon Sep 17 00:00:00 2001 From: Emily M Klassen <760204+forivall@users.noreply.github.com> Date: Thu, 25 Sep 2025 18:12:31 -0700 Subject: [PATCH 2/5] style(language-server): fix formatting --- .../workspace/src/lib/get-project-graph-output.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/language-server/workspace/src/lib/get-project-graph-output.ts b/libs/language-server/workspace/src/lib/get-project-graph-output.ts index 53c6193e87..24cf890300 100644 --- a/libs/language-server/workspace/src/lib/get-project-graph-output.ts +++ b/libs/language-server/workspace/src/lib/get-project-graph-output.ts @@ -20,12 +20,12 @@ export async function getProjectGraphOutput(workspacePath: string) { async function getCacheDir(workspacePath: string): Promise { const importPath = await findNxPackagePath( workspacePath, - join('src', 'utils', 'cache-directory.js') + join('src', 'utils', 'cache-directory.js'), ); if (!importPath) { lspLogger.log( - `Unable to load the "nx" package from the workspace. Please ensure that the proper dependencies are installed locally.` + `Unable to load the "nx" package from the workspace. Please ensure that the proper dependencies are installed locally.`, ); throw 'local Nx dependency not found'; } From ba7482479b5b35fa3914d0edb643481c3c4df45b Mon Sep 17 00:00:00 2001 From: Emily M Klassen <760204+forivall@users.noreply.github.com> Date: Fri, 26 Sep 2025 18:42:06 -0700 Subject: [PATCH 3/5] build(nxls): use string-replace-loader for non-webpack require --- apps/nxls/webpack.config.js | 19 ++++++++++++++++++- .../npm/src/lib/workspace-dependencies.ts | 6 +----- package.json | 1 + yarn.lock | 14 +++++++++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/apps/nxls/webpack.config.js b/apps/nxls/webpack.config.js index 0be4f4f03c..3ebd5df16b 100644 --- a/apps/nxls/webpack.config.js +++ b/apps/nxls/webpack.config.js @@ -4,5 +4,22 @@ const { composePlugins, withNx } = require('@nx/webpack'); module.exports = composePlugins(withNx(), (config) => { // Note: This was added by an Nx migration. Webpack builds are required to have a corresponding Webpack config file. // See: https://nx.dev/recipes/webpack/webpack-config-setup - return config; + return { + ...config, + module: { + ...config.module, + rules: [ + { + test: /libs\/shared\/npm\/src\/lib\/workspace-dependencies/, + loader: 'string-replace-loader', + options: { + search: 'require[(]([^\'"])', + replace: '__non_webpack_require__($1', + flags: 'g', + }, + }, + ...config.module?.rules, + ], + }, + }; }); diff --git a/libs/shared/npm/src/lib/workspace-dependencies.ts b/libs/shared/npm/src/lib/workspace-dependencies.ts index e6847d2fe6..93830bb130 100644 --- a/libs/shared/npm/src/lib/workspace-dependencies.ts +++ b/libs/shared/npm/src/lib/workspace-dependencies.ts @@ -2,7 +2,6 @@ import { gte, NxVersion } from '@nx-console/nx-version'; import { directoryExists } from '@nx-console/shared-file-system'; import type { Logger } from '@nx-console/shared-utils'; import { stat } from 'fs/promises'; -import Module from 'module'; import type { ProjectGraphProjectNode } from 'nx/src/devkit-exports'; import { platform } from 'os'; import { join } from 'path'; @@ -81,16 +80,13 @@ export async function workspaceDependencyPath( export function importWorkspaceDependency( importPath: string, logger?: Logger, - workspacePath: string = __dirname, ): Promise { if (platform() === 'win32') { importPath = importPath.replace(/\\/g, '/'); } - const workspaceRequire = Module.createRequire(workspacePath); - // eslint-disable-next-line @typescript-eslint/no-var-requires - const imported = workspaceRequire(importPath); + const imported = require(importPath); logger?.log(`Using local Nx package at ${importPath}`); diff --git a/package.json b/package.json index d169c106b2..3fecb3dcf4 100644 --- a/package.json +++ b/package.json @@ -124,6 +124,7 @@ "prettier-plugin-tailwindcss": "^0.6.11", "rimraf": "^3.0.2", "storybook": "9.0.9", + "string-replace-loader": "^3.2.0", "tailwindcss": "^3.3.2", "ts-jest": "29.4.0", "ts-node": "10.9.1", diff --git a/yarn.lock b/yarn.lock index e9f454d07c..97f7e59f0c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16645,6 +16645,7 @@ __metadata: rxjs: 7.5.6 semver: ^7.7.1 storybook: 9.0.9 + string-replace-loader: ^3.2.0 tailwindcss: ^3.3.2 tar-stream: ^3.1.7 ts-jest: 29.4.0 @@ -19439,7 +19440,7 @@ __metadata: languageName: node linkType: hard -"schema-utils@npm:^4.0.0, schema-utils@npm:^4.0.1, schema-utils@npm:^4.2.0, schema-utils@npm:^4.3.0, schema-utils@npm:^4.3.2": +"schema-utils@npm:^4, schema-utils@npm:^4.0.0, schema-utils@npm:^4.0.1, schema-utils@npm:^4.2.0, schema-utils@npm:^4.3.0, schema-utils@npm:^4.3.2": version: 4.3.2 resolution: "schema-utils@npm:4.3.2" dependencies: @@ -20255,6 +20256,17 @@ __metadata: languageName: node linkType: hard +"string-replace-loader@npm:^3.2.0": + version: 3.2.0 + resolution: "string-replace-loader@npm:3.2.0" + dependencies: + schema-utils: ^4 + peerDependencies: + webpack: ^5 + checksum: da0fe6ad577465840986e56c5af1a357532b606d76e2bc93d560be99fe88b06b6d0b91857702e1c9ab31d15fe66c5ce266469fca24cd1cba3f3236348fea5824 + languageName: node + linkType: hard + "string-width-cjs@npm:string-width@^4.2.0, string-width@npm:^1.0.2 || 2 || 3 || 4, string-width@npm:^4.1.0, string-width@npm:^4.2.0, string-width@npm:^4.2.3": version: 4.2.3 resolution: "string-width@npm:4.2.3" From 970f8e2c1438d51197b0e1a61c244249c8a8c78b Mon Sep 17 00:00:00 2001 From: Emily M Klassen <760204+forivall@users.noreply.github.com> Date: Fri, 26 Sep 2025 18:50:59 -0700 Subject: [PATCH 4/5] fix(nxls): also allow pnp api require in webpack build --- apps/nxls/webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/nxls/webpack.config.js b/apps/nxls/webpack.config.js index 3ebd5df16b..ae6a21386f 100644 --- a/apps/nxls/webpack.config.js +++ b/apps/nxls/webpack.config.js @@ -10,7 +10,7 @@ module.exports = composePlugins(withNx(), (config) => { ...config.module, rules: [ { - test: /libs\/shared\/npm\/src\/lib\/workspace-dependencies/, + test: /libs\/shared\/npm\/src\/lib\/[^/]+-dependencies/, loader: 'string-replace-loader', options: { search: 'require[(]([^\'"])', From 99cb9d4a4319bc2f04434aee36fb1d3e1f52fa29 Mon Sep 17 00:00:00 2001 From: Emily M Klassen <760204+forivall@users.noreply.github.com> Date: Fri, 26 Sep 2025 18:52:15 -0700 Subject: [PATCH 5/5] build(nxls): add debug configuration for nxls package target --- apps/nxls/project.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/nxls/project.json b/apps/nxls/project.json index a45891ffc4..13297d76f9 100644 --- a/apps/nxls/project.json +++ b/apps/nxls/project.json @@ -24,6 +24,12 @@ "optimization": true, "extractLicenses": true, "webpackConfig": "apps/nxls/webpack.config.js" + }, + "configurations": { + "debug": { + "sourceMap": true, + "optimization": false + } } }, "build": {