Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin search on pnpm #9167

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog_unreleased/api/pr-9167.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### Fix plugins search with `pnpm` (#9167 by @fisker)

Previously, when install `prettier` and plugins with `pnpm`, the installed plugins can't load automatically.
6 changes: 4 additions & 2 deletions src/common/load-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const partition = require("lodash/partition");
const globby = require("globby");
const mem = require("mem");
const internalPlugins = require("../languages");
const thirdParty = require("./third-party");
const { findParentDir } = require("./third-party");
const resolve = require("./resolve");

const memoizedLoad = mem(load, { cacheKey: JSON.stringify });
Expand All @@ -27,7 +27,9 @@ function load(plugins, pluginSearchDirs) {
}
// unless pluginSearchDirs are provided, auto-load plugins from node_modules that are parent to Prettier
if (!pluginSearchDirs.length) {
const autoLoadDir = thirdParty.findParentDir(__dirname, "node_modules");
const autoLoadDir =
findParentDir(__dirname, "node_modules/.pnpm") ||
findParentDir(__dirname, "node_modules");
Comment on lines +30 to +32
Copy link

@mdurling mdurling Feb 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this will work.node_modules/.pnpm is the default pnpm virtual store directory, but can be overridden by the virtual-store-dir setting in .npmrc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the information, any suggestion?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. It seems the default path (or whatever is specified in pluginSearchDirs) is being resolved relative to process.cwd() which sits inside the pnpm virtual store. Packages in the virtual store aren't stored hierarchically so the concept of findParentDir does not really work. With pnpm, the package hierarchy is captured in the symlink structure, so the plugin searching will need to use the symlink (not the target) somehow. The only way I have gotten plugins to load is by specifying them in .prettierrc using a relative path, e.g. "plugins": ["./node_modules/prettier-plugin-packagejson"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also use eslint plugins which seem to work fine with pnpm ... Looking at the eslint docs/internals it seems they resolve plugins as if you were to require them from the config file.

Copy link

@aminya aminya Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you accept this simple change as is and defer fine-tuned configuration? Prettier doesn't work with PNPM

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prettier does work with pnpm, it just won’t auto load plugins. You can load them manually in the .prettierrc, for example: "plugins": ["./node_modules/prettier-plugin-packagejson"]

if (autoLoadDir) {
pluginSearchDirs = [autoLoadDir];
}
Expand Down