Skip to content

[Feature] Tell lifecycle scripts when pack or publish is a dry run #7323

Open

Description

@privatenumber
  • 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 # undefined

The 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-known npm_* 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-run flags of both commands.

Describe alternatives you've considered

  • A contrib plugin: a plugin can add variables with the setupScriptEnvironment hook, but the hook receives only the project and the environment. It can't see the --dry-run flag of the core pack and npm publish commands.
  • The beforeWorkspacePacking hook: a plugin can change the packed manifest without touching the file on disk. That covers package.json changes, but it is Yarn-specific, so tools that support npm, pnpm, and Yarn run from prepack.
  • 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions