berry icon indicating copy to clipboard operation
berry copied to clipboard

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

Open Joelasaur opened this issue 1 year ago • 8 comments

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

  • .yarn
  • yarnrc.yml
  • packages
  • yarn.lock
  • apps/my-app/package.json

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": "[email protected]",
  "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 avatar Apr 11 '23 13:04 Joelasaur

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.

Joelasaur avatar Apr 12 '23 22:04 Joelasaur

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

kubmir avatar Apr 17 '23 13:04 kubmir

I met the same problem, how to solve it?

CassandraCat avatar May 06 '23 14:05 CassandraCat

How to solve it? It is very frustrating problem.

forest1102 avatar Jul 08 '23 15:07 forest1102

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

robertknight avatar Aug 01 '23 16:08 robertknight

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

ivanafanasyeu avatar Feb 05 '24 12:02 ivanafanasyeu

Creating a new yarn.lock resolved the problem!!

Acexhat avatar Aug 17 '24 19:08 Acexhat