nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.63k stars 2.36k forks source link

VS Code cannot find @nrwl/nx/workspace custom eslint rules without Nx Console extension installed #13187

Closed zjkipping closed 1 year ago

zjkipping commented 1 year ago

Current Behavior

VS Code shows error Definition for rule '@nrwl/nx/workspace/my-custom-rule' was not found.eslint(@nrwl/nx/workspace/my-custom-rule) when Nx Console extension isn't installed on the VS Code editor. This occurs when looking at a file the custom rule is applied to from the .eslintrc.json.

Expected Behavior

I would expect there to be an easy way to configure this if the Nx Console extension is the thing orchestrating the wiring of the custom workspace rules to the VS Code ESLint extension (or however it works).

Or just for this to work regardless of Nx Console being installed. Some of us in the community don't bother with it since we only really use the terminal CLI. It also isn't obvious that the reason the lookup of the rule fails is because of Nx Console extension not being installed.

Steps to Reproduce

I created a fresh nx15 workspace with a custom workspace linting rule generated. The README explains how to reproduce the error. https://github.com/zjkipping/nx-custom-workspace-eslint-rules

The gist of the repo is that without the Nx Console extension installed you get an error in VS Code while the eslint extension tries to lookup the custom workspace rule. If you install the Nx Console extension and restart the ESLint Server you won't get the error anymore.

Failure Logs

Environment

Node : 18.12.0 OS : win32 x64 npm : 8.19.2

nx : 15.1.0 @nrwl/angular : 15.1.0 @nrwl/cypress : 15.1.0 @nrwl/detox : Not Found @nrwl/devkit : 15.1.0 @nrwl/esbuild : Not Found @nrwl/eslint-plugin-nx : 15.1.0 @nrwl/expo : Not Found @nrwl/express : Not Found @nrwl/jest : 15.1.0 @nrwl/js : 15.1.0 @nrwl/linter : 15.1.0 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : Not Found @nrwl/nx-cloud : Not Found @nrwl/nx-plugin : Not Found @nrwl/react : Not Found @nrwl/react-native : Not Found @nrwl/rollup : Not Found @nrwl/schematics : Not Found @nrwl/storybook : 15.1.0 @nrwl/web : Not Found @nrwl/webpack : 15.1.0 @nrwl/workspace : 15.1.0 typescript : 4.8.4

Local workspace plugins:

Community plugins:

zjkipping commented 1 year ago

As a side note, I tried making use of the eslint options rulePaths in the VS Code .vscode/settings.json file following this stackoverflow without much luck.

zjkipping commented 1 year ago

@AgentEnder @meeroslav Any updates with this? Has it been picked up at all?

meeroslav commented 1 year ago

Unfortunately, I could not reproduce it.

Here is the scenario I tried (each try was with ESLint server restart + full VS Code reload):

This was on Mac. I will give it another go on Windows

meeroslav commented 1 year ago

Same results on Windows. It's (or was) likely an issue with your installation of packages or VS Code. Try following:

zjkipping commented 1 year ago

I will try this, but it doesn't make too much sense given I was able to reproduce in a separate repo with different machines. On my actual work project this is affecting all the developers (30+). We have had people wiping node_modules, recloning the repo, and re-installing vs code galore before we realized the extension was the successful solution.

meeroslav commented 1 year ago

Ok, I'll ask my colleagues working on Nx Console to investigate this further as I didn't manage to reproduce it.

meeroslav commented 1 year ago

@zjkipping Are all your colleagues using Windows machines and similar versions of Node/Npm?

zjkipping commented 1 year ago

@meeroslav Yes on the node versions, most folks are on Windows machines & a few on Mac. We are either on LTS or slightly below on the node Versions.

meeroslav commented 1 year ago

Can you try to disable/remove all the VS Code extensions and try again with/without Nx Console?

I'm suspecting something else is interfering and Nx Console's typescript program might be fixing that. So far tested your repo on 4 different machines (M1 Mac, Intel Mac, Ubuntu, and Windows) and they are all working fine.

meeroslav commented 1 year ago

As you can see, the Nx Console is not installed and there are no link errors in app.component.ts. My node version is 18.12.1 but I tried also with v16 and there were no differences.

Screenshot 2022-12-09 at 14 07 44

zjkipping commented 1 year ago

I will try using a clean vs-code instance Monday! Thanks for going through this so much @meeroslav, it's appreciated.

zjkipping commented 1 year ago

Having disabled and then re-enabled all my extensions it looks like I can't reproduce this anymore... I guess something went haywire with the eslint extension in tangent with another extension (or possibly not). Either way I'll close this issue. thanks for the help @meeroslav!

Hypenate commented 1 year ago

We're having the same issue with Webstorm. Not using the official plugins from NX, but the community ones.

meeroslav commented 1 year ago

@Hypenate

Unfortunately, we can't help you out with the community plugin-related issues. You would have to contact the original authors. Assuming you are referring to https://plugins.jetbrains.com/plugin/15000-nx-console-ui please contact @etkachev

That being said, there is now also an official IntelliJ plugin for Nx so you might try to see if the same issue appears there as well: https://plugins.jetbrains.com/plugin/21060-nx-console

GrumpyMeow commented 1 year ago

@meeroslav I've just upgraded my NX-workspace from v15.0.13 to v15.8.9 and also get errors on my CI-pipeline about that my custom eslint-rules are not found.

/__w/1/s/libs/shared/core-lib/src/lib/whitelabel/components/message/message.component.spec.ts(9,1): error @nrwl/nx/workspace/validate-component-template : Definition for rule '@nrwl/nx/workspace/validate-component-template' was not found.
/__w/1/s/libs/shared/core-lib/src/lib/whitelabel/components/message/message.component.ts(1,1): error @nrwl/nx/workspace/validate-core-lib-refs : Definition for rule '@nrwl/nx/workspace/validate-core-lib-refs' was not found.
meeroslav commented 1 year ago

It's hard to tell. If I would have to guess I'd first check the container.

Can you share the repo where this happens or can you provide a minimal repo where this is reproducible?

GrumpyMeow commented 1 year ago

I wish i could. But corporate policy makes this impossible for me. I can only provide log output :-S When i run: DEBUG=* nx lint myapp-app
I see in the output

...
 eslintrc:config-array-factory Loading plugin "@nrwl/nx" from /workspaces/MyWorkspace/.eslintrc.json +3ms
  eslintrc:config-array-factory Loaded: @nrwl/eslint-plugin-nx@15.8.9 (/workspaces/MyWorkspace/node_modules/@nrwl/eslint-plugin-nx/src/index.js) +2ms
  @swc-node Read config file from /workspaces/MyWorkspace/tools/eslint-rules/tsconfig.json +0ms
  eslintrc:config-array-factory Plugin /workspaces/MyWorkspace/node_modules/@nrwl/eslint-plugin-nx/src/index.js loaded in: 1406ms +1s
  eslintrc:config-array-factory Loading plugin "json" from /workspaces/MyWorkspace/.eslintrc.json +0ms
...

Out global .eslintrc file:

{
  "root": true,
  "ignorePatterns": ["**/*"],
  "extends": ["plugin:jasmine/recommended", "plugin:storybook/recommended", "prettier"],
  "plugins": ["@nrwl/nx", "json", "sonarjs", "jsdoc", "jasmine", "yaml", "deprecation"],
  "settings": {
    "json/sort-package-json": true,
    "json/json-with-comments-files": []
  },
  "overrides": [
    {
      // Custom linting rules and configuration
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "extends": ["plugin:jsdoc/recommended", "plugin:sonarjs/recommended"],
      "rules": {
        "no-unused-vars": "off",
...

In the global .eslintrc some rules using our custom eslint-rules:

...
{
      "files": ["project.json"],
      "rules": {
        "@nrwl/nx/workspace/validate-project-json": "error"
      }
    },
    {
      "files": ["test-setup.ts"],
      "rules": {
        "@nrwl/nx/workspace/validate-test-setup-ts": "error",
        "@typescript-eslint/no-unsafe-call": "off"
      }
    },
    {
      "files": ["*.component.html"],
      "rules": {
        "@nrwl/nx/workspace/validate-component-stories": "error",
        "@nrwl/nx/workspace/validate-component-template": "error"
      }
    },
...

nx.json:

"lint": {
      "inputs": ["default", "{workspaceRoot}/.eslintrc.json", "{workspaceRoot}/tools/eslint-rules/**/*"]
    },

Is there maybe a command which i could run to trigger a compile of my eslint-rules?

GrumpyMeow commented 1 year ago

I managed to get output of an error.. https://github.com/nrwl/nx/blob/0b977b751e6982a0d27cf1adb6eb10f34eb18d3b/packages/eslint-plugin-nx/src/resolve-workspace-rules.ts#L28

err Error: Bindings not found
    at Compiler.transformSync (/workspaces/AthoraNx/node_modules/@swc/core/index.js:250:15)
    at transformSync (/workspaces/AthoraNx/node_modules/@swc/core/index.js:348:21)
    at transformSync (/workspaces/AthoraNx/node_modules/@swc-node/core/index.ts:70:26)
    at compile (/workspaces/AthoraNx/node_modules/@swc-node/register/register.ts:51:40)
    at exts (/workspaces/AthoraNx/node_modules/@swc-node/register/register.ts:62:38)
    at Module._compile (/workspaces/AthoraNx/node_modules/pirates/lib/index.js:130:29)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.newLoader [as .ts] (/workspaces/AthoraNx/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)

I added some more logging here: https://github.com/swc-project/swc/blob/d5c0dec40c1bb46bb5674242aa0a85f51c0826f7/node-swc/src/index.ts#L30

and got:

catch: Error: Cannot find module '@swc/core-linux-x64-gnu'
Require stack:
- /workspaces/AthoraNx/node_modules/@swc/core/binding.js
- /workspaces/AthoraNx/node_modules/@swc/core/index.js
- /workspaces/AthoraNx/node_modules/@swc-node/core/lib/index.js
- /workspaces/AthoraNx/node_modules/@swc-node/register/lib/register.js
- /workspaces/AthoraNx/node_modules/nx/src/utils/register.js
- /workspaces/AthoraNx/node_modules/nx/src/utils/nx-plugin.js
- /workspaces/AthoraNx/node_modules/nx/src/config/workspaces.js
- /workspaces/AthoraNx/node_modules/nx/src/command-line/run.js
- /workspaces/AthoraNx/node_modules/nx/bin/run-executor.js

@meeroslav Does this provide some clues? In our devDependencies:

"@swc-node/register": "^1.5.5",
"@swc/core": "^1.3.31",

I resolved my issue with: npm install -D @swc/core@latest. This upgraded v1.3.31 to v1.3.42. [Update] Unfortunatly this only resolved the issue on my machine. This issue seems to be related: https://github.com/swc-project/swc/issues/1430 Also related: https://github.com/npm/cli/issues/4828

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.