Description
Summary
Globally installed prettier with no workspace root package.json
fails to resolve correct prettier config. We're using Rush which has a specific way of handling prettier config, see https://rushjs.io/pages/maintainer/enabling_prettier/. Generally, this works fine, but for one of our users, they were unable to use this VSCode extension due to Invalid prettier configuration file detected.
errors everytime they saved the file. After debugging, we discovered that they had prettier installed globally and this extension was attempting to use that version instead of the workspace one. I believe this is happening because this function doesn't stop at the workspace root and continues to resolve up, despite the resolveGlobalModules
flag not being set.
Github Repository to Reproduce Issue
Since this is an interaction between global and local modules, will add steps below.
Steps To Reproduce:
(these will not replicate the exact same error, but I believe they indicate the same root cause)
- Install VSCode prettier extension.
- Ensure
resolve global modules
is unchecked in the prettier settings. - Create a
package.json
at~/package.json
. - Add the following to that file,
{"dependencies": {"prettier": "2.2.3"}}
. The file just needs to exist, the version doesn't matter. - Restart VSCode.
- Save a file.
Expected result
Continued to the use the workspace version for prettier.
Actual result
["INFO" - 11:30:15 AM] Formatting file:///Users/aramis.sennyey/Projects/my-project/.prettierrc.js
["INFO" - 11:30:15 AM] Attempted to determine module path from /Users/aramis.sennyey
["ERROR" - 11:30:15 AM] Failed to load module. If you have prettier or plugins referenced in package.json, ensure you have run `npm install`
["ERROR" - 11:30:15 AM] Cannot find module 'prettier' from '/Users/aramis.sennyey'
Additional information
Feel free to attach a screenshot.
VS Code Version:
Version: 1.91.1 (Universal)
Commit: f1e16e1e6214d7c44d078b1f0607b2388f29d729
Date: 2024-07-09T22:07:54.982Z
Electron: 29.4.0
ElectronBuildId: 9728852
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Darwin arm64 **23.5.0**
Prettier Extension Version: v10.4.0
OS and version: MacOS
Prettier Log Output
["INFO" - 11:30:15 AM] Formatting file:///Users/aramis.sennyey/Projects/my-project/.prettierrc.js
["INFO" - 11:30:15 AM] Attempted to determine module path from /Users/aramis.sennyey
["ERROR" - 11:30:15 AM] Failed to load module. If you have prettier or plugins referenced in package.json, ensure you have run `npm install`
["ERROR" - 11:30:15 AM] Cannot find module 'prettier' from '/Users/aramis.sennyey'
Error: Cannot find module 'prettier' from '/Users/aramis.sennyey'
at Function.e.exports [as sync] (/Users/aramis.sennyey/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:41190)
at t.ModuleResolver.findPkg (/Users/aramis.sennyey/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:8482)
at t.ModuleResolver.getPrettierInstance (/Users/aramis.sennyey/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:4633)
at t.ModuleResolver.getResolvedConfig (/Users/aramis.sennyey/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:7691)
at t.default.format (/Users/aramis.sennyey/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:14793)
at t.PrettierEditProvider.provideEdits (/Users/aramis.sennyey/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:12887)
at t.PrettierEditProvider.provideDocumentFormattingEdits (/Users/aramis.sennyey/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:9117)
at V.provideDocumentFormattingEdits (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:154:108134)
at /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:154:137096
at Pe.s (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:154:131471)
at Pe.$provideDocumentFormattingEdits (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:154:137083)
at S (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:151:5986)
at S.Q (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:151:5752)
at S.M (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:151:4739)
at S.L (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:151:3605)
at n.value (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:151:2297)
at r.B (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:83:737)
at r.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:83:954)
at o.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:108:14502)
at n.value (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:177:8639)
at r.B (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:83:737)
at r.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:83:954)
at o.fire (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:108:14502)
at MessagePortMain.<anonymous> (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:177:6765)
at MessagePortMain.emit (node:events:514:28)
at Object.MessagePortMain._internalPort.emit (node:electron/js2c/utility_init:2:2285)
["INFO" - 11:30:15 AM] Using config file at /Users/aramis.sennyey/Projects/myproject/.prettierrc.js