yarnpkg/yarn · error · MessageError

Invalid hosted git fragment $0.

Error message

Invalid hosted git fragment $0.

What it means

explodeHostedGitFragment() strips protocol/hash/.git then splits by '/'; if user or repo ends up undefined (the fragment cannot split into a user/repo pair), it throws. Used for github:/bitbucket:/gitlab: style dependencies.

Source

Thrown at src/resolvers/exotics/hosted-git-resolver.js:42

export function explodeHostedGitFragment(fragment: string, reporter: Reporter): ExplodedFragment {
  const hash = parseHash(fragment);

  const preParts = fragment.split('@');
  if (preParts.length > 2) {
    fragment = preParts[1] + '@' + preParts[2];
  }

  const parts = fragment
    .replace(/(.*?)#.*/, '$1') // Strip hash
    .replace(/.*:(.*)/, '$1') // Strip prefixed protocols
    .replace(/.git$/, '') // Strip the .git suffix
    .split('/');

  const user = parts[parts.length - 2];
  const repo = parts[parts.length - 1];

  if (user === undefined || repo === undefined) {
    throw new MessageError(reporter.lang('invalidHostedGitFragment', fragment));
  }

  return {
    user,
    repo,
    hash,
  };
}

export default class HostedGitResolver extends ExoticResolver {
  constructor(request: PackageRequest, fragment: string) {
    super(request, fragment);

    const exploded = (this.exploded = explodeHostedGitFragment(fragment, this.reporter));
    const {user, repo, hash} = exploded;
    this.user = user;
    this.repo = repo;
    this.hash = hash;

View on GitHub (pinned to c2dda503f3)

Solutions

  1. Use the format github:<user>/<repo> or github:<user>/<repo>#<commit>
  2. Check the dependency URL has both a user and repo segment
  3. Remove trailing slashes or extra path segments beyond user/repo

Example fix

// before
"my-dep": "github:user"
// after
"my-dep": "github:user/repo"
Defensive patterns

Strategy: validation

Validate before calling

function isValidHostedGitFragment(fragment: string): boolean {
  const cleaned = fragment.replace(/(.*?)#.*/, '$1').replace(/.*:(.*)/, '$1').replace(/.git$/, '');
  const parts = cleaned.split('/');
  return parts[parts.length - 2] !== undefined && parts[parts.length - 1] !== undefined;
}

Type guard

function isWellFormedHostedGit(fragment: string): boolean {
  const parts = fragment.replace(/(.*?)#.*/, '$1').replace(/.*:(.*)/, '$1').replace(/.git$/, '').split('/');
  const user = parts[parts.length - 2];
  const repo = parts[parts.length - 1];
  return !!user && !!repo;
}

Prevention

When it happens

Trigger: A hosted-git dependency string that, after normalization, lacks a user or repo segment (parts[parts.length-2] or parts[parts.length-1] is undefined).

Common situations: Malformed github: URLs missing a segment; trailing slashes confusing the split; pasting only a repo without user.

Related errors


AI-assisted analysis of yarnpkg/yarn@c2dda503f3 (2026-08-13). Data as JSON: /api/errors/80c276f4ce7dad27. Report an issue: GitHub.