idahogurl / vs-code-prettier-eslint

A Visual Studio Code Extension to format JavaScript and TypeScript code using the prettier-eslint package.
MIT License
190 stars 47 forks source link

Error: Cannot find module 'eslint' when running in a workspace that uses Yarn Berry without the nodeLinker: node-modules. #180

Closed emadzz closed 11 months ago

emadzz commented 1 year ago

Describe the bug When trying to format a file with Prettier ESLint in a worksapce that uses Yarn Berry without the nodeLinker: node-modules, the extension fails with the following error trace:

Error: Cannot find module 'eslint'
Require stack:
- /workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js
- /ide/out/vs/loader.js
- /ide/out/bootstrap-amd.js
- /ide/out/bootstrap-fork.js 
Error: Cannot find module 'eslint'
Require stack:
- /workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js
- /ide/out/vs/loader.js
- /ide/out/bootstrap-amd.js
- /ide/out/bootstrap-fork.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Function.resolve (node:internal/modules/cjs/helpers:108:19)
    at j4 (/workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js:209:36)
    at zme (/workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js:185:599)
    at vy (/workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js:209:3779)
    at /workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js:209:6494
    at Generator.next (<anonymous>)
    at /workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js:1:734
    at new Promise (<anonymous>)
    at _y (/workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js:1:554)
    at wge (/workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js:209:6054)
    at Object.provideDocumentRangeFormattingEdits (/workspace/.vscode-remote/extensions/rvest.vs-code-prettier-eslint-5.1.0-universal/dist/extension.js:210:74)
    at J.provideDocumentRangeFormattingEdits (/ide/out/vs/workbench/api/node/extensionHostProcess.js:101:45314)
    at /ide/out/vs/workbench/api/node/extensionHostProcess.js:101:68425
    at se.s (/ide/out/vs/workbench/api/node/extensionHostProcess.js:101:62837)
    at se.$provideDocumentRangeFormattingEdits (/ide/out/vs/workbench/api/node/extensionHostProcess.js:101:68412)
    at t.N (/ide/out/vs/workbench/api/node/extensionHostProcess.js:111:11199)
    at t.M (/ide/out/vs/workbench/api/node/extensionHostProcess.js:111:10965)
    at t.H (/ide/out/vs/workbench/api/node/extensionHostProcess.js:111:10007)
    at t.G (/ide/out/vs/workbench/api/node/extensionHostProcess.js:111:9122)
    at i.value (/ide/out/vs/workbench/api/node/extensionHostProcess.js:111:7952)
    at w (/ide/out/vs/workbench/api/node/extensionHostProcess.js:60:1902)
    at w.fire (/ide/out/vs/workbench/api/node/extensionHostProcess.js:60:2119)
    at fire (/ide/out/vs/workbench/api/node/extensionHostProcess.js:76:13931)
    at i.value (/ide/out/vs/workbench/api/node/extensionHostProcess.js:127:30340)
    at w (/ide/out/vs/workbench/api/node/extensionHostProcess.js:60:1902)
    at w.fire (/ide/out/vs/workbench/api/node/extensionHostProcess.js:60:2119)
    at fire (/ide/out/vs/workbench/api/node/extensionHostProcess.js:76:13931)
    at s.z (/ide/out/vs/workbench/api/node/extensionHostProcess.js:76:16976)
    at i.value (/ide/out/vs/workbench/api/node/extensionHostProcess.js:76:15428)
    at w (/ide/out/vs/workbench/api/node/extensionHostProcess.js:60:1902)
    at w.fire (/ide/out/vs/workbench/api/node/extensionHostProcess.js:60:2119)
    at i.acceptChunk (/ide/out/vs/workbench/api/node/extensionHostProcess.js:76:11705)
    at i.value (/ide/out/vs/workbench/api/node/extensionHostProcess.js:76:10992)
    at w (/ide/out/vs/workbench/api/node/extensionHostProcess.js:60:1902)
    at w.fire (/ide/out/vs/workbench/api/node/extensionHostProcess.js:60:2119)
    at w.u (/ide/out/vs/workbench/api/node/extensionHostProcess.js:127:19064)

To Reproduce Steps to reproduce the behavior: (it doesn't have to be nestjs, but it already has eslint and prettier setup)

  1. npm i -g @nestjs/cli
  2. nest new my-project
  3. code my-project
  4. Having installed Prettier ESLint, it should currently be working fine if, for example, you remove a semicolon from the end of a line, then you run format file (Option + Shift + F on macOS)
  5. Now start using the latest Yarn Berry version yarn set version berry
  6. yarn
  7. Install the VS Code sdks yarn dlx @yarnpkg/sdks vscode.
  8. (optional) Change the TS version in VS Code to use the workspace TS version.
  9. (Everything should be working as expected so far)
    1. Remove nodeLinker: node-modules from .yarnrc.yml
    2. Run yarn
    3. Repeating step 4, results in the error mentioned in this issue from Prettier ESLint.

Expected behavior The extension is able to locate the eslint module in Yarn Berry projects that don't use the node-modules linker. And the file is formatted successfully.

Example Project This behaviour can be replicated using a new nestjs proejct that can be setup as described in the To Reproduce section.

Screenshots N/A

Versions (please complete the following information):

System Specifications (please complete the following information):

Additional context ESLint alone works fine at raising errors/warns of non-confirming code.

zhangzhuang15 commented 1 year ago

Information is not enough, please provide your eslint config file and the whole project structure.By the way, you'd better install eslint and prettier locally.

ThaJay commented 1 year ago

I have the same error but I'm not using yarn or Yarn Berry. I do not use global node modules either. I do not get why it is not searching the local project folder like I would expect.

No multi root or multi repo weirdness. Just a normal react-native project with config in the root and an index.js file that points to tsx files.

Error message:

Error: Cannot find module 'eslint'
Require stack:
- c:\Users\SKIKK\.vscode\extensions\rvest.vs-code-prettier-eslint-5.1.0\dist\extension.js
- c:\Users\SKIKK\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\loader.js
- c:\Users\SKIKK\AppData\Local\Programs\Microsoft VS Code\resources\app\out\bootstrap-amd.js
- c:\Users\SKIKK\AppData\Local\Programs\Microsoft VS Code\resources\app\out\bootstrap-fork.js
-  
Error: Cannot find module 'eslint'
Require stack:
- c:\Users\SKIKK\.vscode\extensions\rvest.vs-code-prettier-eslint-5.1.0\dist\extension.js
- c:\Users\SKIKK\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\loader.js
- c:\Users\SKIKK\AppData\Local\Programs\Microsoft VS Code\resources\app\out\bootstrap-amd.js
- c:\Users\SKIKK\AppData\Local\Programs\Microsoft VS Code\resources\app\out\bootstrap-fork.js
- 
    at Module._resolveFilename (node:internal/modules/cjs/loader:1129:15)
    at Function.s._resolveFilename (node:electron/js2c/utility_init:2:2786)
    at Function.g.resolve (c:\Users\SKIKK\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\loader.js:4:709)
    at j4 (c:\Users\SKIKK\.vscode\extensions\rvest.vs-code-prettier-eslint-5.1.0\dist\extension.js:209:36)
    at zme (c:\Users\SKIKK\.vscode\extensions\rvest.vs-code-prettier-eslint-5.1.0\dist\extension.js:185:599)
    at vy (c:\Users\SKIKK\.vscode\extensions\rvest.vs-code-prettier-eslint-5.1.0\dist\extension.js:209:3779)
    at c:\Users\SKIKK\.vscode\extensions\rvest.vs-code-prettier-eslint-5.1.0\dist\extension.js:209:6494
    at Generator.next (<anonymous>)
    at c:\Users\SKIKK\.vscode\extensions\rvest.vs-code-prettier-eslint-5.1.0\dist\extension.js:1:796
    at new Promise (<anonymous>)
    at _y (c:\Users\SKIKK\.vscode\extensions\rvest.vs-code-prettier-eslint-5.1.0\dist\extension.js:1:616)
    at wge (c:\Users\SKIKK\.vscode\extensions\rv (...)

.eslintrc.js:

module.exports = {
  root: true,
  extends: ['prettier', '@react-native-community'],
  plugins: ['import', 'simple-import-sort', '@react-native-community'],
  settings: {
    'import/parsers': { '@typescript-eslint/parser': ['.ts', '.tsx'] },
    'import/resolver': { typescript: { alwaysTryTypes: true } },
  },
  rules: {
    'semi': ['error', 'never'],
    'curly': ['error', 'multi-line'],
    'linebreak-style': ['error', 'unix'],
    '@typescript-eslint/no-unused-vars': 'error',
    'import/first': 'error',
    'import/newline-after-import': 'error',
    'import/no-duplicates': 'error',
    'import/no-unresolved': 'error',
    'react/jsx-one-expression-per-line': ['error', { allow: 'single-child' }],
    'simple-import-sort/exports': 'error',
    'simple-import-sort/imports': 'error',
  },
}
zhangzhuang15 commented 1 year ago

 你好 ,您发给我的邮件我已经收到。

github-actions[bot] commented 11 months ago

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] commented 11 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.

zhangzhuang15 commented 11 months ago

 你好 ,您发给我的邮件我已经收到。

davidfarinha commented 10 months ago

Can confirm this is an issue with this extension. Tried installing the yarn sdk's for VSCode and installing eslint/prettier globally but it didn't work sadly

image
zhangzhuang15 commented 10 months ago

Yes, it's not related with yarn sdk or Yarn Berry.

I follow the steps @emadzz and reproduce it. To tell the truth, it's not caused by this Project. It's "prettier-eslint". In "prettier-eslint", it uses require.resolve to search a module path, unfortunately , it doesn't search module under <user_workspace>/node_modules as expected, this is why we meet "cannot find module XXX".

So, I try my best to fix it. To make everything simple, you can visit my release page, download the .vsix file, install the plugin from this file. I hope my work could help you. @ThaJay @davidfarinha

By the way, you have to pay attention to the version of some dependencies: