berry: [Bug?]: Yarn pnp / sdk generation doesn't work with ESLint new flat config support

Self-service

  • I’d be willing to implement a fix

Describe the bug

ESLint’s new flat config support fails to load when using flat config module files. It does work when using common JS config files.

This bahviour gor reported against the VS Code ESLint extension (see https://github.com/microsoft/vscode-eslint/issues/1620) but it is independent of the VS Code extension and can be reproduce using the eslint npm package standalone

To reproduce

Setup:

package.json

{
  "name": "yarn-eslint-test",
  "packageManager": "yarn@4.1.1",
  "type": "module",
  "scripts": {
    "eslint": "eslint test.js"
  },
  "devDependencies": {
    "@eslint/js": "^8.57.0",
    "eslint": "^8.57.0"
  }
}

eslint.config.mjs

import js from "@eslint/js";

export default [
	js.configs.recommended,
	{
		files: ["**/*.js"],
		rules: {
			"no-var": "error",
		}
	}
];

test.js

var x = 10;

Run the following commands in a shell:

> corepack enable
> yarn install
> yarn dlx @yarnpkg/sdks vscode
> 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({ useFlatConfig: true }))(); const report = await eslint.lintText(`var test = "hello";`); console.log(report); }
main().catch((error) => { console.error(error); process.exitCode = 1; });

You get:

Image

Expected behavior: the eslint.config.mjs can successfully be loaded. Converting the config file to cjs makes everything work.

Environment

System: OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish) CPU: (20) x64 12th Gen Intel® Core™ i7-12700K Binaries: Node: 18.18.2 - /tmp/xfs-e72dc863/node Yarn: 4.1.1 - /tmp/xfs-e72dc863/yarn npm: 9.8.1 - ~/.nvs/default/bin/npm

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 16
  • Comments: 38 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I’m looking right now at migrating this repository to Eslint 9; I’ll take a look at that at the same time.

I think I figured it out - v18.19.0 is the first version that supports register (to add ESM loaders at runtime), and VSCode ships with Electron 28, which is tied to Node 18.18. So unfortunately I’m afraid we can’t do much right now 🫤

I just triggered a release. Using the 4.3.0 the original test case doesn’t crash anymore. In case you still face issues, please make sure you use both Yarn 4.3 and the SDK 3.1.3. If you do, open a new thread (let’s not reuse this one, I’d like to keep investigation separate as something that may look related may be a completely different bug).

For reference, here’s a script to easily run the commands from the op; note I updated packageManager to 4.3:

#!/usr/bin/env bash

cd $(mktemp -d)

cat > package.json <<EOF
{
  "name": "yarn-eslint-test",
  "packageManager": "yarn@4.3.0",
  "type": "module",
  "scripts": {
    "eslint": "eslint test.js"
  },
  "devDependencies": {
    "@eslint/js": "^8.57.0",
    "eslint": "^8.57.0"
  }
}
EOF

cat > eslint.config.mjs <<EOF
import js from "@eslint/js";

export default [
	js.configs.recommended,
	{
		files: ["**/*.js"],
		rules: {
			"no-var": "error",
		}
	}
];
EOF

cat > test.js <<EOF
var x = 10;
EOF

yarn install
yarn dlx @yarnpkg/sdks vscode
export NODE_PATH=".yarn/sdks"

node <<EOF
require.resolve('eslint');

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

Is there any solution? Or not use other packages manager?

Instead build the cli from the correct branch mael/eslint-flat-config (yarn build:cli) yourself and link the new cli in your .yarnrc.yml with yarnPath. It should work.

could I ask you what steps you’d suggest to correctly install this in a clean project?

#6278 is already merged 4 days ago, so just running yarn dlx @yarnpkg/sdks vscode should be enough by now.

I can reproduce this without the extension using the sdk version from https://github.com/yarnpkg/berry/pull/6278 and running

node --import ./.yarn/sdks/eslint/lib/api.js ./eslint.config.mjs ❌

node@22.2.0

If i run the config through yarn node it can resolve all modules.

yarn node ./eslint.config.mjs ✅

UPDATE It works, i only installed the updated sdk. But i needed to build the full cli so the .pnp.loader.mjs gets updated aswell.

@n0099 Do not use yarn dlx -p '@yarnpkg/sdks@https://github.com/yarnpkg/berry#head=mael/eslint-flat-config&workspace=@yarnpkg/sdks' @yarnpkg/sdks vscode Instead build the cli from the correct branch https://github.com/yarnpkg/berry/tree/mael/eslint-flat-config (yarn build:cli) yourself and link the new cli in your .yarnrc.yml with yarnPath. It should work.

I can confirm the same as @n0099. I still get ERR_MODULE_NOT_FOUND using multiple setups with https://github.com/yarnpkg/berry/pull/6278 and node@20.9:

  • vscode-insider@1.91, dbaeumer.vscode-eslint@3.0.7, eslint@^9.4.0 - ❌
  • vscode-insider@1.91, dbaeumer.vscode-eslint@3.0.7, eslint@^8.57.0 - ❌
  • vscode-insider@1.91, dbaeumer.vscode-eslint@2.4.4 (eslint.experimental.useFlatConfig: true), eslint@^9.4.0 - ❌
  • vscode-insider@1.91, dbaeumer.vscode-eslint@2.4.4 (eslint.experimental.useFlatConfig: true), eslint@^8.57.0 - ❌

I debugged a little and it is still unable to find findPnpApi() in the resolve hook for me when executed in the extension. Cli execution works fine.

Maybe i am missing something in my setup?

Happy to provide more info if needed 😊

You need both my PR (https://github.com/yarnpkg/berry/pull/6278) and Node >v20.6.0, v18.19.0; you can try it by cloning my branch and running your repro in this repo.