berry: The nearest package directory doesn't seem to be part of the project
Description
We have a monorepo with a package.json in the root that lists the workspaces:
"workspaces": [
"packages/core/*",
"packages/react/*",
],
I wanted to upgrade our version of Yarn so I removed all node_modules and our yarn.lock in root and tried yarn after the upgrade but got the following error:
Usage Error: The nearest package directory ({path}) doesn't seem to be part of the project declared in {path}.
This used to work fine with .yarn/releases/yarn-berry.js but not with .yarn/releases/yarn-2.4.0.cjs.
Solution
I created an empty yarn.lock in the root and tried yarn again and everything worked.
This was all a bit unintuitive. I’m not sure if maybe the error should suggest that as a solution or if it should just create one for me(?) but thought I’d raise for discussion.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 23 (14 by maintainers)
The error happens when you have a
yarn.lock(orpackage.json) file higher up in the filetree, at that point Yarn thinks you’re in a workspace you haven’t declared and throws.Maybe clearer expression of my idea is: Yarn 2 tries to exercise control outside of the current project upwards, without boundaries, I think it is not right. Yarn should exercise control inside project boundaries and it should not bother Yarn what is upwards in directory hierarchy, if there is
yarn.lock,package.jsonor anything else upwards in the directory hierarchy. In order to exercise control within project boundaries Yarn should not miss real project boundary and go upwards past it, how to do it right is a separate question.Well I’ll be blowed! It was moaning about my home directory which to my knowledge didn’t have a
yarn.lockin, but I just checked and there was a stray one there for some unknown reason 😅I do wonder if there is a way to make this all a bit clearer. If nothing else, at least this issue is here for others to stumble across if they face the same error I guess.
@arcanis To be honest I think this behaviour is part of bad UX. I think it’s pretty uncommon for commands to search for themselves up the directory tree for files to determine if the current directory is a workspace or a root-dir. That’s confusing to people. Better here would be to chose a good standard value. And I think it would be better to assume that the current directory is the always the
rootand if I want to change that behaviour let me use a flag like--workspaceto actively search up the directory tree. And the error message should be also more clear, imho.Thanks, that’s helpful! So this is what we printed:
I think the key point from your message is that the project directory wasn’t right. The error referenced
/home/projectas project directory, with/home/user/projectbeing the package directory. However I can see that being confusing, the nuance is fairly subtle when you just want to use a random project.Perhaps the following (plus colors, etc) would be clearer?
I’m not sure blocking it altogether in
findProjectCwdis a good idea since it’s called before each command, but we can at least check that before throwing the error, to make it even more to the point.The detection is a problem. It is not the only complaint, we see them again and again. And there were no such complaints for v1. No matter how well the error message will be done, the problem will not be solved, because it is not in the error message.
Which “special” cases? It was not a problem in v1, was it? The packages in the n_m folders are in n_m folders, we can detect that and take into account.