cli
cli copied to clipboard
[BUG] npm-version does not git-commit nor git-tag when package in subdirectory
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:
- initialize an npm package in the root of an initialized git repo
-
npm version minor
successfully bumps the version, commits and tags - move the npm package into a subdirectory of the repo
-
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
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 does it still occur with v7.0.15?
@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.
This still occours in 7.5.4
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.
Reopening; latest comment suggests it's still an issue in npm 7.
This should be really easy to solve by using git rev-parse --show-toplevel
.
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.
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 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:
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'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
Reopening; latest comment suggests it's still an issue in npm 7.
This is also in npm 8
Sorry to ping you directly @ljharb (you helped me in the past) but do you think my approach acceptable?
@fernandopasik i have no idea; i'm not an npm maintainer
Still a problem...
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.
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.
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"
}
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
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.
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?
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'
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
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"
}
yikes