Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lerna --version incorrectly updates package-lock.json generated with npm@7 #2832

Closed
TomaszG opened this issue Feb 26, 2021 · 11 comments · Fixed by #3091
Closed

lerna --version incorrectly updates package-lock.json generated with npm@7 #2832

TomaszG opened this issue Feb 26, 2021 · 11 comments · Fixed by #3091

Comments

@TomaszG
Copy link

TomaszG commented Feb 26, 2021

Expected Behavior

Package version should be correctly updated in package-lock.json version 2 with lerna --version command.

Current Behavior

When package-lock.json in version 2 is used (generated by npm@7) then lerna --version command updates only the first (root) version information in the package-lock.json. The information in the packages section of package-lock.json is left with an old version.

{
  "name": "@space/types",
  "version": "1.0.13",
  "lockfileVersion": 2,
  "requires": true,
  "packages": {
    "": {
      "name": "@space/types",
      "version": "1.0.11",
      "dependencies": {
        "@space/constants": "file:../constants",
        "fp-ts": "^2.9.5",
        "io-ts": "^2.2.14",
        "joi": "^17.4.0"
      }
    },
(...)

Steps to Reproduce (for bugs)

  1. Install and use npm@7
  2. Create a lerna monorepo with "version": "independent"
  3. Add a package, publish it to have an initial version published
  4. Make some changes in the package, commit, push
  5. Run lerna --version to tag a new version
  6. Review pushed changes in package-lock.json
lerna.json

{
  "packages": [
    "packages/*"
  ],
  "version": "independent",
  "command": {
    "publish": {
      "ignoreChanges": [
        "*.md"
      ],
      "registry": "<redacted>"
    }
  }
}

Your Environment

Executable Version
lerna --version 3.22.1
npm --version 7.6.0
node --version 14.15.0
OS Version
macOS Catalina 10.15.7
@tex0l
Copy link

tex0l commented Mar 18, 2021

I can reproduce the issue and I understand what's happening, but I have no solution.

It comes from an inconsistent installation of npm. The version you have in your path differs from the one actually used by lerna in the packages.

I use npm@7.6.3 with node@14.16.0. I installed the node@14 package with Homebrew which initially comes with npm@6.14.11 but updated it with npm i -g npm --force. When I use npm from my shell:

✗ npm --version 
7.6.3

But lerna doesn't use the same binary for npm, it uses the one intially installed with node@14

✗ lerna exec -- npm --version
lerna notice cli v3.22.1
lerna info Executing command in 5 packages: "npm --version"
6.14.11
6.14.11
6.14.11
6.14.11
6.14.11
lerna success exec Executed command in 5 packages: "npm --version"

This is confirmed by test which npm from the shell:

✗ which npm
/usr/local/bin/npm

And from lerna exec:

✗ lerna exec -- which npm    
lerna notice cli v3.22.1
lerna info Executing command in 5 packages: "which npm"
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
lerna success exec Executed command in 5 packages: "which npm"

Using lerna info, this is not detected:

✗ lerna info
lerna notice cli v3.22.1

 Environment info:

  System:
    OS: macOS 11.2.3
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.6.3 - /usr/local/bin/npm
  Utilities:
    Git: 2.30.0 - /usr/local/bin/git
  npmPackages:
    lerna: ^3.22.1 => 3.22.1 

I didn't find what $PATH lerna exec uses, nor how to configure it to use the same one as the current shell.

@rarkins

This comment has been minimized.

@arantes555
Copy link

I think the problem with PATH and npm versions described in the comments is completely unrelated to the initially described bug : my lerna uses the correct npm@7, and still only updates one version field when doing lerna version

@tex0l
Copy link

tex0l commented Mar 18, 2021

@arantes555 You're right, the problem I have with the lockfileVersion in the package-lock.json is a bit different and most certainly unrelated with the initial bug report, I created a separate issue: #2850

@korgon
Copy link

korgon commented Apr 20, 2021

I too am encountering this issue with Node 15 and Lerna 4. The versioning in packages section of package-lock.json is left with an old version after running publish. Subsequent attempts to publish after re-running npm install fail due to: lerna ERR! EUNCOMMIT Working tree has uncommitted changes

@kanoshin
Copy link

kanoshin commented Jun 8, 2021

@evocateur could you take a look? This blocks from using lerna version with npm v7. Looks like there is PR with fix already.

@mysterycommand
Copy link

Just ran into this issue yesterday. Any update? Anything I can do to help?

@trevor-scheer
Copy link

trevor-scheer commented Sep 30, 2021

For those interested, we'll be working around this issue soon via this PR

Note the top-level package.json script "version": "npm install"
The lerna version hook allows us to make changes after version updates, but pre-commit. For us, running npm install has no effects besides updating the package-lock.json at the root as expected. YMMV, of course, if you have other hooks implemented around the the native npm install lifecycle i.e. preinstall.

@k-koehler
Copy link

I am also having this error breaking my CI/CD pipeline after upgrading to lockfileVersion: 2, causing the following error: npm ERR! Invalid Version in our CI/CD pipeline.

Is there any fix/workaround for this?

@hudochenkov
Copy link

@k-koehler I'm using my fix from this PR #2914 with https://github.com/ds300/patch-package

@Magador
Copy link

Magador commented Feb 2, 2022

Is the file update-lockfile-version.js at fault ?

if (obj) {
obj.version = pkg.version;
return writeJsonFile(lockfilePath, obj, {
detectIndent: true,
indent: 2,
}).then(() => lockfilePath);
}

This seems to only update the package-lock.version property and not package-lock.packages[''].version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.