Skip to content

fix: drop replaced stage outs from .gitignore on stage add --force - #11114

Draft
adarshsm wants to merge 1 commit into
treeverse:mainfrom
adarshsm:fix/6036-stage-add-force-gitignore
Draft

adarshsm wants to merge 1 commit into
treeverse:mainfrom
adarshsm:fix/6036-stage-add-force-gitignore

Conversation

@adarshsm

@adarshsm adarshsm commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #6036

Bug

dvc stage add --force (and dvc run --force) replaces a stage in dvc.yaml, but the outputs the old stage had and the new one doesn't stay in .gitignore:

dvc stage add -n test_stage -o file1 -o file2 python test_script.py     # .gitignore: /file1 /file2
dvc stage add --force -n test_stage -o file1 -o file3 python test_script.py
# .gitignore: /file1 /file2 /file3   (expected: /file1 /file3)

I reproduced this on current main with the Python API (repo.stage.add(..., force=True)).

Cause

StageLoad.add dumps the new stage and calls stage.ignore_outs() for its outputs only. The stage it overwrites is never looked at, so its dropped outputs are never un-ignored. dvc remove does un-ignore them (via Output.ignore_remove).

Fix

When force is set and the stage being written already exists in dvc.yaml, load the old stage before it is overwritten. After the new stage is dumped, call ignore_remove() on every old output whose path is not an output of the new stage. Only pipeline stages (dvc.yaml) are handled, since the issue is about dvc stage add. Without --force nothing changes.

Tests

New test_stage_add_force_drops_replaced_outs_from_gitignore in tests/func/test_stage.py. It fails on main and passes with this change. tests/func/test_stage.py and tests/func/test_run.py pass locally (76 passed). ruff check and ruff format --check are clean.

  • ❗ I have followed the Contributing to DVC checklist.

  • 📖 If this PR requires documentation updates, I have created a separate PR (or issue, at least) in dvc.org and linked it here. (No docs change needed.)

🤖 Generated with Claude Code

`dvc stage add --force` only added the new stage's outputs to .gitignore.
Outputs the overwritten stage had but the new one doesn't stayed ignored.
Load the stage being replaced and un-ignore the outputs that are gone.

Fixes treeverse#6036

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>