-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[Bug] Prettier is unable to load plugins in PNP environment #1903
Comments
* feat(pnpify): Optionally load PnPify in SDKs Using `--compat prettier,eslint`, the user can have the PnPify filesystem virtualization enabled in an SDK wrapper. This will allow users to get around packages (like `prettier`) that heavily rely on a `node_modules` file system layout to auto-load their plugins. Fixes #1903 * fix: Use correct property name for argument * fix: Don't import `dirname` if it's not needed * Use compat instead of usePnpify
* fix(pnpify): Load prettier with in-memory node_modules enabled (#1905) * feat(pnpify): Optionally load PnPify in SDKs Using `--compat prettier,eslint`, the user can have the PnPify filesystem virtualization enabled in an SDK wrapper. This will allow users to get around packages (like `prettier`) that heavily rely on a `node_modules` file system layout to auto-load their plugins. Fixes #1903 * fix: Use correct property name for argument * fix: Don't import `dirname` if it's not needed * Use compat instead of usePnpify * Enables in-memory node_modules for prettier Co-authored-by: Oliver Salzburg <oliver.salzburg@gmail.com>
The merge was done in a branch (yarnpkg:larixer/prettier-pnpify) which does not exist anymore and was not done in master. When will this be present in |
@asfernandes After discussion we decided to enable pnpify runtime on a per-tool basis and not shift the decision to the users, because they don't know the tradeoffs involved. For |
Hi folks! A Yarn 2 experimenter here 😁 It seems that I've ran |
It hasn't been released yet, but you can apply it manually by editing #!/usr/bin/env node
const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {resolve, dirname} = require(`path`);
const relPnpApiPath = "../../../.pnp.js";
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require prettier/index.js
require(absPnpApiPath).setup();
}
const pnpifyResolution = require.resolve(`@yarnpkg/pnpify`, {paths: [dirname(absPnpApiPath)]});
if (typeof global[`__yarnpkg_sdk_is_using_pnpify__`] === `undefined`) {
Object.defineProperty(global, `__yarnpkg_sdk_is_using_pnpify__`, {configurable: true, value: true});
process.env.NODE_OPTIONS += ` -r ${pnpifyResolution}`;
// Apply PnPify to the current process
absRequire(pnpifyResolution).patchFs();
}
}
// Defer to the real prettier/index.js your application uses
module.exports = absRequire(`prettier/index.js`); |
Thanks for the snippet @merceyz! I might try it next weekend if the new version of pnpify plugin is not yet released. I’m not in the rush with It is possible that Prettier might support |
Turns out that |
Is there an ETA for |
@asfernandes Until it's released you can apply it manually, see #1903 (comment) |
Unofrtunately, prettier still does not work for me in vscode. See the logs:
The file |
You're missing |
I've it installed as described in the docs (as dependency - not devDependency)! My project uses workspaces and to be sure, I've installed it in the root and all sub packages. |
Please open a seperate issue with a reproduction |
This is what my |
Should we wait for new |
I'll see if we can release a new version of #!/usr/bin/env node
const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {resolve} = require(`path`);
const relPnpApiPath = "../../../.pnp.js";
const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require prettier/index.js
require(absPnpApiPath).setup();
}
let pnpifyResolution;
try {
pnpifyResolution = absRequire.resolve(`@yarnpkg/pnpify`);
} catch (err) {}
if (pnpifyResolution) {
if (typeof global[`__yarnpkg_sdk_is_using_pnpify__`] === `undefined`) {
Object.defineProperty(global, `__yarnpkg_sdk_is_using_pnpify__`, {configurable: true, value: true});
process.env.NODE_OPTIONS += ` -r ${pnpifyResolution}`;
// Apply PnPify to the current process
absRequire(pnpifyResolution).patchFs();
}
}
}
// Defer to the real prettier/index.js your application uses
module.exports = absRequire(`prettier/index.js`); |
nice, looks like |
It still does not work for me.
|
Waiting for release ! |
@valdestron Thanks for the notification! |
I've tried replacing |
I had this problem on VSCode, and after some debugging, figured out that when prettier is calling
(Please note the |
@Shayan-To's patch fixed this issue for me. Specifically, prettier in VSCode installed using local yarn could not find the plugin prettier-plugin-organize-imports, so crashed every time it started. Running |
Why is this issue closed? Prettier cannot load its plugins when run from VSCode in PNP environment. Is this problem tracked in another issue? @merceyz Who should I mention about this? |
A new issue (referencing the old one) would have been better than resurrecting a more-than-one-year-old closed issue 🙂 Nonetheless, it doesn't seem to be a Yarn issue to me - if there's an invalid That said, I find curious it'd work with node_modules if it was passing an invalid |
I'm no expert in any of this. I just added some From my experience, when I set Should I open an issue here? Or something else? |
Describe the bug
When using Prettier with plugins in a PNP environment, Prettier will not be able to find its plugins.
To Reproduce
We reproduced this on Discord with this repo:
https://github.com/oliversalzburg/prettier-plugin-pnp
Running
yarn prettier src/index.ts
will not remove the unused import, for which theprettier-plugin-organize-imports
was installed.Additional context
It was suggested that this is due to missing PNP functionality in the SDK wrapper. Enabling
usePnpif
in https://github.com/yarnpkg/berry/blob/master/packages/yarnpkg-pnpify/sources/generateSdk.ts#L119 should be a valid solution to the problem.This should probably be enabled through a CLI flag in
yarn pnpify
to allow an adjusted SDK wrapper to be generated.To actually fix the issue, I also need to supply the plugin search directory to Prettier manually. Otherwise it will search in a completely inapproriate location by default.
The text was updated successfully, but these errors were encountered: