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.37k stars 2.72k forks source link

yarn workspace fails to build app in Docker container with react vite #9048

Closed karolis-666 closed 2 months ago

karolis-666 commented 2 months ago

Weird issues using yarn workspace when trying to build application inside Docker container. Note: we are using react with vite for front-end

An example of commands being run: RUN yarn build:admin throws error Error: [vite]: Rollup failed to resolve import "@global/theme/theme.css" RUN yarn workspace admin build throws error Unknown workspace "admin" (note commands are same as declared in package.json, but return different errors)

Locally running yarn admin or yarn portal starts local dev server and applications load as expected with "@global/theme/theme.css" being loaded fine.

If we remove @global/theme/theme.css and build app, it's valid, so we suspect issue lies within how packages are handled during build process

Our root file structure looks like this:

/app-admin:
/app-portal
/global
package.json

Our package.json (root)(excluding dependencies):

  "name": "frontend-monorepo",
  "version": "1.0.0",
  "packageManager": "yarn@3.6.4",
  "private": true,
  "workspaces": [
    "admin",
    "portal",
    "global/*"
  ],
  "scripts": {
    "test": "",
    "admin": "yarn workspace admin dev",
    "portal": "yarn workspace portal dev",
    "build:admin": "yarn workspace admin build",
    "build:portal": "yarn workspace portal build"
  },

Package.json of apps (admin & portal):

{
  "name": "app-insight",
  "version": "1.0.0",
  "main": "./src/index.tsx",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build"
  },
  "dependencies": {
    "@global/theme": "workspace:^"
  }
}

We are also adding Docker file as an example (currently used for testing, but fails with build):

FROM node:21.7.2 as deps
ARG BUILD_CONTEXT
WORKDIR /base
COPY package.json ./
COPY yarn.lock ./
RUN yarn install --immutable

FROM node:21.7.2 as builder
WORKDIR /base
COPY --from=deps /base/node_modules ./node_modules
COPY . .
RUN ls
RUN yarn build:admin

CMD ["yarn", "test"]