Skip to content

feat: add opt-in support for repository.directory in file() - #344

Open
ljharb wants to merge 1 commit into
npm:mainfrom
ljharb:repository-directory
Open

ljharb wants to merge 1 commit into
npm:mainfrom
ljharb:repository-directory

Conversation

@ljharb

@ljharb ljharb commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

This adds an opt-in way for file() to resolve paths inside a monorepo package's subdirectory, using the manifest's repository.directory.

const manifest = {
  repository: {
    url: 'git+https://github.com/npm/cli.git',
    directory: 'workspaces/arborist',
  },
}

hostedGitInfo.fromManifest(manifest, { useDirectory: true }).file('package.json')
// https://raw.githubusercontent.com/npm/cli/HEAD/workspaces/arborist/package.json

Changes

  • fromManifest() takes a new useDirectory option. When it's true and repository.directory is a string, that value is passed on as the directory option. It defaults to false, so existing consumers keep getting repository-root URLs.
  • fromUrl() and file() take a new directory option. file() resolves path relative to it. Other methods (browse(), https(), etc.) are not affected.
    • Empty and . segments are removed, and / means the repository root.
    • A non-string directory is ignored.
    • Passing directory to file() overrides the instance's value, so { directory: '' } gives the repository root.
  • A directory that contains a .. segment is ignored completely. repository.directory comes from untrusted manifests, so a value like ../../other/repo could otherwise make the URL point at a different repository. Segments that only contain .., such as ..foo, are kept.
  • The instances are cached separately: a useDirectory instance is never shared with the default one.
  • README is updated.

Notes

This is independent of #, which url-encodes file() paths. Whichever PR lands second will have a small conflict in file(): the directory prefix should be applied first, and encodePath run on the combined path. Once both have landed, I'll add tests that directory segments are url-encoded and that lone surrogates in directory don't throw.

@ljharb
ljharb requested a review from a team as a code owner October 14, 2026 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant