feat: clean up stale targets removed from config on next build
All checks were successful
Continuous Integration / Build Package (push) Successful in 28s
Continuous Integration / Lint, Check & Test (push) Successful in 47s

When a target is present in the state file but no longer in the config,
its output file is deleted (or archived if archive_folder is set) and
its state entry is removed. This runs at the start of every build.
This commit is contained in:
Konstantin Fickel 2026-02-21 18:51:39 +01:00
parent 7503672942
commit d8e0ed561d
Signed by: kfickel
GPG key ID: A793722F9933C1A5
3 changed files with 167 additions and 0 deletions

View file

@ -44,6 +44,7 @@ class BuildEvent(enum.Enum):
TARGET_FAILED = "failed"
TARGET_DEP_FAILED = "dep_failed"
TARGET_NO_PROVIDER = "no_provider"
TARGET_REMOVED = "removed"
ProgressCallback = Callable[[BuildEvent, str, str], None]
@ -184,6 +185,30 @@ async def _build_single_target(
)
def _cleanup_stale_targets(
config: ProjectConfig,
project_dir: Path,
state: BuildState,
on_progress: ProgressCallback = _noop_callback,
) -> None:
"""Remove or archive output files for targets no longer in the config.
Also removes their entries from the build state.
"""
stale = [name for name in state.targets if name not in config.targets]
for name in stale:
output_path = project_dir / name
if output_path.exists():
if config.archive_folder is not None:
dest = archive_file(output_path, project_dir, config.archive_folder)
detail = str(dest.relative_to(project_dir)) if dest else ""
else:
output_path.unlink()
detail = "deleted"
on_progress(BuildEvent.TARGET_REMOVED, name, detail)
del state.targets[name]
async def run_build(
config: ProjectConfig,
project_dir: Path,
@ -218,6 +243,13 @@ async def run_build(
graph = get_subgraph_for_target(graph, target)
state = load_state(project_dir, project_name)
# Clean up targets that are in state but no longer in config.
stale_before = set(state.targets)
_cleanup_stale_targets(config, project_dir, state, on_progress)
if set(state.targets) != stale_before:
save_state(state, project_dir, project_name)
generations = get_build_order(graph)
target_names = set(config.targets)