Skip to content

Commit 1db0301

Browse files
committed
fix(extensions): deploy config where it is read, and contain the write
Addresses @Copilot's review. Config now lands in .specify/extensions/<id>/ rather than the .specify/ root. ConfigManager._get_project_config() reads .specify/extensions/<id>/<id>-config.yml, and the bundled scripts and READMEs use the same path, so a scaffolded git-config.yml was being written somewhere the git extension never looks. Containment is checked component by component before .specify is used as the root. Resolving it first and trusting the result let a symlinked component point outside the project, after which every target satisfied relative_to and copy2 wrote externally. This matches the project safe-write path in shared_infra. mkdir moved inside the OSError handler. A nested target like foo/config.yml raised out of scaffolding when its parent could not be created, and on extension add that happened after the extension was already installed. The 'Configuration may be required' warning is now conditional. It ran unconditionally after the scaffolding block, so it contradicted the success output directly above it and fired for extensions with no provides.config at all. Tests cover the corrected location, a symlinked config root, and an uncreatable nested target.
1 parent 1f32ec0 commit 1db0301

3 files changed

Lines changed: 123 additions & 12 deletions

File tree

‎src/specify_cli/extensions/__init__.py‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,31 @@ def install_from_zip(
24782478
extension_dir, speckit_version, priority=priority, force=force
24792479
)
24802480

2481+
def _config_root_is_contained(self, specify_dir: Path) -> bool:
2482+
"""Report whether `.specify` is a real directory inside the project.
2483+
2484+
Checked component by component so a symlink anywhere on the path is
2485+
rejected before it becomes the containment root. A missing `.specify`
2486+
is fine: scaffolding creates it under the project root.
2487+
"""
2488+
try:
2489+
root = self.project_root.resolve()
2490+
except OSError:
2491+
return False
2492+
current = self.project_root
2493+
for part in specify_dir.relative_to(self.project_root).parts:
2494+
current = current / part
2495+
if current.is_symlink():
2496+
return False
2497+
if not current.exists():
2498+
return True
2499+
try:
2500+
if current.resolve().relative_to(root) is None:
2501+
return False
2502+
except (OSError, ValueError):
2503+
return False
2504+
return current.is_dir()
2505+
24812506
def scaffold_config(self, extension_id: str) -> tuple[List[str], List[str], List[str]]:
24822507
"""Deploy config templates from an installed extension to the project.
24832508
@@ -2515,7 +2540,20 @@ def scaffold_config(self, extension_id: str) -> tuple[List[str], List[str], List
25152540
return deployed, skipped_existing, ["provides.config"]
25162541

25172542
ext_dir_resolved = ext_dir.resolve()
2518-
specify_dir_resolved = (self.project_root / ".specify").resolve()
2543+
# Config is deployed beneath the extension's own directory because that
2544+
# is where it is read from: ConfigManager._get_project_config() loads
2545+
# `.specify/extensions/<id>/<id>-config.yml`, and the bundled scripts
2546+
# and READMEs use the same location. Writing to `.specify/<name>` put
2547+
# the file somewhere nothing ever looks.
2548+
config_dir = self.project_root / ".specify" / "extensions" / extension_id
2549+
# Resolving that directory and trusting the result as the containment
2550+
# root lets a symlinked component point outside the project: every
2551+
# target would then satisfy relative_to and copy2 would write
2552+
# externally. Refuse a symlinked component up front, matching the
2553+
# project safe-write path in shared_infra.
2554+
if not self._config_root_is_contained(config_dir):
2555+
return deployed, skipped_existing, ["provides.config"]
2556+
config_dir_resolved = config_dir.resolve()
25192557

25202558
for config_entry in manifest.config:
25212559
template_name = config_entry.get("template", "")
@@ -2530,10 +2568,10 @@ def scaffold_config(self, extension_id: str) -> tuple[List[str], List[str], List
25302568

25312569
template_candidate = ext_dir / template_name
25322570
template_path = template_candidate.resolve()
2533-
target_path = (self.project_root / ".specify" / target_name).resolve()
2571+
target_path = (config_dir / target_name).resolve()
25342572
try:
25352573
template_path.relative_to(ext_dir_resolved)
2536-
target_path.relative_to(specify_dir_resolved)
2574+
target_path.relative_to(config_dir_resolved)
25372575
except ValueError:
25382576
failed.append(failure_name)
25392577
continue
@@ -2546,8 +2584,12 @@ def scaffold_config(self, extension_id: str) -> tuple[List[str], List[str], List
25462584
skipped_existing.append(target_name)
25472585
continue
25482586

2549-
target_path.parent.mkdir(parents=True, exist_ok=True)
25502587
try:
2588+
# mkdir belongs inside the handler: a nested target like
2589+
# foo/config.yml must land in `failed` when `.specify/foo` is a
2590+
# file or cannot be created, not raise out of scaffolding after
2591+
# `extension add` has already installed the extension.
2592+
target_path.parent.mkdir(parents=True, exist_ok=True)
25512593
shutil.copy2(template_path, target_path)
25522594
except OSError:
25532595
failed.append(target_name)

‎src/specify_cli/extensions/_commands.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,11 @@ def extension_add(
713713

714714
# Scaffold config templates automatically
715715
deployed, skipped, failed = manager.scaffold_config(manifest.id)
716+
config_home = f".specify/extensions/{_escape_markup(str(manifest.id))}"
716717
if deployed:
717718
console.print("\n[bold cyan]Config scaffolded:[/bold cyan]")
718719
for cfg in deployed:
719-
console.print(f" • .specify/{_escape_markup(str(cfg))}")
720+
console.print(f" • {config_home}/{_escape_markup(str(cfg))}")
720721
if skipped:
721722
console.print(f"\n[dim]Config files already exist (preserved): {_escape_markup(', '.join(skipped))}[/dim]")
722723
if failed:
@@ -726,8 +727,13 @@ def extension_add(
726727
"Verify the extension manifest and template files."
727728
)
728729

729-
console.print("\n[yellow]⚠[/yellow] Configuration may be required")
730-
console.print(f" Check: .specify/extensions/{_escape_markup(str(manifest.id))}/")
730+
# Only warn when configuration is actually unresolved. Scaffolding that
731+
# deployed or preserved every template has already answered this, and an
732+
# extension without provides.config has nothing to configure; the blanket
733+
# warning contradicted the output directly above it.
734+
if failed or not (deployed or skipped):
735+
console.print("\n[yellow]⚠[/yellow] Configuration may be required")
736+
console.print(f" Check: {config_home}/")
731737

732738
except ValidationError as e:
733739
console.print(f"\n[red]Validation Error:[/red] {_escape_markup(str(e))}")

‎tests/test_extensions.py‎

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10328,7 +10328,7 @@ def _make_extension(self, ext_dir, config_entries=None):
1032810328
return manifest
1032910329

1033010330
def test_scaffold_config_deploys_template(self, tmp_path):
10331-
"""Config template should be copied to .specify/ on scaffold."""
10331+
"""Config template lands where ConfigManager reads it, not in .specify/ root."""
1033210332
from specify_cli.extensions import ExtensionManager
1033310333
project = tmp_path / "project"
1033410334
specify_dir = project / ".specify"
@@ -10348,16 +10348,20 @@ def test_scaffold_config_deploys_template(self, tmp_path):
1034810348
assert deployed == ["test-config.yml"]
1034910349
assert skipped == []
1035010350
assert failed == []
10351-
assert (specify_dir / "test-config.yml").exists()
10352-
assert (specify_dir / "test-config.yml").read_text() == "setting: default"
10351+
# ConfigManager._get_project_config() reads
10352+
# .specify/extensions/<id>/<name>, so that is where scaffolding must
10353+
# put it. Deploying to the .specify/ root left the file somewhere the
10354+
# extension never looks.
10355+
assert (ext_dir / "test-config.yml").exists()
10356+
assert (ext_dir / "test-config.yml").read_text() == "setting: default"
10357+
assert not (specify_dir / "test-config.yml").exists()
1035310358

1035410359
def test_scaffold_config_preserves_existing(self, tmp_path):
1035510360
"""Existing config files should never be overwritten."""
1035610361
from specify_cli.extensions import ExtensionManager
1035710362
project = tmp_path / "project"
1035810363
specify_dir = project / ".specify"
1035910364
specify_dir.mkdir(parents=True)
10360-
(specify_dir / "test-config.yml").write_text("setting: custom")
1036110365
ext_dir = specify_dir / "extensions" / "test-ext"
1036210366
self._make_extension(ext_dir, config_entries=[{
1036310367
"name": "test-config.yml",
@@ -10366,14 +10370,15 @@ def test_scaffold_config_preserves_existing(self, tmp_path):
1036610370
"required": True,
1036710371
}])
1036810372
(ext_dir / "config-template.yml").write_text("setting: default")
10373+
(ext_dir / "test-config.yml").write_text("setting: custom")
1036910374

1037010375
manager = ExtensionManager(project)
1037110376
deployed, skipped, failed = manager.scaffold_config("test-ext")
1037210377

1037310378
assert deployed == []
1037410379
assert skipped == ["test-config.yml"]
1037510380
assert failed == []
10376-
assert (specify_dir / "test-config.yml").read_text() == "setting: custom"
10381+
assert (ext_dir / "test-config.yml").read_text() == "setting: custom"
1037710382

1037810383
def test_scaffold_config_no_config_section(self, tmp_path):
1037910384
"""Extensions without config section should return empty list."""
@@ -10506,3 +10511,61 @@ def test_scaffold_config_missing_manifest_returns_consistent_result(self, tmp_pa
1050610511
manager = ExtensionManager(project)
1050710512

1050810513
assert manager.scaffold_config("missing") == ([], [], [])
10514+
10515+
def test_scaffold_config_rejects_symlinked_config_root(self, tmp_path):
10516+
"""A symlinked .specify must not become the containment root.
10517+
10518+
Resolving .specify first and trusting the result lets a symlink point
10519+
anywhere: every target then satisfies relative_to and copy2 writes
10520+
outside the project.
10521+
"""
10522+
from specify_cli.extensions import ExtensionManager
10523+
project = tmp_path / "project"
10524+
project.mkdir()
10525+
outside = tmp_path / "outside"
10526+
outside.mkdir()
10527+
(project / ".specify").symlink_to(outside, target_is_directory=True)
10528+
10529+
ext_dir = outside / "extensions" / "test-ext"
10530+
self._make_extension(ext_dir, config_entries=[{
10531+
"name": "test-config.yml",
10532+
"template": "config-template.yml",
10533+
"description": "Test config",
10534+
"required": True,
10535+
}])
10536+
(ext_dir / "config-template.yml").write_text("setting: default")
10537+
10538+
manager = ExtensionManager(project)
10539+
deployed, skipped, failed = manager.scaffold_config("test-ext")
10540+
10541+
assert deployed == []
10542+
assert skipped == []
10543+
assert failed == ["provides.config"]
10544+
assert not (outside / "extensions" / "test-ext" / "test-config.yml").exists()
10545+
10546+
def test_scaffold_config_reports_unwritable_nested_target(self, tmp_path):
10547+
"""A nested target whose parent cannot be created is a failure, not a crash.
10548+
10549+
mkdir used to run outside the OSError handler, so this raised out of
10550+
scaffolding after `extension add` had already installed the extension.
10551+
"""
10552+
from specify_cli.extensions import ExtensionManager
10553+
project = tmp_path / "project"
10554+
specify_dir = project / ".specify"
10555+
specify_dir.mkdir(parents=True)
10556+
ext_dir = specify_dir / "extensions" / "test-ext"
10557+
self._make_extension(ext_dir, config_entries=[{
10558+
"name": "nested/test-config.yml",
10559+
"template": "config-template.yml",
10560+
"description": "Test config",
10561+
"required": True,
10562+
}])
10563+
(ext_dir / "config-template.yml").write_text("setting: default")
10564+
# `nested` is a file, so creating it as a directory fails.
10565+
(ext_dir / "nested").write_text("not a directory")
10566+
10567+
manager = ExtensionManager(project)
10568+
deployed, skipped, failed = manager.scaffold_config("test-ext")
10569+
10570+
assert deployed == []
10571+
assert failed == ["nested/test-config.yml"]

0 commit comments

Comments
 (0)