Repository navigation
Conversation
`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)
There was a problem hiding this comment.
🟢 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.ExitandSystemExit, 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)
There was a problem hiding this comment.
🟡 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 |
| _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() |
|
Please address Copilot feedback. If not applicable, please explain why |


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, theexcept (typer.Exit, SystemExit): raiseclause of the inittryblock.Root cause: init removes a directory it created when initialization fails. That cleanup only runs in the
except Exceptionbranch._install_shared_infra_or_exit(src/specify_cli/__init__.py) reports anOSError/ValueErroritself and raisestyper.Exit(1). Init re-raisestyper.Exitbefore 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_exitis the only call in thattryblock that raisestyper.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):With this change:
Fix: for a non-zero
typer.Exit/SystemExit, run the same new-directory cleanup as the generic failure path, with the same guards:--here;initial_directory_state(project_path) is None, so lifecycle-owned rollback is left alone.ignore_errors=Truestops 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 raiseOSError(28). It asserts that init exits 1, prints the shared-infra error and removesproj, and that a retry then succeeds. It fails onmain(assert not (tmp_path / "proj").exists()) and passes with the fix.test_shared_infra_failure_keeps_existing_project_dir(negative) runsinit proj --forceinto 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 passedfull 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:mainin 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) andmcp_server/test_stdio.py::test_real_stdio_server_initializes_discovers_and_calls_first_class_tools(an MCPtools/calltimeout under-n 6).workflows/test_step_capture.py::…::test_streaming_is_unchanged_without_capture, where macOS printed aMallocStackLoggingline on stderr, and onetest_installed_adapters.pycase.ruff check src tests(ruff 0.15.0, as inlint.yml): cleanTested locally with
.venv/bin/specify --helpRan relevant existing tests with
.venv/bin/python -m pytest tests -q(withLC_ALL=en_US.UTF-8on macOS)Tested with a sample project: real
specify initabove, with the failure on a full disk and a successful retrySlash commands: the change only touches the error path of
specify initafter 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
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