yarnpkg / berry

šŸ“¦šŸˆ Active development trunk for Yarn āš’
https://yarnpkg.com
BSD 2-Clause "Simplified" License
7.25k stars 1.08k forks source link

[Bug] Prettier is unable to load plugins in PNP environment #1903

Closed oliversalzburg closed 3 years ago

oliversalzburg commented 3 years ago

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 the prettier-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.

asfernandes commented 3 years ago

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 @yarnpkg/pnpify?

larixer commented 3 years ago

@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 prettier we will always enable pnpify runtime starting from the next @yarnpkg/pnpify version: https://github.com/yarnpkg/berry/pull/1913

kachkaev commented 3 years ago

Hi folks! A Yarn 2 experimenter here šŸ˜

It seems that I've ran @yarnpkg/pnpify with the discussed change (v 2.2.3), but my plugins still have not loaded. Any ideas what I should check? Here is the PR: https://github.com/kachkaev/njt/pull/29

merceyz commented 3 years ago

It hasn't been released yet, but you can apply it manually by editing .yarn\sdks\prettier\index.js and changing it to

#!/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`);
kachkaev commented 3 years ago

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 config ā†’ plugins ā†’ require.resolve at some point. See https://github.com/prettier/prettier/issues/7073#issuecomment-711420129. Iā€™m still new to Yarn 2, so happy to be corrected if my suggestion does not lead to a working solution. The end goal is to enable this chain: prettier ā†’ @my-company/prettier-config ā†’ some-prettier-plugin. Installing all plugins into the project is quite inconvenient ā€“ Iā€™d like the config package to bring them on.

kachkaev commented 3 years ago

Turns out that config ā†’ plugins ā†’ require("some-plugin") is already supported by Prettier! This means that autodiscovery is not the only mechanism devs can rely on šŸŽ‰ Here is a short discussion: https://github.com/prettier/prettier/issues/7073#issuecomment-711954542

asfernandes commented 3 years ago

@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 prettier we will always enable pnpify runtime starting from the next @yarnpkg/pnpify version:

1913

Is there an ETA for @yarnpkg/pnpify next version including this?

merceyz commented 3 years ago

@asfernandes Until it's released you can apply it manually, see https://github.com/yarnpkg/berry/issues/1903#issuecomment-711432671

asfernandes commented 3 years ago

@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:

Error: Cannot find module '@yarnpkg/pnpify'
Require stack:
- /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/internal.js
Require stack:
- /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js
- /home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js
- /snap/code/48/usr/share/code/resources/app/out/vs/loader.js
- /snap/code/48/usr/share/code/resources/app/out/bootstrap-amd.js
- /snap/code/48/usr/share/code/resources/app/out/bootstrap-fork.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:844:17)
    at Function.external_module_.Module._resolveFilename (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:12954:46)
    at callNativeResolution (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13363:36)
    at resolveToUnqualified (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13569:24)
    at resolveRequest (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13821:29)
    at Object.resolveRequest (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13899:26)
    at Function.external_module_.Module._resolveFilename (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13003:34)
    at Function.r.resolve (/snap/code/48/usr/share/code/resources/app/out/vs/loader.js:17:406)
    at Object.<anonymous> (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js:18:36)
    at Module.i._compile (/snap/code/48/usr/share/code/resources/app/out/vs/loader.js:17:571)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1051:10)
    at Module.load (internal/modules/cjs/loader.js:862:32)
    at Module._load (internal/modules/cjs/loader.js:774:14)
    at Module._load (electron/js2c/asar.js:769:28)
    at t._load (/snap/code/48/usr/share/code/resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:1057:776)
    at i._load (/snap/code/48/usr/share/code/resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:1027:486)
    at n._load (/snap/code/48/usr/share/code/resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:1023:767)
    at Function.external_module_.Module._load (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:12862:36)
    at Module.require (internal/modules/cjs/loader.js:899:19)
    at r (/snap/code/48/usr/share/code/resources/app/out/vs/loader.js:17:346)
    at t.ModuleResolver.loadNodeModule (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js:1:30610)
    at t.ModuleResolver.requireLocalPkg (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js:1:29738)
    at t.ModuleResolver.getPrettierInstance (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js:1:28424)
    at t.LanguageResolver.getSupportLanguages (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js:1:26531)
    at t.LanguageResolver.getSupportedFileExtensions (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js:1:26367)
    at t.default.selectors (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js:1:56123)
    at t.default.registerFormatter (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js:1:55092)
    at t.activate (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.7.1/dist/extension.js:1:22370)
    at Function._callActivateOptional (/snap/code/48/usr/share/code/resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:927:841)
    at Function._callActivate (/snap/code/48/usr/share/code/resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:927:492)
    at /snap/code/48/usr/share/code/resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:925:841
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async Promise.all (index 0)

The file /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/internal.js does not exist.

merceyz commented 3 years ago

You're missing @yarnpkg/pnpify as a dependency

asfernandes commented 3 years ago

You're missing @yarnpkg/pnpify as a dependency

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.

merceyz commented 3 years ago

Please open a seperate issue with a reproduction

derekschinke commented 3 years ago

It hasn't been released yet, but you can apply it manually by editing .yarn\sdks\prettier\index.js and changing it to

#!/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`);

This is what my .yarn\sdks\prettier\index.js already looks like on the current version. I have @yarnpkg/pnpify installed as a dependency too.

asfernandes commented 3 years ago

Should we wait for new @yarnpkg/pnpify version or can you post a manual .yarn/sdks/prettier/index.js version so we can test?

merceyz commented 3 years ago

I'll see if we can release a new version of @yarnpkg/pnpify, in the meantime here is the updated version

#!/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`);
derekschinke commented 3 years ago

nice, looks like @yarnpkg/pnpify doesn't have to be installed when using this .yarn/sdks/prettier/index.js

asfernandes commented 3 years ago

I'll see if we can release a new version of @yarnpkg/pnpify, in the meantime here is the updated version

#!/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`);

It still does not work for me.

["INFO" - 2:50:12 PM] Extension Name: esbenp.prettier-vscode.
["INFO" - 2:50:12 PM] Extension Version: 5.8.0.
["INFO" - 2:50:13 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:50:13 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:50:13 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:50:13 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:50:13 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:50:13 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:50:13 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:50:13 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:50:13 PM] Enabling prettier for languages: ansible, css, graphql, home-assistant, html, javascript, javascriptreact, json, json5, jsonc, less, markdown, mdx, mongo, postcss, scss, typescript, typescriptreact, vue, yaml
["INFO" - 2:50:13 PM] Enabling prettier for file extensions: .JSON-tmLanguage, ._js, .avsc, .bones, .cjs, .component.html, .css, .es, .es6, .frag, .geojson, .gltf, .gql, .graphql, .graphqls, .gs, .har, .htm, .html, .html.hl, .ice, .inc, .jake, .js, .js.flow, .jsb, .jscad, .jsfl, .jsm, .json, .json5, .jsonc, .jss, .jsx, .less, .markdown, .mcmeta, .md, .mdown, .mdwn, .mdx, .mir, .mjml, .mjs, .mkd, .mkdn, .mkdown, .njs, .pac, .pcss, .postcss, .reek, .ronn, .rviz, .scss, .sjs, .ssjs, .st, .sublime-build, .sublime-commands, .sublime-completions, .sublime-keymap, .sublime-macro, .sublime-menu, .sublime-mousemap, .sublime-project, .sublime-settings, .sublime-syntax, .sublime-theme, .sublime-workspace, .sublime_metrics, .sublime_session, .syntax, .tfstate, .tfstate.backup, .topojson, .ts, .tsx, .vue, .webapp, .webmanifest, .workbook, .wxs, .wxss, .xht, .xhtml, .xsjs, .xsjslib, .yaml, .yaml-tmlanguage, .yaml.sed, .yml, .yml.mysql, .yy, .yyp
["INFO" - 2:50:13 PM] Enabling prettier for range supported languages: graphql, javascript, javascriptreact, json, typescript, typescriptreact
["INFO" - 2:57:11 PM] Formatting /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/packages/node-firebird-driver/src/test/tests.ts
["INFO" - 2:57:11 PM] Using ignore file (if present) at /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/packages/node-firebird-driver/.prettierignore
["INFO" - 2:57:11 PM] Loaded module 'prettier@2.2.1' from '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/sdks/prettier/index.js'
["INFO" - 2:57:11 PM] File Info:
{
  "ignored": false,
  "inferredParser": "typescript"
}
["INFO" - 2:57:11 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 2:57:11 PM] Using config file at '/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.prettierrc.json'
["INFO" - 2:57:11 PM] Prettier Options:
{
  "filepath": "/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/packages/node-firebird-driver/src/test/tests.ts",
  "parser": "typescript",
  "useTabs": true,
  "tabWidth": 4,
  "singleQuote": true,
  "trailingComma": "none",
  "arrowParens": "avoid",
  "plugins": ["prettier-plugin-organize-imports"]
}
["ERROR" - 2:57:11 PM] Error formatting document.
["ERROR" - 2:57:11 PM] Qualified path resolution failed - none of the candidates can be found on the disk.

Source path: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js.js
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js.json
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js.node
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js.mjs

Require stack:
- /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/cache/prettier-npm-2.2.1-e0670992f8-92c6c9f4b8.zip/node_modules/prettier/index.js
- /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js
Error: Qualified path resolution failed - none of the candidates can be found on the disk.

Source path: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js.js
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js.json
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js.node
Rejected candidate: /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/node_modules/prettier-plugin-organize-imports/index.js.mjs

Require stack:
- /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/cache/prettier-npm-2.2.1-e0670992f8-92c6c9f4b8.zip/node_modules/prettier/index.js
- /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js
    at internalTools_makeError (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:12770:34)
    at resolveUnqualified (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13803:13)
    at resolveRequest (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13827:14)
    at Object.resolveRequest (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13899:26)
    at Function.external_module_.Module._resolveFilename (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:13003:34)
    at Function.external_module_.Module._load (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.pnp.js:12868:48)
    at Module.require (internal/modules/cjs/loader.js:899:19)
    at r (/snap/code/52/usr/share/code/resources/app/out/vs/loader.js:16:346)
    at /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/cache/prettier-npm-2.2.1-e0670992f8-92c6c9f4b8.zip/node_modules/prettier/index.js:57503:6
    at Array.map (<anonymous>)
    at Object.load (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/cache/prettier-npm-2.2.1-e0670992f8-92c6c9f4b8.zip/node_modules/prettier/index.js:57501:118)
    at Object.load [as loadPlugins] (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/cache/prettier-npm-2.2.1-e0670992f8-92c6c9f4b8.zip/node_modules/prettier/index.js:16612:23)
    at /home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/cache/prettier-npm-2.2.1-e0670992f8-92c6c9f4b8.zip/node_modules/prettier/index.js:57540:28
    at Object.format (/home/asfernandes/projects/github/node-fb/node-firebird-drivers.git/.yarn/cache/prettier-npm-2.2.1-e0670992f8-92c6c9f4b8.zip/node_modules/prettier/index.js:57562:12)
    at t.default.<anonymous> (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.8.0/dist/extension.js:1:17599)
    at Generator.next (<anonymous>)
    at s (/home/asfernandes/.vscode/extensions/esbenp.prettier-vscode-5.8.0/dist/extension.js:1:11597)
["INFO" - 2:57:11 PM] Formatting completed in 135.951654ms.
valdestron commented 3 years ago

Waiting for release !

oliversalzburg commented 3 years ago

@valdestron Thanks for the notification!

alecmev commented 3 years ago

I've tried replacing .yarn\sdks\prettier\index.js as prescribed, that didn't do anything. Then I tried listing/requiring the plugins explicitly in the config, like here, to no avail, I get a Qualified path resolution failed error. The plugin I'm trying this with is prettier-plugin-tailwind. Am I missing something?

Shayan-To commented 2 years ago

I had this problem on VSCode, and after some debugging, figured out that when prettier is calling require (or something), it's passing an options object with a path property pointing to VSCode's installation directory. And I figured that dropping the options object will fix the problem. So here is what I came up with for .yarn\sdks\prettier\index.js:

#!/usr/bin/env node

const {existsSync} = require(`fs`);
const external_module_ = require(`module`);
const {createRequire, createRequireFromPath} = external_module_;
const {resolve} = require(`path`);

const relPnpApiPath = "../../../.pnp.cjs";

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 originalModuleResolveFilename = external_module_.Module._resolveFilename;
external_module_.Module._resolveFilename = function (request, parent, isMain, options) {
    return originalModuleResolveFilename.call(this, request, parent, isMain);
}

// Defer to the real prettier/index.js your application uses
module.exports = absRequire(`prettier/index.js`);

(Please note the relPnpApiPath variable. In my case, I had to change the extension from .js to .cjs. Change it according to your configuration.)

EdNutting commented 1 year ago

@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 yarn prettier -w . direct from cmdline worked fine. Using the above patch to the SDK resolved the issue (Windows 10)

Shayan-To commented 1 year ago

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?

arcanis commented 1 year ago

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 paths option (I didn't check myself yet), it should be fixed in whatever the caller is (the Prettier extension?). The Yarn SDK is mostly intended to setup PnP later so that the Node.js require API keep their semantics, but that presupposes this API is properly used.

That said, I find curious it'd work with node_modules if it was passing an invalid paths...

Shayan-To commented 1 year ago

I'm no expert in any of this. I just added some console.logs to the places the exception was coming from, and figured a way to workaround it.

From my experience, when I set "plugins": ["some-plugin"] for prettier, it works fine with yarn PNP when run from the CLI, but breaks in VSCode. And when using node_modules, I remember I had to use "plugins": ["./node_modules/some-plugin"] for it to work.

Should I open an issue here? Or something else?