prettier / prettier-vscode

Visual Studio Code extension for Prettier
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
MIT License
5.04k stars 446 forks source link

Cannot resolve config if `prettierPath` is defined #3100

Closed leifmarcus closed 1 month ago

leifmarcus commented 9 months ago

Summary

When using the prettier.prettierPath setting in VS Code, the configuration file cannot be resolved and prettier will not format the current file when editor.formatOnSave is set to true.

Github Repository to Reproduce Issue

I created a git repository, where the Error can be reproduced: https://github.com/leifmarcus/prettier-bug-prettierPath

Steps To Reproduce:

  1. Clone the linked repository
  2. Install all recommended extensions in vs code.
  3. Open the Test.vue file (or the test.ts file).
  4. Save the file in VS Code
  5. See the error in the prettier console.

Expected result

Prettier should format the file without errors

Actual result

Error is thrown in the prettier console

Additional information

image

VS Code Version:

Version: 1.80.2
Commit: 2ccd690cbff1569e4a83d7c43d45101f817401dc
Date: 2023-07-27T20:57:59.134Z
Electron: 22.3.14
ElectronBuildId: 22695494
Chromium: 108.0.5359.215
Node.js: 16.17.1
V8: 10.8.168.25-electron.0
OS: Darwin arm64 22.5.0

Prettier Extension Version: v10.1.0

OS and version: Mac OSVentura 13.4.1 ©

Prettier Log Output

["INFO" - 11:32:56] Formatting file:///Users/leifmarcus/_coding/prettier-bug-prettierPath/Test.vue
["ERROR" - 11:32:56] Error resolving prettier configuration for /Users/leifmarcus/_coding/prettier-bug-prettierPath/Test.vue
["ERROR" - 11:32:56] Invalid host defined options
TypeError: Invalid host defined options
    at Object.<anonymous> (/Users/leifmarcus/_coding/prettier-bug-prettierPath/node_modules/prettier/index.cjs:600:23)
    at Module.u._compile (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/loader.js:4:1271)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1243:10)
    at Module.load (node:internal/modules/cjs/loader:1058:32)
    at Module._load (node:internal/modules/cjs/loader:893:12)
    at Function.f._load (node:electron/js2c/asar_bundle:2:13330)
    at Function.l._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:127:28070)
    at Function.p._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:127:25404)
    at Function.u._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:91:22191)
    at Module.require (node:internal/modules/cjs/loader:1082:19)
    at g (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/loader.js:4:647)
    at t.loadNodeModule (/Users/leifmarcus/.vscode/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:2829)
    at t.PrettierMainThreadInstance.import (/Users/leifmarcus/.vscode/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:17760)
    at t.ModuleResolver.getPrettierInstance (/Users/leifmarcus/.vscode/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:5728)
    at t.default.handleActiveTextEditorChanged (/Users/leifmarcus/.vscode/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:10771)
    at t.default.handleActiveTextEditorChangedSync (/Users/leifmarcus/.vscode/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:10373)
    at t.default.registerDisposables (/Users/leifmarcus/.vscode/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:13531)
    at /Users/leifmarcus/.vscode/extensions/esbenp.prettier-vscode-10.1.0/dist/extension.js:1:82528
nrutman commented 9 months ago

After days of trying to figure out why Prettier would not work properly, this was great. Thanks for finding this.

Any thoughts on when a fix will be available?

timotheeguerin commented 9 months ago

I solved this error by specifying the prettier commonjs entrypoint instead of the folder I had before.

In .vscode/settings.json i changed this

-"prettier.prettierPath": "./packages/compiler/node_modules/prettier",
+"prettier.prettierPath": "./packages/compiler/node_modules/prettier/index.cjs",

This feels like the extension should resolve it automatically but this seems to at least be a workaround for now.

qoo1476 commented 9 months ago

@timotheeguerin thank you saved my day! works well

brandonscript commented 9 months ago

@timotheeguerin thanks, for me (using 2.2.0 because 3.0 is broken it seems?) I had to make it

-"prettier.prettierPath": "./packages/compiler/node_modules/prettier",
+"prettier.prettierPath": "./packages/compiler/node_modules/prettier/index.js",
//                                                                         ^
vsiao commented 9 months ago

Maybe duplicate of https://github.com/prettier/prettier-vscode/issues/3059?

weberhofer commented 9 months ago

I'm experiencing the same problem in one of my projects. I could solve it by appending index.cjs to the path as described above. Strange thing is, that in another project the configuration works without that addition.

bamf2077 commented 9 months ago

I solved this error by specifying the prettier commonjs entrypoint instead of the folder I had before.

In .vscode/settings.json i changed this

-"prettier.prettierPath": "./packages/compiler/node_modules/prettier",
+"prettier.prettierPath": "./packages/compiler/node_modules/prettier/index.cjs",

This feels like the extension should resolve it automatically but this seems to at least be a workaround for now.

Is there any solution for Yarn with PnP? After run yarn dlx @yarnpkg/sdks vscode, my VSCode hangs forever with FormatOnSave.

"prettier.prettierPath": ".yarn/sdks/prettier/index.js"
20manas commented 9 months ago

I solved this error by specifying the prettier commonjs entrypoint instead of the folder I had before. In .vscode/settings.json i changed this

-"prettier.prettierPath": "./packages/compiler/node_modules/prettier",
+"prettier.prettierPath": "./packages/compiler/node_modules/prettier/index.cjs",

This feels like the extension should resolve it automatically but this seems to at least be a workaround for now.

Is there any solution for Yarn with PnP? After run yarn dlx @yarnpkg/sdks vscode, my VSCode hangs forever with FormatOnSave.

"prettier.prettierPath": ".yarn/sdks/prettier/index.js"

I downgraded prettier to version 2.x.x and then restarted VS Code. This fixed the hang issue for me.

todd-zeal commented 8 months ago

I solved this error by specifying the prettier commonjs entrypoint instead of the folder I had before. In .vscode/settings.json i changed this

-"prettier.prettierPath": "./packages/compiler/node_modules/prettier",
+"prettier.prettierPath": "./packages/compiler/node_modules/prettier/index.cjs",

This feels like the extension should resolve it automatically but this seems to at least be a workaround for now.

Is there any solution for Yarn with PnP? After run yarn dlx @yarnpkg/sdks vscode, my VSCode hangs forever with FormatOnSave.

"prettier.prettierPath": ".yarn/sdks/prettier/index.js"

Seems they fixed it in 3.0.3!

vycos-zen commented 8 months ago

I was investigating this issue as well, after upgrading to prettier 3.0.3 and adding

"prettier.prettierPath": ".yarn/sdks/prettier/index.js"

it started to work and the hanging for ever stopped.

I solved this error by specifying the prettier commonjs entrypoint instead of the folder I had before. In .vscode/settings.json i changed this

-"prettier.prettierPath": "./packages/compiler/node_modules/prettier",
+"prettier.prettierPath": "./packages/compiler/node_modules/prettier/index.cjs",

This feels like the extension should resolve it automatically but this seems to at least be a workaround for now.

Is there any solution for Yarn with PnP? After run yarn dlx @yarnpkg/sdks vscode, my VSCode hangs forever with FormatOnSave.

"prettier.prettierPath": ".yarn/sdks/prettier/index.js"

Seems they fixed it in 3.0.3!

ssbarnea commented 8 months ago

Any chance to get this fixed for good as it is very annoying to have prettier extension broken when prettier is current 3.0.3 installed globally.

trusktr commented 7 months ago

I don't have prettierPath configured, but I keep getting this error after a while. Then I restart, am good for a while, then it comes back again. I keep restarting VS Code each time to fix it for a while.

salgadopietrini commented 7 months ago

Thanks, that worked for me!

ExpliuM commented 6 months ago

Thanks, worked for me!

github-actions[bot] commented 2 months ago

This issue has been labeled as stale due to inactivity. Reply to keep this issue open.

CowDotDev commented 1 month ago

This is still an issue for me, on 3.2.5. Using prettier.config.js Restarting VS Code solves the issue... until it comes back, every time.