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

Extraneous dependencies with Yarn v3 #93

Closed
EvgenyOrekhov opened this issue Dec 15, 2021 · 16 comments
Closed

Extraneous dependencies with Yarn v3 #93

EvgenyOrekhov opened this issue Dec 15, 2021 · 16 comments

Comments

@EvgenyOrekhov
Copy link

When I try to use Putout with Yarn v3, I get the following error:

🐊 @putout/formatter-progress-bar: simport tried to access @putout/formatter-progress-bar, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: @putout/formatter-progress-bar (via "@putout/formatter-progress-bar/package.json")
Required by: simport@npm:1.2.0 (via /Users/evgeny/Downloads/putout-yarn3/.yarn/cache/simport-npm-1.2.0-c4f61c3792-85716020b4.zip/node_modules/simport/simport.js)

Steps to reproduce:

  1. git clone git@github.com:EvgenyOrekhov/putout-yarn3.git
  2. cd putout-yarn3
  3. yarn
  4. yarn putout .

Looks similar to #92.

@coderaiser
Copy link
Owner

It looks like an issue of yarn, simport simplifies dynamic loading of ESM and fixes issues related to windows it can import any module and do not have to have it in package.json. Same with dynamic nature of loading plugins in 🐊Putout. ESLint works in similar way but with CommonJS.

@coderaiser
Copy link
Owner

OK, so there is a thing called PnP loose mode to get this working, .yarnrc should be updated to:

yarnPath: ".yarn/releases/yarn-berry.cjs"
+pnpMode: "loose"

But after that a new error appear:

(node:79903) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:79903) [MODULE_NOT_FOUND] Error: debug tried to access supports-color (a peer dependency) but it isn't provided by its ancestors; this makes the require call ambiguous and unsound.
(node:79903) [MODULE_NOT_FOUND] Error: simport tried to access @putout/formatter-progress-bar, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

🐊 @putout/formatter-progress-bar: chalk tried to access #ansi-styles, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: #ansi-styles (via "#ansi-styles/package.json")
Required by: chalk@npm:5.0.0 (via /Users/coderaiser/putout-yarn3/.yarn/cache/chalk-npm-5.0.0-7be183234e-6eba7c518b.zip/node_modules/chalk/source/index.js)

The thing is chalk uses imports map and #ansi-styles doesn't even have package.json.

Yarn Plug'n'play is nice feature, but it's not ready for using in real world yet (especially ESM which just trying to get on his feet in dual packages mode working closely to CommonJS).

So I'm glad to help, and will try to do steps required from my side, but there is a lot things that needs to be done from other maintainers of popular packages and node.js itself, because node loaders is still experimental and subject to change.

@coderaiser
Copy link
Owner

coderaiser commented Dec 15, 2021

I understood, it's possible to avoid a lot of warnings if createRequire hack will be used by @putout/engine-loader.

This is the only way .pnp.cjs will work in correct way, because it puts everything from node_modules to itself:

-rwxr-xr-x    1 coderaiser  staff   1.2M Dec 15 18:59 .pnp.cjs*

About formatters, putout v21 uses CommonJS versions of formatters.

@coderaiser
Copy link
Owner

Landed ability to benefit from Yarn PnP in @putout/engine-loader v4.10.0 🎉 !
It will work without warnings :)! And no loose mode needed. This is when 🐊Putout used as a plugin for ESLint.

If 🐊Putout used as standalone, pnpMode should be set to loose, and version of 🐊Putout should be decreased to v22, because chalk v5 isn't supported by yarn PnP yarnpkg/berry#3843.

Or CommonJS version of formatter should be installed, or json, json-lines or eslint formatter used.

I think we can come back to this in the future, when yarn PnP became more mature :).

@coderaiser
Copy link
Owner

Landed support of yarn PnP in 🐊Putout v22.8.0 🎉 !
Is it works for you?

@coderaiser
Copy link
Owner

coderaiser commented Dec 16, 2021

I found out a little more details from yarn documentation:

image

And did some modifications.
I described details in docs. Could you please try to run eslint with:

PUTOUT_YARN_PNP=eslint-config-hardcore yarn lint .

@spa5k @EvgenyOrekhov is this way of running works for you?

@coderaiser
Copy link
Owner

coderaiser@localcmd:~/putout-yarn3$ yarn lint
./pages/1.tsx
1:22  Error: Replace `'xx'` with `"xx"`  prettier/prettier
1:22  Error: Strings must use doublequote.  quotes
3:1  Error: exported declaration 'setSession' not used within other modules  import/no-unused-modules
3:1  Error: Prefer default export.  import/prefer-default-export
3:14  Error: Expected a function declaration.  func-style
4:1  Error: Replace `····` with `··`  prettier/prettier
5:1  Error: Replace `····` with `··`  prettier/prettier
5:5  Error: Shorthand properties should be used (apply-shorthand-properties)  putout/putout 🔥🔥🔥🔥🔥🔥🔥
7:1  Error: Delete ``  prettier/prettier

info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
coderaiser@localcmd:~/putout-yarn3$ ls -lha node_modules/ 🔥🔥🔥🔥🔥🔥🔥
total 0 
drwxr-xr-x   3 coderaiser  staff    96B Dec 16 20:19 ./
drwxr-xr-x  21 coderaiser  staff   672B Dec 17 16:20 ../
drwxr-xr-x   3 coderaiser  staff    96B Dec 16 20:19 .cache/
coderaiser@localcmd:~/putout-yarn3$ vi .m
coderaiser@localcmd:~/putout-yarn3$ ls -lha
total 3480
drwxr-xr-x   21 coderaiser  staff   672B Dec 17 16:20 ./
drwxr-xr-x+ 210 coderaiser  staff   6.6K Dec 17 16:19 ../
-rw-r--r--    1 coderaiser  staff    54B Dec 17 15:44 .eslintrc.json
drwxr-xr-x   12 coderaiser  staff   384B Dec 17 15:32 .git/
-rw-r--r--    1 coderaiser  staff   495B Dec 16 15:55 .gitignore
drwxr-xr-x    3 coderaiser  staff    96B Dec 16 15:59 .next/
-rwxr-xr-x    1 coderaiser  staff   1.3M Dec 17 16:20 .pnp.cjs* 🔥🔥🔥🔥🔥🔥🔥
-rw-r--r--    1 coderaiser  staff   7.8K Dec 16 16:09 .pnp.loader.mjs 
-rw-r--r--    1 coderaiser  staff    43B Dec 16 16:09 .putout.json
drwxr-xr-x    6 coderaiser  staff   192B Dec 17 15:11 .yarn/
-rw-r--r--    1 coderaiser  staff    58B Dec 16 19:42 .yarnrc.yml
-rw-r--r--    1 coderaiser  staff   1.5K Dec 16 15:55 README.md
-rw-r--r--    1 coderaiser  staff   201B Dec 16 15:55 next-env.d.ts
-rw-r--r--    1 coderaiser  staff    87B Dec 16 15:55 next.config.js
drwxr-xr-x    3 coderaiser  staff    96B Dec 16 20:19 node_modules/
-rw-r--r--    1 coderaiser  staff   668B Dec 17 15:59 package.json
drwxr-xr-x    6 coderaiser  staff   192B Dec 17 15:59 pages/
drwxr-xr-x    4 coderaiser  staff   128B Dec 16 15:55 public/
drwxr-xr-x    4 coderaiser  staff   128B Dec 16 15:55 styles/
-rw-r--r--    1 coderaiser  staff   509B Dec 16 15:55 tsconfig.json
-rw-r--r--    1 coderaiser  staff   406K Dec 17 16:20 yarn.lock
coderaiser@localcmd:~/putout-yarn3$ cat package.json
{
  "name": "next-putout",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "PUTOUT_YARN_PNP=eslint-config-hardcore next lint", 🔥🔥🔥🔥🔥🔥🔥
    "putout": "putout"
  },
  "dependencies": {
    "eslint-config-hardcore": "^23.6.1",
    "eslint-plugin-prettier": "^4.0.0",
    "next": "12.0.7",
    "prettier": "^2.5.1",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@types/node": "16.11.13",
    "@types/react": "17.0.37",
    "eslint": "8.4.1",
    "eslint-config-next": "12.0.7",
    "putout": "^22.8.0",
    "typescript": "4.5.4"
  },
  "packageManager": "yarn@3.1.1"
}
coderaiser@localcmd:~/putout-yarn3$ cat .yarnrc.yml
yarnPath: .yarn/releases/yarn-3.1.1.cjs
nodeLinker: "pnp" 🔥🔥🔥🔥🔥🔥🔥
coderaiser@localcmd:~/putout-yarn3$ cat .eslintrc.json
{
  "extends": ["next/core-web-vitals", "hardcore"] 🔥🔥🔥🔥🔥🔥🔥
}

coderaiser added a commit that referenced this issue Dec 17, 2021
We need to create a couple version of require for plugins, formatters and processors:
- declared in 🐊Putout package.json;
- declared in module that want to extend 🐊Putout;

https://yarnpkg.com/advanced/rulebook#modules-shouldnt-hardcode-node_modules-paths-to-access-other-modules
@EvgenyOrekhov
Copy link
Author

@coderaiser I tried following your example (I added PUTOUT_YARN_PNP=eslint-config-hardcore and nodeLinker: "pnp"), still getting the following error:

❯ yarn lint       
Error: Error while loading rule 'putout/putout': @putout/engine-loader tried to access putout, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: putout
Required by: @putout/engine-loader@npm:4.12.0 (via /Users/evgeny/Downloads/next-ts/.yarn/cache/@putout-engine-loader-npm-4.12.0-5f341bbac6-8b4eda1515.zip/node_modules/@putout/engine-loader/lib/)

Require stack:
- /Users/evgeny/Downloads/next-ts/.yarn/cache/@putout-engine-loader-npm-4.12.0-5f341bbac6-8b4eda1515.zip/node_modules/@putout/engine-loader/lib/load.js
- /Users/evgeny/Downloads/next-ts/.yarn/cache/@putout-engine-loader-npm-4.12.0-5f341bbac6-8b4eda1515.zip/node_modules/@putout/engine-loader/lib/index.js
- /Users/evgeny/Downloads/next-ts/.yarn/cache/putout-npm-22.8.2-a2610271e6-0eb95d2c51.zip/node_modules/putout/lib/putout.js
- /Users/evgeny/Downloads/next-ts/.yarn/__virtual__/eslint-plugin-putout-virtual-bf92a01363/0/cache/eslint-plugin-putout-npm-12.7.1-027a8abd29-50896c563d.zip/node_modules/eslint-plugin-putout/lib/array-element-newline/index.js
- /Users/evgeny/Downloads/next-ts/.yarn/__virtual__/eslint-plugin-putout-virtual-bf92a01363/0/cache/eslint-plugin-putout-npm-12.7.1-027a8abd29-50896c563d.zip/node_modules/eslint-plugin-putout/lib/index.js
- /Users/evgeny/Downloads/next-ts/.yarn/cache/@eslint-eslintrc-npm-1.0.5-bd5ac53f2a-b35b50d7b6.zip/node_modules/@eslint/eslintrc/dist/eslintrc.cjs
Occurred while linting /Users/evgeny/Downloads/next-ts/pages/_app.tsx

I created a reproducible case here: https://github.com/EvgenyOrekhov/putout-yarn3/tree/PUTOUT_YARN_PNP

Steps to reproduce:

git clone git@github.com:EvgenyOrekhov/putout-yarn3.git
cd putout-yarn3
git checkout PUTOUT_YARN_PNP
yarn lint

@coderaiser
Copy link
Owner

@EvgenyOrekhov, yarn add putout should solve the problem :)

@EvgenyOrekhov
Copy link
Author

@coderaiser Hmm I have putout declared in eslint-config-hardcore's dependencies:

https://github.com/EvgenyOrekhov/eslint-config-hardcore/blob/ab59bf254d228f561f2b7a77d9cd470b2cfb2e2f/package.json#L75

I expected that it should work without using yarn add putout.

@coderaiser
Copy link
Owner

coderaiser commented Dec 20, 2021

Looks that Yarn PnP thinks that 🐊Putout should be on top level 🤷‍♂️

@spa5k
Copy link

spa5k commented Dec 21, 2021

Try adding this - https://www.npmjs.com/package/@rushstack/eslint-patch

It is useful in cases of yarn and monorepos.

@mrienstra
Copy link

mrienstra commented Sep 29, 2022

I think we can come back to this in the future, when yarn PnP became more mature :).

The future is peeking up over the horizon, yarnpkg/berry#3843 was fixed yesterday. The fix is available in 4.0.0-rc.22. Run yarn set version canary if you want to try it. v4 release is still a little ways off ("may take a couple more months"), but:

[...] what's in master is stable, and I'd recommend you to try it. The only notable difference with stable is that we reserve the right to land a couple more breaking changes in future RCs, but in terms of stability it's almost always better to use RCs than stable.

... according to yarnpkg/berry#4895

See yarnpkg/berry#3591 for v4 breaking changes. I've seen two mentions of people stumbling over enableGlobalCache default changing from false to true. (Google search: site:yarnpkg.com "enableGlobalCache")

Edit: Update: the fix that adds support for the package.json includes field is also in the Yarn 3.2.4 release.

@coderaiser
Copy link
Owner

@mrienstra Yarn PnP is supported for a long time. Is it works for you?

@mrienstra
Copy link

@coderaiser, just spreading the news about yarnpkg/berry#3843. This comment:

If 🐊Putout used as standalone, pnpMode should be set to loose, and version of 🐊Putout should be decreased to v22, because chalk v5 isn't supported by yarn PnP yarnpkg/berry#3843.

Or CommonJS version of formatter should be installed, or json, json-lines or eslint formatter used.

I think we can come back to this in the future, when yarn PnP became more mature :).

.. states that standalone usage of 🐊Putout is limited to <= v22 due to yarnpkg/berry#3843, wanted to share that this will likely no longer be the case when Yarn v4 is used.

@coderaiser
Copy link
Owner

.. states that standalone usage of 🐊Putout is limited to <= v22

That’s no longer the case, 🐊Putout uses lower version of chalk, and yarn PnP is supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants