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

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

Open
jasonkarns opened this issue Oct 21, 2020 · 26 comments · May be fixed by #5442
Open

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

jasonkarns opened this issue Oct 21, 2020 · 26 comments · May be fixed by #5442
Labels
Bug thing that needs fixing cmd:version related to `npm version` Priority 2 secondary priority issue Release 7.x work is associated with a specific npm 7 release Release 8.x work is associated with a specific npm 8 release

Comments

@jasonkarns
Copy link
Contributor

jasonkarns commented Oct 21, 2020

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: npm/npm#18795

@jasonkarns jasonkarns added Bug thing that needs fixing Needs Triage needs review for next steps Release 6.x work is associated with a specific npm 6 release labels Oct 21, 2020
@niedzielski
Copy link

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 added a commit to oidoid/atlas-pack that referenced this issue Nov 30, 2020
@ljharb
Copy link
Contributor

ljharb commented Nov 30, 2020

@niedzielski does it still occur with v7.0.15?

@niedzielski
Copy link

@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.

@darcyclarke darcyclarke removed the Needs Triage needs review for next steps label Feb 13, 2021
@amoscatelli
Copy link

amoscatelli commented Feb 23, 2021

This still occours in 7.5.4

@darcyclarke
Copy link
Contributor

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.

@ljharb
Copy link
Contributor

ljharb commented Jun 2, 2021

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

@ljharb ljharb reopened this Jun 2, 2021
@ljharb ljharb added Release 7.x work is associated with a specific npm 7 release and removed Release 6.x work is associated with a specific npm 6 release labels Jun 2, 2021
@StefanBrand
Copy link

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

@StefanBrand
Copy link

StefanBrand commented Jun 17, 2021

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.

@Yegorich555
Copy link

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?

@StefanBrand
Copy link

@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. 🙂

@wraithgar wraithgar added the Priority 2 secondary priority issue label Jul 1, 2021
@lukekarrys lukekarrys added cmd:version related to `npm version` Release 8.x work is associated with a specific npm 8 release labels Mar 1, 2022
@fernandopasik
Copy link

fernandopasik commented Apr 30, 2022

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
Copy link

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

@fernandopasik
Copy link

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

This is also in npm 8

@fernandopasik
Copy link

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

@ljharb
Copy link
Contributor

ljharb commented May 11, 2022

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

@NilsBaumgartner1994
Copy link

Still a problem...

@msweaver
Copy link

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.

@grafitto
Copy link

grafitto commented Dec 7, 2022

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.

@martinmiglio
Copy link

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"
}

@jongbelegen
Copy link

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

@ncook-hxgn
Copy link

ncook-hxgn commented Aug 15, 2023

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.

@dmcweeney
Copy link

dmcweeney commented Nov 9, 2023

Any progress on this being fixed - just encountered after upgrading from npm@6.14.16 to npm@8.19.4?

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

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

@ncook-hxgn
Copy link

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'

@dmcweeney
Copy link

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

kariudo pushed a commit to kariudo/ng2-pdfjs-viewer that referenced this issue Feb 20, 2024
@kariudo
Copy link

kariudo commented Feb 20, 2024

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"
  }

@wouterds
Copy link

yikes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing cmd:version related to `npm version` Priority 2 secondary priority issue Release 7.x work is associated with a specific npm 7 release Release 8.x work is associated with a specific npm 8 release
Projects
None yet