Installing package from a branch with yarn.lock present installs old version of package
#4,734 opened on Oct 18, 2017
Description
This is bug report and a follow-up to issue #2280.
Problem:
When installing from a git branch with a correct yarn.lock file present, yarn installs an old version of the package instead of the most recent commit (HEAD), as defined in yarn.lock.
This can lead to inconsistent install states when when using a yarn.lock file.
Steps to reproduce:
The following steps simulate a case where a package has been installed from the master branch of a GitHub repository, and what happens when a user tries to install a new commit to master using yarn.
-
Create a new repository with a single commit of a file
version_1.0and push it to GitHub (in this exampleaskielboe/yarn-bug). -
Create a
package.jsonfile with the following content:
{
"name": "yarn-bug",
"version": "1.0.0",
"dependencies": {
"yarn-bug": "askielboe/yarn-bug#master"
}
}
-
Run
yarn installat the location ofpackage.json: yarn correctly installs the package with a single filenode_modules/yarn-bug/version_1.0. -
Create a new commit in
yarn-bugadding a fileversion_2.0, and push the commit to GitHub. -
Run
yarn installagain: yarn installs from cache without checking for new commits at the origin. This means that the yarn installation ofyarn-bugwill not include theHEADcommit with the new fileversion_2.0. -
Delete
node_modulesandyarn.lockand runyarn install: Withyarn.lockgone, yarn now correctly pulls the latest commit and generates a newyarn.lockfile with the HEAD hash. -
Delete
node_modules(but keepyarn.lock) and runyarn install: yarn now installs the initial commit again, instead of the HEAD commit referenced inyarn.lock. Somehow yarn looks at its cache and picks a commit (by random?) to install instead of checking whether it is the HEAD, or the most recent pulled commit.
Notes & workarounds:
-
Running
yarn upgradeafter deletingnode_moduleswill install the correct version of the package. -
Clearing the yarn cache by running
yarn cache clean yarn-bugand thenyarn installwill install the correctHEADcommit without the need to deleteyarn.lock. -
Even when using
--frozen-lockfileyarn still installs an old commit (ignoring the correct hash inyarn.lock). This can lead to inconsistent state and critical issues in continuous integration, when requiring packages from a git branch.
The bug(s) can be reproduced using the following script: (replace askielboe/yarn-bug with a reference to your own test-repository). Please read through the script before running it, as it creates new directories and clears the yarn cache: https://gist.github.com/askielboe/0c0bc3fdf88193bb17fc4fef5d332ad1
This bug report has been created using yarn 1.2.1 and node 8.7.0 on macOS 10.12.6.