Skip to content

fix: handle commit SHA refs in subdirectory package clones#178

Merged
sergio-sisternes-epam merged 4 commits intomicrosoft:mainfrom
sergio-sisternes-epam:fix/177-lockfile-commit-sha-clone
Mar 6, 2026
Merged

fix: handle commit SHA refs in subdirectory package clones#178
sergio-sisternes-epam merged 4 commits intomicrosoft:mainfrom
sergio-sisternes-epam:fix/177-lockfile-commit-sha-clone

Conversation

@sergio-sisternes-epam
Copy link
Collaborator

Fixes #177

Problem

download_subdirectory_package passed lockfile commit SHAs as --branch to shallow clones, which git does not support. This caused every subdirectory package to fail on the second apm install when a lockfile was present.

Reproduction

  1. Clean install (rm -rf apm_modules apm.lock) → succeeds, writes apm.lock with resolved_commit: <sha>
  2. Run apm install again → all subdirectory packages fail with fatal: Remote branch <sha> not found in upstream origin

Solution

  • Detect when the ref is a commit SHA (7-40 hex chars) in download_subdirectory_package
  • For commit SHAs: do a full clone, then git checkout <sha>
  • For branches/tags: keep existing shallow clone behavior (--depth=1 --branch=<ref>)

Testing

  • All 100 existing tests pass (33 skipped — environment-gated)
  • Manual verification: apm install is now fully idempotent across repeated runs

Copilot AI review requested due to automatic review settings March 6, 2026 00:34
@sergio-sisternes-epam sergio-sisternes-epam added this to the 0.8.0 milestone Mar 6, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes apm install idempotency for virtual subdirectory (monorepo) packages when a lockfile provides a pinned commit SHA, by avoiding git clone --depth=1 --branch <sha> (which fails for arbitrary SHAs) and instead cloning then checking out the SHA.

Changes:

  • Detect “ref looks like commit SHA” in download_subdirectory_package.
  • For commit SHAs, perform a non-shallow clone and git checkout <sha>; otherwise keep shallow clone behavior for branch/tag refs.

You can also share your feedback on Copilot code review. Take the survey.

When a lockfile contains a resolved commit SHA, download_subdirectory_package
was passing it as --branch to a shallow clone, which git does not support.
Now detects commit SHAs and uses a full clone + checkout instead.

Fixes microsoft#177
- Use no_checkout=True instead of full working-tree clone for SHA refs
- Separate try/except for clone vs checkout with descriptive error messages
- Use exception chaining (raise ... from e) to preserve tracebacks
- Remove unnecessary ref.lower() call
- Add 4 unit tests covering SHA, branch, no-ref, and checkout failure paths
Copy link
Collaborator

@danielmeppiel danielmeppiel left a comment

Choose a reason for hiding this comment

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

LGTM — correct fix for a real bug (lockfile commit SHAs breaking --branch).

What I verified:

  • Root cause accurately identified: git clone --branch <sha> is unsupported for arbitrary commit SHAs ✓
  • SHA detection regex r'^[a-f0-9]{7,40}$' is the standard pattern (7-char min avoids false positives on short branch names) ✓
  • no_checkout=True → GitPython translates to --no-checkout for Repo.clone_from — correct ✓
  • _try_sparse_checkout also fails for commit SHAs (git fetch origin <sha> --depth=1), but gracefully returns False and falls through to the now-fixed full clone path — flow is correct ✓
  • Exception chaining (from e) added — nice improvement ✓
  • 4 tests cover SHA, branch, no-ref, and checkout failure cases ✓

Non-blocking note for future: Full clone for SHA refs could be slow on large repos. A more optimal approach would be git init + git fetch origin <sha> + git checkout FETCH_HEAD, but this is an acceptable v1. Could be optimized later if users report perf issues.

Merge order: PR 2 of 3. Rebase onto main after #176 merges.

@sergio-sisternes-epam sergio-sisternes-epam merged commit 8a4f0fe into microsoft:main Mar 6, 2026
6 checks passed
@sergio-sisternes-epam sergio-sisternes-epam deleted the fix/177-lockfile-commit-sha-clone branch March 6, 2026 11:18
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.

apm install fails on second run when lockfile contains commit SHAs for subdirectory packages

3 participants