cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] npm-version does not git-commit nor git-tag when package in subdirectory

Open jasonkarns opened this issue 4 years ago • 26 comments

Current Behavior:

npm version <version> is not committing the modified package.json or package-lock.json; nor git-tagging.

We were using npm version {major|minor|patch} extensively (and successfully). It stopped working once the package was moved out of the root of the repo and into a subdirectory.

Expected Behavior:

npm version <version> should continue to create a git-commit and git-tag as indicated in the docs:

If run in a git repo, it will also create a version commit and tag. This behavior is controlled by git-tag-version (see below), and can be disabled on the command line by running npm --no-git-tag-version version. It will fail if the working directory is not clean, unless the -f or --force flag is set.

Steps To Reproduce:

  1. initialize an npm package in the root of an initialized git repo
  2. npm version minor successfully bumps the version, commits and tags
  3. move the npm package into a subdirectory of the repo
  4. npm version minor still bumps the version in package.json and package-lock.json, but git is not committed nor tagged.

Environment:

  • macOS 10.15.7
  • node: v14.13.1
  • npm: 6.14.8
  • git: git version 2.28.0

This is apparently an existing bug going as far back as npm v3: https://github.com/npm/npm/issues/18795

jasonkarns avatar Oct 21 '20 22:10 jasonkarns

I just hit this issue using NPM workspaces on Node v15.2.1 / NPM v7.0.8 for Ubuntu. The problem may become more popular with workspaces containing independently versioned packages as they tend to use subdirectories.

niedzielski avatar Nov 30 '20 01:11 niedzielski

@niedzielski does it still occur with v7.0.15?

ljharb avatar Nov 30 '20 03:11 ljharb

@ljharb, yes. I can repro on NPM v7.0.15. Using a local installation of NPM:

../node_modules/.bin/npm version prerelease --preid=foo

Changes the package version in my repo to 3.0.1-foo.0 but doesn't commit the change. Explicitly setting --git-tag-version=true also has no effect.

niedzielski avatar Nov 30 '20 04:11 niedzielski

This still occours in 7.5.4

amoscatelli avatar Feb 23 '21 16:02 amoscatelli

npm v6 is no longer in active development; We will continue to push security releases to v6 at our team's discretion as-per our Support Policy.

If your bug is preproducible on v7, please re-file this issue using our new issue template.

If your issue was a feature request, please consider opening a new RRFC or RFC. If your issue was a question or other idea that was not CLI-specific, consider opening a discussion on our feedback repo

Closing: This is an automated message.

darcyclarke avatar Jun 02 '21 21:06 darcyclarke

Reopening; latest comment suggests it's still an issue in npm 7.

ljharb avatar Jun 02 '21 23:06 ljharb

This should be really easy to solve by using git rev-parse --show-toplevel.

StefanBrand avatar Jun 17 '21 13:06 StefanBrand

Workaround:

git tag $(npm version patch) && ga package.json package-lock.json && gc -m "Bump version"

Edit.: My bad, the above tags the commit before "Bump version". This works:

NEW_VERSION=$(npm version patch) \
&& add package.json package-lock.json \
&& git commit -m 'Bump version' \
&& git tag $NEW_VERSION

I ended up putting everything into a release.sh script:

#!/usr/bin/env bash
if [ "$1" != "major" ] && [ "$1" != "minor" ] && [ "$1" != "patch" ];
then
    echo "Could not release!"
    echo "Usage: 'npm run release -- (major|minor|patch)'"
    echo ""
    exit 1
fi

NEW_VERSION=$(npm version $1)

git add package.json package-lock.json
git commit -m 'Bump version'
git tag $NEW_VERSION
echo "Bumped version to $NEW_VERSION"

# Prompt for pushing
read -p "Push HEAD and tags to $NEW_VERSION? y/n " PUSH
if [ $PUSH = "y" ]
then 
    git push && git push --tags
else
    echo "Not pushing."
fi

Edit: Our new version of the release script.

StefanBrand avatar Jun 17 '21 14:06 StefanBrand

Workaround:

git tag $(npm version patch) && ga package.json package-lock.json && gc -m "Bump version"

How can I point tagged version in commit message in this case?

Yegorich555 avatar Jun 21 '21 15:06 Yegorich555

@Yegorich555 Sorry, I found out myself that my workaround does not work as intended, but I forgot to update my post. I edited the above post and added my current solution. :slightly_smiling_face:

StefanBrand avatar Jun 22 '21 07:06 StefanBrand

I think this will be fixed by replacing this call of git.is with git.find

const isGitDir = newversion === 'from-git' || Boolean(await git.find(opts))

I'm happy to create a PR if a maintaner is happy with this approach

fernandopasik avatar Apr 30 '22 21:04 fernandopasik

I've tested this actually with workspaces as I'm interested in what's going on in https://github.com/npm/rfcs/issues/570 This issue might be needed to be fixed before implementing that rfc

fernandopasik avatar May 04 '22 18:05 fernandopasik

Reopening; latest comment suggests it's still an issue in npm 7.

This is also in npm 8

fernandopasik avatar May 04 '22 19:05 fernandopasik

Sorry to ping you directly @ljharb (you helped me in the past) but do you think my approach acceptable?

fernandopasik avatar May 11 '22 18:05 fernandopasik

@fernandopasik i have no idea; i'm not an npm maintainer

ljharb avatar May 11 '22 18:05 ljharb

Still a problem...

NilsBaumgartner1994 avatar May 24 '22 15:05 NilsBaumgartner1994

Could we at least update the really misleading documentation about this? The work arounds are easy enough to implement, but its frustrating to follow the docs exactly and see unexpected behaviors.

msweaver avatar Nov 28 '22 21:11 msweaver

I was facing the same issue, However, I noticed I was not in the root folder. I moved to the project root folder run npm version <version> and now a commit is made and tags are created.

grafitto avatar Dec 07 '22 08:12 grafitto

same issue on 8.12, came up with a work around for windows using cmd.exe as shell for npm scripts:

"scripts": {
    "push-release": "git add package.json package-lock.json & git commit -m \"Bump version\" &git tag -m \"Bump Version\" -a v%npm_package_version%  & git push & git push --tags",
    "bump-patch": "npm version patch & npm run push-release"
}

martinmiglio avatar Dec 15 '22 21:12 martinmiglio

I replaced my yarn implementation with npm using a workspace. And I'm getting the following error when not in de root folder:

npm verb version Not tagging: not in a git repo or no git cmd

jongbelegen avatar Apr 03 '23 12:04 jongbelegen

this is still present in NPM v9.5.0

It also silently killed my build, my build continued until I tried to push the tag created by npm version, which didn't exist...

    - task: Npm@1
      displayName: 'NPM Version'
      inputs:
        command: 'custom'
        workingDir: '$(Build.SourcesDirectory)/webapp'
        customCommand: 'version --allow-same-version $(GitVersion.SemVer)'
        verbose: true

Yields:

npm verb title npm version 1.2.3
npm verb argv "version" "--allow-same-version" "1.2.3"
npm timing npm:load:setTitle Completed in 2ms
npm timing config:load:flatten Completed in 4ms
npm timing npm:load:display Completed in 7ms
npm verb logfile logs-max:10 dir:C:\Users\ContainerAdministrator\AppData\Local\npm-cache\_logs
npm verb logfile C:\Users\ContainerAdministrator\AppData\Local\npm-cache\_logs\2023-08-15T11_01_38_245Z-debug-0.log
npm timing npm:load:logFile Completed in 7ms
npm timing npm:load:timers Completed in 0ms
npm timing npm:load:configScope Completed in 0ms
npm timing npm:load Completed in 50ms
npm verb version Not tagging: not in a git repo or no git cmd
npm timing command:version Completed in 20ms
npm verb exit 0
npm timing npm Completed in 1124ms
npm info ok

I think that the Not tagging: not in a git repo or no git cmd should have been a warning at least, I'm not sure exiting 0 is OK here because the thing I asked for failed: my software hasn't been versioned.

I think this will be fixed by replacing this call of git.is with git.find

const isGitDir = newversion === 'from-git' || Boolean(await git.find(opts))

I'm happy to create a PR if a maintaner is happy with this approach

-- I'd merge it.

ncook-hxgn avatar Aug 15 '23 11:08 ncook-hxgn

Any progress on this being fixed - just encountered after upgrading from [email protected] to [email protected]?

I also agree with @ncook-hxgn suggestion to upgrade to a warning.

@ncook-hxgn did you figure a solution or workaround to this problem?

dmcweeney avatar Nov 09 '23 17:11 dmcweeney

Indeedy I do, @dmcweeney


    - task: CmdLine@2
      displayName: 'Git Tag'
      condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
      inputs:
        script: |
          echo "##[command]https://github.com/npm/cli/issues/2010"
          echo "##[group]workaround [BUG] npm-version does not git-commit nor git-tag when package in subdirectory #2010"
          git add package.json package-lock.json
          git commit -m "CI: bump package to v$(GitVersion.Semver)"
          git tag -a v$(GitVersion.SemVer) -m v$(GitVersion.SemVer)
          echo "##[endgroup]"
          git push origin v$(GitVersion.SemVer)
        workingDirectory: '$(Build.SourcesDirectory)/webapp'

ncook-hxgn avatar Nov 10 '23 16:11 ncook-hxgn

Thanks @ncook-hxgn ! I just worked around it myself by creating an empty .git folder in the subfolder that contains the package.json as part of the pipeline script. Thanks, Donal

dmcweeney avatar Nov 10 '23 17:11 dmcweeney

Thanks @ncook-hxgn ! I just worked around it myself by creating an empty .git folder in the subfolder that contains the package.json as part of the pipeline script. Thanks, Donal

I worked around this similarly in my npm scripts as:

"scripts": {
...
    "bump": "mkdir .git && npm version patch",
    "version": "npm run build && git add -A .",
    "postversion": "git push && git push --tags && rmdir .git"
  }

kariudo avatar Feb 20 '24 19:02 kariudo

yikes

wouterds avatar Apr 16 '24 14:04 wouterds