prettier/prettier-vscode

Unexpectedly resolving globally installed prettier version

aramissennyeydd opened this issue · 2 comments

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)

  1. Install VSCode prettier extension.
  2. Ensure resolve global modules is unchecked in the prettier settings.
  3. Create a package.json at ~/package.json.
  4. Add the following to that file, {"dependencies": {"prettier": "2.2.3"}}. The file just needs to exist, the version doesn't matter.
  5. Restart VSCode.
  6. 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

This is definitely happening to me as well, I have "devDependencies": { "prettier": "3.3.3" } in my package.json, but in prettier plugin log, I see:

PrettierInstance:
{
  "modulePath": "<project path>/node_modules/prettier/index.cjs", // reference to locally installed prettier v3.3.3
  "messageResolvers": {},
  "version": "3.2.4" // I thought that this is a globally installed version, but it is not, and it is not the version of above prettier module?
}

EDIT: It's even weirder, according to the below script, I have no 3.2.4 prettier installed. Now I am very confused.

find ${HOME} -type d -name "node_modules" -maxdepth 5 -exec sh -c '
    find "$1" -maxdepth 1 -type d -name "prettier" -print0 |
    while IFS= read -r -d "" dir; do
        echo "$dir"
        if [ -f "$dir/package.json" ]; then
            jq -r ".version // \"Version not found\"" "$dir/package.json"
        else
            echo "No package.json found"
        fi
        echo
    done
' sh {} \;

Hmm, I had Prettier Monkey C extension installed. I tried completely uninstalling prettier plugin from VS code and installing it again, and as Prettier Monkey C is dependent on Prettier, I had to uninstall it too.

Removing both and installing back just Prettier (11.0.0 0 just like before the uninstall) seems to fix it - in the logs I now see 3.3.3 used.

Absolutely not sure what was happening, as the monkey c plugin was using different version:

.vscode/extensions/markw65.prettier-extension-monkeyc-2.0.103/node_modules/prettier
2.8.7