Skip to content

fix: keep an index's own children out of its reported referrers - #2174

Merged
TerryHowe merged 3 commits into
oras-project:mainfrom
somaz94:fix/cp-index-children-referrers
Sep 22, 2026
Merged

TerryHowe merged 3 commits into
oras-project:mainfrom
somaz94:fix/cp-index-children-referrers

Conversation

@somaz94

@somaz94 somaz94 commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

oras cp -r fails with MANIFEST_UNKNOWN when the source registry does not implement the Referrers API. ORAS falls back to the referrers tag schema and requests sha256-<hex>; a registry that resolves that as the digest sha256:<hex> answers with the index itself, so the index's own children are read back as its referrers.

prepareCopyOption (cmd/oras/root/cp.go:293) passes those through untouched. oras.ExtendedCopyGraph then walks upwards from the index into the children, takes them for the graph roots and copies only their sub-graphs, so the index is never pushed. The dst.Tag at cmd/oras/root/cp.go:279 asks the destination for a manifest that is not there, which is the 404 at the end of the log in the issue. It is also why a plain oras cp followed by cp -r succeeds: the index is already at the destination by then.

A successor can never be a predecessor, so this drops any reported referrer the index already lists in its own manifests (cmd/oras/root/cp.go:327). The index is left with no predecessors, becomes a graph root itself and is copied before it is tagged. Genuine referrers are untouched, and a child that both appears in manifests and declares the index as its subject is still copied, as a successor.

The upstream half of this report, oras-project/oras-go#1396, was fixed in oras-project/oras-go#1403 on oras-go main, which is v3. This repo is on oras.land/oras-go/v2 v2.6.2 and no v2 release carries it, so the failure still reproduces here.

Scope: the fix covers cp -r and backup, since backup --include-referrers goes through the same recursiveCopy. restore and countReferrers read OCI layouts, so they were never affected. oras discover still lists the phantom referrers, because it calls registry.Referrers on the remote directly (cmd/oras/root/discover.go:173), and oras attach has the same exposure one layer down in oras-go's updateReferrersIndex. Those two wait on a v2 backport of oras-project/oras-go#1403.

On the alternative suggested in the issue, an oras.CopyGraph of the root before dst.Tag: I built that first and measured a regression in cp -r --force. --force wraps the destination in contentutil.TraversingTarget (cmd/oras/root/cp.go:197), whose Exists returns false for every manifest media type (internal/contentutil/traverse.go:56), so the extra CopyGraph cannot short-circuit and walks the manifest graph a second time. Against two local registries that turned a 14 line cp -r --force into 20 lines, with the index and both child manifests pushed twice. Clearing the status callbacks does not cover the TTY path, because the progress rows come from the tracked target's Push rather than from the handler. I am happy to switch to that shape if you prefer it.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #2148

Please check the following list:

  • Does the affected code have corresponding tests, e.g. unit test, E2E test?
  • Does this change require a documentation update?
  • Does this introduce breaking changes that would require an announcement or bumping the major version?
  • Do all new files have an appropriate license header?

Local validation (macOS arm64, Go 1.26.7):

go test -run Test_recursiveCopy_strandedRoot ./cmd/oras/root/ on unmodified main: FAIL cp_test.go:666: recursiveCopy() error = failed to perform "Tag" on destination: sha256:d2ef7640...: application/vnd.oci.image.index.v1+json: not found. With the change: ok.

go test -race ./...: all packages ok. go test -race -count=50 ./cmd/oras/root/ -run Test_recursiveCopy: ok. golangci-lint run ./...: 0 issues. gofmt -l cmd internal: clean. go vet ./...: clean.

End to end against two local registry:3 instances, comparing this build with one built from main. Copying a two platform index that carries a referrer: first copy, idempotent recopy and cp -r --force recopy all produce output identical to main. The #1728 shape, an index with no referrer of its own but a referrer on one child, is also identical, and the child's referrer arrives at the destination.

Comment thread cmd/oras/root/cp.go Resolved
Comment thread cmd/oras/root/cp_test.go Resolved
somaz94 and others added 2 commits September 21, 2026 11:46
`oras cp -r` fails with MANIFEST_UNKNOWN against a registry that does not
implement the Referrers API. Such a registry can resolve the referrers tag
`sha256-<hex>` as the digest `sha256:<hex>` and answer with the index itself,
so its own children are read back as its referrers.

prepareCopyOption passes those through, and extended copy walks upwards from
the index into the children, takes them for the graph roots and copies only
their sub-graphs. The index is never pushed, so the root tagging in
recursiveCopy asks the destination for a manifest that is not there.

A successor can never be a predecessor, so drop any reported referrer the
index already lists in `manifests`. The index is then left with no
predecessors, becomes a graph root itself and is copied before it is tagged.

Signed-off-by: somaz <[email protected]>
@somaz94
somaz94 force-pushed the fix/cp-index-children-referrers branch from f729054 to 9dcaca5 Compare September 21, 2026 02:46
@codecov

codecov Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.78%. Comparing base (87495d5) to head (d7f7abd).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
cmd/oras/root/cp.go 85.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2174      +/-   ##
==========================================
- Coverage   87.81%   87.78%   -0.04%     
==========================================
  Files         139      139              
  Lines        5689     5714      +25     
==========================================
+ Hits         4996     5016      +20     
- Misses        413      417       +4     
- Partials      280      281       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two follow-ups, plus a scope note.

Scope: oras discover still reports the phantom referrers — discover.go:173 calls registry.Referrers on the remote repo directly and has no equivalent filter, so an index there still lists its own children (the visited map stops the recursion, not the wrong output). oras attach has the same exposure one layer down, since oras-go's updateReferrersIndex shares the referrersFromIndex call that oras-go#1403 fixed. Not asking you to widen the PR — but worth a line in the description that cp -r and backup are the intended blast radius (backup --include-referrers gets this for free via recursiveCopy, restore and countReferrers read OCI layouts so they were never affected), and that the rest waits on a v2 backport of #1403.

Comment thread cmd/oras/root/cp_test.go Resolved
Comment thread cmd/oras/root/cp.go Resolved

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@TerryHowe
TerryHowe merged commit bc28b9c into oras-project:main Sep 22, 2026
7 checks passed
@TerryHowe

Copy link
Copy Markdown
Member

Thanks!

Soundcreates pushed a commit to Soundcreates/oras that referenced this pull request Sep 22, 2026
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.

cp -r fails with 'MANIFEST_UNKNOWN'

2 participants