Skip to content

[BUG] Ineffective caching of git dependencies #5170

Open

Description

@kristoffer-zliide

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

After upgrading from Node 14 to Node 16.16, we're seeing an extreme increase in npm install times, in one case going from taking seconds to taking 17 minutes! We have a handful of git dependencies in those projects, built with TypeScript in their prepare script. I believe what made this process fast, and ultimately extremely useful, in Node 14 as that the packages were cached after the prepare step, which seems, in some cases, not to be the case after upgrading. In other cases, the cache is populated but seems to not be used, and sometime an update is not even properly performed.

Expected Behavior

npm install should be correct and fast by caching prepared git packages.

Steps To Reproduce

I have created a minimal project with a 30 second prepare script here and here, and a project that depends on it and can write the version of the dependency to the console here. The dependency there was was created with npm install git+ssh://git@github.com/kristoffer-zliide/npm-git-dep-test.git#9e32a70222842ce2033fb876d4af8182f369f22b with the output of "changed 1 package, and audited 2 packages in 3m", so even though the prepare step takes 30 seconds, installing the package takes more than four times that. Interestingly, running npm install without arguments changes git+ssh: to github:, but seemingly does nothing else, which is possibly also a bug. The project prints out "Version 1" as expected. Now

  1. Change the git hash from 9e32a70222842ce2033fb876d4af8182f369f22b (Version 1) to 42146c03ce8b0d80de39d099baee5a2f1c979f89 (Version 2) in package.json
  2. Run npm install - again this takes more than four times the expected 30 seconds
  3. node index.js confirms that we're on Version 2
  4. Change the git hash from 42146c03ce8b0d80de39d099baee5a2f1c979f89 back to 9e32a70222842ce2033fb876d4af8182f369f22b in package.json
  5. Run npm install - this takes more than 2 minutes and it used to be almost instant on Node 14.

In the above scenario, an integrity property is added to the package-lock.json file with value sha512-iWdHaexWcOA0KPZtqFHI89eCkA34AB3wQCBJ8sP4Umcxqt0jWzL1N3o0KPB2vZD6+AqW03q/hn1i31Y3hmgvHQ==, and the prepared package is indeed to be found in npm-cache\_cacache\content-v2\sha512\89\67\ (confirmed with tar -xf 476...).

Clearly, there's some special handling of github URLs at play, so also (without the steps above) try

  1. Uninstalling the package with npm uninstall npm-git-dep-test
  2. Install the package from the other repo (git.zliide.com is a CNAME for bitbucket.org. The repo is public, but you need your ssh key to be added to a bitbucket account): npm install git+ssh://git@git.zliide.com/kristoffer_zliide/npm-git-dep-test.git#9e32a70222842ce2033fb876d4af8182f369f22b. This takes about one minute, now "only" twice the 30 seconds of the prepare script. Note that there's no integrity property, and that node index.js prints out Version 1
  3. Change the git hash from 9e32a70222842ce2033fb876d4af8182f369f22b to 42146c03ce8b0d80de39d099baee5a2f1c979f89 in package.json
  4. Run npm install - this takes no time, but also does nothing!!! node index.js still prints out Version 1. Is this a critical bug, or am I assuming too much of npm install?
  5. Run npm update - this fixes it, again in about 1 minute; node index.js prints out Version 2. Still no integrity property. Instead of editing package.json and running npm install, doing a npm install git+ssh://git@git.zliide.com/kristoffer_zliide/npm-git-dep-test.git#42146c03ce8b0d80de39d099baee5a2f1c979f89, gets you here as well, without that scary Step 4.
  6. Run npm install git+ssh://git@git.zliide.com/kristoffer_zliide/npm-git-dep-test.git#9e32a70222842ce2033fb876d4af8182f369f22b to downgrade to Version 1. This again takes about a minute and used to be almost instant on Node 14.

Environment

  • npm: 8.11.0
  • Node.js: 16.16.0
  • OS Name: Windows 11
  • npm config:
; "builtin" config from C:\Program Files\nodejs\node_modules\npm\npmrc

prefix = "C:\\Users\\...\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files\nodejs\node.exe
; node version = v16.16.0
; npm local prefix = C:\Users\...\Source\temp\npm-git-test
; npm version = 8.11.0
; cwd = C:\Users\...\Source\temp\npm-git-test
; HOME = C:\Users\...

Activity

  1. added
    Bug
    Needs Triage
    Release 8.x
    on Jul 14, 2022
  2. nlf commented on Jul 14, 2022

    @nlf
    Contributor

    the caching of git dependencies is something we know we need to address, you're definitely right it's ineffective as it is now

  3. added
    Priority 2
    and removed
    Needs Triage
    on Jul 14, 2022
  4. kristoffer-zliide commented on Oct 12, 2022

    @kristoffer-zliide
    Author

    I guess that's one way of putting it. Another would be that it's unusable and dangerously broken; TLDR:

    1. if we add a fairly benign git dependency the npm i command takes more than 17 minutes to run,
    2. if we change the version of a git dependency in package.json, and run npm i runs without any errors, but has not updated the dependency

    This is not an issue with Node 14, but is blocking us from upgrading to Node 16.

    @lukekarrys: sorry to drag you into this, but do you think this could have anything to do with it?

  5. olegade commented on Aug 24, 2023

    @olegade

    Any updates on this issue?

  6. Mxde59 commented on Aug 31, 2023

    @Mxde59

    Hi team,

    This has been bothering our dev team for over a year now ...
    Making a new git branch to overcome a cache problem is too time-consuming !
    Does anyone have the skills to resolve this?

    Thanks.

  7. olegade commented on Sep 8, 2023

    @olegade

    Hi.
    This issue is really a blocker for our team. Is anyone able to look into it?

    Thanks

  8. AndreVirtimo commented on Feb 9, 2026

    @AndreVirtimo

    This is still an issue and is slowing down our builds by minutes :-(

  9. ychampion commented on Nov 21, 2026

    @ychampion
    Contributor

    I tested #10251 with the two commits from this report, using a local Git mirror of the original dependency and its unchanged 30-second prepare. On Node 22.22.2, the parent (bd8b3390) kept returning version 1 after I changed the manifest to the version 2 commit. On 0781e2c1, the installed module returned 2 after the switch and 1 after switching back. The lockfile followed both pins.

    The repeated builds are still reproducible. With one cache for the whole sequence, the PR head ran prepare twice on the cold install, zero times on an unchanged npm install, twice on a clean npm ci at the same pin, and twice on returning to the previously built commit. Each two-run operation took about a minute. I counted invocations of the original prepare.js and called the installed module after each command.

    I also ran the original consumer with its public github: dependency. Both the parent and #10251 selected the right commit in both directions. On the PR head the cold install, switch to B, and return to A each ran prepare four times and took 123–124 seconds. An unchanged install ran it zero times. A warm-cache npm ci ran it twice in 62 seconds. The lockfile contained an integrity value in every case.

    Tracing the four invocations showed two temporary checkouts. In each one, npm’s nested install ran the script first, then pacote’s directory packer ran it again. The two invocations on a clean install came from that same pair in a single checkout.

    I reproduced the duplicate without running a consumer install. With the released pacote 22.0.1 from npm 12.2.3, one direct pacote.tarball() call on the pinned Git mirror ran the script twice (61 s). Packing the same source directory ran it once (30 s), and reading the built tarball ran it zero times. All three produced a module that returned 1.

    One constraint on that fix: skipping the packer’s prepare whenever the child install exits 0 cut the reporter’s Git fetch from 61 s to 31 s and produced a byte-identical tarball. With Yarn 4.18.1 as npmBin, or with npmInstallCmd set to install an explicit local dependency, the child exited 0 without running the root’s prepare. Both modified tarballs packed successfully but failed to load with MODULE_NOT_FOUND. Stock pacote ran the packer hook once in both cases and the modules loaded.

    Would you welcome a separate PR in npm/pacote for running prepare only once when packing a Git dependency, with a regression that counts invocations? That would address the duplicate build within one fetch. Reusing a prepared build across subsequent clean installs could stay tracked here.

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

    BugPriority 2Release 8.xarea:git-dependencies

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions