Skip to content

Pnpm doesn't set npm_config_user_agent if lockfile is already up to date #12003

Description

@eagarwal-notion

Verify latest release

  • I verified that the issue exists in the latest pnpm release

pnpm version

No response

Which area(s) of pnpm are affected? (leave empty if unsure)

Package manager compatibility

Link to the code that reproduces this issue or a replay of the bug

main...eagarwal-notion:pnpm:eagarwal-pnpm-lifecycle-scripts-env-var

Reproduction steps

Run the test from that commit.
cd pnpm && pnpm test test/install/lifecycleScripts.ts

Basically:

  1. Create a pnpm-workspace.yaml with
  • At least 1 dependency
  • optimisticRepeatInstall set to true.
  • A pre-install script which prints the value of npm_config_user_agent
  1. Create an up to date lockfile (IE: run pnpm install --lockfile-only) but don't actually install any dependencies
  2. Run pnpm install again
  3. Check if npm_config_user_agent is defined/not empty
    Running the test locally, the npm_config_user_agent variable is undefined.
  ● lifecycle script runs with the correct user agent with dependencies (lockfile exists = true)

    expect(received).toContain(expected) // indexOf

    Expected substring: "pnpm/11.4.0 "
    Received string:    "Lockfile is up to date, resolution step is skipped
    Progress: resolved 1, reused 0, downloaded 0, added 0
    Packages: +1
    +
    Progress: resolved 1, reused 0, downloaded 1, added 1, done·
    dependencies:
    + is-positive 1.0.0·
    undefined
    Done in 469ms using pnpm v11.4.0
    "

      56 |   expect(result.status).toBe(0)
      57 |   const expectedUserAgentPrefix = `${pnpmPkg.name}/${pnpmPkg.version} `
    > 58 |   expect(result.stdout.toString()).toContain(expectedUserAgentPrefix)
         |                                    ^
      59 | })
      60 |
      61 |

      at test/install/lifecycleScripts.ts:58:36

Describe the Bug

We have a preinstall script which reads npm_config_user_agent to determine if its run with pnpm or npm.

If the lockfile already exists but we still need to install dependencies, pnpm doesn't set npm_config_user_agent when running pnpm install. This breaks our preinstall script.

Expected Behavior

npm_config_user_agent is set correctly even if the lockfile exists

Which Node.js version are you using?

24.6.0

Which operating systems have you used?

  • macOS
  • Windows
  • Linux

If your OS is a Linux based, which one it is? (Include the version if relevant)

Debian

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions