From bde080f4c6d546fea1313bff76960c99045b2ae7 Mon Sep 17 00:00:00 2001 From: Jay Pokale Date: Thu, 26 Nov 2026 00:34:31 +0530 Subject: [PATCH] fix(ui-styling): find installed shadcn components under src/ and custom aliases (#602) get_installed_components() only stripped a literal "@/" from aliases.components and ignored aliases.ui, so projects with a src/ directory, a "~/" alias, or a custom ui alias reported no installed components and the already-installed guard in add_components() never triggered. --- .../skills/ui-styling/scripts/shadcn_add.py | 24 +++++++++++++++---- .../scripts/tests/test_shadcn_add.py | 19 +++++++++++++++ .../skills/ui-styling/scripts/shadcn_add.py | 24 +++++++++++++++---- .../scripts/tests/test_shadcn_add.py | 19 +++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/.claude/skills/ui-styling/scripts/shadcn_add.py b/.claude/skills/ui-styling/scripts/shadcn_add.py index 983d500..4b7c82f 100644 --- a/.claude/skills/ui-styling/scripts/shadcn_add.py +++ b/.claude/skills/ui-styling/scripts/shadcn_add.py @@ -52,18 +52,32 @@ class ShadcnInstaller: with open(self.components_json, encoding="utf-8") as f: config = json.load(f) - components_dir = self.project_root / config.get("aliases", {}).get( - "components", "components" - ).replace("@/", "") - ui_dir = components_dir / "ui" + aliases = config.get("aliases", {}) + ui_alias = aliases.get("ui") or ( + aliases.get("components", "components").rstrip("/") + "/ui" + ) + ui_dir = self._resolve_alias_dir(ui_alias) - if not ui_dir.exists(): + if ui_dir is None: return [] return [f.stem for f in ui_dir.glob("*.tsx") if f.is_file()] except (json.JSONDecodeError, KeyError, OSError): return [] + def _resolve_alias_dir(self, alias: str) -> Optional[Path]: + """Map a components.json import alias (e.g. "@/components/ui") to a directory. + + The "@/" and "~/" prefixes point at the project root, or at src/ when + the project uses a src directory (the shadcn default for Next.js). + """ + relative = alias[2:] if alias[:2] in ("@/", "~/") else alias + for base in (self.project_root, self.project_root / "src"): + candidate = base / relative + if candidate.is_dir(): + return candidate + return None + def _get_shadcn_version(self) -> str: """Read shadcn version from project package.json; fall back to a pinned default.""" pkg_json = self.project_root / "package.json" diff --git a/.claude/skills/ui-styling/scripts/tests/test_shadcn_add.py b/.claude/skills/ui-styling/scripts/tests/test_shadcn_add.py index 806b6a4..dbbbce6 100644 --- a/.claude/skills/ui-styling/scripts/tests/test_shadcn_add.py +++ b/.claude/skills/ui-styling/scripts/tests/test_shadcn_add.py @@ -86,6 +86,25 @@ class TestShadcnInstaller: assert sorted(installed) == ["button", "card"] + @pytest.mark.parametrize( + "aliases, ui_path", + [ + ({"components": "@/components", "ui": "@/components/ui"}, "src/components/ui"), + ({"components": "~/components"}, "components/ui"), + ({"components": "@/components", "ui": "@/components/shadcn"}, "components/shadcn"), + ], + ) + def test_get_installed_components_resolves_aliases(self, tmp_path, aliases, ui_path): + """Installed components are found under src/, ~/ and custom ui aliases.""" + (tmp_path / "components.json").write_text(json.dumps({"aliases": aliases})) + ui_dir = tmp_path / ui_path + ui_dir.mkdir(parents=True) + (ui_dir / "button.tsx").write_text("export const Button = () => {}") + + installer = ShadcnInstaller(project_root=tmp_path) + + assert installer.get_installed_components() == ["button"] + def test_get_installed_components_no_config(self, tmp_path): """Test getting installed components without config.""" installer = ShadcnInstaller(project_root=tmp_path) diff --git a/cli/assets/skills/ui-styling/scripts/shadcn_add.py b/cli/assets/skills/ui-styling/scripts/shadcn_add.py index 983d500..4b7c82f 100644 --- a/cli/assets/skills/ui-styling/scripts/shadcn_add.py +++ b/cli/assets/skills/ui-styling/scripts/shadcn_add.py @@ -52,18 +52,32 @@ class ShadcnInstaller: with open(self.components_json, encoding="utf-8") as f: config = json.load(f) - components_dir = self.project_root / config.get("aliases", {}).get( - "components", "components" - ).replace("@/", "") - ui_dir = components_dir / "ui" + aliases = config.get("aliases", {}) + ui_alias = aliases.get("ui") or ( + aliases.get("components", "components").rstrip("/") + "/ui" + ) + ui_dir = self._resolve_alias_dir(ui_alias) - if not ui_dir.exists(): + if ui_dir is None: return [] return [f.stem for f in ui_dir.glob("*.tsx") if f.is_file()] except (json.JSONDecodeError, KeyError, OSError): return [] + def _resolve_alias_dir(self, alias: str) -> Optional[Path]: + """Map a components.json import alias (e.g. "@/components/ui") to a directory. + + The "@/" and "~/" prefixes point at the project root, or at src/ when + the project uses a src directory (the shadcn default for Next.js). + """ + relative = alias[2:] if alias[:2] in ("@/", "~/") else alias + for base in (self.project_root, self.project_root / "src"): + candidate = base / relative + if candidate.is_dir(): + return candidate + return None + def _get_shadcn_version(self) -> str: """Read shadcn version from project package.json; fall back to a pinned default.""" pkg_json = self.project_root / "package.json" diff --git a/cli/assets/skills/ui-styling/scripts/tests/test_shadcn_add.py b/cli/assets/skills/ui-styling/scripts/tests/test_shadcn_add.py index 806b6a4..dbbbce6 100644 --- a/cli/assets/skills/ui-styling/scripts/tests/test_shadcn_add.py +++ b/cli/assets/skills/ui-styling/scripts/tests/test_shadcn_add.py @@ -86,6 +86,25 @@ class TestShadcnInstaller: assert sorted(installed) == ["button", "card"] + @pytest.mark.parametrize( + "aliases, ui_path", + [ + ({"components": "@/components", "ui": "@/components/ui"}, "src/components/ui"), + ({"components": "~/components"}, "components/ui"), + ({"components": "@/components", "ui": "@/components/shadcn"}, "components/shadcn"), + ], + ) + def test_get_installed_components_resolves_aliases(self, tmp_path, aliases, ui_path): + """Installed components are found under src/, ~/ and custom ui aliases.""" + (tmp_path / "components.json").write_text(json.dumps({"aliases": aliases})) + ui_dir = tmp_path / ui_path + ui_dir.mkdir(parents=True) + (ui_dir / "button.tsx").write_text("export const Button = () => {}") + + installer = ShadcnInstaller(project_root=tmp_path) + + assert installer.get_installed_components() == ["button"] + def test_get_installed_components_no_config(self, tmp_path): """Test getting installed components without config.""" installer = ShadcnInstaller(project_root=tmp_path)