Can’t leverage Docker cache when Yarn workspaces are used

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

What is the current behavior?
We usually structure our Dockerfiles to take advantage of the Docker cache mechanism, so we first copy package.json and yarn.lock, then run yarn --pure-lockfile and then copy the rest of the package.

Doing so, the dependencies, that change less often than the package source code, can be cached by Docker, and we can often avoid to have Yarn run on each build.

With Yarn workspaces, you can’t run yarn if you have just the package.json and the yarn.lock file, because it will complain about the missing workspace packages.

If the current behavior is a bug, please provide the steps to reproduce.

Have a Dockerfile like this:

FROM node:9-alpine
WORKDIR /app
COPY package.json .
COPY yarn.lock .
RUN yarn --pure-lockfile
COPY . .
RUN yarn test

What is the expected behavior?

There should be a way to tell Yarn to install all the dependencies and defer the packages linking to a later step once the whole source code is copied.
Doing so we can reduce the repetitive task to just the packages linking, and cache the installation/fetching of the dependencies.

Author: Fantashit

3 thoughts on “Can’t leverage Docker cache when Yarn workspaces are used

  1. I am currently struggling with caching in a multi-stage build. Since it’s not possible to use COPY to match files with a glob pattern and preserve the directory layout, I’ve decided to manually copy each individual package.json from my workspace packages, do a yarn install and the copy everything into the final image.

  2. +1 I’m thinking of adding a flag like

    yarn --no-workspace
    

    then

    yarn --link-workspace
    

    So that the prior command can be cached.
    If this seems right, I’ll raise a PR for the same.

Comments are closed.