prettier-vscode icon indicating copy to clipboard operation
prettier-vscode copied to clipboard

Cannot resolve config if `prettierPath` is defined

Open leifmarcus opened this issue 1 year ago • 14 comments

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

leifmarcus avatar Aug 01 '23 09:08 leifmarcus

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?

nrutman avatar Aug 01 '23 16:08 nrutman

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.

timotheeguerin avatar Aug 01 '23 18:08 timotheeguerin

@timotheeguerin thank you saved my day! works well

qoo1476 avatar Aug 02 '23 07:08 qoo1476

@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",
//                                                                         ^

brandonscript avatar Aug 02 '23 17:08 brandonscript

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

vsiao avatar Aug 02 '23 18:08 vsiao

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.

weberhofer avatar Aug 03 '23 09:08 weberhofer

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"

bamf2077 avatar Aug 05 '23 11:08 bamf2077

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.

20manas avatar Aug 22 '23 11:08 20manas

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!

todd-zeal avatar Aug 29 '23 17:08 todd-zeal

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!

vycos-zen avatar Aug 29 '23 18:08 vycos-zen

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.

ssbarnea avatar Aug 31 '23 17:08 ssbarnea

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.

  • https://github.com/prettier/prettier-vscode/issues/3114

trusktr avatar Oct 16 '23 22:10 trusktr

Thanks, that worked for me!

salgadopietrini avatar Oct 18 '23 21:10 salgadopietrini

Thanks, worked for me!

ExpliuM avatar Nov 13 '23 08:11 ExpliuM

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

github-actions[bot] avatar Mar 14 '24 01:03 github-actions[bot]

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.

CowDotDev avatar Apr 17 '24 17:04 CowDotDev

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jul 17 '24 00:07 github-actions[bot]