dockerfile-rails icon indicating copy to clipboard operation
dockerfile-rails copied to clipboard

Yarn handling in the Dockerfile doesn't account for modern installs

Open jamesmeneghello opened this issue 11 months ago • 5 comments

During the build, the dockerfile will pull across the node_modules directory to the final image. This has a few ramifications:

  • Assumes Yarn 1/2 is being used
  • Yarn 3 pnp stores packages in .yarn instead
  • Yarn 3 can be set to use the node_modules linker which still stores packages in node_modules, but requires overriding through .yarnrc.yml, which is also not copied across in the Dockerfile

This should probably be a config option (yarn classic/modern):

Classic: copies node_modules across Modern: copies .yarn and .yarnrc.yml, and respects node-linker, which means copying either node_modules or .yarn across

jamesmeneghello avatar Jan 21 '25 04:01 jamesmeneghello

Instead of a config option, is it possible to tell via inspecting the contents of the files on the machine what the right thing to do is?

rubys avatar Jan 21 '25 15:01 rubys

Yes - the presence of .yarnrc.yml indicates the user is running Yarn 3+. Parsing .yarnrc.yml and pulling out the value of nodeLinker (either node-modules for legacy behaviour or pnp for modern behaviour, default pnp if no value) will let you determine whether the we need to copy .yarn/ (modern, nodeLinker: pnp), node_modules/ (classic), or both (modern, nodeLinker: node-modules).

jamesmeneghello avatar Jan 21 '25 22:01 jamesmeneghello

A typical Dockerfile produced for a Rails application using a jsBundler looks like the following:

https://github.com/fly-apps/dockerfile-rails/blob/main/test/results/esbuild/Dockerfile#L74-L76

What options are you specifying?

rubys avatar Jan 22 '25 00:01 rubys

I realised I was missing that info yesterday when I was thinking about it - this is specifically for --precompile=defer. In fairness that's an edge case, and so the purpose of this issue may just be document the issue for others to come across, rather than provide a solution.

jamesmeneghello avatar Jan 23 '25 03:01 jamesmeneghello

To the contrary, that's totally fair. When I started looking into it I came to the conclusion that it would only apply in that case, and was wondering if I was missing something.

rubys avatar Jan 23 '25 03:01 rubys