From 066493c002cc447d67b4b1702bdbc5657cac8f29 Mon Sep 17 00:00:00 2001 From: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:15:46 -0800 Subject: [PATCH] fix(init): remove a new project directory when shared infra fails `specify init ` 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) --- src/specify_cli/command_init.py | 13 ++++- tests/specify_cli/test_command_init.py | 66 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/specify_cli/command_init.py b/src/specify_cli/command_init.py index a2fa33ee21..56cdffd431 100644 --- a/src/specify_cli/command_init.py +++ b/src/specify_cli/command_init.py @@ -1024,7 +1024,18 @@ def init( ensure_constitution_from_template(project_path, tracker=tracker) tracker.complete("final", "project ready") - except (typer.Exit, SystemExit): + except (typer.Exit, SystemExit) as exc: + # A failing step that already reported its own error exits + # here (e.g. a shared-infrastructure OSError becomes + # typer.Exit(1)); remove a directory this run created, as the + # generic failure path below does. + exit_code = getattr(exc, "exit_code", getattr(exc, "code", 1)) + 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 + ): + shutil.rmtree(project_path, ignore_errors=True) raise except Exception as e: tracker.error("final", str(e)) diff --git a/tests/specify_cli/test_command_init.py b/tests/specify_cli/test_command_init.py index f09f463f24..a5f22e726a 100644 --- a/tests/specify_cli/test_command_init.py +++ b/tests/specify_cli/test_command_init.py @@ -189,6 +189,72 @@ def test_invalid_integration_value_is_rendered_literally(tmp_path: Path): assert "nope[/red]" in _strip(result.stdout) +def _fail_shared_infra(monkeypatch): + """Make the shared-infrastructure step fail part-way, as a full disk would. + + ``_install_shared_infra_or_exit`` turns the ``OSError`` into + ``typer.Exit(1)``, which is how init sees a real shared-infra failure. + """ + import specify_cli + + def partial_install(project_path, *args, **kwargs): + scripts = Path(project_path) / ".specify" / "scripts" + scripts.mkdir(parents=True, exist_ok=True) + (scripts / "partial.sh").write_text("#!/usr/bin/env bash\n", encoding="utf-8") + raise OSError(28, "No space left on device") + + monkeypatch.setattr(specify_cli, "_install_shared_infra", partial_install) + + +def test_shared_infra_failure_removes_new_project_dir(tmp_path: Path, monkeypatch): + """A failed init into a new directory removes it, so a retry can succeed. + + The shared-infrastructure failure arrives as ``typer.Exit(1)``. It used to + be re-raised before the cleanup ran, leaving a half-initialized project + that made the retry fail with "Directory already exists". + """ + _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() + retry = _init(tmp_path, "proj") + assert retry.exit_code == 0, _strip(retry.stdout) + assert (tmp_path / "proj" / ".specify").is_dir() + + +def test_shared_infra_failure_keeps_existing_project_dir(tmp_path: Path, monkeypatch): + """A failed init never deletes a directory that existed before it ran.""" + existing = tmp_path / "proj" + existing.mkdir() + keep = existing / "notes.txt" + keep.write_text("user data\n", encoding="utf-8") + _fail_shared_infra(monkeypatch) + + previous = os.getcwd() + os.chdir(tmp_path) + try: + result = CliRunner().invoke( + app, + [ + "init", "proj", "--force", + "--integration", "generic", + "--integration-options", "--commands-dir .agent/commands", + "--ignore-agent-tools", "--offline", + ], + catch_exceptions=True, + ) + finally: + os.chdir(previous) + + assert result.exit_code == 1, _strip(result.stdout) + assert keep.read_text(encoding="utf-8") == "user data\n" + + def _cd_argument(stdout: str) -> str: """Return the argument of the printed `cd` command, verbatim.