prettier-vscode: Cannot resolve config if `prettierPath` is defined

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

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 15
  • Comments: 16 (1 by maintainers)

Most upvoted comments

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.

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"

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

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.

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!

@timotheeguerin thank you saved my day! works well

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?

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.