Repository navigation
Description
- I'd be willing to implement this feature (contributing guide)
- This feature is important to have in this repository; a contrib plugin wouldn't do
Describe the user story
Some prepack scripts modify files in the working tree. For example, clean-pkg-json removes development-only fields from package.json before packing. During a dry run, these scripts should leave the working tree unchanged.
yarn pack --dry-run and yarn npm publish --dry-run run prepack, but the script can't tell that it's a dry run. So a dry run modifies files the same way a real pack does.
Reproduction with Yarn 4.18.1:
{
"name": "dry-run-env",
"version": "1.0.0",
"packageManager": "[email protected]",
"scripts": {
"prepack": "node -p process.env.npm_config_dry_run > prepack-output.txt"
}
}yarn pack --dry-run
cat prepack-output.txt # undefinedThe full environment of prepack is the same for yarn pack --dry-run and yarn pack, except for per-run temporary paths. In the source, prepareForPack runs prepack and postpack without the command's dryRun option (packUtils.ts#L56-L68). This is unchanged on master.
Describe the solution you'd like
When yarn pack or yarn npm publish runs with --dry-run, set npm_config_dry_run=true in the environment of the prepack and postpack scripts.
npm sets this variable for npm pack --dry-run and npm publish --dry-run, and pnpm 10 did too. Using the same name means existing scripts work with Yarn without changes.
Describe the drawbacks of your solution
- It adds an
npm_config_*variable, while Yarn otherwise sets only a small set of well-knownnpm_*variables. A Yarn-specific name would avoid that, but scripts would then need to check a different variable for each package manager. - It is one more variable to keep in sync with the
--dry-runflags of both commands.
Describe alternatives you've considered
- A contrib plugin: a plugin can add variables with the
setupScriptEnvironmenthook, but the hook receives only the project and the environment. It can't see the--dry-runflag of the corepackandnpm publishcommands. - The
beforeWorkspacePackinghook: a plugin can change the packed manifest without touching the file on disk. That coverspackage.jsonchanges, but it is Yarn-specific, so tools that support npm, pnpm, and Yarn run fromprepack. - Reading the parent process's arguments from the script: this depends on the platform, and it breaks when Yarn runs through Corepack or another wrapper.
- Asking users to set their own variable, such as
MY_DRY_RUN=1 yarn pack --dry-run: users have to remember it, and forgetting it modifies their files.
Activity