prettier: Plugin autoload doesn't work for packages installed from GitHub and for Yarn PnP

Environments:

  • Prettier Version: 2.0.5
  • Running Prettier via: CLI
  • Runtime: Node.js v14
  • Operating System: Windows

Steps to reproduce:

yarn add prettier/prettier @prettier/plugin-pug

Expected behavior:

Can format pug file with npx prettier test.pug

Actual behavior:

The CLI can’t load pug plugin automatically


Problem should be here, when install from github, ./node_modules/prettier maybe has own node_modules dir, so the autoLoadDir will be ./node_modules/prettier, not expected ./, so we are looking for ./node_modules/prettier/node_modules/@prettier/plugin-* not ./node_modules/@prettier/plugin-* (where the @prettier/plugin-pug is installed).

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 28 (24 by maintainers)

Most upvoted comments

So far Prettier didn’t promise to support such plugin packs. They might get installed in such a way that there will be an inner node_modules too. Prettier won’t be able to find plugins installed to that inner node_modules. Only direct dependencies are guaranteed to end up in the top-level node_modules. That’s why I think plugin packs should behave like plugins and follow the naming scheme for plugins. Also I still think Prettier, unless it’s installed globally, should use package.json to discover installed plugins. Not instead of the current behavior but in addition to it.

I think we have a wrong logic here right now https://github.com/prettier/prettier/blob/master/src/common/load-plugins.js.

Plugin(s) should be added to package.json. For loading plugins we should use require. Ideally we should not glob node_modules. Just note - Yarn PnP, doesn’t have node_modules.

Eslint looking at a configuration to find out which plugins are installed. But we 0CJS, so some plugins can’t be listed in the configuration. package.json only one place to get list of plugins. But we should not forget package.json also may not exist, for example - Deno. In this case I think we should implement --plugin flag for CLI (like do Eslint).

My vision:

  • Load package.json to get list of plugins (they should be prettier-plugin-*/@prettier/plugin-*).
  • Using require to load them.
  • Implement --plugin option for non package.json environments and for projects which doesn’t have package.json.

Pros:

  • Simple logic.
  • Does not overload filesystem for searching plugins on each start.
  • Supports environments without package.json.
  • Yarn PnP works fine.

Cons:

  • We don’t need --plugin-search-dir, so we should remove it and it is breaking change. But we can implement it without breaking change.
  • Can be broken for some developers, because developer can have a package with prettier-plugin-name in dependencies, now we load a plugin, after this we can’t load. But we can keep globbing from node_modules to avoid breaking change.

These are my thoughts on this issue, I would like to hear other opinions.

Maybe we should take a look at Eslint logic. It is pretty well designed.

@kachkaev yeah i have, i actually made a bug report explicitly about the issue that the plugins get loaded but their file extensions dont get detected if load them using require() in the .prettierrc #13276

Ok so if I’m understanding correctly, the next major version of Prettier (3.0.0) will have the plugin autoload / automatic search feature removed, so that they will need to be specified always.

I like the idea to check listed plugins in package.json, but I don’t like only load these plugins.

If I want @prettier/plugin-pug, I prefer add it to my @fisker/prettier-config instead of adding to project package.json.

I prefer auto load

  1. plugins in package.json
  2. plugins in .prettierrc (No support yet)

@evilebottnawi It supports that. It accepts what require accepts.