fix: cascade-delete all orphaned objects in PreviewHost cleanup#42
Merged
Iron-Ham merged 1 commit intoMay 11, 2026
Merged
Conversation
The previous `removeExisting` only removed the first PreviewHost native target and a single matching root-group child. Everything else attached to the target was orphaned in the pbxproj on every preview run: - XCBuildConfigurations (Debug + Release) and their XCConfigurationList - PBXSourcesBuildPhase / PBXFrameworksBuildPhase / PBXResourcesBuildPhase (and any embed-frameworks copy-files phase) plus their PBXBuildFiles - PBXTargetDependency entries and their PBXContainerItemProxy - XCSwiftPackageProductDependency stubs forwarded onto the target - The PBXFileReference for `PreviewHost.app` (and its slot in Products) - Additional PreviewHost PBXGroups and their child file refs Because each leak left another `PreviewHost` target / group in the tree, the next run's `.first(where:)` only peeled one off, and every subsequent inject piled another full set on top — so projects accumulated hundreds of lines of cruft and broken file references pointing at the deleted `.preview-host-XXXXX/` temp directories. Cleanup now: - Iterates over ALL PreviewHost targets (not just `.first`) and cascade-deletes their build phases + build files, configuration list + configurations, dependencies + container item proxies, package product deps, and product reference (also removing it from the Products group). - Recursively scrubs every PreviewHost PBXGroup and its children. - Final pass removes orphan `PreviewHost.app` refs from the Products group left behind by earlier broken cleanups. Adds a regression test that runs inject + cleanup three times on the same project and asserts no PreviewHost-related target, group, file ref, or build configuration survives any cleanup pass.
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
removeExistingonly deleted the first matchingPreviewHostnative target and a single root-group child. Everything else attached to the target was orphaned in the pbxproj on every preview run, and because each leak left anotherPreviewHosttarget / group, the next run's.first(where:)only peeled one off — so projects accumulated hundreds of lines of cruft and broken file references pointing at deleted.preview-host-XXXXX/temp dirs.What changed
removeExistingnow:PreviewHostnative targets (not just.first) and cascade-deletes:PreviewHost.appproduct reference (also removing it from the Products group),project.targetsand the target object itself.PreviewHostPBXGroups and deletes each child file ref before removing the group.PreviewHost.apprefs in the Products group left behind by earlier broken cleanups.Test plan
testCleanupLeavesNoOrphanedObjectsruns inject + cleanup three times on the same project and asserts no PreviewHost-related target, group, file ref, or build configuration survives any cleanup pass.ProjectInjectorTestspass (cleanup, idempotent injection, scheme creation, etc.)./previewruns add 0 lines toproject.pbxproj(and actually scrub pre-existing orphan refs from prior buggy runs).