-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
Use peerDependencies to avoid duplicate dependencies #858
Comments
I have considered that, but a part of the community is still using an npm version <6, which does not automatically install peer dependencies. This would make the library a huge pain to use for those people. Also, in principle, npm will deduplicate properly on a fresh install now, if the version ranges are compatible. It's just when you upgrade or have otherwise complicated install procedures that it sometimes messes up and needlessly duplicates libraries. The situation seems to come up less frequently, once you no longer use 0.x versions, but yes, it unfortunately still comes up. You can usually work around it by removing your package lock and reinstalling. |
I think that should work fine when a dependency is declared in both |
That's a very interesting trick. Do you know of any major packages that are using it, currently? Any potential pitfalls? |
All I was able to find (and I wrote a script that checked 1250 most popular dependencies) is https://github.com/tailwindlabs/tailwindcss, however because this was introduced before npm/rfcs#324 was accepted this is likely a mistake rather than intentional choice. As for pitfalls, the biggest one I can think of that it's necessary to keep versions in sync - npm won't inform about mismatches and if mismatch occurs then different package managers will see different versions which could potentially cause issues. |
Albeit, thinking about it, this is not really necessary. Like, realistically CodeMirror is going to be used with extensions, and I think those extensions could easily declare a |
FIX: Declare package dependencies as peer dependencies as an attempt to avoid duplicated package issues. Issue #12 Issue codemirror/dev#858
I've released @codemirror/autocomplete 6.0.2 with both peer- and regular dependencies. If that doesn't cause trouble, I'll do the same for other packages. |
I realized that this hack doesn't actually work :(. As a test, I started with this: npm install --save @codemirror/autocomplete Then I intentionally installed old version of npm install --save @codemirror/state@0.20.0
npm install --save @codemirror/state@6.0.0 This results in I think instead it may be reasonable to provide |
That said, for what it's worth, now that I checked, Yarn (both legacy and 3) does recognize having both "dependencies": {
"@codemirror/language": "^6.0.0",
"@lezer/common": "^1.0.0"
},
"peerDependencies": {
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"@lezer/common": "^1.0.0"
},
|
I think a better approach is to not include It makes sense to have Here is a patch of what I'm suggesting codemirror/autocomplete@main...masad-frost:patch-1 |
As I stated in a previous comment, I suspect that will be a huge pain for users without recent npm versions, who will have to manually make sure they include every I feel the root problem here is npm inexplicably duplicating packages that don't need to be duplicated and not yet providing a reliable way to avoid this, and it unfortunately seems like there is not a lot package authors can do to avoid this problem. |
another option is a postinstall you can staple "pre" or "post" onto the name of any lifecycle script time and you get another script time therefore "postinstall" is an event that runs after install, in which you can install peer deps, git submodules, python workspaces, or any other stuff like that lots of tools check for a dep then if they see one don't check for the other dep types. this will go south if you start installing things like husky (admittedly i'm against husky, but still.) this way you don't need any shenanigans against the concept of deps |
I get the instance of error like this
from this code
|
I think another argument for using peer dependencies is that it communicates the proper relationship to tools like dependabot, which otherwise will always treat each package independently no matter how big the version jump. |
I very much support this, as although it is true that deleting the lockfile and |
Is anyone still seeing this npm issue (pointless dependency duplication on upgrade) in recent versions of npm? I tried to isolate it but can't get it to happen anymore with 9.6 and up. |
@marijnh I just ran into this issue today (using I would recommend switching for these reasons:
I'd also be happy to open a PR to do 2 things...
|
Don't use yarn. If you use a recent version of npm, this problem shouldn't be common anymore (I'm not sure if it's entirely eliminated, but I can't get it to happen anymore). |
@marijnh LOL I don't think an npm package should drive which package manager folks are using, yarn is widely used (I think like ~40% of folks...) & would be the equivalent (honestly worse) of saying "we don't support firefox". If I want to extend Most of the usage (I spot checked) seemed to be mostly types no? If thats case this could be a devDependency (I think there was Range manipulation being done... soo maybe doesn't make sense). Also I'll acknowledge I'm very new the CodeMirror/ProseMirror (❤️ the work done here).... but as outsider/new user I was caught off guard as most other core npm packages I use rely on peerDeps for things that aren't 100% tied together (could be my misunderstanding) |
Sure. And I don't think companies should bring out broken tools that fragment the community duplicating standard tools, but we don't live in a perfect world, so here we are. |
I know messaging doens't get across emotion/sarcasms but from my perspective I don't think the snark is helpful to the conversation. This is about how you design and ship dependencies. If you don't have a valid reason for not switching just say you don't want to do it as its too much work etc. (IMO as maintainer... that is very valid reason) and close the issue. Hiding behind "I think yarn is dumb" isn't a valid reason + your discounting almost half of the potential users out there, many of which can't change due to their organization's choices (I choose yarn so I'm happy to work around on my end, which I did...). Why build cool shit if you don't want ~half the people to use it? |
yarn is a relatively rare tool. no, it's not 40% of the community. it's not every package maintainer's responsibility to modify their packages to cope with yarn's bugs. provide a valid reproduction. |
A reproduction isn't needed. I know yarn and old npm versions do needless duplication when you upgrade a single package in an existing lock/dep tree. But peer dependencies have their own problem, and don't match the library's package structure well. So I consider the future to be modern npm with regular dependencies. If you must use yarn, don't use piecemeal upgrade, just blow away your package lock and reinstall everything. I am closing this and asking everyone to stop responding in inflammatory ways. |
I think closing is a good choice so folks know this won't change / happen + they can see the history of the conversation. I'd like to sign off of this convo with @marijnh I appreciate the work you did & are continuing to do with CodeMirror and ProseMirror ❤️. |
This thread has actually made me finally switch from Yarn to pnpm 😆 |
Currently none of CodeMirror projects declare
peerDependencies
. This tells the package manager that it can choose to have duplicate packages innode_modules
, which is pretty bad when CodeMirror does depend oninstanceof
checks.You may consider adding
peerDependencies
to avoid this issue. For example, in https://github.com/codemirror/view/blob/main/package.json the dependencies could be declared as follows:This tells the package manager that
@codemirror/state
andstyle-mod
dependencies are part of a public API and as such it should always dedupe those.This is not a theoretical issue, npm can choose to do so in certain situations, I noticed this particular issue with xfix/live@88d62fc. If
package.json
here gets replaced with the following:Then after running
npm install
(tested with npm 8.5.5 and 8.9.0),@codemirror/view
will be in all those locations:Which causes issues until
npm dedupe
gets executed.The text was updated successfully, but these errors were encountered: