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

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

Closed
1 task done
oliversalzburg opened this issue Sep 30, 2020 · 25 comments · Fixed by #1905, #1913 or #2238
Closed
1 task done

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

oliversalzburg opened this issue Sep 30, 2020 · 25 comments · Fixed by #1905, #1913 or #2238
Assignees
Labels
bug Something isn't working external bug This issue highlights a bug in another project

Comments

@oliversalzburg
Copy link
Contributor

oliversalzburg commented Sep 30, 2020

  • I'd be willing to implement a fix

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.

@oliversalzburg oliversalzburg added the bug Something isn't working label Sep 30, 2020
@larixer larixer added the external bug This issue highlights a bug in another project label Oct 1, 2020
larixer pushed a commit that referenced this issue Oct 2, 2020
* 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
arcanis pushed a commit that referenced this issue Oct 9, 2020
* 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>
@asfernandes
Copy link

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
Copy link
Member

larixer commented Oct 18, 2020

@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

@kachkaev
Copy link

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: kachkaev/njt#29

@merceyz
Copy link
Member

merceyz commented Oct 18, 2020

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
Copy link

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 configpluginsrequire.resolve at some point. See prettier/prettier#7073 (comment). 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-configsome-prettier-plugin. Installing all plugins into the project is quite inconvenient – I’d like the config package to bring them on.

@kachkaev
Copy link

Turns out that configpluginsrequire("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: prettier/prettier#7073 (comment)

@asfernandes
Copy link

@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
Copy link
Member

merceyz commented Nov 3, 2020

@asfernandes Until it's released you can apply it manually, see #1903 (comment)

@asfernandes
Copy link

@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
Copy link
Member

merceyz commented Nov 4, 2020

You're missing @yarnpkg/pnpify as a dependency

@asfernandes
Copy link

asfernandes commented Nov 4, 2020

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
Copy link
Member

merceyz commented Nov 21, 2020

Please open a seperate issue with a reproduction

@derekschinke
Copy link

derekschinke commented Dec 14, 2020

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
Copy link

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
Copy link
Member

merceyz commented Dec 18, 2020

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
Copy link

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

@asfernandes
Copy link

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
Copy link

Waiting for release !

@oliversalzburg
Copy link
Contributor Author

@valdestron Thanks for the notification!

@alecmev
Copy link

alecmev commented Mar 23, 2021

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
Copy link

Shayan-To commented Feb 17, 2022

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
Copy link

@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
Copy link

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
Copy link
Member

arcanis commented Sep 30, 2022

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working external bug This issue highlights a bug in another project
Projects
None yet