berry: [Bug?]: Yarn pnp fails with synckit and prettier plugin in eslint context

Self-service

  • I’d be willing to implement a fix

Describe the bug

Yarn pnp can’t load synckit worker hence prettier plugin fails to run.

Why do I file this against yarn? Everything works as expected when installing the dependencies with npm.

To reproduce

Use NodeJS 20.9.0

Clone https://github.com/shermify/eslint-plugin-repro

> yarn install
> export NODE_PATH=".yarn/sdks"

Setting the NODE_PATH is equivalent to setting the VS Code eslint.nodePath setting.

start node REPL and execute the following commands (to ensure to mimic the executing since a VS Code extension):

require.resolve('eslint');

We will load the eslint npm module from .yarn/sdks

const library = require('eslint');
async function main() { const eslint = new (await library.loadESLint())(); const report = await eslint.lintText(`var test = "hello";`); console.log(report); }
main().then(() => { console.log('Promise resolved'); } ).catch((error) => { console.error(error); process.exitCode = 1; });

Observe: the promise never resolves. You can’t even terminate NodeJS using Ctrl+C. You have to kill it.

I debugged it in the context of the VS Code ESLint extension and the findings are here: https://github.com/microsoft/vscode-eslint/issues/1856#issuecomment-2162280404 and in the following comments.

Environment

System: OS: Linux 5.15 Debian GNU/Linux 11 (bullseye) 11 (bullseye) CPU: (20) x64 12th Gen Intel® Core™ i7-12700K Binaries: Node: 20.9.0 - /tmp/xfs-426f4fe7/node Yarn: 4.3.0 - /tmp/xfs-426f4fe7/yarn npm: 10.1.0 - ~/.nvs/default/bin/npm pnpm: 9.1.1 - /usr/local/share/npm-global/bin/pnpm

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 16
  • Comments: 21 (5 by maintainers)

Most upvoted comments

Do you know why, when it’s unplugged, the dynamic import still doesn’t seem to work but it does work when you change it to require?

Because the Node.js ESM loader pipeline is still broken 😞

Digging into the issue, it seems the --loader flag that SyncKit adds is ignored by the new Worker constructor, which seems to be the subject of https://github.com/nodejs/node/issues/47747. That’s what causes the ERR_MODULE_NOT_FOUND error when dynamically importing prettier, which in turns causes a deadlock probably because of a faulty error handling code somewhere in the Node.js internals.

I opened an issue on SyncKit (https://github.com/un-ts/synckit/issues/174) to suggest a workaround.

if i set nodeLinker: node-modules the promise gets resolved, doesn’t matter which node version it is in this case. Doesn’t that mean it’s yarn pnp related?

Yes and no. The Node.js ESM loader implementation is quite fluctuating, so bugs sometimes appear and get fixed on later versions (for instance some 22.x versions introduce a deadlock with some other loaders). As a result problems are very rarely about PnP itself, but more the APIs we’re given to integrate with ESM in Node.

is there anything I can capture that might help you to understand what is going on?

I managed to repro - I was running yarn node (which translates into node --require ./.pnp.cjs --loader ./.pnp.loader.mjs), but in your repro it’s just a good old node. It shouldn’t make a difference, but perhaps Node regressed and is somehow registering the loaders twice even when they have the same path? Investigating.

I was able to fix this issue in my project by downgrading eslint-plugin-prettier to v5.3.1 from v5.4.0 (also affected is v5.4.1): https://github.com/retrixe/IveBot/commit/4550fecf2a96c52606e91a411a4d8b51bbea71f5

This suggests to me the bug was introduced (reintroduced?) by some behavioural change caused by https://github.com/prettier/eslint-plugin-prettier/commit/59a0cae5f27801d7e00f257c6be059a848b32fbe (the only commit in v5.4.0)

This is still an issue as of 0.11.0.

Repro:

[Info  - 12:24:13 PM] ESLint server is starting.
[Info  - 12:24:14 PM] ESLint server running in node v20.18.3
[Info  - 12:24:14 PM] ESLint server is running.
[Info  - 12:24:15 PM] ESLint library loaded from: /Users/tpict/Projects/eslint-example/.yarn/sdks/eslint/lib/api.js
loaded the config!

Then update the nodeLinker setting:

diff --git a/.yarnrc.yml b/.yarnrc.yml
index a398e49..91b1101 100644
--- a/.yarnrc.yml
+++ b/.yarnrc.yml
@@ -2,4 +2,4 @@ compressionLevel: mixed
 
 enableGlobalCache: false
 
-nodeLinker: pnp
+nodeLinker: node-modules

and run yarn again, restart VSCode, observe squiggly lines in failing files.

@agarwalvaibhav0211 yes it has been fixed with latest release of synckit (https://github.com/un-ts/synckit/releases/tag/v0.9.1) which is a dependency of eslint-plugin-prettier. Just make sure you have synckit@0.9.1 in your packages. If reinstalling eslint-plugin-prettier package doesn’t fetch latest synckit then you can enforce it through yarnresolutions like this:

"resolutions": {
  "synckit": "^0.9.1"
}

Fixing the root compatibility issue, which only appears only in the newest version of Yarn (it was working before), is a real solution.

@sheldonmaschmeyer right, it wasn’t posed as a solution, but as a demonstration that the fault is with the pnp linker