Skip to content

fix(init): remove a new project directory when shared infra fails - #5481

Open
divya0795 wants to merge 1 commit into
github:mainfrom
divya0795:fix/init-cleanup-on-shared-infra-exit
Open

divya0795 wants to merge 1 commit into
github:mainfrom
divya0795:fix/init-cleanup-on-shared-infra-exit

Conversation

@divya0795

Copy link
Copy Markdown

Description

specify init <new-dir> leaves a half-initialized project behind when installing shared infrastructure fails, and the retry then fails with "Directory already exists".

Where: src/specify_cli/command_init.py, the except (typer.Exit, SystemExit): raise clause of the init try block.

Root cause: init removes a directory it created when initialization fails. That cleanup only runs in the except Exception branch. _install_shared_infra_or_exit (src/specify_cli/__init__.py) reports an OSError/ValueError itself and raises typer.Exit(1). Init re-raises typer.Exit before the cleanup, so this failure skips it. Integration setup has already written .agent/ and .specify/ into the new directory by then. _install_shared_infra_or_exit is the only call in that try block that raises typer.Exit, so in practice the clause only ever bypasses the cleanup. The comment above the journal (A new directory is removed on failure instead.) states the intended behavior.

Reproduction with a real disk-full error (no mocks). I used a 4 MB HFS+ disk image on macOS, filled until about 230 KB was free, and ran specify init proj --integration generic --integration-options "--commands-dir .agent/commands" --ignore-agent-tools --offline.

On main (59d61c5):

Error: Failed to install shared infrastructure: [Errno 28] No space left on device
exit=1
$ ls -A proj
.agent
.specify
# free the space and retry
retry exit=1
│  Directory already exists: 'proj'

With this change:

Error: Failed to install shared infrastructure: [Errno 28] No space left on device
exit=1
$ ls -A proj
ls: proj: No such file or directory
retry exit=0
Project ready.

Fix: for a non-zero typer.Exit/SystemExit, run the same new-directory cleanup as the generic failure path, with the same guards:

  • not --here;
  • the directory did not exist before;
  • initial_directory_state(project_path) is None, so lifecycle-owned rollback is left alone.

ignore_errors=True stops a cleanup error from masking the original exit. A zero exit code is never cleaned up.

Testing

Regression tests in tests/specify_cli/test_command_init.py:

  • test_shared_infra_failure_removes_new_project_dir (positive) makes shared-infra install write a partial file and raise OSError(28). It asserts that init exits 1, prints the shared-infra error and removes proj, and that a retry then succeeds. It fails on main (assert not (tmp_path / "proj").exists()) and passes with the fix.
  • test_shared_infra_failure_keeps_existing_project_dir (negative) runs init proj --force into an existing directory with a user file. The file must survive the failure. It passes before and after; it guards the existing-directory case.

Commands run on macOS, Python 3.14 (CI matrix version):

  • .venv/bin/python -m pytest tests/specify_cli/test_command_init.py -q: 171 passed

  • full suite LC_ALL=en_US.UTF-8 .venv/bin/python -m pytest tests -q -n 6: 16790 passed, 620 skipped, 7 failed. None of the 7 is related to this change:

    • 2 fail the same way on unmodified main in my environment: test_version_execution.py::…::test_non_windows_uv_tool_dry_run_still_previews_installer (Rich wraps the long temp path, so the expected command string is split) and mcp_server/test_stdio.py::test_real_stdio_server_initializes_discovers_and_calls_first_class_tools (an MCP tools/call timeout under -n 6).
    • 5 failed only under parallel load: 4 parametrizations of workflows/test_step_capture.py::…::test_streaming_is_unchanged_without_capture, where macOS printed a MallocStackLogging line on stderr, and one test_installed_adapters.py case.
    • Re-running those files serially passes: 94 passed.
  • ruff check src tests (ruff 0.15.0, as in lint.yml): clean

  • Tested locally with .venv/bin/specify --help

  • Ran relevant existing tests with .venv/bin/python -m pytest tests -q (with LC_ALL=en_US.UTF-8 on macOS)

  • Tested with a sample project: real specify init above, with the failure on a full disk and a successful retry

Slash commands: the change only touches the error path of specify init after a failed shared-infrastructure install, and no generated file changes on success. So no /speckit.* command behavior is affected, and I did not run one through an agent.

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

AI disclosure: Found, reproduced, fixed and tested by Claude Code (model: Claude Opus 5.5) running autonomously on behalf of GitHub user divya0795. AI wrote the code, the tests and this description. The commit carries an Assisted-by: trailer. No human reviewed this change line by line before submission. Any later replies on this PR made by the agent will say so.

🤖 Generated with Claude Code

`specify init <new-dir>` creates the project directory, then installs
shared infrastructure. `_install_shared_infra_or_exit` reports an
OSError/ValueError there and raises typer.Exit(1), but init re-raised
typer.Exit before the cleanup that removes a directory the run created.
A disk-full failure therefore left a half-initialized project behind
and the retry failed with "Directory already exists".

Run the same new-directory cleanup for a non-zero typer.Exit/SystemExit,
keeping the existing guards for --here, pre-existing directories and
lifecycle-owned rollback.

Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous)
@divya0795
divya0795 requested a review from mnriem as a code owner November 18, 2026 12:20
Copilot AI adaptive review requested due to automatic review settings November 18, 2026 12:20

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.

🟢 Approval recommended

No active review issues are identified for the change that cleans up newly created project directories after initialization failures.

No open issues 🎉

What changed in this PR

Fixes Specify CLI initialization so a failed shared-infrastructure install removes a newly created project directory, allowing a retry without a “Directory already exists” error.

Changes:

  • Adds cleanup for nonzero typer.Exit and SystemExit, preserving existing directories and lifecycle-owned rollback.
  • Adds regression tests for new-directory removal, successful retry, and preservation of existing user files.

The PR reports before-and-after regression results; these were not independently rerun.

File Description
tests/​specify_cli/​test_command_init.py Tests failure cleanup, retry success, and existing-file preservation.
src/​specify_cli/​command_init.py Cleans up newly created directories on failure exits without masking the original error.

Review effort: Balanced (adaptive)

@mnriem mnriem added the triage-can-wait Verdict: valid and in-scope but deprioritized; held behind the evidence gate label Nov 18, 2026
@mnriem
mnriem requested an adaptive review from Copilot November 18, 2026 17:50

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.

🟡 Changes recommended

A noncanonical target path can trigger deletion of unowned files before lifecycle rollback, and the retry test removes environment isolation, making it depend on the user’s configuration.

2 open issues

Review effort: Balanced (adaptive)

if (
exit_code not in (0, None)
and not here and project_path.exists() and not dir_existed_before
and initial_directory_state(project_path) is None
Comment on lines +216 to +224
_fail_shared_infra(monkeypatch)

result = _init(tmp_path, "proj")

assert result.exit_code == 1, _strip(result.stdout)
assert "Failed to install shared infrastructure" in _strip(result.stdout)
assert not (tmp_path / "proj").exists()

monkeypatch.undo()
@mnriem

mnriem commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Please address Copilot feedback. If not applicable, please explain why

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triage-can-wait Verdict: valid and in-scope but deprioritized; held behind the evidence gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants