Skip to content

[BUG] npm repo <pkg> assumes a master branch #4867

Closed
@billyjanitsch

Description

@billyjanitsch

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

Given the following package:

{
  "name": "b",
  "repository": {
    "type": "git",
    "url": "https://github.com/a/b.git",
    "directory": "c"
  }
}

npm repo b points to https://github.com/a/b/tree/master/c. In other words, it assumes that a master branch exists, which increasingly isn't always the case with many repos moving to other default branch names like main.

(Note: this only happens when the repository spec specifies a directory, because otherwise the command points to the repo base, e.g., https://github.com/a/b.)

There are a few possible failure states. GitHub redirects to the default branch if master doesn't exist, but it can't always handle this gracefully (and I'm not sure how other hosts behave). One unfortunate example is when a repo switched from master to main but kept the former as an archive, meaning that npm repo opens to an outdated view of the repo, e.g., npm repo @babel/[email protected].

The underlying issue is here, or in one of the other places that this library hardcodes the string 'master'.

Expected Behavior

npm repo <pkg> should open to the default branch of the repository. Perhaps HEAD could be used instead of master?

Steps To Reproduce

  1. Run npm repo @babel/[email protected]. (Including the version for posterity, but it's not currently necessary.)
  2. Note that you've landed on https://github.com/babel/babel/tree/master/packages/babel-cli, which is not the default branch of the repo.

Environment

  • npm: 8.9.0
  • Node.js: 16.15.0
  • OS Name: macOS
  • System Model Name: MacBook Pro
  • npm config: n/a

Metadata

Metadata

Assignees

Labels

Bugthing that needs fixingEnhancementnew feature or improvementRelease 8.xwork is associated with a specific npm 8 releaseRelease 9.xwork is associated with a specific npm 9 releasesemver:majorbackwards-incompatible breaking changes

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions