yarnpkg / yarn

The 1.x line is frozen - features and bugfixes now happen on https://github.com/yarnpkg/berry
https://classic.yarnpkg.com
Other
41.44k stars 2.73k forks source link

Failed to install dependencies in workspace: expected workspace package to exist #7807

Open zamotany opened 4 years ago

zamotany commented 4 years ago

Do you want to request a feature or report a bug? Bug

What is the current behavior? yarn install fails with:

error An unexpected error occurred: "expected workspace package to exist for \"@babel/template\"".

The error started occurring after upgrading yarn to 1.19 and it still persists in latest stable version 1.21.1

Similar errors can be observed in #7797 and #7734

If the current behavior is a bug, please provide the steps to reproduce. The error can be reproduced when installing dependencies in https://github.com/callstack/haul

  1. git clone git@github.com:callstack/haul.git
  2. cd haul
  3. yarn install

What is the expected behavior?

yarn install should successfully install dependencies.

Please mention your node.js, yarn and operating system version.

day-scene commented 4 years ago

In my case, it might be the version confilcts of @babel/core. I solved it by: check out the version installed by yarn why @babel/core, add resolution to the package which is not the the same version to unify the version.

danvoyce commented 4 years ago

Adding this incase someone else (god help them) has a similar issue as I've just spent half my weekend debugging / basically reformatting my computer...

I setyarn policies set-version 1.19.1 thinking everything was fine. A few hours later I did a build of my Next.js app and was getting this Error occurred prerendering page.... I literally tried everything under the sun, and have just realised that doing yarn policies set-version 1.19.1 was the cause.

What's even stranger is it destroys my local project. If I switch to a stable branch, delete all the node modules, yarn.lock etc etc, switch back to the latest version yarn, and run yarn install, then build my Next.js app again, I get the same error.

I've no idea what's going on tbh. I literally re-installed node, yarn etc. The only solution was to delete the app and clone it again.

xddz9 commented 4 years ago

I had the same issue with eslint package. Turns out the problem was my workspace root had eslint as a dev dependency, but I also had a workspace package that depended on an npm package that depended on a different version of eslint, which I assume ended up breaking the hoisting process somehow. All I did was make sure all packages depended on the same version of eslint and the error went away.

pyramation commented 4 years ago

also experiencing this issue. @export-mike 's solution works as a hotfix, thank you

thefat32 commented 4 years ago

Is there any official response/fix roadmap from yarn dev team about this?

danvoyce commented 4 years ago

My solution was to switch to pnpm. Highly recommend it!

schmod commented 3 years ago

I'm 99% sure this is the same issue as #7734 – can we please close this as a duplicate?

customcommander commented 3 years ago

I'm 99% sure this is the same issue as #7734 – can we please close this as a duplicate?

I would prefer if we could keep it open. It has some valuable information about how to reproduce. The other issue feels more like a "i have the same issue" and "downgrading worked for me" kind of thread in comparison.

Linking the two and possibly getting similar issues under a common label would help but closing this isn't going to revive the discussion on the other one.

pyramation commented 3 years ago

agree @customcommander and FWIW, this issue is the one that google continues to bring up when googling the error since I literally run into this problem every other day 😂

I am working in many workspaces and this issue has for some reason, become more prevalent lately.

programmer-yang commented 3 years ago

node:v14.15.0 yarn:1.22.10

I also encountered this problem, and my solution was to delete the yarn.lock file and re-execute the yarn install command, and the problem was solved

ps: I had this problem after upgrading my MAC system to Big Sur

This method is not recommended because reinstalling Packages can change the current running environment and cause unpredictable problems

jamesdbruner commented 3 years ago

node:v14.15.0 yarn:1.22.10

I also encountered this problem, and my solution was to delete the yarn.lock file and re-execute the yarn install command, and the problem was solved

ps: I had this problem after upgrading my MAC system to Big Sur

Deleting your yarn.lock isn't a valid solution, you should never delete your yarn.lock without knowing exactly what you're doing. I learned this one the hard way.

thefat32 commented 3 years ago

To extend @jamesdbruner warning, deleting your yarn.lock file will cause yarn to update all your packages. If dependency versions where not locked down, then every time the dependencies are installed through yarn install, the fetched dependencies may be different. If one of the dependencies has a new version available and the available version is within the specified version range in the package.json, then the newest dependency will be installed. This could be very dangerous, mostly if your version range in the package.json allows major update (because this updates can have public API changes), but sometimes you can find a new bug when updating minor versions too. The nature of yarn.lock is to have complete control over which version of a dependency is used to have a stable enviroment for development.

FrozenKiwi commented 3 years ago

a slightly simpler workaround: directly add your dependency to package.json and then run yarn in the project root.

worked for me, yarn 1.22.4 on monorepo/lerna

joeyfigaro commented 3 years ago

Any updates or progress on investigating this? Using yarn policies set-version to rollback/specify a different version does not fix the issue for us. Only workaround currently is to add packages to package.json by hand prior to install.

MKruschke commented 3 years ago

Hi, unfortunately we encountered the same issue after upgrading to a newer babel version and adding one or two additional eslint plugins. The package that is doing a violation is already added in the package.json on the root level.

As workaround I downgraded to v1.18.0. We used before 1.22.10 and also 1.19.2 had the same issue (see screenshot)

yarn-error_log_—_move-dealer-webapp__Workspace_
bitjoo commented 3 years ago

Hi, unfortunately we encountered the same issue after upgrading to a newer babel version and adding one or two additional eslint plugins. The package that is doing a violation is already added in the package.json on the root level.

As workaround I downgraded to v1.18.0. We used before 1.22.10 and also 1.19.2 had the same issue (see screenshot)

@MKruschke This bug can be fixed, without downgrading to v1.18.0, if you add the following to your package.json

"resolutions": {
    "**/@babel/traverse": "PLACE_YOUR_BABEL_TRAVERSE_VERSION_HERE"
  },
joartola commented 3 years ago

I just had the same problem on a monorepo Lerna + Yarn (v1.22). Solved re-creating the yarn.lock.

THANKS! Delete root yarn.lock!

nikaupp commented 3 years ago

deleting yarn.lock helped to solve this for me.

bryanvaz commented 3 years ago

Just an FYI to anyone who hits this problem and is on yarn > 1.18 and it happens after running yarn upgrade, this is how you fix the issue without destroying your entire monorepo:

  1. DO NOT DELETE YOUR yarn.lock!! This will likely cause a cascade of compatibility issues for you app (issues scale exponentially based on the number of dependancies you have and how large/old your app is).
  2. Stash your changes with git stash or what ever code versioning you use if you have other changes beside the yarn upgrade. Your yarn.lock is corrupted beyond repair at this point.
  3. Hard reset back to before you ran yarn upgrade. - This restores the yarn.lock that is correct and still works.
  4. Optional: Reset your node environment. I use nvm, so I just bumped up to the newest LTS release. You don't have to do this, but if you're paranoid, it doesn't hurt.
  5. Remove your yarn > 1.18 and install yarn 1.18 globally. For some reason, just setting the yarn policy for the monorepo may not work (caused by certain operations using the global version, not the local policy version - I don't know which ones, but there's no margin for error when you are in this situation as you will have to start again if the wrong version of yarn runs even once during the upgrade operation). If you reset your node environment like me, you can just run npm i -g yarn@1.18.
  6. Optional: Run yarn policies set-version 1.18.0. We did this to ensure all developers are running 1.18 minimizing the chance of the lock file corrupting.
  7. Perform your yarn upgrade operations. Remember that if you upgrade the version of a dependency in one workspace, YOU MUST upgrade the dependency version in all workspaces, if you want to go back to a new version of yarn after upgrading. Even if you do not go back to a newer version of yarn this is still recommended to prevent mismatched dependency versions from running. We suspect that yarn workspaces is not resolving dependency trees correctly when different top level dependencies are specified across workspaces.
  8. Optional: Restore the newer version of yarn if you need to.
  9. Apply your git stash, but reject all yarn.lock and package.json changes in your stash as those are corrupted.

Anyone who is currently running lerna and yarn workspaces should roll back to yarn 1.18 when performing upgrades just as a precaution. This issue seems to appear when you have multiple large projects in a single monorepo that share the same "complex" dependencies (with at least 2+ levels of dependencies below). When upgrading the dependencies in a single project, if the new version of the dependency has a new sub-dependency, yarn >= 1.19 does not resolve the new dependency tree correctly. Yarn's workspace checker appears to expects that all occurrences of the dependency throughout the monorepo to have the new sub-dependency, even though the other projects are using an older version that does not require it - at which point yarn breaks and refuses to perform any new upgrades.

Upgrading all instances of the dependency to the same new version using yarn 1.18 allows the yarn.lock file to be structured correctly. Then after the upgrade, you can switch back to the newest version of yarn and everything should work fine.

For larger teams, I recommend performing the upgrades in a container (e.g. docker.) This ensures the environments are clean and the yarn.lock files resolve and generate correctly, without having to alter your local environment.

thefat32 commented 3 years ago

Does yarn team acknowledge this as a bug? Or we are doom to use older yarn version in our repos?

kylemh commented 3 years ago

I think the guidance from the yarn team is to move to yarn 2.

bryanvaz commented 3 years ago

Not sure if it is a bug present in yarn@2, but we're testing yarn@2 next week under the same conditions to see if the same error crops up.

If anyone else has encountered this error, try upgrading to yarn@2. It would be useful to have more than 1 datapoint.

Also note that you have to revert back to yarn@1.22 before you can upgrade to yarn@2. yarn@1.18 does not have the yarn set command needed to upgrade using the defined migration instructions

Cheers, Bryan

schmod commented 3 years ago

If you're one of the many who are impacted by this issue, don't get your hopes up for a bugfix – Yarn 1 appears to have been quietly deprecated, and the maintainers no longer appear to be reviewing pull-requests in this repository.

If Yarn 2 doesn't work for you, consider switching to npm or pnpm.

mike-marcacci commented 3 years ago

FWIW I've slowly come to understand this bug more. It's very much still alive.

In particular the flaw appears to be a discrepancy between yarn planning dependencies and then executing. On a multi-workspace repo, I often run into this when performing a yarn upgrade-interactive --latest. However, running a yarn upgrade followed by another yarn upgrade-interactive --latest will almost always allow it to proceed. Interestingly, running another yarn upgrade often updates the lockfile further.

Essentially, my workaround is to alternate between yarn upgrade-interactive --latest and yarn upgrade until they succeed and make no further changes.

What I think is happening:

  1. yarn calculates its target optimized directory structure based on the latest matching versions
  2. yarn then plans its order of installation based on the assumption that transitive dependencies will exist at the "optimal" (flattest) level
  3. yarn executes upgrades on each workspace, one-at-a-time and the mismatches between the some-new/some-old workspaces results in nested dependencies (instead of the optimized "flatter" structure it was expecting), which are not available to subsequent workspaces

This is all speculation based on behavior and without any understanding of yarn's internals... but it follows what I imagine could be an easy false assumption when writing an optimizer and execution planner, and I figured I would toss this here in case it nudges somebody more familiar with the codebase toward a solution.

vitorgt commented 3 years ago

Not sure if it is a bug present in yarn@2, but we're testing yarn@2 next week under the same conditions to see if the same error crops up.

@bryanvaz do you have any update on this? How did it went?

varl commented 3 years ago

Your mileage may vary on this one.

We've had success using yarn-deduplicate to "solve" this situation: https://github.com/atlassian/yarn-deduplicate

Eventually the yarn.lock grows in complexity again and the problem pops up, so then we need to run npx yarn-deduplicate yarn.lock again to resolve it.

It changes your lockfile so some form of regression testing is necessary.

khaledosman commented 3 years ago

200 IQ "fix": instead of using yarn add, manually add the dependency to package.json :shipit:

giautm commented 2 years ago

200 IQ "fix": instead of using yarn add, manually add the dependency to package.json :shipit:

I ran into this issue when running expo upgrade in a mono-repo, which upgrades ~30 packages for given SDK version. I can't change every package version. So, the only way to do is downgrade yarn to 1.18.0 with yarn policies set-version 1.18.0

prkeshri commented 2 years ago

Did npm install at the end. Phew!

dwiyatci commented 2 years ago

I have been experiencing the same issues since v1.19. yarn upgrade-interactive became unusable; It would fail to apply the version updates.

After updating to v1.21 I'm not able to yarn install anymore. It always throws this error:

expected workspace package to exist for ...

Downgrading to 1.18 fixed both issues.

I should point out that these problems only occur on one project, which is a monorepo that uses lerna and yarn workspaces.

Upgrading to 1.22.17 seems to fix this yarn upgrade-interactive issue for me. I'm also in a monorepo project of lerna with yarn as its npmClient and workspaces. 🚝

richardvanbergen commented 2 years ago

Downgrading also works for me although I decided that it'd be better to upgrade to berry in the end. Works fine as long as you're not using zero-installs which I have never managed to make work.

denik1981 commented 2 years ago

An external but simple workaround would be to use volta (a toolchain manager) and pin to the downgrade version of yarn you need. This, of course, is just another alternative to the many workaround solutions proposed on this thread.

Bec-k commented 2 years ago

This is still reproduced in latest yarn v1.22.17

error An unexpected error occurred: "expected workspace package to exist for \"@typescript-eslint/eslint-plugin\"".
info If you think this is a bug, please open a bug report with the information provided in "/var/local/acp/dashboards/packages/vpn/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
ashalfarhan commented 2 years ago

200 IQ "fix": instead of using yarn add, manually add the dependency to package.json :shipit:

Still facing the same issue here, this is the only workaround that I've tried and works perfectly fine.

yarn: 1.22.17 node: 16.13.2

cliffordfajardo commented 2 years ago

instead of using yarn add, manually add the dependency to package.json :shipit: Resolved by doing this ^, same as last comment ^

yarn: 1.22.17 node: 16.13.2

tbergquist-godaddy commented 2 years ago

yarn upgrade-interactive is still failing for me

yarn: 1.22.17 node: 16.13.2 && 16.14.0

aclopezcertinia commented 2 years ago

Still failing for me... yarn workspace test add chance error An unexpected error occurred: "expected workspace package to exist for \"@jest/core\"". Yarn: 1.22.17 Node: 16.14.0

askreet commented 2 years ago

Is downgrading yarn three minor versions using yarn policies still the workaround for this? Worked for me, but seems "bad".

mattvb91 commented 2 years ago

just as an fyi for people still posting in here. This repo is abandoned by the devs and the new "yarn" is over here: https://github.com/yarnpkg/berry

we had to drop yarn completely due to no longer having an upgrade path

rogerprz commented 2 years ago

I deleted my yarn.lock file and and re-installed. That worked for me.

kimmy-wang commented 2 years ago

same issue

dwiyatci commented 2 years ago

I deleted my yarn.lock file and and re-installed. That worked for me.

Avoid deleting/re-creating yarn.lock file (it's there for a reason). Instead, try to dedupe its entries and then (re-)install.

EDIT (thanks to @as-zlynn-philipps for noticing my typo 😅 ):

npx yarn-deduplicate && yarn install
as-zlynn-philipps commented 2 years ago

Avoid deleting/re-creating yarn.lock file (it's there for a reason). Instead, try to dedupe its entries and then (re-)install.

npx yarn-dedupe && yarn install

I have been a full-stack developer for 10 years. I only ever delete my yarn.lock as an absolute last resort. Trust me. And, in the last two months, I've encountered about 5 separate situations where that was the only thing that fixed a work-blocking issue. The issue in this thread is the latest one. The fix is as inexplicable as each issue itself.

So, at this point, I'm pretty desensitized to it. If deleting the yarn.lock saves hours of scouring GitHub issues, docs, and StackOverflow, just heckin' do it. If all your deps are following semver and your ranges are properly scoped in package.json, shouldn't break anything to just get the latest acceptable version of everything. That's my two cents. 🤷‍♂️

Re: deduping, this package actually dedupes: https://medium.com/@bnaya/yarn-deduplicate-the-hero-we-need-f4497a362128

askdesigners commented 2 years ago

It's almost like all this javascript tooling is insane :)

Millions of packages for every conceivable thing, hundreds of megabytes of node_modules, tens of thousands of files... Yarn workspaces seems like it's yet another layer of indirection and abstraction on top of an already hideously complicated stack.

Devp00l commented 2 years ago

I had this problem and could solve it by setting all corresponding packages in all workspaces to the same version.

Nantris commented 2 years ago

@as-zlynn-philipps the package you mentioned yarn-dedupe is probably not the one you meant to mention yarn-deduplicate: https://www.npmjs.com/package/yarn-deduplicate

dwiyatci commented 2 years ago

@as-zlynn-philipps the package you mentioned yarn-dedupe is probably not the one you meant to mention yarn-deduplicate: https://www.npmjs.com/package/yarn-deduplicate

@Slapbox That was my mistake. He actually corrected it :)

elenamik commented 2 years ago

It stopped failing for me once I fixed my workspace names.

package.json
packages/
    hardhat/
        package.json
     ui/
        package.json

root package.json:

  "name": "web3-boilerplate",
  "private": true,
  "workspaces": ["packages/ui", "packages/hardhat"],
   //etc

packages/hardhat/package.json:

    "name": "@web3-boilerplate/hardhat",
    "version": "1.0.0",
    //etc

I previously had named the packages just hardhat and ui and this seems to have been causing the issue. Changing to @web3-boilerplate/hardhat and @web3-boilerplate/ui seems to have fixed it!!

kelzenberg commented 2 years ago

Instead of downgrading yarn to 1.18.0, @elenamik solution fixed it for us. It was a workspace package.json name: "@..." issue. Thanks!

Also don't forget to remove your yarn.lock.

leonheess commented 2 years ago

How can this be? Yarn is broken for 8 months now and nothing is happening? Is the project dead?