Skip to content

Fix BuildArchive updates for existing jar entries - #11552

Merged
simonrozsival merged 11 commits into
mainfrom
copilot/fix-buildarchive-incremental-build-bug
Aug 18, 2026
Merged

simonrozsival merged 11 commits into
mainfrom
copilot/fix-buildarchive-incremental-build-bug

Conversation

Copilot AI commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

BuildArchive could remove or preserve stale Java/JAR resources from incremental APK publishes when the previous APK already contained those entries. Duplicate jar-entry adds were skipped, but existing output entries were not distinguished from duplicates added by current build inputs.

  • Archive retention and updates

    • Marks jar-sourced FilesToAddToArchive paths as retained before cleanup.
    • Keeps existing APK entries that are still part of the current build inputs.
    • Updates stale existing APK entries when the source jar entry contents change.
    • Preserves existing duplicate handling when a current packaged resource or earlier archive item already added the same path.
    • Preserves cleanup behavior for entries no longer present in FilesToAddToArchive.
  • Regression coverage

    • Adds focused BuildArchive tests for existing jar-sourced APK entries.
    • Verifies stale Java resource entries are replaced with current jar contents.
    • Verifies up-to-date Java resource entries are skipped.
    • Verifies duplicate current-build jar entries keep the first-added APK entry.
    • Verifies unrelated stale entries are still removed.

Copilot AI changed the title [WIP] Fix incremental build bug for Java resources in APK Fix BuildArchive retention for existing jar entries Jun 1, 2026
Copilot AI requested a review from simonrozsival June 1, 2026 10:41
@simonrozsival

Copy link
Copy Markdown
Member

@copilot The current fix prevents deletion of existing jar-sourced entries, but it still preserves stale contents forever when the source jar entry changes.

Please update BuildArchive so jar-sourced entries distinguish between:

  1. entries that were present in the existing output APK and should be retained/updated, and
  2. entries already added by the current build inputs, where the existing duplicate semantics should still win.

A better shape is:

var wasExistingOutputEntry = existingEntries.Remove (apk_path);

using (var stream = File.OpenRead (jar_file_path))
using (var jar = ZipArchive.Open (stream)) {
	var jar_item = jar.ReadEntry (jar_entry_name);

	if (apk.ContainsEntry (apk_path)) {
		if (!wasExistingOutputEntry) {
			// Current packaged resource or earlier archive item wins.
			Log.LogDebugMessage ("Failed to add jar entry {0} from {1}: the same file already exists in the apk", jar_entry_name, Path.GetFileName (jar_file_path));
			continue;
		}

		if (apk.GetEntry (apk_path).CRC == jar_item.CRC) {
			Log.LogDebugMessage ("Skipping {0} from {1} as it is up to date.", jar_entry_name, jar_file_path);
			continue;
		}

		apk.DeleteEntry (apk_path);
	}

	// extract jar_item and add it as today
}

Then update the regression test so the existing APK entry has stale contents and the jar has current contents, and assert that the APK ends with the current contents. That proves the incremental bug is fixed rather than only hiding the deletion symptom. The test can still keep an unrelated stale entry to verify cleanup behavior.

Copilot AI commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

@copilot The current fix prevents deletion of existing jar-sourced entries, but it still preserves stale contents forever when the source jar entry changes.

Please update BuildArchive so jar-source...

Addressed in 95e7370 and 2c430e7. Jar-sourced entries now distinguish existing output entries from current-build duplicates, refresh stale existing APK contents from the jar, skip up-to-date CRC matches, and keep cleanup coverage for unrelated stale entries.

Copilot AI changed the title Fix BuildArchive retention for existing jar entries Fix BuildArchive updates for existing jar entries Jun 1, 2026
@simonrozsival

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

@github-actions github-actions Bot 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.

@copilot ⚠️ Needs Changes — The fix logic is correct and well-structured. A few items to address:

Issues (1 ⚠️, 3 💡):

  • ⚠️ Test field tempDirectory should be nullable (string?) for [SetUp]-initialized fields
  • 💡 New test file missing #nullable enable and file-scoped namespace
  • 💡 Minor perf trade-off: jar is now opened before the duplicate check (acceptable for correctness)
  • 💡 Consider a test for duplicate jar items targeting the same apk_path

Positive notes:

  • CRC comparison for staleness detection is consistent with the existing pattern used for ApkInputPath entries (line 120)
  • Good separation of the "existing output entry" vs "current build duplicate" cases
  • Regression tests cover both stale-update and up-to-date-skip paths

CI: Public checks pass. Internal Xamarin.Android-PR pipeline status should be verified separately.

Generated by Android PR Reviewer for issue #11552 · ● 7M

@simonrozsival

Copy link
Copy Markdown
Member

@copilot address code review

Copilot AI commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address code review

Addressed in c1168b1 and 2e47ed5. Updated the duplicate jar-entry path to short-circuit before opening duplicate jars, applied nullable/file-scoped test conventions, and added duplicate current-build jar entry coverage.

@simonrozsival

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

❌ Android PR Reviewer failed. Please review the logs for details.

@simonrozsival

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

❌ Android PR Reviewer failed. Please review the logs for details.

@simonrozsival
simonrozsival marked this pull request as ready for review June 4, 2026 08:44
Copilot AI review requested due to automatic review settings June 4, 2026 08:44

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.

Pull request overview

This PR improves incremental APK publishing behavior in BuildArchive for files sourced from jar entries, ensuring that existing APK entries are treated as “retained” build outputs (not as duplicates) and are updated when stale.

Changes:

  • Update BuildArchive jar-entry handling to distinguish existing output entries from same-build duplicates and to replace stale existing entries when contents change (CRC differs).
  • Add focused unit tests covering jar-sourced entries that already exist in the output APK (update, skip when up-to-date, and duplicate handling).
Show a summary per file
File Description
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/BuildArchiveTests.cs Adds regression tests validating update/skip/duplicate behavior for existing jar-sourced APK entries.
src/Xamarin.Android.Build.Tasks/Tasks/BuildArchive.cs Adjusts jar-entry logic to retain existing entries, skip true duplicates, and replace stale entries based on CRC comparison.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

@simonrozsival

Copy link
Copy Markdown
Member

@copilot address review comments

@github-actions

github-actions Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

@github-actions github-actions Bot 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.

@copilot ✅ LGTM — Clean, well-tested fix.

What the PR does: Fixes incremental BuildArchive behavior so that jar-sourced APK entries from previous builds are properly updated (or removed) instead of being incorrectly treated as "already added" duplicates.

Analysis:

  • The core logic change (existingEntries.Remove + CRC-based up-to-date check + DeleteEntry before re-add) is correct. All code paths converge correctly for stale, up-to-date, duplicate, and missing-entry scenarios.
  • IZipArchive.GetEntry throws on missing entries, so the unchecked call on line 181 is safe given the hasApkEntry guard.
  • The existingEntries re-add when jar_item == null correctly restores cleanup semantics — the stale APK entry will be removed during the end-of-task cleanup loop.
  • Test coverage is solid: 4 focused unit tests exercise the key incremental scenarios without needing a full build.

Issues: 0 ❌ · 0 ⚠️ · 2 💡

Severity File Summary
💡 BuildArchiveTests.cs Use [] collection expressions for ITaskItem[] array initialization (6 instances)
💡 BuildArchive.cs:181 Consider adding a brief comment explaining why CRC-only comparison is used here vs CRC+CompressedSize in the input-APK refresh path

CI: ✅ All checks passing.

Generated by Android PR Reviewer for issue #11552 · ● 30M

@simonrozsival

Copy link
Copy Markdown
Member

@copilot address review comments

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address review comments

Addressed in the latest commit:

  1. [] collection expressions — replaced all new ITaskItem [] { ... } initializers in BuildArchiveTests.cs with [...] (6 instances).
  2. CRC comment — added an inline comment on the CRC == jar_item.CRC check explaining that CRC is computed on uncompressed data so a matching CRC means identical content regardless of compression settings.
  3. ContainsEntry before ReadEntry — ZipArchive.ReadEntry throws ZipException on a missing entry rather than returning null (discovered via test failure). Replaced the null-check pattern with jar.ContainsEntry(jar_entry_name) guard before calling ReadEntry, consistent with how ZipArchiveEx.cs uses the API.
  4. Missing-jar-entry test — updated the assertion to handle the case where the APK file itself is removed when its last entry is deleted by the zip library.

Copilot AI and others added 10 commits August 13, 2026 16:42
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
…disk in duplicate test

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
…anup on missing jar entry, add null-jar-entry test

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
…ainsEntry before ReadEntry

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Exercise three unchanged archive builds so the regression test detects the original alternating JAR-entry removal while continuing to verify stale-entry cleanup.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 3f5c44bc-27d8-41e9-b3ae-8585e08c00de
@jonathanpeppers
jonathanpeppers force-pushed the copilot/fix-buildarchive-incremental-build-bug branch from b79c00b to 1e9d788 Compare August 13, 2026 21:48

@simonrozsival simonrozsival 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 — the implementation correctly fixes retention and stale updates for jar-sourced archive entries, includes consecutive-build regression coverage, and the refreshed CI matrix is fully green.

@simonrozsival

Copy link
Copy Markdown
Member

@jonathanpeppers The branch is updated, all review threads are resolved, and the refreshed CI matrix is green. This is ready for final review.

@simonrozsival
simonrozsival enabled auto-merge (squash) August 18, 2026 10:39
@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Aug 18, 2026
@jonathanpeppers

Copy link
Copy Markdown
Member

@dalexsoto review

@dalexsoto dalexsoto 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.

The incremental BuildArchive update, retention, and deletion paths are correct, with complete regression coverage and passing CI.

@simonrozsival
simonrozsival merged commit 286b38c into main Aug 18, 2026
44 checks passed
@simonrozsival
simonrozsival deleted the copilot/fix-buildarchive-incremental-build-bug branch August 18, 2026 13:03
jonathanpeppers added a commit that referenced this pull request Aug 21, 2026
…rites (#12463)

## Summary

- Backports #11552 (`286b38c3109cd39b5512fcfd23530e7cbba7db6f`) so incremental `BuildArchive` updates retain unchanged JAR resources, replace updated entries, and remove stale entries correctly.
- Backports #12450 (`66d3f745f5b231e5c82b78ee9fcf105f883dd9bd`) so in-place post-link processing excludes satellite assemblies from the NuGet package cache, while preserving normal packaging and non-in-place behavior.

## RC1 justification

These are narrowly scoped customer-facing correctness and reliability fixes. They prevent stale or incorrect APK contents during incremental builds, prevent mutation of NuGet package-cache assemblies, and avoid concurrent-build XAAMP7024 failures caused by writes to shared cached satellite assemblies.

There are no public API changes and no dependency changes.

## Validation

- PASS: `BuildArchiveTests` — 4 passed, 0 failed.
- PASS: `Xamarin.Android.Build.Tests` and its product dependencies compiled successfully with the host .NET 11 SDK after building the existing bootstrap task prerequisites.
- PASS: `Xamarin.Android.Common.targets` XML validation.
- The `BuildDoesNotModifyNuGetPackageCache` integration test compiled and started, but could not execute its nested app build because this checkout does not contain the repo-local SDK at `bin\Debug\dotnet\dotnet`; CI should provide full-build coverage.

Upstream references: #11552 and #12450.

Co-authored-by: Šimon Rozsíval <simon@rozsival.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BuildArchive incremental build bug: Java resources alternate between present and missing in APK on consecutive publishes

5 participants