Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 2, 2025

Description

When users configure prettier.prettierPath to a binary executable (e.g., /Users/user/.nvm/versions/node/v18.19.0/bin/prettier or node_modules/.bin/prettier), the extension fails with this.prettierModule.resolveConfigFile is not a function because it attempts to load the binary as a module directory.

Solution

Auto-detect and resolve binary paths to their containing module directories:

Path Resolution Logic:

  • Detect file vs directory paths in getModuleFromPrettierPath
  • Resolve symlinks to real paths (handles node_modules/.bin/prettiernode_modules/prettier/bin/prettier.cjs)
  • Traverse up to find module directory based on bin or .bin parent directories
  • Verify module identity via package.json name check

Security:

  • Validate resolved paths contain node_modules or prettier to prevent symlink attacks
  • Use hasOwnProperty checks when parsing JSON to prevent prototype pollution

Error Handling:

  • Clear error messages when binary paths cannot be resolved
  • Graceful fallback to bundled prettier on resolution failure
  • Debug logging for troubleshooting path resolution issues

Backward Compatibility

Module directory paths continue to work unchanged. Binary path resolution is additive.

Related Issue

Fixes #3482

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • CI/Build changes

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the code style of this project (npm run lint passes)
  • I have run npm run prettier to format my code
  • I have added/updated tests that prove my fix or feature works
  • All new and existing tests pass (npm test)
  • I have updated the CHANGELOG.md with a summary of my changes
Original prompt

This section details on the original issue you should resolve

<issue_title>this.prettierModule.resolveConfigFile is not a function error when formatting</issue_title>
<issue_description>### Summary

Prettier files a error message when using 'format document with -> prettier'

Github Repository to Reproduce Issue

Steps To Reproduce:

Prettier files a error message when using 'format document with -> prettier'

Expected result

Describe what should have happened.

Actual result

Here's the log:

👇 when I restarted the editor

["INFO" - 11:47:03 AM] Extension Name: esbenp.prettier-vscode.
["INFO" - 11:47:03 AM] Extension Version: 10.4.0.
["ERROR" - 11:47:03 AM] Error handling text editor change
["ERROR" - 11:47:03 AM] Failed to load Prettier instance: /Users/user/.nvm/versions/node/v18.19.0/bin/prettier
Error: Failed to load Prettier instance: /Users/user/.nvm/versions/node/v18.19.0/bin/prettier
    at t.PrettierMainThreadInstance.import (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:18055)
    at t.ModuleResolver.getPrettierInstance (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:5771)
    at t.default.handleActiveTextEditorChanged (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:10967)
    at t.default.handleActiveTextEditorChangedSync (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:10569)
    at t.default.registerDisposables (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:13735)
    at /Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:83500

👇when focusing any editor window

["ERROR" - 11:47:17 AM] Error handling text editor change
["ERROR" - 11:47:17 AM] Invalid version
Error: Invalid version
    at t.isAboveV3 (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:24917)
    at t.default.getSelectors (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:11745)
    at t.default.handleActiveTextEditorChanged (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:11131)

👇when trying 'format document with prettier'

["INFO" - 11:48:20 AM] Formatting file:///Users/user/Projects/xxx.tsx
["ERROR" - 11:48:20 AM] Error resolving prettier configuration for /Users/user/Projects/xxx.tsx
["ERROR" - 11:48:20 AM] this.prettierModule.resolveConfigFile is not a function
TypeError: this.prettierModule.resolveConfigFile is not a function
    at t.PrettierMainThreadInstance.resolveConfigFile (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:18684)
    at t.ModuleResolver.resolveConfig (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:6856)
    at t.ModuleResolver.getResolvedConfig (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:7735)
    at t.default.format (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:14767)
    at t.PrettierEditProvider.provideEdits (/Users/user/.vscode/extensions/esbenp.prettier-vscode-10.4.0/dist/extension.js:1:12876)

Additional information

image

VS Code Version: Version: 1.85.2 (Universal)

Prettier Extension Version: 10.4.0

Prettier package version: 3.2.2

Other Libs:
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "5.1.3",

OS and version: Mac OS 13.6.3 (22G436)

Prettier Log Output

LOG GOES HERE. DO NOT USE A SCREENSHOT, COPY AND PASTE THE TEXT

</issue_description>

Comments on the Issue (you are @copilot in this section)

@ntotten We need to have better error handling for this. Ideally we'd detect if the path is valid and show a useful error.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits December 2, 2025 13:01
- Detect when prettierPath points to a file instead of directory
- Resolve binary/symlink paths to actual module directory
- Add better error messages for invalid paths
- Handle both node_modules/.bin and prettier/bin cases

Co-authored-by: ntotten <[email protected]>
- Add explicit try-catch blocks for JSON.parse calls
- Extract long error message to constant
- Add debug logging for parse errors

Co-authored-by: ntotten <[email protected]>
- Add isPrettierModule helper to reduce code duplication
- Improve error handling for file reading and JSON parsing
- Better separation of concerns

Co-authored-by: ntotten <[email protected]>
- Use hasOwnProperty check to prevent prototype pollution in JSON parsing
- Validate resolved paths contain node_modules or prettier
- Add additional logging for security-related validations

Co-authored-by: ntotten <[email protected]>
Copilot AI changed the title [WIP] Fix prettier instance loading error during document formatting Fix: Auto-resolve binary paths in prettier.prettierPath setting Dec 2, 2025
Copilot AI requested a review from ntotten December 2, 2025 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

this.prettierModule.resolveConfigFile is not a function error when formatting Markdown formula incorrectly formatted

2 participants