Skip to content

fix(git): check out out-of-cone link targets with Git's own checkout - #3673

Merged
Daniel Meppiel (danielmeppiel) merged 7 commits into
mainfrom
danielmeppiel-fix-link-target-eol
Nov 12, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 7 commits into
mainfrom
danielmeppiel-fix-link-target-eol

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

TL;DR

Fixes the post-merge red on Build & Validate (macOS x86_64) since 90e76bf (#3603). When a link's target sat outside a sparse checkout, APM read the target with git --attr-source=HEAD cat-file --filters. If that call failed it fell back to plain cat-file --filters. That fallback never reads the index, so it skipped the target's .gitattributes and wrote an eol=crlf file with LF. Out-of-worktree targets are now written by Git's own checkout path (read-tree HEAD into a throwaway index, then checkout-index into a throwaway work tree). The bytes equal what git checkout writes, on every Git version.

Refs #3457

Problem (WHY)

  • Main CI: tests/unit/deps/test_git_file_transport_links.py::test_sparse_file_fetch_returns_link_target_content[pkg/notes.crlf-shared/notes.crlf] fails with assert b'line one\nline two\n' == b'line one\r\nline two\r\n' on macos-15-intel. It has failed every main run from 90e76bf through 9e0a4a8; PR CI (Linux x64) passes.
  • Mechanism, reproduced locally: forcing main's --attr-source call to fail yields exactly that assertion. git cat-file --filters without --attr-source returns LF in a sparse checkout even though git check-attr reports eol: crlf, because cat-file does not load the index.
  • [!] The previous design depended on a version-gated global option (--attr-source, Git 2.40+) and had a silent wrong-bytes fallback. Local Git 2.50/2.56 do not reproduce the failure, so the exact trigger on the runner's Git 2.55 is not pinned down. Any --attr-source failure lands in the same wrong fallback.
  • Note: the Linux arm64 (ubuntu-24.04-arm) red on the same runs is unrelated. It is the GLIBC_2.35 dist/apm-linux-arm64/_internal/libgcc_s.so.1 packaging check.

Approach (WHAT)

# Fix
1 _CheckoutBytes.prime() collects every source of a materialization. Targets Git already wrote in the work tree are read as before (no subprocess).
2 The rest go through one batch: GIT_INDEX_FILE=<tmp> git read-tree HEAD, then git --work-tree=<tmp> checkout-index --stdin -z. core.sparseCheckout=false and index.sparse=false keep the throwaway index full, so nothing can hide a path. Attributes are read from that index exactly as git checkout reads them.
3 No --attr-source, no cat-file --filters, no fallback. A path Git did not write as a regular file raises.
4 Line-ending pins from #3599 still apply. The content_hash domain is unchanged; only the wrong LF bytes become the checkout bytes.

Implementation (HOW)

  • src/apm_cli/utils/git_sparse.py: _CheckoutBytes rewritten (prime, _worktree_bytes, _check_out). materialize_tracked_symlinks primes all sources before writing.
  • tests/unit/deps/test_git_file_transport_links.py: new regression test_out_of_sparse_eol_target_keeps_checkout_bytes_without_attr_source. It emulates a Git that rejects --attr-source (exit 129), so the condition reproduces on every CI lane, Linux x64 included.
  • tests/integration/test_sparse_cone_symlink_repair.py: two fix(git): materialize package links from Git objects on every host #3603 downloader tests built a bare object.__new__ downloader. Since fix(cache): clone SSH-declared non-GitHub deps over SSH in the persistent cache #3640 (_persistent_cache_remote), that downloader lacks transport attributes, so the cache path fell through to clone. They now stub the persistent-checkout seam directly. These tests run only in integration lanes, so main CI did not show it.

Trade-offs

  • Throwaway index vs. --attr-source. read-tree HEAD walks the whole tree once per materialization, but only when some target is outside the work tree. It uses the same machinery as git checkout on every Git version. Rejected: keeping --attr-source with a better fallback, because any fallback must reimplement attribute lookup.
  • Batch instead of per-file. One checkout-index for all out-of-cone targets, replacing one cat-file per file.
  • No build-release dispatch on this branch. build-release.yml has no workflow_dispatch, and its repository_dispatch runs on the default branch only. The new regression test reproduces the failing condition deterministically in PR CI instead.

Benefits

  1. macOS x86_64 post-merge lane: the failing assertion's code path no longer exists.
  2. Link-target bytes no longer depend on the host Git version.
  3. One subprocess pair per materialization for out-of-cone targets instead of one per file.

Issue and approved scope

Issue: Refs #3457

Human scope-approval comment: #3457 (comment) (extended scope: same bytes and content_hash as the POSIX result on every platform).

Does this PR complete the issue, or what remains? #3457 is closed by #3603; this repairs a platform-specific defect in that delivery.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Local (macOS arm64, Git 2.56.0), one process at a time, no xdist:

Command Result
pytest tests/unit/deps/test_git_file_transport_links.py 5 passed
same, new test only, with src/apm_cli/utils/git_sparse.py from main 1 failed: assert b'line one\nline two\n' == b'line one\r\nline two\r\n' (the CI assertion)
pytest tests/unit/utils/test_git_sparse.py 24 passed
pytest tests/unit/cache/test_git_cache_symlinks.py tests/unit/cache/test_git_text_eol_pin.py tests/unit/cache/test_git_symlink_config.py 57 passed
pytest tests/unit/cache/test_git_cache_sparse.py tests/unit/deps/test_github_downloader_phase3.py tests/test_gitlab_git_transport.py 191 passed
pytest tests/integration/test_sparse_cone_symlink_repair.py 8 passed (2 failed on main before the fixture fix)
APM_BINARY_PATH=.venv/bin/apm pytest tests/integration/test_git_object_links_lifecycle.py 8 passed
Lint mirror: ruff check / format --check, pylint R0801, lint-auth-signals, lint_architecture_boundaries, Mode B clean

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour.

Co-authored-by: Copilot [email protected]

A link whose target a sparse checkout left out was read with
`git --attr-source=HEAD cat-file --filters`, falling back to plain
`cat-file --filters` when that call failed. cat-file reads no index, so
in a sparse checkout the fallback silently skipped the target's
.gitattributes and wrote e.g. an eol=crlf file with LF. That is the
post-merge red on macOS x86_64 since 90e76bf: the path: transport link
to shared/notes.crlf returned LF while the checked-out target was CRLF.

Out-of-worktree targets are now written by `checkout-index` into a
throwaway work tree from a throwaway full index (`read-tree HEAD`), in
one batch per materialization. Attributes come from that index exactly
as `git checkout` reads them, on every Git version, with no
version-gated option and no silent fallback. In-worktree targets still
read the bytes Git already wrote. The line-ending pins (#3599) still
apply; the content_hash domain is unchanged.

Regression: test_out_of_sparse_eol_target_keeps_checkout_bytes_without_attr_source
emulates a Git that rejects --attr-source; it fails on main with the CI
assertion and passes here. Also re-points two #3603 downloader tests at
the persistent-checkout seam that #3640 changed underneath them.

Refs #3457

apm-spec-waiver: Git checkout byte fidelity for link targets; no OpenAPM statement or hash-domain change.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 21:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Previously pinned sparse shards retain incorrect LF link bytes and fail cross-platform frozen verification, while the new scratch directory ignores configured temporary paths and can fail in restricted environments.

2 open issues
What changed in this PR

This follow-up to #3457 fixes how APM reads symlink targets outside a sparse checkout, using Git’s checkout machinery to preserve repository-defined line endings and consistent package hashes.

Changes:

  • Replaces the cat-file --filters fallback with a batched checkout using a temporary index and work tree.
  • Adds a regression test for Git environments that reject --attr-source.
  • Updates downloader test fixtures and records the follow-up in the changelog.
File Description
tests/​unit/​deps/​test_git_file_transport_links.py Verifies out-of-cone targets retain CRLF bytes without --attr-source.
tests/​integration/​test_sparse_cone_symlink_repair.py Stubs the persistent-checkout boundary in two downloader tests.
src/​apm_cli/​utils/​git_sparse.py Loads target bytes before materialization and batches missing targets through Git checkout.
CHANGELOG.md Adds this PR to the existing cross-platform symlink fix entry.

Review effort: Balanced

Comment thread src/apm_cli/utils/git_sparse.py Resolved
Comment thread src/apm_cli/utils/git_sparse.py Resolved
A target that checkout-index leaves unwritten raised a bare
FileNotFoundError; it now raises the 'did not check out ... as a regular
file' diagnostic before any link is written. Panel follow-up
(test-coverage-expert).

Co-authored-by: Copilot App <[email protected]>
…TEMP_DIR

Review follow-ups (Copilot):
- The shard marker is versioned to apm.linkMaterialization=git-objects-checkout.
  A sparse shard pinned by the first Git-object materializer could hold
  an out-of-cone eol=crlf target with LF and would otherwise be served
  as is; it is now rematerialized on its next cache hit. Regression:
  test_cache_hit_rematerializes_shard_without_link_materialization[pre-checkout-marker]
  (fails if the marker value is reverted).
- The throwaway checkout scratch directory uses get_apm_temp_dir(), like
  the downloader's own scratch space, so APM_TEMP_DIR / temp_dir keep
  working on hosts with a restricted system temp directory.

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_now

Repairs the macOS x86_64 post-merge red from #3603: out-of-cone link targets are now written by Git's own checkout (throwaway index plus checkout-index), not the attribute-blind cat-file fallback.

panel-mode=lean; personas=supply-chain-security-expert,test-coverage-expert,performance-expert,apm-ceo (performance-expert ad hoc: the fix adds read-tree HEAD on the materialization path)

cc Daniel Meppiel (@danielmeppiel) Sergio Sisternes (@sergio-sisternes-epam) -- a fresh advisory pass is ready for your review.

All three panelists converge. Supply-chain security found no issues and calls the change a net improvement. The throwaway index and work tree are confined to a temp dir with cleanup, entry paths are validated before checkout-index, smudge exposure equals the prior cat-file --filters, and the silent fallback that dropped .gitattributes is gone. The regression trap emulates a Git that rejects --attr-source, so it reproduces the macOS failure on every lane: it fails on main with the exact CI assertion and passes here. Performance confirms no regression on the no-link and all-in-cone paths. N per-file cat-file calls collapse to two subprocesses, and read-tree HEAD runs only when a target lies outside the work tree.

Material delta since the panel ran. Test-coverage's one gap (no test of the "did not check out ... as a regular file" diagnostic) is folded. Writing it exposed that an unwritten path raised a bare FileNotFoundError; it now raises the diagnostic before any link is written. Copilot then raised two findings, both fixed and resolved. The scratch dir now honors APM_TEMP_DIR/temp_dir. The shard marker is versioned (apm.linkMaterialization=git-objects-checkout), so sparse shards pinned by the old materializer, which could hold LF for an eol=crlf target, are rebuilt on their next hit (mutation-break checked).

Aligned with: Portable by manifest -- link targets get the bytes git checkout writes on every host and Git version. Secure by default -- confined scratch space, validated paths, no silent wrong-bytes fallback. Multi-host -- no dependence on the Git 2.40+ --attr-source option.

Panel summary

Persona B R N Takeaway
Supply Chain Security Expert 0 0 0 Net security improvement; contained scratch index/tree, validated paths, silent attribute-dropping fallback removed.
Test Coverage Expert 0 1 0 Regression trap reproduces the CI failure on every lane; diagnostic-path gap folded.
Performance Expert 0 3 0 Two subprocesses per batch instead of one per file; read-tree only for out-of-cone targets; lazy fetch limited to named blobs.

B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.

Top 1 follow-up

  1. [Performance Expert] Consider one _CheckoutBytes per install so read-tree HEAD is built once when several packages have out-of-cone targets. -- It is O(repo files) per materialization today; bounded and only on the out-of-cone path, so track only if measured.

Recommendation

No findings remain open, CI is green at this head (including Windows Compatibility Gate and Windows Native Symlink Acceptance), and the regression trap ties the fix to the reported red. Recommend landing it; the macOS x86_64 lane on main confirms on the next post-merge run.


Full per-persona findings

Supply Chain Security Expert

No findings.

Test Coverage Expert

  • [recommended] No test exercises the RuntimeError raised when checkout-index fails to produce a regular file at src/apm_cli/utils/git_sparse.py
    New user-facing diagnostic on a defensive path had no assertion.
    Folded: tests/unit/utils/test_git_sparse.py::test_target_git_does_not_check_out_fails_instead_of_writing (also fixed the bare FileNotFoundError).

Performance Expert

  • [recommended] Batch checkout is a net subprocess and round-trip win for multi-target packages.
  • [recommended] read-tree HEAD builds a full-tree index at O(files) cost, only when out-of-cone targets exist. Suggested: hoist _CheckoutBytes per install if profiling shows it.
  • [recommended] checkout-index in partial clones fetches only the named blobs; no full-repo lazy-fetch risk.

This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.


Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.

@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_now

Delta pass on the final head. Since the lean panel at db111f9 (#3673 (comment)), the only changes are restoring uv.lock to main's bytes (a local uv rewrite had been swept into 40462c3) and merging origin/main (#3614, APM_HOME; overlap with this PR is CHANGELOG only).

panel-mode=delta; personas=apm-ceo (no persona-owned code changed in range, so no specialist re-run)

cc Daniel Meppiel (@danielmeppiel) Sergio Sisternes (@sergio-sisternes-epam) -- a fresh advisory pass is ready for your review.

No new findings. CI is green at this head, including Windows Compatibility Gate and Windows Native Symlink Acceptance. All 4 review threads are resolved. The earlier recommendation stands unchanged: land it, and the macOS x86_64 post-merge lane confirms the repair.

Recommendation

Ship. The single tracked follow-up (one _CheckoutBytes per install, performance) is optional and non-blocking.

This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.


Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.

@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

APM Review Panel: ship_now

Delta pass on the final head. Since d1c913c the only change is a clean merge of origin/main (#3626 moved neighbouring sparse-checkout calls to run_git_text; the bytes-mode link checkout path is untouched).

panel-mode=delta; personas=apm-ceo (no persona-owned code in this PR changed in range)

cc Daniel Meppiel (@danielmeppiel) Sergio Sisternes (@sergio-sisternes-epam) -- a fresh advisory pass is ready for your review.

No new findings. CI is green at this head, including Windows Compatibility Gate and Windows Native Symlink Acceptance, with 0 unresolved threads. The owner suites re-pass at this head: 31, 62, 8 and 8 tests, the last with the real binary. The recommendation stands: land it.

This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.


Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 5c376a7 into main Nov 12, 2026
24 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-fix-link-target-eol branch November 12, 2026 23:01
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.

2 participants