nodejs / corepack

Zero-runtime-dependency package acting as bridge between Node projects and their package managers
MIT License
2.52k stars 165 forks source link

feat: store `packageManager` property also inside the lock file #465

Open ruscon opened 5 months ago

ruscon commented 5 months ago

Our current flow: We use docker + pnpm fetch

COPY --chown=node:node pnpm-*.yaml .npmrc ./
RUN npm install -g pnpm@^8 # curernt usage without corepack
# RUN corepack enable #  <= but we want this one
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm fetch --loglevel=warn --ignore-scripts

This means we don't cache the package.json file before the pnpm fetch execution. This allows us to worry about package.json changes. But corepack will always install the latest version of pnpm instead of a specific one.

If store the packageManager property in a lock file, this will solve the problem.

styfle commented 5 months ago

This sounds like it should be implemented in pnpm and not corepack

ruscon commented 5 months ago

@styfle Perhaps, but I don’t know how corepack works under the hood. Most likely some kind of layer is needed for interaction between corepack and a package manager, so that the package manager itself updates its lock file?

aduh95 commented 5 months ago

Have you considered setting a packageManager in your package.json? That way, Corepack would pick up the correct version when run inside the container. I'm not sure I understand why package.json couldn't be the "lock file" in this case.

ruscon commented 5 months ago

@aduh95 The question is related to how the docker cache and pnpm fetch work. pnpm fetch and npm ci can be run without a package.json file, which helps cache the installation of npm packages inside the docker image even if you make changes to the package.json file.

aduh95 commented 5 months ago

I see, pardon my ignorance, I should have read the docs. From what I can see, pnpm docs recommend downloading a specific version of pnpm with cURL globally. The equivalent with corepack would be to run corepack install -g pnpm@8.x. So IIUC, what you're requesting would be to have a way to skip the command and have pnpm fetch select the correct version. I don't know enough pnpm internals to tell how easy/hard that would be to implement, but I agree that would require changes on pnpm side – so that package.json it generates contains the correct "packageManager" field.