cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] repository URL normalization breaks non GitHub URLs

Open louwers opened this issue 8 months ago • 3 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

This issue exists in the latest npm version

  • [x] I am using the latest npm

Current Behavior

I am publishing a package with this in its package.json:

{
  "repository": {
    "type": "git",
    "url": "https://git.sr.ht/~bl/live-server"
  }
}

I get the following error:

npm warn publish npm auto-corrected some errors in your package.json when publishing. Please run "npm pkg fix" to address these errors.

However, this is wrong:

$ git clone https://git.sr.ht/~bl/live-server.git
Cloning into 'live-server'...
fatal: repository 'https://git.sr.ht/~bl/live-server.git/' not found

Without the 'normalized' URL it works:

 git clone https://git.sr.ht/~bl/live-server
Cloning into 'live-server'...
remote: Enumerating objects: 84, done.
remote: Counting objects: 100% (46/46), done.
remote: Compressing objects: 100% (46/46), done.
remote: Total 84 (delta 26), reused 0 (delta 0), pack-reused 38 (from 1)
Receiving objects: 100% (84/84), 28.34 KiB | 9.45 MiB/s, done.
Resolving deltas: 100% (42/42), done.

Expected Behavior

The documentation states:

The URL should be a publicly available (perhaps read-only) URL that can be handed directly to a VCS program without any modification. It should not be a URL to an html project page that you put in your browser. It's for computers.

However, the normalization makes it impossible.

Steps To Reproduce

  1. Use any URL that doesn't end in .git.
  2. Run npm publish

Environment

  • npm: 11.11.0
  • Node.js: v25.8.1
  • OS Name: Linux
  • System Model Name:
  • npm config:
; "user" config from /home/bart/.npmrc

//npm.louwers.dev/:_authToken = (protected)
prefix = "/home/bart/.npm"

; node bin location = /nix/store/aa1vj9zdiadncjywhds4qlh8wdfay2yb-nodejs-slim-25.8.1/bin/node
; node version = v25.8.1
; npm local prefix = /home/bart
; npm version = 11.11.0
; cwd = /home/bart
; HOME = /home/bart
; Run `npm config ls -l` to show all defaults.

louwers avatar Mar 31 '26 20:03 louwers

Hey! Great bug report with clear repro steps, thanks for filing it.

I dug into the root cause and it's a chain across two packages:

hosted-git-info's Sourcehut host definition in lib/hosts.js unconditionally appends .git to HTTPS clone URLs via its httpstemplate. @npmcli/package-json's fixRepositoryField in lib/normalize.js then rewrites repository.url to whatever hosted-git-info returns, so a perfectly valid URL like https://git.sr.ht/~bl/live-server becomes https://git.sr.ht/~bl/live-server.git, which Sourcehut's server 404s.

GitHub, GitLab, and Bitbucket all tolerate the .git suffix, which is why this only shows up with Sourcehut!

The fix belongs in hosted-git-info, removing .git from hosts.sourcehut.httpstemplate, with a corresponding update to test/sourcehut.js and a new Sourcehut test case in @npmcli/package-json's test/fix.js.

Happy to open a PR if that would be helpful! 😊

yetval avatar Apr 03 '26 04:04 yetval

@yetval Interesting that sourcehut is already taken into account.

Sounds like an easy fix. If you want to pick it up, would be great, if I have to guess I think it would be happily accepted.

louwers avatar Apr 03 '26 09:04 louwers

Hey! Opened a fix in hosted-git-info where the root cause is: npm/hosted-git-info#326

yetval avatar Apr 03 '26 14:04 yetval