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

Extension can't use plugin object (CLI can; Extension can use plugin's path/name string)

Open tomchen opened this issue 8 months ago • 7 comments
trafficstars

Summary

Extension shows error while CLI is good with a local custom plugin.

Edit: it seems Extension can't use a imported plugin configuration object (but CLI can). Extension can only use plugin's path/name string

Steps To Reproduce:

  • New folder, npm init -y, latest VS Code, install latest Prettier extension and set it as default JS formatter, prettier not installed globally with npm
  • npm install -D prettier
  • Add ./prettier.config.mjs:
import customPlugin from "./prettier-plugin-custom.mjs";

export default {
  plugins: [customPlugin],
};
  • Add ./prettier-plugin-custom.mjs:
import { parsers as parsersBabel } from "prettier/plugins/babel";

export default {
  parsers: {
    babel: parsersBabel.babel,
  },
};
  • npx prettier --write prettier-plugin-custom.mjs -> No problem
  • Shift + Alt + F to format prettier-plugin-custom.mjs -> Error, log simply shows Invalid prettier configuration file detected. See log for details.

Additional information

VS Code Version:

Latest

Version: 1.98.0 (user setup)
Commit: 6609ac3d66f4eade5cf376d1cb76f13985724bcb
Date: 2025-03-04T21:06:18.612Z
Electron: 34.2.0
ElectronBuildId: 11044223
Chromium: 132.0.6834.196
Node.js: 20.18.2
V8: 13.2.152.36-electron.0
OS: Windows_NT x64 10.0.26100

Prettier Extension Version: Latest 11.0.0

OS and version: Windows 11

Prettier Log Output

Simply

Invalid prettier configuration file detected. See log for details.

I don't see any details

Further attempts

I changed Node.js from v22.13.1 to v20.18.2 with nvm to align with VS Code Electron's Node version to see if it is caused by that, but CLI is still good.

I changed the files to Common JS:

prettier.config.cjs

const customPlugin = require("./prettier-plugin-custom.cjs");

module.exports = {
  plugins: [customPlugin],
};

prettier-plugin-custom.cjs

const { parsers: parsersBabel } = require("prettier/parser-babel");

module.exports = {
  parsers: {
    babel: parsersBabel.babel,
  },
};

Still CLI good extension bad

tomchen avatar Mar 07 '25 07:03 tomchen

Invalid prettier configuration file detected. See log for details.

This is the most frustrating thing about this issue - there is no log that shows anything other than this message. Combined with the fact that it works fine on the command line it makes this completely impossible to debug.

SystemParadox avatar Mar 20 '25 17:03 SystemParadox

Help -> Toggle developer tools in vscode may be helpful. This at least lets me see console.log statements in the prettier file. In my case the prettierrc is loading fine, so I think it's something in prettier or this plugin that then throws for some reason?

Changing to use plugins: ['prettier-plugin-svelte'] rather than plugins: [ require('prettier-plugin-svelte') ] seems to fix it which is odd. Does prettier not allow loading plugins like this anymore? But then why does it work on the CLI?

SystemParadox avatar Mar 21 '25 11:03 SystemParadox

It appears related to https://github.com/prettier/prettier/discussions/15167 so potentially something to do with RPC boundaries and the fact that required modules cannot be sent to the appropriate worker thread.

I was able to workaround it with:

import url from 'node:url';

export default {
  ...,
  plugins: [
    url.fileURLToPath(import.meta.resolve('prettier-plugin-packagejson')),
  ]
}

But the simple string works too if it's in the root node_modules (I'm using a monorepo approach with PNPM which does not hoist plugins by default anymore)

kingston avatar Mar 21 '25 14:03 kingston

We're also using pnpm but we've added public-hoist-pattern[]='prettier-plugin-svelte' to .npmrc to hoist it.

The fileURLToPath example is useful though, I might try that instead.

SystemParadox avatar Mar 21 '25 14:03 SystemParadox

OK. Updated the title and summary

tomchen avatar Mar 21 '25 20:03 tomchen

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

github-actions[bot] avatar May 21 '25 02:05 github-actions[bot]

Even if this isn't something that prettier-vscode can fix, it does at least need documenting.

SystemParadox avatar May 21 '25 07:05 SystemParadox