yarnpkg / berry

📦🐈 Active development trunk for Yarn ⚒
https://yarnpkg.com
BSD 2-Clause "Simplified" License
7.42k stars 1.11k forks source link

[Bug?]: The nearest package directory (/) doesn't seem to be part of the project declared in /. (docker) #5380

Open Joelasaur opened 1 year ago

Joelasaur commented 1 year ago

Describe the bug

I'm seeing this issue https://github.com/yarnpkg/berry/issues/2212 happening while building a docker image. Opening a new issue thread because the solution in the resolved issue was related to a nested yarn project, but this is happening in the root directory of a docker container, which doesn't make any sense to me.

Here is a simplified version of the dockerfile:

ARG NODE_VERSION='18.15.0'
ARG YARN_VERSION='1.22.19'
ARG CHROME_VERSION='107.0.5304.121-1'
ARG CYPRESS_VERSION='12.1.0'
FROM cypress/factory

ARG NPM_TOKEN

ENV GH_REGISTRY_TOKEN=${NPM_TOKEN}

COPY --chown=node:node .yarn ./.yarn
COPY --chown=node:node .yarnrc.yml ./.yarnrc.yml
COPY --chown=node:node packages ./packages

COPY --chown=node:node apps/my-app/package.json ./
COPY --chown=node:node yarn.lock ./

RUN yarn set version 3.5.0

RUN yarn add @myCompany/auth@link:./packages/myPackage

RUN yarn install

ENTRYPOINT ["tail", "-f", "/dev/null"]

Project folder structure (this is a monorepo with multiple workspaces): Root

The package.json is located in a subdirectory workspace, but you can see we are copying it to the root of the image via

COPY --chown=node:node apps/my-app/package.json ./

Here is the error:

 ---> bc35d40a58b6
Step 20/22 : RUN yarn add     @myCompany/auth@link:./packages/myPackage 
 ---> Running in 85d4a0e7229c
Usage Error: The nearest package directory (/) doesn't seem to be part of the project declared in /.

- If / isn't intended to be a project, remove any yarn.lock and/or package.json file there.
- If / is intended to be a project, it might be that you forgot to list  in its workspace configuration.
- Finally, if / is fine and you intend  to be treated as a completely separate project (not even a workspace), create an empty yarn.lock file in it.

This option seems to apply to me:

 If / is intended to be a project, it might be that you forgot to list  in its workspace configuration.

but to be honest I'm not sure what it means or where to start debugging this.

Thank you for reading!

To reproduce

My apps/my-app/package.json file looks like this:

{
  "name": "my-app",
  "private": true,
  "author": "",
  "workspaces": [
    "apps/*",
    "packages/*",
    "examples/*"
  ],
  "devDependencies": {
    "@myCompany/myPackage": "workspace:*"
  },

  "packageManager": "yarn@3.5.0",
  "volta": {
    "node": "16.18.0"
  }
}

Then packages/myPackage/package.json looks like this:

{
  "name": "@myCompany/myPackage",
  "source": "./src/myPackage.ts",
  "main": "./dist/cjs/myPackage.js",
  "types": "./dist/es/myPackage.d.ts",
  "module": "./dist/es/myPackage.js",
  "sideEffects": false,
  "exports": {
    ".": {
      "types": "./dist/es/myPackage.d.ts",
      "import": "./dist/es/myPackage.js",
      "require": "./dist/cjs/myPackage.js",
      "node": "./dist/cjs/myPackage.js"
    }
  },
  "typesVersions": {
    "*": {
      "*": [
        "dist/es/myPackage.d.ts"
      ]
    }
  },
  "files": [
    "dist/**/*",
    "src/**/*"
  ],
  "scripts": {
    "build": "tsc -b ./tsconfig.json ./tsconfig.cjs.json",
    "test": "jest"
  },
  "dependencies": {
    "cross-fetch": "^3.1.5",
    "jotai": "^2.0.3"
  },
  "peerDependencies": {
    "@myCompany/myOtherPackage": "workspace:^",
    "react": "^17.0.2"
  },
  "devDependencies": {
    "@types/jest": "^29.4.0",
    "@types/react": "^18.0.28",
    "@myCompany/myOtherPackage": "workspace:^"
  }
}

Environment

Step 14/17 : RUN yarn dlx -q envinfo --preset jest
 ---> Running in 4378cbbe8af2

  System:
    OS: Linux 5.15 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
    CPU: (6) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 18.15.0 - /tmp/xfs-356d500a/node
    Yarn: 3.5.0 - /tmp/xfs-356d500a/yarn
    npm: 9.5.0 - /usr/local/bin/npm

Additional context

No response

Joelasaur commented 1 year ago

I am now getting this exact same error in another repository that is NOT a monorepo, so I don't think this has anything to do with yarn workspaces.

kubmir commented 1 year ago

I am getting the same error for workspaces. Yarn 3. Package.json and yarn.lock are copied together with yarnrc

CassandraCat commented 1 year ago

I met the same problem, how to solve it?

forest1102 commented 1 year ago

How to solve it? It is very frustrating problem.

robertknight commented 1 year ago

I encountered this error with Yarn v3.6.1 using a Dockerfile that looked as follows:

FROM node:20.2.0-alpine as frontend-build

ENV NODE_ENV production
RUN mkdir -p /tmp/frontend-build
COPY .babelrc rollup.config.mjs gulpfile.mjs package.json tailwind.config.mjs .yarnrc.yml yarn.lock ./
COPY .yarn .yarn
COPY via/static /via/static

RUN yarn install --immutable
RUN yarn build

I was able to workaround the issue by copying the files into a sub-directory of root (/tmp/frontend-build) and running yarn install there:

FROM node:20.2.0-alpine as frontend-build

ENV NODE_ENV production
RUN mkdir -p /tmp/frontend-build
COPY .babelrc rollup.config.mjs gulpfile.mjs package.json tailwind.config.mjs .yarnrc.yml yarn.lock /tmp/frontend-build
COPY .yarn /tmp/frontend-build/.yarn
COPY via/static /tmp/frontend-build/via/static

WORKDIR /tmp/frontend-build
RUN yarn install --immutable
RUN yarn build
ivanafanasyeu commented 8 months ago

try to delete yarn.lock and create an empty yarn.lock and run the command again

Acexhat commented 2 months ago

Creating a new yarn.lock resolved the problem!!

information-security commented 1 month ago

Creating an empty yarn.lock file is not a practical solution for all cases. For instance, it breaks NextJS (yarn create next-app) installation in the middle and I have to manually create the lock file and then issue yarn install to complete the installation. So in my heck opinion, the ultimate solution to this issue is:

  1. Enable corepack if you already didn't:

    corepack enable

  2. Change corepack's good known repo for the yarn from v1 to stable:

    corepack install -g yarn@stable

  3. No need for yarn set version berry from now on and therefore no package.json nor .yarnrc files will be generated in the upper directories of your project.

    2212 (just a reference for other potential readers directed by search engines to that issue)