spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

[Bug]: `extension add --dev --force` deletes the extension directory and registers nothing when source path == install path

Open jinaparkdev opened this issue 5 months ago โ€ข 1 comments

Bug Description

Running specify extension add <path> --dev --force where <path> resolves to the extension's own install location (.specify/extensions/<id>) deletes the entire extension directory and does not register the extension. The source files are destroyed and no registration occurs โ€” pure data loss with no successful outcome.

Steps to Reproduce

  1. Have a local custom extension at .specify/extensions/code-review/ containing extension.yml and commands/speckit.code-review.md.
  2. Run: specify extension add .specify/extensions/code-review --dev --force

Expected Behavior

  • The extension is registered (appears in specify extension list), OR
  • The command fails fast with a clear error (e.g. "source path is the install destination") without deleting anything.

Actual Behavior

  • .specify/extensions/code-review/ is deleted entirely (extension.yml and commands/speckit.code-review.md removed).
  • specify extension list still does not show the extension โ€” registration failed.
  • A catalog cache is created at .specify/extensions/.cache/ as a side effect.
  • Files were only recoverable because they happened to be tracked in git (git checkout -- .specify/extensions/code-review/...).

Specify CLI Version

0.10.2

AI Agent

Claude Code

Operating System

macOS 25.5.0

Python Version

3.12.12

Error Logs


Additional Context

Likely cause (hypothesis)

--dev appears to create a symlink at the install destination (see #2930). With --force, the existing destination is removed first; when source == destination, this removes the source before the link/copy step, leaving nothing. The destructive removal seems to happen before validating that the operation can complete.

Suggested fix

  • Detect and reject source == install destination before any filesystem mutation.
  • Do not remove the destination until the source has been validated/staged.
  • On any failure, leave the original directory untouched.

jinaparkdev avatar Jun 16 '26 03:06 jinaparkdev

Bug assessment โ€” extension-dev-force-self-delete: Valid ยท severity high โ€” already fixed in v0.11.1


Bug Assessment: extension add --dev --force Self-Delete

  • Slug: extension-dev-force-self-delete
  • Created: 2026-06-26T12:57:36Z
  • Source: issue #2990
  • Verdict: valid
  • Severity: high

Report (summarized)

Reporter (jinaparkdev) running specify-cli v0.10.2 on macOS with Python 3.12.12 observed that running:

specify extension add .specify/extensions/code-review --dev --force

where the source path resolves to the extension's own install destination (.specify/extensions/code-review/) silently deletes the entire extension directory โ€” including extension.yml and commands/speckit.code-review.md โ€” and then fails to register the extension. Files were recoverable only because they happened to be tracked in git. The reporter correctly hypothesized the root cause: --force removes the existing destination before copying, and when source == destination this destroys the only copy before any copy operation can succeed.

Symptom

Running specify extension add <path> --dev --force where <path> is the extension's own install directory destroys all extension files without warning and leaves the extension unregistered. Expected behavior: either register successfully, or fail fast with a clear error and leave all files intact.

Reproduction

  1. Have a local custom extension at .specify/extensions/code-review/ (with extension.yml and at least one command file).
  2. Run: specify extension add .specify/extensions/code-review --dev --force
  3. Observe that .specify/extensions/code-review/ is now empty/gone.
  4. Run specify extension list โ€” extension does not appear.

Suspected Code Paths

  • src/specify_cli/extensions/__init__.py:1365โ€“1385 (install_from_directory) โ€” the --force path calls self.remove(manifest.id) (line 1381) and then unconditionally does shutil.rmtree(dest_dir) (line 1385) when dest_dir.exists(). When source_dir == dest_dir both of these operations destroy the source. No same-location guard existed prior to the fix.
  • src/specify_cli/extensions/_commands.py:472โ€“481 (extension_add) โ€” --dev branch resolves source_path and passes it directly to manager.install_from_directory(..., force=force) without checking whether source_path is already the install destination.

Root Cause Hypothesis

In install_from_directory(), when force=True and the extension is already installed, the code removes the existing installation first (via self.remove()), then calls shutil.rmtree(dest_dir) if the destination still exists, and finally does shutil.copytree(source_dir, dest_dir). When source_dir and dest_dir are the same path, the removal steps destroy the source before copytree runs, resulting in an empty or absent source and therefore data loss with no successful copy. Confidence: high โ€” the code path is clear and the reporter's reproduction matches the sequence.

Proposed Remediation

Preferred (already implemented in commit de18d21): Add a guard at the top of install_from_directory(), before any filesystem mutations, that resolves both source_dir and dest_dir and raises ValidationError when they are the same path:

# src/specify_cli/extensions/__init__.py, lines 1349โ€“1363 (current HEAD)
dest_dir = self.extensions_dir / manifest.id
try:
    same_location = source_dir.resolve(strict=False) == dest_dir.resolve(strict=False)
except (OSError, RuntimeError):
    same_location = source_dir.absolute() == dest_dir.absolute()
if same_location:
    raise ValidationError(
        f"Source path is the install destination for '{manifest.id}' "
        f"({dest_dir}). Refusing to proceed to avoid deleting the "
        f"extension. Install from a copy in a different location instead."
    )

The ValidationError is caught by the extension_add command wrapper at line 622 of _commands.py and displayed as [red]Validation Error:[/red] <message> with exit code 1 โ€” no data loss, clear user-facing error.

Alternatives:

  • Stage the copy to a temp directory first, then atomically swap into place โ€” more complex, but would also protect against partial-copy failures from other causes. Trade-off: significantly more code and temp-space usage.
  • Detect and reject only in the --force path (not --no-force) โ€” simpler but incomplete; the shutil.rmtree(dest_dir) at line 1384 runs unconditionally when dest_dir.exists() regardless of the force flag, so the guard is needed in both cases.

Files likely to change:

  • src/specify_cli/extensions/__init__.py (guard already added)
  • tests/test_extensions.py (regression tests already added)

Tests to add or update:

  • test_install_from_install_dir_is_rejected_without_data_loss (line 1157) โ€” regression test verifying that ValidationError is raised and all files are intact. โœ… Added in de18d21.
  • test_install_from_install_dir_is_rejected_when_resolve_fails (line 1180) โ€” tests fallback to .absolute() when .resolve() raises OSError. โœ… Added in de18d21.

Fix Status

Already fixed. Commit de18d21 ("fix: prevent extension self-install from deleting source dir (#2990) (#2991)"), authored by Jina Park and merged 2026-06-17, is present in releases v0.11.1 and later. The bug affects versions up through v0.10.x (confirmed absent in v0.10.4 and earlier; fix lands in v0.11.1). Users on v0.10.2 (as in this report) should upgrade to โ‰ฅ v0.11.1.

Risks & Considerations

  • Upgrade path: Users on v0.10.x still face data loss. A release note or changelog callout for v0.11.1 (or a backport to v0.10.x if an LTS branch exists) would be prudent.
  • Symlink edge case: The guard uses resolve(strict=False) which resolves symlinks. If the user passed a symlink that points to the install directory, resolve() correctly catches it. The .absolute() fallback (when resolve() raises) compares raw paths and may miss symlink aliases โ€” considered acceptable given it only activates on abnormal filesystem states.
  • No regression risk: The fix is purely additive (new early-return before any mutations). Existing installs from different source directories are unaffected.

Open Questions

  • None โ€” report is fully actionable and the fix has already been merged.

Generated by ๐Ÿ› Assess Bug from Labeled Issue for issue #2990 ยท 364.4 AIC ยท โŒ– 13.4 AIC ยท โŠž 33.2K ยท โ—ท

github-actions[bot] avatar Jun 26 '26 13:06 github-actions[bot]