Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

refactor(php): one composer reader and framework-facts registry, with PHP references the graph used to miss (#2505)

Raghav Chamadiyaced7e05a1d97
diff --git a/docs/layers/LANGUAGE_SUPPORT.md b/docs/layers/LANGUAGE_SUPPORT.md
index bec4d821b..2582bf17d 100644
--- a/docs/layers/LANGUAGE_SUPPORT.md
+++ b/docs/layers/LANGUAGE_SUPPORT.md
@@ -254,7 +254,7 @@ bindings, heritage and a workspace resolver where their syntax supports them.
|----------|-----------|--------------|
| **C** | `.c` | `#include` via `compile_commands.json` (shares the C++ grammar) |
| **Swift** | `.swift` | SPM `Package.swift` target → directory mapping, intra-module type references, `@main` entry points |
-| **PHP** | `.php` | `use Foo\Bar\Baz` with composer.json PSR-4 longest-prefix resolution; Laravel, TYPO3 edges |
+| **PHP** | `.php` | `use` declarations (grouped `use A\{B, C}` included) resolved through PSR-4 from the root and nested `composer.json` files, longest prefix first as composer does; same-namespace and `\Fully\Qualified` class references; Laravel, TYPO3 edges |
| **Dart** | `.dart` | `import` / `export` / `part` URIs, `package:` via every `pubspec.yaml`, Flutter route tables and `runApp()` edges. **Health markers included** |
| **COBOL** | `.cbl` `.cob` `.cobol` `.cpy` | Program IDs, sections, paragraphs and data levels; literal `CALL` and `PERFORM` targets resolve to program/procedure symbols. Dynamic calls and `COPY` edges are deliberately silent |
diff --git a/docs/scale/WORKSPACES.md b/docs/scale/WORKSPACES.md
index ef8c3c710..dffeb31e0 100644
--- a/docs/scale/WORKSPACES.md
+++ b/docs/scale/WORKSPACES.md
@@ -302,9 +302,19 @@ excluded from matching and reported under the `external_host` diagnostics reason
### Package Dependency Scanning
-Reads package manifests (`package.json`, `pyproject.toml`, `Cargo.toml`, `go.mod`,
-`.csproj`, and Maven `pom.xml`) to detect when one repo depends on another as a
-package or project.
+Reads package manifests (`package.json`, `composer.json`, `pyproject.toml`,
+`Cargo.toml`, `go.mod`, `.csproj`, and Maven `pom.xml`) to detect when one repo
+depends on another as a package or project.
+
+npm and composer dependencies match in two ways: a local path (`file:` specs,
+workspace globs, composer `path` repositories) that resolves into a sibling repo,
+or a package name that exactly one sibling repo publishes. For npm, a repo
+publishes its root `package.json` and its declared workspace members, so a
+vendored or fixture `package.json` never claims a name. For composer, every
+`composer.json` up to three directories deep counts, outside `vendor/`, hidden
+directories, tests, fixtures and examples, which covers split packages such as
+`src/Illuminate/Support/composer.json`. A name two repos publish links to
+neither.
Maven matching is filesystem-only and coordinate-based. Repowise resolves local
reactor modules, local parents, properties, and dependency-management versions,
@@ -327,7 +337,7 @@ unsupported.
The contracts, package dependencies, and co-changes above are each a flat list. repowise folds them into a single normalized **system graph**, the one structure every cross-repo view reads. It is rebuilt automatically on every `repowise update --workspace` and persisted to `.repowise-workspace/system_graph.json`.
-**Nodes are services, not repos.** A monorepo with three detected service boundaries (a `package.json` / `go.mod` / `Cargo.toml` sub-directory) shows three nodes; the repo is a grouping attribute on each node. A repo with no sub-boundary collapses to a single repo-root node. Each node carries its provider/consumer counts, the contract types it participates in, and flags for orphan/isolated services.
+**Nodes are services, not repos.** A monorepo with three detected service boundaries (a `package.json` / `composer.json` / `go.mod` / `Cargo.toml` sub-directory) shows three nodes; the repo is a grouping attribute on each node. A repo with no sub-boundary collapses to a single repo-root node. Each node carries its provider/consumer counts, the contract types it participates in, and flags for orphan/isolated services.
**Edges are typed and honest.** Every edge carries:
diff --git a/packages/core/src/repowise/core/generation/editor_files/tech_stack.py b/packages/core/src/repowise/core/generation/editor_files/tech_stack.py
index 0d1491876..8d7af431d 100644
--- a/packages/core/src/repowise/core/generation/editor_files/tech_stack.py
+++ b/packages/core/src/repowise/core/generation/editor_files/tech_stack.py
@@ -9,6 +9,8 @@ import json
import re
from pathlib import Path
+from ...ingestion.composer import COMPOSER_JSON, read_composer
+from ...ingestion.framework_facts import detect_php_framework
from ...precedent.structural import declares_ruff_format
from .data import TechStackItem
@@ -315,27 +317,13 @@ def _detect_tech_stack_uncached(repo_path: Path) -> list[TechStackItem]:
add("Ruby", None, "language")
# --- composer.json (PHP) ---
- composer_json = repo_path / "composer.json"
+ composer_json = repo_path / COMPOSER_JSON
if composer_json.exists():
add("PHP", None, "language")
- try:
- composer = json.loads(composer_json.read_text(encoding="utf-8"))
- except Exception:
- composer = None
- if isinstance(composer, dict):
- requires = {
- **(composer.get("require") or {}),
- **(composer.get("require-dev") or {}),
- }
- if (
- composer.get("type") == "typo3-cms-extension"
- or "typo3/cms-core" in requires
- ):
- add("TYPO3", None, "framework")
- elif "symfony/framework-bundle" in requires or "symfony/symfony" in requires:
- add("Symfony", None, "framework")
- elif "laravel/framework" in requires:
- add("Laravel", None, "framework")
+ composer = read_composer(composer_json)
+ framework = detect_php_framework(composer) if composer is not None else None
+ if framework is not None:
+ add(framework.name, None, "framework")
# --- .NET / C# (.csproj / .sln / Directory.Build.props) ---
# Walk the tree (bounded) so monorepos whose projects live under
diff --git a/packages/core/src/repowise/core/ingestion/cohesion.py b/packages/core/src/repowise/core/ingestion/cohesion.py
index bbaa75850..31b2be44f 100644
--- a/packages/core/src/repowise/core/ingestion/cohesion.py
+++ b/packages/core/src/repowise/core/ingestion/cohesion.py
@@ -29,6 +29,10 @@ from typing import Any
#: Hint stamped on an edge between two files of one package / build target.
SAME_PACKAGE_HINT = "same_package"
+#: Hint stamped on an edge to a type declared in the file's own namespace
+#: (C#, VB.NET, PHP), where the language needs no import to name it.
+SAME_NAMESPACE_HINT = "same_namespace"
+
#: Languages whose import statement names a *compilation unit* that is exactly a
#: directory, so a fan-out landing in the importer's own directory landed on its
#: siblings — and a unit cannot depend on itself.
@@ -62,7 +66,7 @@ UNIT_FANOUT_LANGUAGES: frozenset[str] = frozenset({"go", "java"})
COHESION_HINTS: frozenset[str] = frozenset(
{
SAME_PACKAGE_HINT, # JVM siblings; Go/JVM/C++ unit fan-out onto siblings
- "same_namespace", # C# same-namespace types
+ SAME_NAMESPACE_HINT, # C# / VB.NET / PHP same-namespace types
"global_using", # C# project-wide global usings
"same_module", # Swift SPM target siblings
"partial_class", # C# fragments of one partial type
diff --git a/packages/core/src/repowise/core/ingestion/composer.py b/packages/core/src/repowise/core/ingestion/composer.py
new file mode 100644
index 000000000..bbd2012ab
--- /dev/null
+++ b/packages/core/src/repowise/core/ingestion/composer.py
@@ -0,0 +1,243 @@
+"""The one ``composer.json`` reader.
+
+Import resolution (PSR-4), TYPO3 extension discovery, PHP framework detection,
+workspace service boundaries and cross-repo package edges all read the same few
+fields of a ``composer.json`` through :class:`ComposerManifest`, and
+:func:`find_composer_manifests` is the one answer to "which manifests does this
+repo hold".
+"""
+
+from __future__ import annotations
+
+import json
+import posixpath
+from collections.abc import Mapping
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import TYPE_CHECKING, Any
+
+from repowise.core.fs_walk import PRUNED_DIRS, walk_repo
+from repowise.core.test_paths import is_test_related_path
+
+if TYPE_CHECKING:
+ from .resolvers.context import ResolverContext
+
+COMPOSER_JSON = "composer.json"
+
+#: How deep below the repo root a nested manifest is looked for. Monorepos keep
+#: packages at ``packages/<name>/`` or ``src/<Vendor>/<Package>/``; anything
+#: deeper is almost always a fixture or a vendored copy.
+_MAX_NESTED_DEPTH = 3
+
+#: Never a first-party package root. ``vendor`` is read separately (installed
+#: packages sit exactly two levels below it), ``var`` and ``Build`` are TYPO3
+#: cache and tooling trees.
+_SKIP_DIRS = PRUNED_DIRS | {"vendor", "var", "Build"}
+
+#: Demo trees whose manifests describe something the repo does not ship. Test
+#: and fixture trees are recognised by the shared test-path classifier.
+_EXAMPLE_DIRS = frozenset({"example", "examples", "sample", "samples"})
+
+
+def is_package_dir(rel_dir: str) -> bool:
+ """Whether a ``composer.json`` in *rel_dir* (repo-relative posix) is first-party.
+
+ Exposed so a caller already walking the repo (the workspace manifest
+ index) applies the same rule without a second walk.
+ """
+ parts = rel_dir.split("/") if rel_dir else []
+ return (
+ len(parts) <= _MAX_NESTED_DEPTH
+ and not any(
+ p.startswith(".") or p in _SKIP_DIRS or p.lower() in _EXAMPLE_DIRS for p in parts
+ )
+ and not is_test_related_path(posixpath.join(rel_dir, COMPOSER_JSON))
+ )
+
+
+@dataclass(frozen=True, slots=True)
+class ComposerManifest:
+ """The fields of one ``composer.json`` that repowise reads.
+
+ Autoload directories are repo-relative posix paths ('' for the repo root),
+ already joined onto the manifest's own directory, so a nested package's
+ ``"src/"`` reads as ``packages/billing/src``.
+ """
+
+ rel_dir: str # directory holding the manifest, '' for the repo root
+ name: str = ""
+ type: str = ""
+ require: Mapping[str, str] = field(default_factory=dict)
+ require_dev: Mapping[str, str] = field(default_factory=dict)
+ #: ``(namespace prefix, dirs)`` pairs, ``autoload`` before ``autoload-dev``.
+ #: Prefixes keep composer's trailing ``\\``.
+ psr4: tuple[tuple[str, tuple[str, ...]], ...] = ()
+ psr0: tuple[tuple[str, tuple[str, ...]], ...] = ()
+ classmap: tuple[str, ...] = ()
+ #: ``url`` of every ``{"type": "path"}`` repository, relative to ``rel_dir``
+ #: exactly as written (it is a filesystem path, often ``../shared``).
+ path_repositories: tuple[str, ...] = ()
+ extra: Mapping[str, Any] = field(default_factory=dict)
+
+ @property
+ def requires(self) -> dict[str, str]:
+ """``require`` and ``require-dev`` together, runtime entries winning."""
+ return {**self.require_dev, **self.require}
+
+
+def _dirs(value: object) -> list[str]:
+ """An autoload value may be one path or a list of paths."""
+ if isinstance(value, str):
+ return [value]
+ if isinstance(value, list):
+ return [v for v in value if isinstance(v, str)]
+ return []
+
+
+def _rebase(rel_dir: str, path: str) -> str:
+ joined = posixpath.normpath(posixpath.join(rel_dir, path.strip().rstrip("/")))
+ return "" if joined == "." else joined
+
+
+def _prefix_map(
+ data: Mapping[str, Any], standard: str, rel_dir: str
+) -> tuple[tuple[str, tuple[str, ...]], ...]:
+ merged: dict[str, list[str]] = {}
+ for section in ("autoload", "autoload-dev"):
+ block = data.get(section)
+ mapping = block.get(standard) if isinstance(block, dict) else None
+ if not isinstance(mapping, dict):
+ continue
+ for prefix, value in mapping.items():
+ if isinstance(prefix, str):
+ merged.setdefault(prefix, []).extend(_rebase(rel_dir, d) for d in _dirs(value))
+ return tuple((prefix, tuple(dirs)) for prefix, dirs in merged.items())
+
+
+def _str_map(value: object) -> dict[str, str]:
+ if not isinstance(value, dict):
+ return {}
+ return {k: v for k, v in value.items() if isinstance(k, str) and isinstance(v, str)}
+
+
+def parse_composer(data: object, rel_dir: str = "") -> ComposerManifest | None:
+ """Build a manifest from already-decoded JSON, or None when it is not an object."""
+ if not isinstance(data, dict):
+ return None
+ classmap: list[str] = []
+ for section in ("autoload", "autoload-dev"):
+ block = data.get(section)
+ if isinstance(block, dict):
+ classmap.extend(_rebase(rel_dir, d) for d in _dirs(block.get("classmap")))
+ repositories = data.get("repositories")
+ if isinstance(repositories, dict): # the keyed form composer also accepts
+ repositories = list(repositories.values())
+ path_repos = tuple(
+ repo["url"]
+ for repo in (repositories if isinstance(repositories, list) else [])
+ if isinstance(repo, dict) and repo.get("type") == "path" and isinstance(repo.get("url"), str)
+ )
+ name, kind, extra = data.get("name"), data.get("type"), data.get("extra")
+ return ComposerManifest(
+ rel_dir=rel_dir,
+ name=name if isinstance(name, str) else "",
+ type=kind if isinstance(kind, str) else "",
+ require=_str_map(data.get("require")),
+ require_dev=_str_map(data.get("require-dev")),
+ psr4=_prefix_map(data, "psr-4", rel_dir),
+ psr0=_prefix_map(data, "psr-0", rel_dir),
+ classmap=tuple(classmap),
+ path_repositories=path_repos,
+ extra=extra if isinstance(extra, dict) else {},
+ )
+
+
+def read_composer(path: Path, rel_dir: str = "") -> ComposerManifest | None:
+ """Read one ``composer.json``; None when it is missing or not valid JSON."""
+ try:
+ data = json.loads(path.read_text(encoding="utf-8", errors="ignore"))
+ except (OSError, ValueError):
+ return None
+ return parse_composer(data, rel_dir)
+
+
+def _vendor_manifest_paths(vendor_root: Path) -> list[Path]:
+ """``vendor/<vendor>/<package>/composer.json``: composer's layout is flat."""
+ found: list[Path] = []
+ try:
+ vendors = sorted(vendor_root.iterdir())
+ except OSError:
+ return found
+ for vendor_dir in vendors:
+ if not vendor_dir.is_dir() or vendor_dir.name.startswith("."):
+ continue
+ try:
+ packages = sorted(vendor_dir.iterdir())
+ except OSError:
+ continue
+ for pkg_dir in packages:
+ candidate = pkg_dir / COMPOSER_JSON
+ if not pkg_dir.name.startswith(".") and candidate.is_file():
+ found.append(candidate)
+ return found
+
+
+def find_composer_manifests(
+ repo_root: Path, *, prune_nested_git: bool = True
+) -> list[ComposerManifest]:
+ """Every first-party ``composer.json`` in *repo_root*, root first.
+
+ Nested manifests are found up to three directories deep, skipping junk,
+ hidden directories, ``vendor``, test, fixture and example trees, and (by
+ default) nested git repos; :func:`is_package_dir` is the rule.
+ """
+ root = Path(repo_root)
+ manifests: list[ComposerManifest] = []
+ for dirpath, dirnames, filenames in walk_repo(
+ root, prune_dirs=_SKIP_DIRS, prune_nested_git=prune_nested_git
+ ):
+ rel = dirpath.relative_to(root).as_posix()
+ rel = "" if rel == "." else rel
+ dirnames[:] = sorted(
+ d for d in dirnames if is_package_dir(f"{rel}/{d}" if rel else d)
+ )
+ if COMPOSER_JSON in filenames and is_package_dir(rel):
+ manifest = read_composer(dirpath / COMPOSER_JSON, rel)
+ if manifest is not None:
+ manifests.append(manifest)
+ return manifests
+
+
+def find_vendor_manifests(repo_root: Path) -> list[ComposerManifest]:
+ """Installed packages under ``vendor/``.
+
+ Only a TYPO3 project reads these: it keeps its extensions there.
+ """
+ root = Path(repo_root)
+ manifests = (
+ read_composer(path, path.parent.relative_to(root).as_posix())
+ for path in _vendor_manifest_paths(root / "vendor")
+ )
+ return [m for m in manifests if m is not None]
+
+
+def repo_composer_manifests(ctx: ResolverContext) -> list[ComposerManifest]:
+ """:func:`find_composer_manifests` for a resolver context, cached on it.
+
+ Import resolution and the PHP framework handlers all ask within one build.
+ A repo with no PHP file and no root manifest is never walked.
+ """
+ cached = getattr(ctx, "_composer_manifests", None)
+ if cached is None:
+ repo_path = ctx.repo_path
+ has_php = repo_path is not None and (
+ (repo_path / COMPOSER_JSON).is_file()
+ or any(p.endswith(".php") for p in ctx.path_set)
+ )
+ cached = (
+ find_composer_manifests(repo_path, prune_nested_git=ctx.prune_nested_git)
+ if has_php and repo_path is not None
+ else []
+ )
+ ctx._composer_manifests = cached # type: ignore[attr-defined]
+ return cached
diff --git a/packages/core/src/repowise/core/ingestion/extractors/bindings/php.py b/packages/core/src/repowise/core/ingestion/extractors/bindings/php.py
index 75a420cad..4731038e1 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/bindings/php.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/bindings/php.py
@@ -7,35 +7,63 @@ from tree_sitter import Node
from ...models import NamedBinding
from ..helpers import node_text
+_NAME_NODES = frozenset({"name", "qualified_name"})
+
+#: ``use function`` / ``use const`` import a function or constant, never a
+#: class file, so they bind nothing the resolver could follow.
+_NON_CLASS_KINDS = frozenset({"function", "const"})
+
+
+def _clause(clause: Node, prefix: str, src: str) -> tuple[str, str] | None:
+ """``(fully qualified name, local name)`` for one ``namespace_use_clause``."""
+ target = alias = ""
+ saw_as = False
+ for sub in clause.children:
+ if sub.type in _NON_CLASS_KINDS:
+ return None
+ if sub.type == "as":
+ saw_as = True
+ elif sub.type in _NAME_NODES:
+ if saw_as:
+ alias = node_text(sub, src)
+ elif not target:
+ target = node_text(sub, src).lstrip("\\")
+ if not target:
+ return None
+ fqn = f"{prefix}\\{target}" if prefix else target
+ return fqn, alias or fqn.rsplit("\\", 1)[-1]
-def extract_php_bindings(stmt_node: Node, src: str) -> tuple[list[str], list[NamedBinding]]:
- """Extract bindings from PHP use declarations."""
- names: list[str] = []
- bindings: list[NamedBinding] = []
+def php_use_clauses(stmt_node: Node, src: str) -> list[tuple[str, str]]:
+ """Every ``(fully qualified name, local name)`` one ``use`` declaration binds.
+
+ Covers ``use A\\B;``, ``use A\\B as C, D\\E;`` and the grouped
+ ``use A\\{B, C\\D as E};``, whose clauses carry only the part after the
+ shared prefix. Function and constant imports are skipped.
+ """
+ prefix = ""
+ out: list[tuple[str, str]] = []
for child in stmt_node.children:
- if child.type == "namespace_use_clause":
- qualified = ""
- alias = None
- saw_as = False
- for sub in child.children:
- if sub.type == "qualified_name":
- qualified = node_text(sub, src)
- elif sub.type == "as":
- saw_as = True
- elif sub.type == "name" and saw_as:
- alias = node_text(sub, src)
-
- if not qualified:
- continue
-
- local = qualified.rsplit("\\", 1)[-1] if "\\" in qualified else qualified
- effective_local = alias if alias else local
- names.append(effective_local)
- bindings.append(
- NamedBinding(
- local_name=effective_local, exported_name=qualified, source_file=None
- )
- )
-
- return names, bindings
+ if child.type in _NON_CLASS_KINDS:
+ return []
+ if child.type == "namespace_name":
+ prefix = node_text(child, src).lstrip("\\")
+ elif child.type == "namespace_use_clause":
+ if (bound := _clause(child, "", src)) is not None:
+ out.append(bound)
+ elif child.type == "namespace_use_group":
+ for clause in child.children:
+ if clause.type == "namespace_use_clause" and (
+ bound := _clause(clause, prefix, src)
+ ) is not None:
+ out.append(bound)
+ return out
+
+
+def extract_php_bindings(stmt_node: Node, src: str) -> tuple[list[str], list[NamedBinding]]:
+ """Extract bindings from PHP use declarations."""
+ clauses = php_use_clauses(stmt_node, src)
+ return [local for _, local in clauses], [
+ NamedBinding(local_name=local, exported_name=fqn, source_file=None)
+ for fqn, local in clauses
+ ]
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/base.py b/packages/core/src/repowise/core/ingestion/framework_edges/base.py
index 3cb03a4a1..64a092e69 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/base.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/base.py
@@ -16,6 +16,7 @@ from typing import TYPE_CHECKING, Any, Protocol
if TYPE_CHECKING:
import networkx as nx
+ from ..framework_facts import FrameworkFacts
from ..resolvers import ResolverContext
@@ -94,6 +95,21 @@ def add_symbol_edge(graph: nx.DiGraph, source: str, target: str) -> bool:
return True
+def add_entry_edges(graph: nx.DiGraph, facts: FrameworkFacts, root: str, path_set: set[str]) -> int:
+ """Anchor every file *facts* says its runtime loads under *root*.
+
+ The ``framework:`` anchor is what dead-code liveness reads: a file it
+ points at is reached from outside the import graph.
+ """
+ count = 0
+ for target in facts.entry_files(root, path_set):
+ if facts.anchor not in graph:
+ graph.add_node(facts.anchor, language="external")
+ if _add_edge_if_new(graph, facts.anchor, target):
+ count += 1
+ return count
+
+
def read_text(parsed: Any, encoding: str = "utf-8") -> str:
"""Read a parsed file's source text, returning ``""`` on read failure.
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/laravel.py b/packages/core/src/repowise/core/ingestion/framework_edges/laravel.py
index 8f2fe0d40..29b2d41d0 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/laravel.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/laravel.py
@@ -1,5 +1,9 @@
"""Laravel routes / service-provider / Eloquent convention edges.
+The files Laravel loads with no importer (providers, route files, commands,
+factories, ...) are named in ``framework_facts.LARAVEL`` and anchored to
+``framework:laravel`` here.
+
Split out of ``framework_edges.py`` (PR 3.5) — behaviour-preserving move. The
``Route::`` recogniser lives in ``ingestion.framework_routes``, shared with the
contract extractor that reads the same call for its path.
@@ -10,6 +14,8 @@ from __future__ import annotations
import re
from typing import TYPE_CHECKING, Any
+from ..composer import repo_composer_manifests
+from ..framework_facts import LARAVEL
from ..framework_routes import laravel_routes
from ..resolvers import ResolverContext
from .base import (
@@ -17,6 +23,7 @@ from .base import (
FrameworkHandler,
_add_edge_if_new,
_build_class_to_file,
+ add_entry_edges,
read_text,
)
@@ -46,6 +53,11 @@ def _resolve_laravel_class(
return class_to_file.get(short)
+def _manifest_roots(ctx: ResolverContext) -> list[str]:
+ """Directories of every first-party manifest that requires the framework."""
+ return [m.rel_dir for m in repo_composer_manifests(ctx) if LARAVEL.matches(m)]
+
+
def _add_laravel_edges(
graph: nx.DiGraph,
parsed_files: dict[str, Any],
@@ -54,9 +66,20 @@ def _add_laravel_edges(
) -> int:
count = 0
class_to_file = _build_class_to_file(parsed_files, ("php",))
+ roots = _manifest_roots(ctx)
+
+ # ---- files the framework loads by convention (providers, routes, ...) ----
+ # Only where a manifest requires the framework: ``config/*.php`` or
+ # ``app/Providers/*.php`` alone say nothing about Laravel.
+ for root in roots:
+ count += add_entry_edges(graph, LARAVEL, root, path_set)
# ---- routes/web.php / routes/api.php → controllers ----
- for routes_path in ("routes/web.php", "routes/api.php"):
+ for routes_path in (
+ f"{root}/{name}" if root else name
+ for root in dict.fromkeys(["", *roots])
+ for name in ("routes/web.php", "routes/api.php")
+ ):
if routes_path not in path_set:
continue
text = read_text(parsed_files[routes_path])
@@ -108,6 +131,7 @@ class _LaravelHandler:
"laravel" in dctx.stack_lower
or "routes/web.php" in dctx.path_set
or "routes/api.php" in dctx.path_set
+ or bool(_manifest_roots(dctx.ctx))
)
def add_edges(
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/typo3.py b/packages/core/src/repowise/core/ingestion/framework_edges/typo3.py
index aa3e78844..b52620e72 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/typo3.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/typo3.py
@@ -5,17 +5,18 @@ Split out of ``framework_edges.py`` (PR 3.5) — behaviour-preserving move.
from __future__ import annotations
-import fnmatch
-import json
+import posixpath
import re
-from pathlib import Path
from typing import TYPE_CHECKING, Any
+from ..composer import ComposerManifest, find_vendor_manifests, repo_composer_manifests
+from ..framework_facts import TYPO3
from ..resolvers import ResolverContext
from .base import (
DetectionContext,
FrameworkHandler,
_add_edge_if_new,
+ add_entry_edges,
read_text,
)
@@ -32,42 +33,15 @@ if TYPE_CHECKING:
# ``in_degree=0`` and the dead-code analyzer flags them as unreachable.
#
# We attach a ``framework:typo3-core`` synthetic source to each convention
-# file present in an extension. ``Configuration/JavaScriptModules.php`` is
-# also parsed to add edges to the JS modules it registers (CKEditor plugins,
-# backend modules, etc.).
+# file present in an extension (the list is ``framework_facts.TYPO3``).
+# ``Configuration/JavaScriptModules.php`` is also parsed to add edges to the
+# JS modules it registers (CKEditor plugins, backend modules, etc.).
#
# Discovery signal: ``composer.json`` with ``"type": "typo3-cms-extension"``
# (canonical for v11-v14) or, as fallback, any ``ext_emconf.php`` (legacy
# non-composer installs).
# ---------------------------------------------------------------------------
-_TYPO3_EXTERNAL_NODE = "framework:typo3-core"
-
-# Convention files at the extension root (matched by basename).
-_TYPO3_ROOT_FILES: tuple[str, ...] = (
- "ext_localconf.php",
- "ext_emconf.php",
- "ext_tables.php", # legacy v11-v13; absent in v14
- "ext_tables.sql",
-)
-
-# Convention files / globs under ``Configuration/`` (matched relative to the
-# extension root, with forward-slash separators).
-_TYPO3_CONFIG_GLOBS: tuple[str, ...] = (
- "Configuration/JavaScriptModules.php",
- "Configuration/ContentSecurityPolicies.php",
- "Configuration/RequestMiddlewares.php",
- "Configuration/Icons.php",
- "Configuration/Services.php",
- "Configuration/Services.yaml",
- "Configuration/Services.yml",
- "Configuration/TCA/*.php",
- "Configuration/TCA/Overrides/*.php",
- "Configuration/Backend/*.php",
- "Configuration/RTE/*.yaml",
- "Configuration/RTE/*.yml",
-)
-
# JavaScriptModules.php registers JS files via entries like
# ``'@vendor/ext/MyModule' => 'EXT:ext_key/Resources/Public/JavaScript/My.js'``.
# We extract the right-hand value to add edges to the registered files.
@@ -77,123 +51,34 @@ _TYPO3_JS_MODULE_VALUE_RE = re.compile(
)
-def _has_typo3_extension(ctx: ResolverContext, path_set: set[str]) -> bool:
- """Return True if the repo contains at least one TYPO3 extension.
-
- Checks ``composer.json`` ``type`` field first (canonical across v11-v14);
- falls back to any ``ext_emconf.php`` in path_set for legacy installs.
- """
- return bool(_find_typo3_extension_roots(ctx, path_set))
-
-
-def _find_typo3_extension_roots(ctx: ResolverContext, path_set: set[str]) -> set[str]:
- """Return the set of extension root directories (repo-relative, posix).
+def _find_typo3_extensions(
+ ctx: ResolverContext, path_set: set[str]
+) -> dict[str, ComposerManifest | None]:
+ """Extension root (repo-relative posix) -> its composer manifest, if any.
Sources, in order of authority:
- 1. Any ``composer.json`` with ``"type": "typo3-cms-extension"``.
+ 1. Any ``composer.json`` with ``"type": "typo3-cms-extension"``, and, in
+ a TYPO3 project, the packages installed under ``vendor/``.
2. Any ``ext_emconf.php`` (legacy fallback when composer.json is missing).
- """
- roots: set[str] = set()
-
- # 1. composer.json based discovery — walk the filesystem from repo root,
- # bounded depth to avoid deep vendor/node_modules traversal during tests.
- repo_path = getattr(ctx, "repo_path", None)
- if repo_path is not None:
- try:
- for composer in _iter_composer_jsons(Path(repo_path)):
- try:
- data = json.loads(composer.read_text(encoding="utf-8", errors="ignore"))
- except (OSError, ValueError):
- continue
- if not isinstance(data, dict):
- continue
- if data.get("type") != "typo3-cms-extension":
- continue
- ext_root = composer.parent.resolve()
- rel = _to_repo_relative(repo_path, ext_root)
- if rel is not None:
- roots.add(rel)
- except OSError:
- pass
- # 2. ext_emconf.php fallback for repos without a composer.json (legacy
- # non-composer installs, mostly v11 and earlier).
+ Cached on *ctx*: detection and edge emission both ask.
+ """
+ cached = getattr(ctx, "_typo3_extensions", None)
+ if cached is not None:
+ return cached
+ manifests = repo_composer_manifests(ctx)
+ if any(TYPO3.matches(m) for m in manifests) and ctx.repo_path is not None:
+ manifests = [*manifests, *find_vendor_manifests(ctx.repo_path)]
+ roots: dict[str, ComposerManifest | None] = {
+ m.rel_dir: m for m in manifests if m.type in TYPO3.composer_types
+ }
for p in path_set:
- if Path(p).name == "ext_emconf.php":
- parent = Path(p).parent.as_posix()
- roots.add("" if parent == "." else parent)
-
+ if posixpath.basename(p) == "ext_emconf.php":
+ roots.setdefault(posixpath.dirname(p), None)
+ ctx._typo3_extensions = roots # type: ignore[attr-defined]
return roots
-def _iter_composer_jsons(root: Path):
- """Yield composer.json paths likely to declare a TYPO3 extension.
-
- Searched locations:
- - ``<root>/composer.json``
- - ``<root>/<dir>/composer.json`` (single-level, for monorepos of extensions)
- - ``<root>/vendor/<vendor>/<package>/composer.json`` (project-mode TYPO3
- installs where extensions live under ``vendor/``)
-
- ``node_modules``, ``.git``, ``.bare``, and ``Build`` are skipped. Hidden
- directories are skipped at the top level only — vendor packages keep their
- nested layout.
- """
- skip_top = {"node_modules", ".git", ".bare", "var", "Build"}
- if (root / "composer.json").is_file():
- yield root / "composer.json"
-
- try:
- children = list(root.iterdir())
- except OSError:
- return
-
- for child in children:
- if not child.is_dir():
- continue
- if child.name == "vendor":
- yield from _iter_vendor_composer_jsons(child)
- continue
- if child.name in skip_top or child.name.startswith("."):
- continue
- candidate = child / "composer.json"
- if candidate.is_file():
- yield candidate
-
-
-def _iter_vendor_composer_jsons(vendor_root: Path):
- """Yield composer.json files at ``vendor/<vendor>/<package>/composer.json``.
-
- Bounded to two levels deep — composer's flat layout means we never need
- to recurse further. Symlinks are followed at most once.
- """
- try:
- vendors = list(vendor_root.iterdir())
- except OSError:
- return
- for vendor_dir in vendors:
- if not vendor_dir.is_dir() or vendor_dir.name.startswith("."):
- continue
- try:
- packages = list(vendor_dir.iterdir())
- except OSError:
- continue
- for pkg_dir in packages:
- if not pkg_dir.is_dir() or pkg_dir.name.startswith("."):
- continue
- candidate = pkg_dir / "composer.json"
- if candidate.is_file():
- yield candidate
-
-
-def _to_repo_relative(repo_path: Path, abs_path: Path) -> str | None:
- try:
- rel = abs_path.relative_to(Path(repo_path).resolve()).as_posix()
- except ValueError:
- return None
- return "" if rel == "." else rel
-
-
def _add_typo3_edges(
graph: nx.DiGraph,
parsed_files: dict[str, Any],
@@ -207,37 +92,13 @@ def _add_typo3_edges(
- ``Configuration/JavaScriptModules.php`` → each JS file it registers.
"""
count = 0
- roots = _find_typo3_extension_roots(ctx, path_set)
- if not roots:
- return 0
-
- if _TYPO3_EXTERNAL_NODE not in graph:
- graph.add_node(_TYPO3_EXTERNAL_NODE, language="external")
-
- for root in roots:
- for basename in _TYPO3_ROOT_FILES:
- target = f"{root}/{basename}" if root else basename
- if target in path_set and _add_edge_if_new(graph, _TYPO3_EXTERNAL_NODE, target):
- count += 1
-
- for glob in _TYPO3_CONFIG_GLOBS:
- prefix = f"{root}/" if root else ""
- pat = f"{prefix}{glob}"
- for p in path_set:
- if fnmatch.fnmatch(p, pat) and _add_edge_if_new(graph, _TYPO3_EXTERNAL_NODE, p):
- count += 1
-
- # Parse JavaScriptModules.php for registered JS files.
- js_modules_path = (
- f"{root}/Configuration/JavaScriptModules.php"
- if root
- else "Configuration/JavaScriptModules.php"
- )
+ for root, manifest in _find_typo3_extensions(ctx, path_set).items():
+ count += add_entry_edges(graph, TYPO3, root, path_set)
+ js_modules_path = posixpath.join(root, "Configuration/JavaScriptModules.php")
if js_modules_path in path_set:
count += _add_typo3_js_module_edges(
- graph, parsed_files, path_set, root, js_modules_path
+ graph, parsed_files, path_set, root, js_modules_path, _extension_key(manifest)
)
-
return count
@@ -247,6 +108,7 @@ def _add_typo3_js_module_edges(
path_set: set[str],
ext_root: str,
js_modules_path: str,
+ own_ext_key: str | None,
) -> int:
"""Parse JavaScriptModules.php and add edges to each registered JS file.
@@ -260,8 +122,6 @@ def _add_typo3_js_module_edges(
if not text:
return 0
- own_ext_key = _extract_ext_key_from_composer(parsed_files, ext_root)
-
count = 0
for m in _TYPO3_JS_MODULE_VALUE_RE.finditer(text):
ext_key = m.group("ext").lower()
@@ -274,42 +134,26 @@ def _add_typo3_js_module_edges(
return count
-def _extract_ext_key_from_composer(parsed_files: dict[str, Any], ext_root: str) -> str | None:
- """Best-effort extraction of the TYPO3 extension key from composer.json.
+def _extension_key(manifest: ComposerManifest | None) -> str | None:
+ """The TYPO3 extension key a composer manifest declares, or None.
Reads ``extra.typo3/cms.extension-key`` first (canonical), falls back to
deriving the key from the package name (``vendor/ext-key`` → ``ext_key``).
- Returns ``None`` if composer.json is missing or unreadable.
"""
- composer_path = f"{ext_root}/composer.json" if ext_root else "composer.json"
- parsed = parsed_files.get(composer_path)
- abs_path: Path | None = None
- if parsed is not None:
- abs_path = Path(parsed.file_info.abs_path)
- if abs_path is None or not abs_path.is_file():
- return None
- try:
- data = json.loads(abs_path.read_text(encoding="utf-8", errors="ignore"))
- except (OSError, ValueError):
- return None
- if not isinstance(data, dict):
+ if manifest is None:
return None
- extra_raw = data.get("extra")
- extra: dict[str, Any] = extra_raw if isinstance(extra_raw, dict) else {}
- typo3_raw = extra.get("typo3/cms")
- typo3: dict[str, Any] = typo3_raw if isinstance(typo3_raw, dict) else {}
- key = typo3.get("extension-key")
+ typo3 = manifest.extra.get("typo3/cms")
+ key = typo3.get("extension-key") if isinstance(typo3, dict) else None
if isinstance(key, str) and key:
return key.lower()
- name = data.get("name")
- if isinstance(name, str) and "/" in name:
- return name.split("/", 1)[1].replace("-", "_").lower()
+ if "/" in manifest.name:
+ return manifest.name.split("/", 1)[1].replace("-", "_").lower()
return None
class _Typo3Handler:
def detect(self, dctx: DetectionContext) -> bool:
- return "typo3" in dctx.stack_lower or _has_typo3_extension(dctx.ctx, dctx.path_set)
+ return "typo3" in dctx.stack_lower or bool(_find_typo3_extensions(dctx.ctx, dctx.path_set))
def add_edges(
self,
diff --git a/packages/core/src/repowise/core/ingestion/framework_facts.py b/packages/core/src/repowise/core/ingestion/framework_facts.py
new file mode 100644
index 000000000..c752412e6
--- /dev/null
+++ b/packages/core/src/repowise/core/ingestion/framework_facts.py
@@ -0,0 +1,121 @@
+"""What each PHP framework loads by convention, named once.
+
+A framework recognised here is recognised the same way everywhere: tech-stack
+detection reads :func:`detect_php_framework`, and the framework-edge handlers
+anchor :attr:`FrameworkFacts.entry_globs` to a ``framework:`` node so dead-code
+liveness sees the files the runtime loads without any importer.
+"""
+
+from __future__ import annotations
+
+import fnmatch
+import re
+from collections.abc import Iterable
+from dataclasses import dataclass
+from functools import cache
+
+from .composer import ComposerManifest
+
+
+@dataclass(frozen=True, slots=True)
+class FrameworkFacts:
+ name: str # tech-stack display name
+ anchor: str = "" # graph node the convention edges hang off
+ composer_packages: tuple[str, ...] = () # any of these in require marks it
+ composer_types: tuple[str, ...] = () # or a composer ``type`` value
+ #: Files the runtime loads with no importer, relative to the app (or
+ #: extension) root. ``*`` spans ``/``, as in the dead-code never-flag globs.
+ entry_globs: tuple[str, ...] = ()
+
+ def matches(self, manifest: ComposerManifest) -> bool:
+ if manifest.type in self.composer_types:
+ return True
+ requires = manifest.requires
+ return any(pkg in requires for pkg in self.composer_packages)
+
+ def entry_files(self, root: str, paths: Iterable[str]) -> list[str]:
+ """The members of *paths* that match an entry glob under *root*, sorted.
+
+ The root is matched literally and only the part below it against the
+ globs, so a directory named ``ext[old]`` is not read as a pattern.
+ """
+ if not self.entry_globs:
+ return []
+ prefix = f"{root}/" if root else ""
+ pattern = _compiled(self.entry_globs)
+ return sorted(
+ p for p in paths if p.startswith(prefix) and pattern.match(p, len(prefix))
+ )
+
+
+@cache
+def _compiled(globs: tuple[str, ...]) -> re.Pattern[str]:
+ # Case-sensitive on every platform, like the paths it is matched against.
+ return re.compile("|".join(fnmatch.translate(g) for g in globs))
+
+
+TYPO3 = FrameworkFacts(
+ name="TYPO3",
+ anchor="framework:typo3-core",
+ composer_packages=("typo3/cms-core",),
+ composer_types=("typo3-cms-extension",),
+ # Relative to each extension root.
+ entry_globs=(
+ "ext_localconf.php",
+ "ext_emconf.php",
+ "ext_tables.php", # legacy v11-v13; absent in v14
+ "ext_tables.sql",
+ "Configuration/JavaScriptModules.php",
+ "Configuration/ContentSecurityPolicies.php",
+ "Configuration/RequestMiddlewares.php",
+ "Configuration/Icons.php",
+ "Configuration/Services.php",
+ "Configuration/Services.yaml",
+ "Configuration/Services.yml",
+ "Configuration/TCA/*.php",
+ "Configuration/Backend/*.php",
+ "Configuration/RTE/*.yaml",
+ "Configuration/RTE/*.yml",
+ ),
+)
+
+SYMFONY = FrameworkFacts(
+ name="Symfony",
+ composer_packages=("symfony/framework-bundle", "symfony/symfony"),
+)
+
+LARAVEL = FrameworkFacts(
+ name="Laravel",
+ anchor="framework:laravel",
+ composer_packages=("laravel/framework",),
+ # Jobs, middleware and events are left out on purpose: code names them
+ # (``Job::dispatch``, the middleware list, ``event(new ...)``), so one
+ # with no importer really is unused.
+ entry_globs=(
+ "routes/*.php",
+ "bootstrap/app.php",
+ "bootstrap/providers.php",
+ "config/*.php",
+ "app/Providers/*.php",
+ "app/Console/Kernel.php",
+ "app/Console/Commands/*.php", # auto-discovered artisan commands
+ "app/Http/Kernel.php",
+ "app/Exceptions/Handler.php",
+ "app/Listeners/*.php", # event discovery
+ "app/Policies/*.php", # policy discovery by model name
+ "database/migrations/*.php",
+ "database/factories/*.php", # resolved from the model by HasFactory
+ "database/seeders/*.php",
+ "lang/*.php", # translation files, loaded by the translator
+ "resources/lang/*.php",
+ ),
+)
+
+#: Precedence order for tech-stack detection: TYPO3 ships Symfony components,
+#: so a TYPO3 package must not read as a Symfony app.
+PHP_FRAMEWORKS: tuple[FrameworkFacts, ...] = (TYPO3, SYMFONY, LARAVEL)
+
+
+def detect_php_framework(manifest: ComposerManifest) -> FrameworkFacts | None:
+ """The first framework in precedence order that *manifest* declares."""
+ return next((facts for facts in PHP_FRAMEWORKS if facts.matches(manifest)), None)
diff --git a/packages/core/src/repowise/core/ingestion/graph/_resolvers.py b/packages/core/src/repowise/core/ingestion/graph/_resolvers.py
index e0ad6b692..9a338d721 100644
--- a/packages/core/src/repowise/core/ingestion/graph/_resolvers.py
+++ b/packages/core/src/repowise/core/ingestion/graph/_resolvers.py
@@ -461,6 +461,34 @@ class ResolveMixin:
if callable(done):
done(phase)
+ def _resolve_php_same_namespace(self, progress: Any | None = None) -> None:
+ """Emit same-namespace ``imports`` edges for PHP files.
+
+ An unqualified class name resolves against the file's own namespace
+ with no ``use``, so a subclass never imported its base class (the
+ Laravel ``Controller`` every controller extends read as unreachable).
+ """
+ from ..languages.php_same_namespace import resolve_php_same_namespace_refs
+ from ..languages.scope_scan import collect_source_texts
+
+ if not any(pf.file_info.language == "php" for pf in self._parsed_files.values()):
+ return
+
+ phase = "graph.same_namespace_php"
+ if progress:
+ progress.on_phase_start(phase, None)
+ try:
+ texts = collect_source_texts(self._parsed_files, ("php",), self._source_map)
+ added = resolve_php_same_namespace_refs(self._graph, self._parsed_files, texts)
+ log.info("same_namespace_edges", language="php", added=added)
+ except Exception as exc:
+ log.warning("php_same_namespace_failed", error=str(exc))
+ finally:
+ if progress:
+ done = getattr(progress, "on_phase_done", None)
+ if callable(done):
+ done(phase)
+
def _resolve_fsharp_compile_order(self, ctx: Any, progress: Any | None = None) -> None:
"""Emit fsproj compile-order ``imports`` hint edges for F# files.
diff --git a/packages/core/src/repowise/core/ingestion/graph/builder.py b/packages/core/src/repowise/core/ingestion/graph/builder.py
index af7864ca1..71dc82051 100644
--- a/packages/core/src/repowise/core/ingestion/graph/builder.py
+++ b/packages/core/src/repowise/core/ingestion/graph/builder.py
@@ -522,6 +522,11 @@ class GraphBuilder(MetricsMixin, ResolveMixin, EdgesMixin, SerializeMixin, Rehyd
# at all. Emit conservative sibling edges so neither reads as orphaned.
self._resolve_dotnet_same_namespace(ctx, progress=progress)
+ # --- PHP same-namespace implicit references ---
+ # An unqualified class name resolves in the file's own namespace with
+ # no ``use``; emit conservative sibling edges for those references.
+ self._resolve_php_same_namespace(progress=progress)
+
# --- Swift intra-module type references ---
# Swift files see same-target siblings with no import statement;
# emit conservative type-reference edges per SPM target.
diff --git a/packages/core/src/repowise/core/ingestion/languages/csharp_same_namespace.py b/packages/core/src/repowise/core/ingestion/languages/csharp_same_namespace.py
index 14e0c22ea..050f9a496 100644
--- a/packages/core/src/repowise/core/ingestion/languages/csharp_same_namespace.py
+++ b/packages/core/src/repowise/core/ingestion/languages/csharp_same_namespace.py
@@ -54,6 +54,7 @@ import re
from pathlib import Path
from typing import TYPE_CHECKING
+from ..cohesion import SAME_NAMESPACE_HINT
from .scope_scan import FileScope, ScopeTier, emit_scope_edges
if TYPE_CHECKING:
@@ -118,7 +119,6 @@ _BCL_COMMON_TYPES = frozenset({
"Flags", "Serializable", "CLSCompliant", "InternalsVisibleTo",
})
-_SAME_NAMESPACE_HINT = "same_namespace"
_GLOBAL_USING_HINT = "global_using"
@@ -198,7 +198,7 @@ def resolve_csharp_same_namespace_refs(
if own_namespaces:
tiers.append(
ScopeTier(
- hint=_SAME_NAMESPACE_HINT,
+ hint=SAME_NAMESPACE_HINT,
lookup=lambda ident: _declarers(own_namespaces, ident),
)
)
diff --git a/packages/core/src/repowise/core/ingestion/languages/php_same_namespace.py b/packages/core/src/repowise/core/ingestion/languages/php_same_namespace.py
new file mode 100644
index 000000000..bff80afb0
--- /dev/null
+++ b/packages/core/src/repowise/core/ingestion/languages/php_same_namespace.py
@@ -0,0 +1,125 @@
+"""PHP class references that no ``use`` statement names.
+
+Two shapes reach a class with no import for the resolver to follow:
+
+* **Same namespace.** An unqualified name resolves against the file's own
+ namespace, so ``class TicketController extends Controller`` needs no ``use``
+ when both live in ``App\\Http\\Controllers``, and a Laravel app's
+ ``Controller.php`` read as unreachable.
+* **Qualified.** ``\\App\\Actions\\Login::class`` names its class outright,
+ typically in a config array; ``Concerns\\CompilesViews`` (no leading
+ separator) is relative to the file's namespace, or to the ``use`` alias its
+ first segment names.
+
+This is the PHP binding of the shared implicit-scope scan (:mod:`.scope_scan`):
+it supplies the namespace index (types the parser found, grouped by the
+file's ``namespace`` declaration) and the shadowing rule (a name bound by a
+``use`` resolves through that import, never the namespace). No skip list is
+needed: an unqualified name in a namespace never falls back to a global class.
+
+Edges carry ``hint_source="same_namespace"`` (as for C#) or
+``"qualified_name"``.
+"""
+
+from __future__ import annotations
+
+import re
+from typing import TYPE_CHECKING, Any
+
+from ..cohesion import SAME_NAMESPACE_HINT
+from .scope_scan import FileScope, ScopeTier, emit_scope_edges
+
+if TYPE_CHECKING:
+ import networkx as nx
+
+_NAMESPACE_RE = re.compile(r"^\s*namespace\s+\\?([A-Za-z_][\w\\]*)\s*[;{]", re.MULTILINE)
+
+#: One whole class name per match, bare (``Controller``) or qualified
+#: (``Rules\\In``, ``\\App\\Models\\User``), so a segment of a qualified name
+#: never reads as a bare reference to this file's namespace. One scan serves
+#: both tiers; a leading separator makes a qualified name absolute.
+_NAME_RE = re.compile(r"(?<![\w\\])\\?[A-Z]\w*(?:\\\w+)*")
+
+_TYPE_KINDS = frozenset({"class", "interface", "trait", "enum"})
+
+QUALIFIED_NAME_HINT = "qualified_name"
+
+
+def file_namespace(text: str) -> str | None:
+ """The one ``namespace`` a PHP file declares, or None.
+
+ None too for a file declaring several (``namespace A; ... namespace B;``):
+ which block a name sits in is not worth a parse here, and guessing the
+ first would index the second block's classes under the wrong namespace.
+ """
+ found = set(_NAMESPACE_RE.findall(text))
+ return found.pop() if len(found) == 1 else None
+
+
+def resolve_php_same_namespace_refs(
+ graph: nx.DiGraph,
+ parsed_files: dict[str, Any],
+ texts: dict[str, str],
+) -> int:
+ """Emit same-namespace and qualified-name ``imports`` edges for PHP files.
+
+ *texts* maps repo-relative path to source for every PHP file. Returns the
+ number of edges added.
+ """
+ namespaces = {path: file_namespace(text) for path, text in texts.items()}
+ index: dict[str, dict[str, list[str]]] = {}
+ for path in sorted(texts):
+ ns = namespaces[path]
+ if ns is None:
+ continue
+ bucket = index.setdefault(ns, {})
+ for sym in parsed_files[path].symbols:
+ if sym.kind in _TYPE_KINDS and not sym.parent_name:
+ files = bucket.setdefault(sym.name, [])
+ if path not in files:
+ files.append(path)
+ if not index:
+ return 0
+
+ def aliases(path: str) -> dict[str, str]:
+ """Local name -> imported FQN for every ``use`` in *path*."""
+ return {
+ binding.local_name: binding.exported_name
+ for imp in parsed_files[path].imports
+ for binding in imp.bindings
+ if binding.local_name and binding.exported_name
+ }
+
+ def declarers(fqn: str) -> list[str]:
+ ns, _, name = fqn.rpartition("\\")
+ return index.get(ns, {}).get(name, [])
+
+ def plan(path: str, _text: str) -> FileScope:
+ ns, bound = namespaces[path], aliases(path)
+ types = index.get(ns or "", {})
+
+ def same_namespace(ident: str) -> list[str]:
+ return [] if "\\" in ident else types.get(ident, [])
+
+ def qualified(ident: str) -> list[str]:
+ if "\\" not in ident:
+ return []
+ if ident.startswith("\\"):
+ return declarers(ident[1:])
+ head, _, rest = ident.partition("\\")
+ if head in bound:
+ return declarers(f"{bound[head]}\\{rest}")
+ return declarers(f"{ns}\\{ident}" if ns else ident)
+
+ return FileScope(
+ tiers=(
+ ScopeTier(hint=SAME_NAMESPACE_HINT, lookup=same_namespace),
+ ScopeTier(hint=QUALIFIED_NAME_HINT, lookup=qualified),
+ ),
+ # A ``use`` binds a bare name; qualified names never collide with it.
+ shadowed=frozenset(bound),
+ )
+
+ return emit_scope_edges(
+ graph, sorted(texts.items()), plan, skip_names=frozenset(), ident_re=_NAME_RE
+ )
diff --git a/packages/core/src/repowise/core/ingestion/languages/scope_scan.py b/packages/core/src/repowise/core/ingestion/languages/scope_scan.py
index fef3679a3..30b9e3337 100644
--- a/packages/core/src/repowise/core/ingestion/languages/scope_scan.py
+++ b/packages/core/src/repowise/core/ingestion/languages/scope_scan.py
@@ -1,7 +1,8 @@
"""One implicit-scope scan, bound to a different declaration index per language.
Several languages let a file name a sibling's type with no import statement:
-JVM same-package, C# same-namespace and ``global using``, Swift same-module.
+JVM same-package, C# same-namespace and ``global using``, Swift same-module,
+PHP same-namespace.
What differs between them is which index answers and what shadows a name, so
those are the parameters; the scan, the one-declaring-file rule and the edge
emission are shared.
diff --git a/packages/core/src/repowise/core/ingestion/languages/vbnet_same_namespace.py b/packages/core/src/repowise/core/ingestion/languages/vbnet_same_namespace.py
index 18a0b7c1f..649f76c4d 100644
--- a/packages/core/src/repowise/core/ingestion/languages/vbnet_same_namespace.py
+++ b/packages/core/src/repowise/core/ingestion/languages/vbnet_same_namespace.py
@@ -25,6 +25,7 @@ import re
from pathlib import Path
from typing import TYPE_CHECKING
+from ..cohesion import SAME_NAMESPACE_HINT
from .csharp_same_namespace import _BCL_COMMON_TYPES
from .scope_scan import FileScope, ScopeTier, emit_scope_edges
@@ -52,7 +53,6 @@ _VB_INTRINSICS = frozenset({
_SKIP_NAMES = _BCL_COMMON_TYPES | _VB_INTRINSICS
-_SAME_NAMESPACE_HINT = "same_namespace"
_PROJECT_IMPORT_HINT = "global_using"
# The shared scan looks for a capitalised ASCII name. VB.NET estates are
@@ -131,7 +131,7 @@ def resolve_vbnet_same_namespace_refs(
if own_namespaces:
tiers.append(
ScopeTier(
- hint=_SAME_NAMESPACE_HINT,
+ hint=SAME_NAMESPACE_HINT,
lookup=lambda ident: _declarers(own_namespaces, ident),
)
)
diff --git a/packages/core/src/repowise/core/ingestion/parser.py b/packages/core/src/repowise/core/ingestion/parser.py
index cf523cd3a..caf3859d5 100644
--- a/packages/core/src/repowise/core/ingestion/parser.py
+++ b/packages/core/src/repowise/core/ingestion/parser.py
@@ -1945,6 +1945,28 @@ class ASTParser:
)
continue
+ # PHP: one ``use`` declaration can name several classes, each its
+ # own file (``use A\B, C\D;`` and the grouped ``use A\{B, C}``).
+ if file_info.language == "php" and stmt_node.type == "namespace_use_declaration":
+ from .extractors.bindings.php import php_use_clauses
+ from .models import NamedBinding
+
+ for fqn, local in php_use_clauses(stmt_node, src):
+ imports.append(
+ Import(
+ raw_statement=raw,
+ module_path=fqn,
+ imported_names=[local],
+ is_relative=False,
+ resolved_file=None,
+ bindings=[
+ NamedBinding(local_name=local, exported_name=fqn, source_file=None)
+ ],
+ is_reexport=False,
+ )
+ )
+ continue
+
# Dart: URIs are relative unless schemed (``package:``/``dart:``),
# ``export`` directives are barrel re-exports, and the legacy
# dotted ``part of library.name;`` form resolves through the
diff --git a/packages/core/src/repowise/core/ingestion/queries/php.scm b/packages/core/src/repowise/core/ingestion/queries/php.scm
index 010e876e2..7639bac3d 100644
--- a/packages/core/src/repowise/core/ingestion/queries/php.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/php.scm
@@ -66,6 +66,13 @@
)
) @import.statement
+; Grouped form: use App\Models\{User, Post as P}; the parser expands the
+; group into one import per clause.
+(namespace_use_declaration
+ (namespace_name) @import.module
+ (namespace_use_group)
+) @import.statement
+
; require/include file imports — double-quoted strings are
; encapsed_string nodes, single-quoted (the common form) are plain
; string nodes, and the __DIR__ . '<path>' idiom nests either inside a
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/context.py b/packages/core/src/repowise/core/ingestion/resolvers/context.py
index a9fa9761b..16984393a 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/context.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/context.py
@@ -46,10 +46,10 @@ class ResolverContext:
source_map: dict[str, bytes] | None = field(default=None, repr=False)
compile_commands_cache: dict[str, dict] | None = field(default=None, repr=False)
# Lazy per-language indexes are stashed via getattr/setattr (e.g.
- # ``_php_psr4_map``, ``_ts_workspace_map``, ``_kotlin_index``,
- # ``_ruby_rails_index``, ``_swift_targets``, ``_scala_index``). This
- # mirrors the dotnet/index.py pattern and keeps language-specific bloat
- # off the dataclass.
+ # ``_php_psr4_map``, ``_composer_manifests``, ``_ts_workspace_map``,
+ # ``_kotlin_index``, ``_ruby_rails_index``, ``_swift_targets``,
+ # ``_scala_index``). This mirrors the dotnet/index.py pattern and keeps
+ # language-specific bloat off the dataclass.
# Cached sorted view of ``path_set``. Resolvers that scan for a *first*
# match MUST iterate this, never the raw set: set iteration order varies
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/php.py b/packages/core/src/repowise/core/ingestion/resolvers/php.py
index c321f594c..c01ac4fcb 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/php.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/php.py
@@ -5,7 +5,13 @@ from __future__ import annotations
import posixpath
from .context import ResolverContext
-from .php_composer import resolve_via_psr4
+from .php_composer import (
+ basename_index,
+ claims_namespace,
+ get_or_build_psr4_map,
+ in_classmap,
+ resolve_via_psr4,
+)
def resolve_php_import(module_path: str, importer_path: str, ctx: ResolverContext) -> str | None:
@@ -32,21 +38,21 @@ def resolve_php_import(module_path: str, importer_path: str, ctx: ResolverContex
psr4_match = resolve_via_psr4(module_path, ctx)
if psr4_match is not None:
return psr4_match
-
- # Convert namespace separators to path separators
- path_form = module_path.replace("\\", "/")
- parts = path_form.split("/")
- local = parts[-1]
-
- # Try stem lookup on the class name
+ # A namespaced class no autoload prefix covers is a dependency's (it lives
+ # in vendor/); a name match would bind ``Illuminate\Support\Facades\Auth``
+ # to an unrelated local ``config/auth.php``. Only a classmapped file may
+ # still claim it.
+ unclaimed = (
+ "\\" in module_path.strip("\\")
+ and bool(get_or_build_psr4_map(ctx))
+ and not claims_namespace(module_path, ctx)
+ )
+
+ local = module_path.replace("\\", "/").rsplit("/", 1)[-1]
result = ctx.stem_lookup(local.lower())
- if result and result.endswith(".php"):
+ if not (result and result.endswith(".php")):
+ # The stem map may rank a same-named non-PHP file first.
+ result = next(iter(basename_index(ctx).get(f"{local}.php", ())), None)
+ if result and (not unclaimed or in_classmap(result, ctx)):
return result
-
- # Try PSR-4 style: namespace path maps to directory
- php_name = f"{local}.php"
- for p in ctx.sorted_paths:
- if p.endswith(php_name):
- return p
-
return ctx.add_external_node(module_path)
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/php_composer.py b/packages/core/src/repowise/core/ingestion/resolvers/php_composer.py
index 51bc119d5..cf990a51e 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/php_composer.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/php_composer.py
@@ -1,95 +1,127 @@
-"""composer.json PSR-4 autoload parsing for PHP import resolution."""
+"""PSR-4 lookup for PHP import resolution, built from the shared composer reader.
+
+Reading ``composer.json`` itself is :mod:`..composer`'s job; this module only
+turns the manifests into the autoload map and answers class-name lookups.
+"""
from __future__ import annotations
-import json
-from pathlib import Path
+import posixpath
from typing import TYPE_CHECKING
+from ..composer import repo_composer_manifests
+
if TYPE_CHECKING:
from .context import ResolverContext
-def _normalise_dirs(value: object) -> list[str]:
- """PSR-4 values may be a single string or a list of strings."""
- if isinstance(value, str):
- return [value.rstrip("/")]
- if isinstance(value, list):
- return [str(v).rstrip("/") for v in value if isinstance(v, str)]
- return []
-
-
-def read_composer_psr4(repo_path: Path | None) -> dict[str, list[str]]:
- """Read root ``composer.json`` and return ``{namespace_prefix: [dir, ...]}``.
+def get_or_build_psr4_map(ctx: ResolverContext) -> dict[str, list[str]]:
+ """``{namespace_prefix: [repo-relative dir, ...]}`` over every first-party manifest.
- Both ``autoload.psr-4`` and ``autoload-dev.psr-4`` are merged. Namespace
- keys are returned with their trailing ``\\`` preserved (composer spec).
+ The root manifest comes first, so its directories are probed first when a
+ nested package declares the same prefix.
"""
- if repo_path is None:
- return {}
- composer = repo_path / "composer.json"
- if not composer.is_file():
- return {}
- try:
- data = json.loads(composer.read_text(encoding="utf-8", errors="ignore"))
- except Exception:
- return {}
-
- result: dict[str, list[str]] = {}
- for section in ("autoload", "autoload-dev"):
- block = data.get(section) if isinstance(data, dict) else None
- if not isinstance(block, dict):
- continue
- psr4 = block.get("psr-4")
- if not isinstance(psr4, dict):
- continue
- for prefix, dirs in psr4.items():
- if not isinstance(prefix, str):
- continue
- result.setdefault(prefix, []).extend(_normalise_dirs(dirs))
- return result
-
-
-def get_or_build_psr4_map(ctx: ResolverContext) -> dict[str, list[str]]:
cached = getattr(ctx, "_php_psr4_map", None)
if cached is not None:
return cached
- psr4 = read_composer_psr4(ctx.repo_path)
+ psr4: dict[str, list[str]] = {}
+ for manifest in repo_composer_manifests(ctx):
+ for prefix, dirs in manifest.psr4:
+ known = psr4.setdefault(prefix, [])
+ known.extend(d for d in dirs if d not in known)
ctx._php_psr4_map = psr4 # type: ignore[attr-defined]
return psr4
+def _claims(ctx: ResolverContext) -> tuple[tuple[str, ...] | None, tuple[str, ...]]:
+ """Namespace prefixes the repo autoloads (None: every namespace), and classmap dirs.
+
+ A ``""`` PSR-4 or PSR-0 prefix is composer's catch-all, so it claims every
+ namespace. Classmap directories claim whatever classes their files declare,
+ which no prefix can express, so they are returned for a path check instead.
+ """
+ cached = getattr(ctx, "_php_claims", None)
+ if cached is None:
+ prefixes: list[str] | None = []
+ classmap: list[str] = []
+ for manifest in repo_composer_manifests(ctx):
+ classmap.extend(manifest.classmap)
+ for prefix, _dirs in (*manifest.psr4, *manifest.psr0):
+ if not prefix:
+ prefixes = None
+ elif prefixes is not None:
+ prefixes.append(prefix)
+ cached = (None if prefixes is None else tuple(prefixes), tuple(classmap))
+ ctx._php_claims = cached # type: ignore[attr-defined]
+ return cached
+
+
+def claims_namespace(module_path: str, ctx: ResolverContext) -> bool:
+ """Whether a first-party autoload prefix covers *module_path*."""
+ prefixes, _classmap = _claims(ctx)
+ if prefixes is None:
+ return True
+ fqn = module_path.replace("/", "\\")
+ return any(fqn.startswith(prefix) for prefix in prefixes)
+
+
+def in_classmap(path: str, ctx: ResolverContext) -> bool:
+ """Whether *path* sits under a directory some manifest classmaps."""
+ _prefixes, classmap = _claims(ctx)
+ return any(not d or path == d or path.startswith(f"{d}/") for d in classmap)
+
+
+def basename_index(ctx: ResolverContext) -> dict[str, list[str]]:
+ """``{file basename: [paths, sorted]}`` for the PHP files in the index, cached on *ctx*."""
+ cached = getattr(ctx, "_php_basename_index", None)
+ if cached is None:
+ cached = {}
+ for p in ctx.sorted_paths:
+ if p.endswith(".php"):
+ cached.setdefault(posixpath.basename(p), []).append(p)
+ ctx._php_basename_index = cached # type: ignore[attr-defined]
+ return cached
+
+
def resolve_via_psr4(module_path: str, ctx: ResolverContext) -> str | None:
"""Try PSR-4 prefix matching from composer.json. Returns repo-relative
path or None.
- *module_path* is the FQN as written in PHP (``Foo\\Bar\\Baz`` form).
+ *module_path* is the FQN as written in PHP (``Foo\\Bar\\Baz`` form). The
+ answer does not depend on the importing file, so it is memoized on *ctx*.
"""
psr4 = get_or_build_psr4_map(ctx)
if not psr4:
return None
+ memo: dict[str, str | None] | None = getattr(ctx, "_php_psr4_memo", None)
+ if memo is None:
+ memo = {}
+ ctx._php_psr4_memo = memo # type: ignore[attr-defined]
+ elif module_path in memo:
+ return memo[module_path]
fqn = module_path.replace("/", "\\")
- # Longest-prefix match wins. Composer prefixes always end in `\\`.
- best: tuple[str, list[str]] | None = None
- for prefix, dirs in psr4.items():
- if not prefix:
- continue
- if fqn.startswith(prefix) and (best is None or len(prefix) > len(best[0])):
- best = (prefix, dirs)
- if best is None:
- return None
- prefix, dirs = best
- tail = fqn[len(prefix) :].replace("\\", "/")
- if not tail:
- return None
- for base_dir in dirs:
- candidate = f"{base_dir}/{tail}.php" if base_dir else f"{tail}.php"
- if candidate in ctx.path_set:
- return candidate
- # Also tolerate paths whose first segment differs (some repos vendor
- # the root differently). Probe by suffix as a forgiving fallback.
- suffix_probe = f"/{candidate}"
- for p in ctx.sorted_paths:
- if p == candidate or p.endswith(suffix_probe):
- return p
- return None
+ # Longest prefix first, then shorter ones, as composer's own class loader
+ # does: ``Illuminate\\Support\\`` may list four directories and still leave
+ # ``Str`` to the ``Illuminate\\`` entry. Composer prefixes end in ``\\``.
+ by_prefix = [
+ [f"{base_dir}/{tail}.php" if base_dir else f"{tail}.php" for base_dir in psr4[prefix]]
+ for prefix in sorted((p for p in psr4 if p and fqn.startswith(p)), key=len, reverse=True)
+ if (tail := fqn[len(prefix) :].replace("\\", "/"))
+ ]
+ found = next((c for group in by_prefix for c in group if c in ctx.path_set), None)
+ if found is None and by_prefix:
+ # Tolerate paths whose first segments differ (some repos vendor the
+ # root differently), for the longest prefix only: a shorter one yields
+ # short tails that end too many unrelated paths.
+ index = basename_index(ctx)
+ found = next(
+ (
+ p
+ for candidate in by_prefix[0]
+ for p in index.get(posixpath.basename(candidate), ())
+ if p.endswith(f"/{candidate}")
+ ),
+ None,
+ )
+ memo[module_path] = found
+ return found
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/ts_workspace.py b/packages/core/src/repowise/core/ingestion/resolvers/ts_workspace.py
index 6228f61a9..af679c1a2 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/ts_workspace.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/ts_workspace.py
@@ -278,7 +278,7 @@ def _match_export_key(
return best_targets
-def _read_workspaces_field(pkg_data: dict) -> list[str]:
+def read_workspaces_field(pkg_data: dict) -> list[str]:
ws = pkg_data.get("workspaces")
if isinstance(ws, list):
return [str(p) for p in ws if isinstance(p, str)]
@@ -338,7 +338,7 @@ def _read_workspace_declaration(repo_path: Path) -> _WorkspaceDeclaration:
if not isinstance(data, dict):
return _WorkspaceDeclaration((), (), include_root=False)
return _WorkspaceDeclaration(
- tuple(_read_workspaces_field(data)), (), include_root=False
+ tuple(read_workspaces_field(data)), (), include_root=False
)
diff --git a/packages/core/src/repowise/core/workspace/cross_repo.py b/packages/core/src/repowise/core/workspace/cross_repo.py
index b13147634..af94a344c 100644
--- a/packages/core/src/repowise/core/workspace/cross_repo.py
+++ b/packages/core/src/repowise/core/workspace/cross_repo.py
@@ -1,4 +1,7 @@
-"""Cross-repo intelligence — co-change detection, manifest scanning, overlay persistence.
+"""Cross-repo intelligence: co-change detection, manifest edges, overlay persistence.
+
+Manifest scanning itself lives in :mod:`.manifests`; this module runs it and
+persists what it finds alongside the co-change edges.
Runs during ``repowise update --workspace`` (write path). The resulting JSON
is loaded by :class:`CrossRepoEnricher` in the MCP server (read path).
@@ -20,11 +23,15 @@ from collections import defaultdict
from dataclasses import asdict, dataclass, field
from datetime import UTC, datetime
from pathlib import Path, PurePosixPath
-from typing import Any
from ..co_change import CO_CHANGE_DECAY_TAU
from ..fsutils import atomic_write_text
from .config import WorkspaceConfig, ensure_workspace_data_dir
+from .manifests import (
+ CrossRepoPackageDep,
+ CrossRepoPackageDiagnostic,
+ detect_package_dependencies_with_diagnostics,
+)
_log = logging.getLogger("repowise.workspace.cross_repo")
@@ -68,11 +75,6 @@ MAX_EDGES_PER_REPO_PAIR: int = 50
# Per-session cap on files paired per side, guarding the O(N*M) cross-product
# against sprawling release/codemod sessions.
_MAX_FILES_PER_SESSION_SIDE: int = 20
-# Package non-matches are useful evidence but ordinary workspaces can declare
-# thousands of external libraries. Persist a deterministic sample plus the
-# uncapped total so diagnostics cannot make the overlay unbounded.
-_MAX_PACKAGE_DIAGNOSTICS: int = 200
-
# ---------------------------------------------------------------------------
# Noise-file filtering
@@ -177,27 +179,6 @@ class CrossRepoCoChange:
last_date: str # ISO date of most recent co-change
-@dataclass
-class CrossRepoPackageDep:
- source_repo: str
- target_repo: str
- source_manifest: str
- kind: str # npm_local_path, pip_path, cargo_path, go_replace, maven_coordinate
- target_package: str = ""
- target_manifest: str = ""
- requested_version: str | None = None
- scope: str = ""
- resolution_basis: str = ""
-
-
-@dataclass
-class CrossRepoPackageDiagnostic:
- repo: str
- source_manifest: str
- code: str
- detail: str = ""
-
-
@dataclass
class CrossRepoOverlay:
version: int = _OVERLAY_VERSION
@@ -597,544 +578,6 @@ def detect_cross_repo_co_changes(
return capped, len(results)
-# ---------------------------------------------------------------------------
-# Package / manifest dependency detection
-# ---------------------------------------------------------------------------
-
-
-def _resolve_target_repo(
- relative_ref: str,
- source_repo_path: Path,
- repo_paths: dict[str, Path],
-) -> str | None:
- """Resolve a relative path reference to a repo alias, or None."""
- try:
- target_abs = (source_repo_path / relative_ref).resolve()
- # Most-specific root first makes physically nested selected repos
- # deterministic. Path.relative_to supplies the separator-aware
- # containment check that string prefixes (``lib`` vs ``lib-old``) do not.
- roots = sorted(
- repo_paths.items(),
- key=lambda item: len(item[1].parts),
- reverse=True,
- )
- for alias, repo_path in roots:
- try:
- target_abs.relative_to(repo_path)
- except ValueError:
- continue
- else:
- return alias
- except Exception:
- # A manifest reference that cannot be resolved to a path is the one
- # way this returns None without having compared anything, so it is
- # worth distinguishing from "resolved fine, matched no repo".
- _log.warning(
- "Could not resolve manifest reference %r relative to %s",
- relative_ref,
- source_repo_path,
- exc_info=True,
- )
- return None
-
-
-def _scan_package_json(
- repo_path: Path,
- repo_paths: dict[str, Path],
- alias: str,
-) -> list[CrossRepoPackageDep]:
- """Scan package.json for local file: references to sibling repos."""
- results: list[CrossRepoPackageDep] = []
- pkg_json = repo_path / "package.json"
- if not pkg_json.is_file():
- return results
-
- try:
- data = json.loads(pkg_json.read_text(encoding="utf-8"))
- except Exception:
- return results
-
- for dep_key in ("dependencies", "devDependencies", "peerDependencies"):
- deps = data.get(dep_key, {})
- if not isinstance(deps, dict):
- continue
- for _name, version in deps.items():
- if isinstance(version, str) and version.startswith("file:"):
- rel_path = version[5:] # strip "file:"
- target = _resolve_target_repo(rel_path, repo_path, repo_paths)
- if target and target != alias:
- results.append(
- CrossRepoPackageDep(
- source_repo=alias,
- target_repo=target,
- source_manifest="package.json",
- kind="npm_local_path",
- )
- )
-
- # Check workspaces field
- workspaces = data.get("workspaces", [])
- if isinstance(workspaces, dict):
- workspaces = workspaces.get("packages", [])
- # Workspaces are globs — we just check if they point to sibling repos
- for ws in workspaces if isinstance(workspaces, list) else []:
- if isinstance(ws, str) and ".." in ws:
- target = _resolve_target_repo(ws.rstrip("/*"), repo_path, repo_paths)
- if target and target != alias:
- results.append(
- CrossRepoPackageDep(
- source_repo=alias,
- target_repo=target,
- source_manifest="package.json",
- kind="npm_workspace",
- )
- )
-
- return results
-
-
-def _scan_pyproject_toml(
- repo_path: Path,
- repo_paths: dict[str, Path],
- alias: str,
-) -> list[CrossRepoPackageDep]:
- """Scan pyproject.toml for path dependencies."""
- results: list[CrossRepoPackageDep] = []
- toml_path = repo_path / "pyproject.toml"
- if not toml_path.is_file():
- return results
-
- try:
- # Use tomllib (Python 3.11+) or tomli
- try:
- import tomllib
- except ImportError:
- import tomli as tomllib # type: ignore[no-redef]
- with open(toml_path, "rb") as f:
- data = tomllib.load(f)
- except Exception:
- return results
-
- # Poetry path dependencies
- poetry_deps = data.get("tool", {}).get("poetry", {}).get("dependencies", {})
- for _name, spec in poetry_deps.items() if isinstance(poetry_deps, dict) else []:
- if isinstance(spec, dict) and "path" in spec:
- target = _resolve_target_repo(spec["path"], repo_path, repo_paths)
- if target and target != alias:
- results.append(
- CrossRepoPackageDep(
- source_repo=alias,
- target_repo=target,
- source_manifest="pyproject.toml",
- kind="pip_path",
- )
- )
-
- # PEP 621 dependencies with path (uncommon but possible via tool configs)
- for group_key in ("dependencies", "optional-dependencies"):
- group = data.get("project", {}).get(group_key, {})
- if isinstance(group, dict):
- for _name, spec in group.items():
- if isinstance(spec, dict) and "path" in spec:
- target = _resolve_target_repo(spec["path"], repo_path, repo_paths)
- if target and target != alias:
- results.append(
- CrossRepoPackageDep(
- source_repo=alias,
- target_repo=target,
- source_manifest="pyproject.toml",
- kind="pip_path",
- )
- )
-
- return results
-
-
-def _scan_cargo_toml(
- repo_path: Path,
- repo_paths: dict[str, Path],
- alias: str,
-) -> list[CrossRepoPackageDep]:
- """Scan Cargo.toml for path dependencies."""
- results: list[CrossRepoPackageDep] = []
- cargo_path = repo_path / "Cargo.toml"
- if not cargo_path.is_file():
- return results
-
- try:
- try:
- import tomllib
- except ImportError:
- import tomli as tomllib # type: ignore[no-redef]
- with open(cargo_path, "rb") as f:
- data = tomllib.load(f)
- except Exception:
- return results
-
- for section in ("dependencies", "dev-dependencies", "build-dependencies"):
- deps = data.get(section, {})
- for _name, spec in deps.items():
- if isinstance(spec, dict) and "path" in spec:
- target = _resolve_target_repo(spec["path"], repo_path, repo_paths)
- if target and target != alias:
- results.append(
- CrossRepoPackageDep(
- source_repo=alias,
- target_repo=target,
- source_manifest="Cargo.toml",
- kind="cargo_path",
- )
- )
-
- return results
-
-
-def _scan_go_mod(
- repo_path: Path,
- repo_paths: dict[str, Path],
- alias: str,
-) -> list[CrossRepoPackageDep]:
- """Scan go.mod for replace directives pointing to sibling repos."""
- results: list[CrossRepoPackageDep] = []
- go_mod = repo_path / "go.mod"
- if not go_mod.is_file():
- return results
-
- try:
- content = go_mod.read_text(encoding="utf-8")
- except Exception:
- return results
-
- # Parse "replace" directives: `replace foo => ../sibling`
- in_replace_block = False
- for line in content.splitlines():
- stripped = line.strip()
- if stripped.startswith("replace ("):
- in_replace_block = True
- continue
- if in_replace_block and stripped == ")":
- in_replace_block = False
- continue
-
- # Single-line replace or inside block
- if stripped.startswith("replace ") or in_replace_block:
- parts = stripped.replace("replace ", "").split("=>")
- if len(parts) == 2:
- target_path = parts[1].strip().split()[0] # first token after =>
- if target_path.startswith("..") or target_path.startswith("./"):
- target = _resolve_target_repo(target_path, repo_path, repo_paths)
- if target and target != alias:
- results.append(
- CrossRepoPackageDep(
- source_repo=alias,
- target_repo=target,
- source_manifest="go.mod",
- kind="go_replace",
- )
- )
-
- return results
-
-
-_CSPROJ_SKIP_DIRS = frozenset(
- {"bin", "obj", ".vs", "packages", "node_modules", ".git", "TestResults"}
-)
-_CSPROJ_SKIP_DIRS_LOWER = frozenset(name.lower() for name in _CSPROJ_SKIP_DIRS)
-
-
-@dataclass
-class _CsprojIndex:
- """Project manifests in the workspace, walked and parsed exactly once.
-
- ``_scan_csproj`` needs two things: the assembly-name → repo-alias map
- (built from *all* repos) and the current repo's own project files. Built
- per repo, the map is rebuilt identically for every alias, so an R-repo
- workspace walked R*R + R trees and re-parsed every project file R times.
- The map depends only on ``repo_paths``, never on the alias being scanned,
- so hoisting it here is a pure de-duplication: same inputs, same map, R
- walks instead of R*R + R.
- """
-
- # Parsed once and shared; ElementTree instances are only ever read.
- trees: dict[Path, Any] = field(default_factory=dict)
- by_repo: dict[str, list[Path]] = field(default_factory=dict)
- poms_by_repo: dict[str, list[Path]] = field(default_factory=dict)
- assembly_to_repo: dict[str, str] = field(default_factory=dict)
-
-
-def _walk_project_manifests(
- repo_root: Path,
- index: _CsprojIndex,
-) -> tuple[list[Path], list[Path]]:
- """Collect Maven and .NET project manifests in one pruned repo walk."""
- from xml.etree import ElementTree as ET
-
- from repowise.core.fs_walk import PRUNED_DIRS, walk_repo
-
- csprojects: list[Path] = []
- poms: list[Path] = []
- # ``packages`` is a .NET package cache convention but also a legitimate
- # Maven module directory, so it cannot be pruned for the shared walk.
- prune = PRUNED_DIRS | {
- "target",
- "dist",
- "build",
- "bin",
- "obj",
- ".vs",
- "TestResults",
- }
- for dirpath, _dirnames, filenames in walk_repo(repo_root, prune_dirs=prune):
- for filename in sorted(filenames):
- path = dirpath / filename
- if filename == "pom.xml":
- poms.append(path)
- elif filename.lower().endswith(".csproj"):
- relative_dirs = path.relative_to(repo_root).parts[:-1]
- if any(part.lower() in _CSPROJ_SKIP_DIRS_LOWER for part in relative_dirs):
- continue
- try:
- index.trees[path] = ET.parse(path)
- except (ET.ParseError, OSError):
- continue
- csprojects.append(path)
- return csprojects, poms
-
-
-def _walk_csproj(repo_root: Path, index: _CsprojIndex) -> list[Path]:
- """Compatibility wrapper for a standalone .NET project scan."""
- projects, _poms = _walk_project_manifests(repo_root, index)
- return projects
-
-
-def _assembly_name(tree: Any, csproj: Path) -> str:
- """The project's assembly name, defaulting to the filename minus extension."""
- for elem in tree.getroot().iter():
- tag = elem.tag.split("}", 1)[1] if elem.tag.startswith("{") else elem.tag
- if tag == "AssemblyName" and elem.text:
- return elem.text.strip()
- return csproj.stem
-
-
-def _index_csproj_files(repo_paths: dict[str, Path]) -> _CsprojIndex:
- """Walk each repo once, parse each ``.csproj`` once, build the assembly map."""
- index = _CsprojIndex()
- # Insertion order matches the previous per-alias rebuild, so a name
- # defined in two repos still resolves to the last repo in repo_paths.
- for alias, path in repo_paths.items():
- found, poms = _walk_project_manifests(path, index)
- for csproj in found:
- index.assembly_to_repo[_assembly_name(index.trees[csproj], csproj)] = alias
- index.by_repo[alias] = found
- index.poms_by_repo[alias] = poms
- return index
-
-
-def _scan_csproj(
- repo_path: Path,
- repo_paths: dict[str, Path],
- alias: str,
- *,
- csproj_index: _CsprojIndex | None = None,
-) -> list[CrossRepoPackageDep]:
- """Scan every .csproj for cross-repo references.
-
- Two patterns are recognised:
-
- 1. ``<ProjectReference Include="..\\..\\OtherRepo\\Foo.csproj"/>`` — a
- relative path that resolves into a sibling indexed repo. Emits
- ``kind="dotnet_project_ref"``.
-
- 2. ``<PackageReference Include="MyOrg.SharedLib"/>`` whose package id
- matches a sibling repo's ``<AssemblyName>`` or ``.csproj`` filename.
- Emits ``kind="dotnet_nuget_internal"`` — the "internal NuGet
- feed" pattern enterprise teams use when their shared libs live
- in a separate repo.
-
- Skips ``bin``/``obj``/``packages``/``.vs``/``TestResults`` build outputs.
-
- ``csproj_index`` lets a caller scanning several repos share one walk;
- omitted, the scan builds its own and behaves exactly as a standalone call.
- """
- index = csproj_index if csproj_index is not None else _index_csproj_files(repo_paths)
-
- own = index.by_repo.get(alias)
- if own is None or repo_paths.get(alias) != repo_path:
- # The index does not describe this exact tree — *alias* is absent from
- # repo_paths, or the caller passed a repo_path that disagrees with it.
- # Walk the tree we were actually handed, and leave assembly_to_repo
- # alone: it is defined by repo_paths, and folding this repo's own
- # names into it would let them shadow a sibling that legitimately
- # owns the same assembly name.
- own = _walk_csproj(repo_path, index)
-
- assembly_to_repo = index.assembly_to_repo
-
- results: list[CrossRepoPackageDep] = []
-
- for csproj in own:
- tree = index.trees[csproj]
- try:
- rel_manifest = csproj.relative_to(repo_path).as_posix()
- except ValueError:
- rel_manifest = csproj.name
-
- for elem in tree.getroot().iter():
- tag = elem.tag.split("}", 1)[1] if elem.tag.startswith("{") else elem.tag
- include = elem.get("Include") if elem.attrib else None
- if not include:
- continue
- if tag == "ProjectReference":
- rel = include.replace("\\", "/")
- target = _resolve_target_repo(rel, csproj.parent, repo_paths)
- if target and target != alias:
- results.append(
- CrossRepoPackageDep(
- source_repo=alias,
- target_repo=target,
- source_manifest=rel_manifest,
- kind="dotnet_project_ref",
- )
- )
- elif tag == "PackageReference":
- pkg = include.strip()
- target = assembly_to_repo.get(pkg)
- if target and target != alias:
- results.append(
- CrossRepoPackageDep(
- source_repo=alias,
- target_repo=target,
- source_manifest=rel_manifest,
- kind="dotnet_nuget_internal",
- )
- )
-
- return results
-
-
-def detect_package_dependencies(
- repo_paths: dict[str, Path],
-) -> list[CrossRepoPackageDep]:
- """Scan all repos for manifest-based cross-repo dependencies."""
- dependencies, _diagnostics, _total_diagnostics = detect_package_dependencies_with_diagnostics(
- repo_paths
- )
- return dependencies
-
-
-def detect_package_dependencies_with_diagnostics(
- repo_paths: dict[str, Path],
-) -> tuple[
- list[CrossRepoPackageDep],
- list[CrossRepoPackageDiagnostic],
- int,
-]:
- """Scan package manifests and retain bounded Maven non-match evidence."""
-
- results: list[CrossRepoPackageDep] = []
- seen: set[tuple[str, str, str, str, str]] = set()
- resolved_repo_paths = {alias: path.resolve() for alias, path in repo_paths.items()}
-
- # One walk per repo, shared across every alias. The .csproj scan is the
- # only existing manifest scanner that walks the tree rather than reading
- # a fixed root file. The same pass now inventories Maven POMs as well.
- project_index = _index_csproj_files(resolved_repo_paths)
-
- for alias, path in resolved_repo_paths.items():
- for scanner in (
- _scan_package_json,
- _scan_pyproject_toml,
- _scan_cargo_toml,
- _scan_go_mod,
- ):
- for dep in scanner(path, resolved_repo_paths, alias):
- key = (
- dep.source_repo,
- dep.target_repo,
- dep.source_manifest,
- dep.kind,
- dep.target_package,
- )
- if key not in seen:
- seen.add(key)
- results.append(dep)
- for dep in _scan_csproj(
- path,
- resolved_repo_paths,
- alias,
- csproj_index=project_index,
- ):
- key = (
- dep.source_repo,
- dep.target_repo,
- dep.source_manifest,
- dep.kind,
- dep.target_package,
- )
- if key not in seen:
- seen.add(key)
- results.append(dep)
-
- from .maven_dependencies import detect_maven_dependencies
-
- maven_links, maven_diagnostics = detect_maven_dependencies(
- resolved_repo_paths,
- project_index.poms_by_repo,
- )
- for link in maven_links:
- dep = CrossRepoPackageDep(
- source_repo=link.source_repo,
- target_repo=link.target_repo,
- source_manifest=link.source_manifest,
- kind="maven_coordinate",
- target_package=link.target_package,
- target_manifest=link.target_manifest,
- requested_version=link.requested_version,
- scope=link.scope,
- resolution_basis=link.resolution_basis,
- )
- key = (
- dep.source_repo,
- dep.target_repo,
- dep.source_manifest,
- dep.kind,
- dep.target_package,
- )
- if key not in seen:
- seen.add(key)
- results.append(dep)
-
- results.sort(
- key=lambda dep: (
- dep.source_repo,
- dep.target_repo,
- dep.source_manifest,
- dep.kind,
- dep.target_package,
- )
- )
- diagnostics = sorted(
- (
- CrossRepoPackageDiagnostic(
- repo=item.repo,
- source_manifest=item.manifest,
- code=item.code,
- detail=item.detail,
- )
- for item in maven_diagnostics
- ),
- key=lambda item: (
- item.code == "external_coordinate",
- item.repo,
- item.source_manifest,
- item.code,
- item.detail,
- ),
- )
- total_diagnostics = len(diagnostics)
- return results, diagnostics[:_MAX_PACKAGE_DIAGNOSTICS], total_diagnostics
-
-
# ---------------------------------------------------------------------------
# Repo summaries
# ---------------------------------------------------------------------------
diff --git a/packages/core/src/repowise/core/workspace/extractors/service_boundary.py b/packages/core/src/repowise/core/workspace/extractors/service_boundary.py
index 5f01b7a1b..38462e64a 100644
--- a/packages/core/src/repowise/core/workspace/extractors/service_boundary.py
+++ b/packages/core/src/repowise/core/workspace/extractors/service_boundary.py
@@ -1,7 +1,8 @@
"""Service boundary detection for monorepo sub-services.
Walks a repo directory tree looking for marker files (package.json, go.mod,
-Dockerfile, etc.) that indicate independent service boundaries. Used to
+Dockerfile, etc., named per ecosystem in :data:`~..manifests.ECOSYSTEMS`) that
+indicate independent service boundaries. Used to
distinguish intra-service calls from inter-service calls when matching
contracts.
@@ -20,26 +21,12 @@ from pathlib import Path
from repowise.core.fs_walk import walk_repo
from repowise.core.ingestion.languages.registry import REGISTRY as _LANG_REGISTRY
+from repowise.core.workspace.manifests import SERVICE_MARKERS
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
-_SERVICE_MARKERS = frozenset(
- {
- "package.json",
- "go.mod",
- "Dockerfile",
- "pom.xml",
- "build.gradle",
- "build.gradle.kts",
- "Cargo.toml",
- "pyproject.toml",
- "requirements.txt",
- "mix.exs",
- }
-)
-
_BLOCKED_DIRS = frozenset(
{
".git",
@@ -134,7 +121,7 @@ def detect_service_boundaries(repo_path: Path) -> list[ServiceBoundary]:
continue # skip repo root itself
# Check for marker files
- found_markers = [f for f in filenames if f in _SERVICE_MARKERS]
+ found_markers = [f for f in filenames if f in SERVICE_MARKERS]
if not found_markers:
continue
diff --git a/packages/core/src/repowise/core/workspace/manifests.py b/packages/core/src/repowise/core/workspace/manifests.py
new file mode 100644
index 000000000..32dad5ebc
--- /dev/null
+++ b/packages/core/src/repowise/core/workspace/manifests.py
@@ -0,0 +1,610 @@
+"""Package manifests across a workspace: one table keyed by ecosystem.
+
+Each :class:`Ecosystem` names the files that mark a directory as a service root
+(read by :mod:`.extractors.service_boundary`) and, where it has one, the scanner
+that turns its manifests into cross-repo package edges (read by
+:func:`detect_package_dependencies_with_diagnostics`). Adding an ecosystem is
+one row; the two consumers cannot drift apart.
+
+Service markers are deliberately not the language registry's package-root
+manifests: a ``Dockerfile`` or ``requirements.txt`` marks a deployable service
+without being a package, while build files such as ``*.csproj`` mark packages
+inside one service.
+
+:func:`_index_manifests` reads every repo once up front: one pruned walk finds
+the .NET, Maven and composer manifests, and the declared npm workspace gives
+the npm packages the repo publishes, so a dependency can match a sibling repo
+by package *name*, not only by a relative path.
+"""
+
+from __future__ import annotations
+
+import json
+import logging
+from collections.abc import Callable, Iterable, Iterator
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import Any
+
+from repowise.core.ingestion.composer import (
+ COMPOSER_JSON,
+ ComposerManifest,
+ is_package_dir,
+ read_composer,
+)
+from repowise.core.ingestion.resolvers.ts_workspace import (
+ build_workspace_map,
+ read_workspaces_field,
+)
+
+_log = logging.getLogger("repowise.workspace.manifests")
+
+PACKAGE_JSON = "package.json"
+
+# Package non-matches are useful evidence but ordinary workspaces can declare
+# thousands of external libraries. Persist a deterministic sample plus the
+# uncapped total so diagnostics cannot make the overlay unbounded.
+_MAX_PACKAGE_DIAGNOSTICS: int = 200
+
+_NPM_DEP_SECTIONS = ("dependencies", "devDependencies", "peerDependencies")
+
+
+@dataclass
+class CrossRepoPackageDep:
+ source_repo: str
+ target_repo: str
+ source_manifest: str
+ # npm_local_path, npm_workspace, npm_package, composer_path, composer_package,
+ # pip_path, cargo_path, go_replace, dotnet_project_ref, dotnet_nuget_internal,
+ # maven_coordinate
+ kind: str
+ target_package: str = ""
+ target_manifest: str = ""
+ requested_version: str | None = None
+ scope: str = ""
+ resolution_basis: str = ""
+
+
+@dataclass
+class CrossRepoPackageDiagnostic:
+ repo: str
+ source_manifest: str
+ code: str
+ detail: str = ""
+
+
+# ---------------------------------------------------------------------------
+# The workspace index
+# ---------------------------------------------------------------------------
+
+_CSPROJ_SKIP_DIRS = frozenset(
+ {"bin", "obj", ".vs", "packages", "node_modules", ".git", "TestResults"}
+)
+_CSPROJ_SKIP_DIRS_LOWER = frozenset(name.lower() for name in _CSPROJ_SKIP_DIRS)
+
+
+@dataclass
+class _ManifestIndex:
+ """Project manifests in the workspace, read and parsed exactly once.
+
+ The assembly-name and package-name maps depend only on ``repo_paths``,
+ never on the alias being scanned, so they are built here once instead of
+ per repo scanned.
+ """
+
+ repo_paths: dict[str, Path] = field(default_factory=dict)
+ # Parsed once and shared; ElementTree instances are only ever read.
+ trees: dict[Path, Any] = field(default_factory=dict)
+ by_repo: dict[str, list[Path]] = field(default_factory=dict)
+ poms_by_repo: dict[str, list[Path]] = field(default_factory=dict)
+ assembly_to_repo: dict[str, str] = field(default_factory=dict)
+ #: alias -> [(repo-relative manifest path, decoded package.json)]
+ npm_by_repo: dict[str, list[tuple[str, dict]]] = field(default_factory=dict)
+ composer_by_repo: dict[str, list[ComposerManifest]] = field(default_factory=dict)
+ #: (ecosystem, published name) -> (alias, manifest), None once two repos claim it
+ published: dict[tuple[str, str], tuple[str, str] | None] = field(default_factory=dict)
+
+ def claim(self, ecosystem: str, name: str, alias: str, manifest: str) -> None:
+ key = (ecosystem, name)
+ if key not in self.published:
+ self.published[key] = (alias, manifest)
+ elif (owner := self.published[key]) is not None and owner[0] != alias:
+ self.published[key] = None # ambiguous: no edge to anyone
+
+ def owner(self, ecosystem: str, name: str) -> tuple[str, str] | None:
+ return self.published.get((ecosystem, name))
+
+
+def _read_json(path: Path) -> Any:
+ try:
+ return json.loads(path.read_text(encoding="utf-8"))
+ except (OSError, ValueError):
+ return None
+
+
+def _npm_packages(repo_path: Path) -> list[tuple[str, dict]]:
+ """The root ``package.json`` and every declared workspace member's.
+
+ Workspace membership is the npm definition of "a package this repo
+ ships"; a nested ``package.json`` that is not a member is a fixture, an
+ example or a vendored copy (``compiled/semver``), and matching on its
+ name would invent an edge.
+ """
+ out: list[tuple[str, dict]] = []
+ # A member glob can reach outside the repo (``../shared``); that package
+ # belongs to the sibling, which claims it itself.
+ members = {
+ d for d in build_workspace_map(repo_path).values() if d != "." and not d.startswith("..")
+ }
+ rels = [PACKAGE_JSON] + [f"{d}/{PACKAGE_JSON}" for d in sorted(members)]
+ for rel in rels:
+ data = _read_json(repo_path / rel)
+ if isinstance(data, dict):
+ out.append((rel, data))
+ return out
+
+
+def _walk_project_manifests(
+ repo_root: Path,
+ index: _ManifestIndex,
+) -> tuple[list[Path], list[Path], list[ComposerManifest]]:
+ """Collect .NET, Maven and first-party composer manifests in one pruned walk."""
+ from xml.etree import ElementTree as ET
+
+ from repowise.core.fs_walk import PRUNED_DIRS, walk_repo
+
+ csprojects: list[Path] = []
+ poms: list[Path] = []
+ composer: list[ComposerManifest] = []
+ # ``packages`` is a .NET package cache convention but also a legitimate
+ # Maven module directory, so it cannot be pruned for the shared walk.
+ prune = PRUNED_DIRS | {
+ "target",
+ "dist",
+ "build",
+ "bin",
+ "obj",
+ ".vs",
+ "TestResults",
+ }
+ for dirpath, _dirnames, filenames in walk_repo(repo_root, prune_dirs=prune):
+ for filename in sorted(filenames):
+ path = dirpath / filename
+ if filename == "pom.xml":
+ poms.append(path)
+ elif filename == COMPOSER_JSON:
+ rel = path.parent.relative_to(repo_root).as_posix()
+ rel = "" if rel == "." else rel
+ if is_package_dir(rel) and (manifest := read_composer(path, rel)) is not None:
+ composer.append(manifest)
+ elif filename.lower().endswith(".csproj"):
+ relative_dirs = path.relative_to(repo_root).parts[:-1]
+ if any(part.lower() in _CSPROJ_SKIP_DIRS_LOWER for part in relative_dirs):
+ continue
+ try:
+ index.trees[path] = ET.parse(path)
+ except (ET.ParseError, OSError):
+ continue
+ csprojects.append(path)
+ # Root first, then by directory: the order a name is first claimed in.
+ composer.sort(key=lambda m: (m.rel_dir != "", m.rel_dir))
+ return csprojects, poms, composer
+
+
+def _assembly_name(tree: Any, csproj: Path) -> str:
+ """The project's assembly name, defaulting to the filename minus extension."""
+ for elem in tree.getroot().iter():
+ tag = elem.tag.split("}", 1)[1] if elem.tag.startswith("{") else elem.tag
+ if tag == "AssemblyName" and elem.text:
+ return elem.text.strip()
+ return csproj.stem
+
+
+def _index_manifests(repo_paths: dict[str, Path]) -> _ManifestIndex:
+ """Read each repo's manifests once, parse each once, build the name maps."""
+ index = _ManifestIndex(repo_paths=repo_paths)
+ # Insertion order matches the previous per-alias rebuild, so a name
+ # defined in two repos still resolves to the last repo in repo_paths.
+ for alias, path in repo_paths.items():
+ found, poms, composer = _walk_project_manifests(path, index)
+ for csproj in found:
+ index.assembly_to_repo[_assembly_name(index.trees[csproj], csproj)] = alias
+ index.by_repo[alias] = found
+ index.poms_by_repo[alias] = poms
+ index.npm_by_repo[alias] = _npm_packages(path)
+ index.composer_by_repo[alias] = composer
+ for rel, data in index.npm_by_repo[alias]:
+ name = data.get("name")
+ if isinstance(name, str) and name:
+ index.claim("npm", name, alias, rel)
+ for manifest in index.composer_by_repo[alias]:
+ if manifest.name:
+ index.claim("composer", manifest.name, alias, _composer_path(manifest))
+ return index
+
+
+def _composer_path(manifest: ComposerManifest) -> str:
+ return f"{manifest.rel_dir}/{COMPOSER_JSON}" if manifest.rel_dir else COMPOSER_JSON
+
+
+# ---------------------------------------------------------------------------
+# Scanners
+# ---------------------------------------------------------------------------
+
+#: What a scanner yields; ``None`` is a reference that matched no sibling.
+_Deps = Iterator["CrossRepoPackageDep | None"]
+
+
+def _resolve_target_repo(
+ relative_ref: str,
+ source_repo_path: Path,
+ repo_paths: dict[str, Path],
+) -> str | None:
+ """Resolve a relative path reference to a repo alias, or None."""
+ try:
+ target_abs = (source_repo_path / relative_ref).resolve()
+ # Most-specific root first makes physically nested selected repos
+ # deterministic. Path.relative_to supplies the separator-aware
+ # containment check that string prefixes (``lib`` vs ``lib-old``) do not.
+ roots = sorted(
+ repo_paths.items(),
+ key=lambda item: len(item[1].parts),
+ reverse=True,
+ )
+ for alias, repo_path in roots:
+ try:
+ target_abs.relative_to(repo_path)
+ except ValueError:
+ continue
+ else:
+ return alias
+ except Exception:
+ # A manifest reference that cannot be resolved to a path is the one
+ # way this returns None without having compared anything, so it is
+ # worth distinguishing from "resolved fine, matched no repo".
+ _log.warning(
+ "Could not resolve manifest reference %r relative to %s",
+ relative_ref,
+ source_repo_path,
+ exc_info=True,
+ )
+ return None
+
+
+def _path_dep(
+ index: _ManifestIndex, alias: str, base: Path, ref: str, manifest: str, kind: str
+) -> CrossRepoPackageDep | None:
+ target = _resolve_target_repo(ref, base, index.repo_paths)
+ if target and target != alias:
+ return CrossRepoPackageDep(
+ source_repo=alias, target_repo=target, source_manifest=manifest, kind=kind
+ )
+ return None
+
+
+def _name_dep(
+ index: _ManifestIndex,
+ alias: str,
+ ecosystem: str,
+ name: str,
+ version: str,
+ manifest: str,
+ kind: str,
+) -> CrossRepoPackageDep | None:
+ owner = index.owner(ecosystem, name)
+ if owner is None or owner[0] == alias:
+ return None
+ return CrossRepoPackageDep(
+ source_repo=alias,
+ target_repo=owner[0],
+ source_manifest=manifest,
+ kind=kind,
+ target_package=name,
+ target_manifest=owner[1],
+ requested_version=version,
+ resolution_basis="unique_workspace_package_name",
+ )
+
+
+def _scan_npm(index: _ManifestIndex, alias: str, repo_path: Path) -> _Deps:
+ """``file:`` paths and workspace globs into a sibling, and deps on a sibling's name."""
+ for rel, data in index.npm_by_repo.get(alias, ()):
+ base = (repo_path / rel).parent
+ for dep_key in _NPM_DEP_SECTIONS:
+ deps = data.get(dep_key, {})
+ if not isinstance(deps, dict):
+ continue
+ for name, version in deps.items():
+ if not isinstance(version, str):
+ continue
+ if version.startswith("file:"):
+ yield _path_dep(index, alias, base, version[5:], rel, "npm_local_path")
+ elif not version.startswith("workspace:"):
+ yield _name_dep(index, alias, "npm", name, version, rel, "npm_package")
+
+ # Workspaces are globs; only those reaching outside the repo can name a sibling.
+ for ws in read_workspaces_field(data):
+ if ".." in ws:
+ yield _path_dep(index, alias, base, ws.rstrip("/*"), rel, "npm_workspace")
+
+
+def _scan_composer(index: _ManifestIndex, alias: str, repo_path: Path) -> _Deps:
+ """Path repositories into a sibling, and requires of a sibling's package name."""
+ for manifest in index.composer_by_repo.get(alias, ()):
+ rel = _composer_path(manifest)
+ base = repo_path / manifest.rel_dir
+ for url in manifest.path_repositories:
+ yield _path_dep(index, alias, base, url, rel, "composer_path")
+ for name, version in manifest.requires.items():
+ yield _name_dep(index, alias, "composer", name, version, rel, "composer_package")
+
+
+def _load_toml(path: Path) -> dict:
+ try:
+ import tomllib
+ except ImportError:
+ import tomli as tomllib # type: ignore[no-redef]
+ try:
+ with open(path, "rb") as f:
+ return tomllib.load(f)
+ except Exception:
+ return {}
+
+
+def _scan_pyproject(index: _ManifestIndex, alias: str, repo_path: Path) -> _Deps:
+ """Poetry and PEP 621 path dependencies."""
+ toml_path = repo_path / "pyproject.toml"
+ if not toml_path.is_file():
+ return
+ data = _load_toml(toml_path)
+ poetry_deps = data.get("tool", {}).get("poetry", {}).get("dependencies", {})
+ specs = list(poetry_deps.values()) if isinstance(poetry_deps, dict) else []
+ # PEP 621 dependencies with path (uncommon but possible via tool configs)
+ for group_key in ("dependencies", "optional-dependencies"):
+ group = data.get("project", {}).get(group_key, {})
+ if isinstance(group, dict):
+ specs.extend(group.values())
+ for spec in specs:
+ if isinstance(spec, dict) and "path" in spec:
+ yield _path_dep(index, alias, repo_path, spec["path"], "pyproject.toml", "pip_path")
+
+
+def _scan_cargo(index: _ManifestIndex, alias: str, repo_path: Path) -> _Deps:
+ """Cargo path dependencies."""
+ cargo_path = repo_path / "Cargo.toml"
+ if not cargo_path.is_file():
+ return
+ data = _load_toml(cargo_path)
+ for section in ("dependencies", "dev-dependencies", "build-dependencies"):
+ for spec in data.get(section, {}).values():
+ if isinstance(spec, dict) and "path" in spec:
+ yield _path_dep(index, alias, repo_path, spec["path"], "Cargo.toml", "cargo_path")
+
+
+def _scan_go_mod(index: _ManifestIndex, alias: str, repo_path: Path) -> _Deps:
+ """``replace`` directives pointing at a sibling repo."""
+ go_mod = repo_path / "go.mod"
+ if not go_mod.is_file():
+ return
+ try:
+ content = go_mod.read_text(encoding="utf-8")
+ except Exception:
+ return
+ in_replace_block = False
+ for line in content.splitlines():
+ stripped = line.strip()
+ if stripped.startswith("replace ("):
+ in_replace_block = True
+ continue
+ if in_replace_block and stripped == ")":
+ in_replace_block = False
+ continue
+ if stripped.startswith("replace ") or in_replace_block:
+ parts = stripped.replace("replace ", "").split("=>")
+ if len(parts) == 2:
+ target_path = parts[1].strip().split()[0] # first token after =>
+ if target_path.startswith("..") or target_path.startswith("./"):
+ yield _path_dep(index, alias, repo_path, target_path, "go.mod", "go_replace")
+
+
+def _scan_csproj(
+ repo_path: Path,
+ repo_paths: dict[str, Path],
+ alias: str,
+ *,
+ csproj_index: _ManifestIndex | None = None,
+) -> list[CrossRepoPackageDep]:
+ """Scan every .csproj for cross-repo references.
+
+ Two patterns are recognised:
+
+ 1. ``<ProjectReference Include="..\\..\\OtherRepo\\Foo.csproj"/>``: a
+ relative path that resolves into a sibling indexed repo. Emits
+ ``kind="dotnet_project_ref"``.
+
+ 2. ``<PackageReference Include="MyOrg.SharedLib"/>`` whose package id
+ matches a sibling repo's ``<AssemblyName>`` or ``.csproj`` filename.
+ Emits ``kind="dotnet_nuget_internal"``: the "internal NuGet
+ feed" pattern enterprise teams use when their shared libs live
+ in a separate repo.
+
+ Skips ``bin``/``obj``/``packages``/``.vs``/``TestResults`` build outputs.
+
+ ``csproj_index`` lets a caller scanning several repos share one walk;
+ omitted, the scan builds its own and behaves exactly as a standalone call.
+ """
+ index = csproj_index if csproj_index is not None else _index_manifests(repo_paths)
+
+ own = index.by_repo.get(alias)
+ if own is None or repo_paths.get(alias) != repo_path:
+ # The index does not describe this exact tree: *alias* is absent from
+ # repo_paths, or the caller passed a repo_path that disagrees with it.
+ # Walk the tree we were actually handed, and leave assembly_to_repo
+ # alone: it is defined by repo_paths, and folding this repo's own
+ # names into it would let them shadow a sibling that legitimately
+ # owns the same assembly name.
+ own, _poms, _composer = _walk_project_manifests(repo_path, index)
+
+ assembly_to_repo = index.assembly_to_repo
+
+ results: list[CrossRepoPackageDep] = []
+
+ for csproj in own:
+ tree = index.trees[csproj]
+ try:
+ rel_manifest = csproj.relative_to(repo_path).as_posix()
+ except ValueError:
+ rel_manifest = csproj.name
+
+ for elem in tree.getroot().iter():
+ tag = elem.tag.split("}", 1)[1] if elem.tag.startswith("{") else elem.tag
+ include = elem.get("Include") if elem.attrib else None
+ if not include:
+ continue
+ if tag == "ProjectReference":
+ rel = include.replace("\\", "/")
+ target = _resolve_target_repo(rel, csproj.parent, repo_paths)
+ if target and target != alias:
+ results.append(
+ CrossRepoPackageDep(
+ source_repo=alias,
+ target_repo=target,
+ source_manifest=rel_manifest,
+ kind="dotnet_project_ref",
+ )
+ )
+ elif tag == "PackageReference":
+ pkg = include.strip()
+ target = assembly_to_repo.get(pkg)
+ if target and target != alias:
+ results.append(
+ CrossRepoPackageDep(
+ source_repo=alias,
+ target_repo=target,
+ source_manifest=rel_manifest,
+ kind="dotnet_nuget_internal",
+ )
+ )
+
+ return results
+
+
+def _scan_dotnet(index: _ManifestIndex, alias: str, repo_path: Path) -> list[CrossRepoPackageDep]:
+ return _scan_csproj(repo_path, index.repo_paths, alias, csproj_index=index)
+
+
+# ---------------------------------------------------------------------------
+# The table
+# ---------------------------------------------------------------------------
+
+Scanner = Callable[[_ManifestIndex, str, Path], Iterable[CrossRepoPackageDep | None]]
+
+
+@dataclass(frozen=True, slots=True)
+class Ecosystem:
+ name: str
+ #: Files whose presence (next to source) marks a directory as a service root.
+ markers: tuple[str, ...]
+ #: Per-repo cross-repo edge scanner. Maven has none here: a coordinate only
+ #: resolves against every repo's reactor at once, so it runs as one
+ #: workspace pass after the table.
+ scan: Scanner | None = None
+
+
+ECOSYSTEMS: tuple[Ecosystem, ...] = (
+ Ecosystem("npm", (PACKAGE_JSON,), _scan_npm),
+ Ecosystem("composer", (COMPOSER_JSON,), _scan_composer),
+ Ecosystem("python", ("pyproject.toml", "requirements.txt"), _scan_pyproject),
+ Ecosystem("cargo", ("Cargo.toml",), _scan_cargo),
+ Ecosystem("go", ("go.mod",), _scan_go_mod),
+ Ecosystem("dotnet", (), _scan_dotnet),
+ Ecosystem("maven", ("pom.xml",)),
+ Ecosystem("gradle", ("build.gradle", "build.gradle.kts")),
+ Ecosystem("mix", ("mix.exs",)),
+ Ecosystem("container", ("Dockerfile",)),
+)
+
+SERVICE_MARKERS: frozenset[str] = frozenset(m for eco in ECOSYSTEMS for m in eco.markers)
+
+
+# ---------------------------------------------------------------------------
+# Detection
+# ---------------------------------------------------------------------------
+
+
+def _dep_key(dep: CrossRepoPackageDep) -> tuple[str, str, str, str, str]:
+ return (dep.source_repo, dep.target_repo, dep.source_manifest, dep.kind, dep.target_package)
+
+
+def detect_package_dependencies(
+ repo_paths: dict[str, Path],
+) -> list[CrossRepoPackageDep]:
+ """Scan all repos for manifest-based cross-repo dependencies."""
+ dependencies, _diagnostics, _total_diagnostics = detect_package_dependencies_with_diagnostics(
+ repo_paths
+ )
+ return dependencies
+
+
+def detect_package_dependencies_with_diagnostics(
+ repo_paths: dict[str, Path],
+) -> tuple[
+ list[CrossRepoPackageDep],
+ list[CrossRepoPackageDiagnostic],
+ int,
+]:
+ """Scan package manifests and retain bounded Maven non-match evidence."""
+
+ results: dict[tuple[str, str, str, str, str], CrossRepoPackageDep] = {}
+ resolved_repo_paths = {alias: path.resolve() for alias, path in repo_paths.items()}
+ index = _index_manifests(resolved_repo_paths)
+
+ for alias, path in resolved_repo_paths.items():
+ for eco in ECOSYSTEMS:
+ if eco.scan is None:
+ continue
+ for dep in eco.scan(index, alias, path):
+ if dep is not None:
+ results.setdefault(_dep_key(dep), dep)
+
+ from .maven_dependencies import detect_maven_dependencies
+
+ maven_links, maven_diagnostics = detect_maven_dependencies(
+ resolved_repo_paths,
+ index.poms_by_repo,
+ )
+ for link in maven_links:
+ dep = CrossRepoPackageDep(
+ source_repo=link.source_repo,
+ target_repo=link.target_repo,
+ source_manifest=link.source_manifest,
+ kind="maven_coordinate",
+ target_package=link.target_package,
+ target_manifest=link.target_manifest,
+ requested_version=link.requested_version,
+ scope=link.scope,
+ resolution_basis=link.resolution_basis,
+ )
+ results.setdefault(_dep_key(dep), dep)
+
+ dependencies = sorted(results.values(), key=_dep_key)
+ diagnostics = sorted(
+ (
+ CrossRepoPackageDiagnostic(
+ repo=item.repo,
+ source_manifest=item.manifest,
+ code=item.code,
+ detail=item.detail,
+ )
+ for item in maven_diagnostics
+ ),
+ key=lambda item: (
+ item.code == "external_coordinate",
+ item.repo,
+ item.source_manifest,
+ item.code,
+ item.detail,
+ ),
+ )
+ total_diagnostics = len(diagnostics)
+ return dependencies, diagnostics[:_MAX_PACKAGE_DIAGNOSTICS], total_diagnostics
diff --git a/tests/integration/test_dotnet_solution.py b/tests/integration/test_dotnet_solution.py
index f29841f05..081ea246e 100644
--- a/tests/integration/test_dotnet_solution.py
+++ b/tests/integration/test_dotnet_solution.py
@@ -122,7 +122,7 @@ class TestSolutionIngestion:
class TestCrossRepoWorkspace:
def test_project_ref_and_internal_nuget_detected(self) -> None:
"""The two-repo .NET workspace produces both kinds of cross-repo deps."""
- from repowise.core.workspace.cross_repo import detect_package_dependencies
+ from repowise.core.workspace.manifests import detect_package_dependencies
repos = {
"api-service": DOTNET_WORKSPACE / "api-service",
diff --git a/tests/unit/dead_code/test_laravel_liveness.py b/tests/unit/dead_code/test_laravel_liveness.py
new file mode 100644
index 000000000..394df7042
--- /dev/null
+++ b/tests/unit/dead_code/test_laravel_liveness.py
@@ -0,0 +1,110 @@
+"""A Laravel app's convention-loaded files are not dead code.
+
+Built through the real traverse -> parse -> graph -> framework-edges path, so
+the test covers the whole chain: the composer reader, the framework facts, the
+Laravel anchor edges and the same-namespace base-class edge.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+from repowise.core.analysis.dead_code import DeadCodeAnalyzer
+from repowise.core.generation.editor_files.tech_stack import detect_tech_stack
+from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder
+
+_FILES = {
+ "routes/api.php": (
+ "<?php\nuse App\\Http\\Controllers\\TicketController;\n"
+ "use Illuminate\\Support\\Facades\\Route;\n"
+ "Route::get('/tickets', [TicketController::class, 'index']);\n"
+ ),
+ "app/Http/Controllers/Controller.php": (
+ "<?php\nnamespace App\\Http\\Controllers;\nabstract class Controller {}\n"
+ ),
+ "app/Http/Controllers/TicketController.php": (
+ "<?php\nnamespace App\\Http\\Controllers;\nuse App\\Models\\Ticket;\n"
+ "class TicketController extends Controller {\n"
+ " public function index() { return Ticket::all(); }\n}\n"
+ ),
+ "app/Models/Ticket.php": "<?php\nnamespace App\\Models;\nclass Ticket {}\n",
+ "app/Providers/AppServiceProvider.php": (
+ "<?php\nnamespace App\\Providers;\nclass AppServiceProvider {}\n"
+ ),
+ "app/Console/Commands/ReleaseHolds.php": (
+ "<?php\nnamespace App\\Console\\Commands;\nclass ReleaseHolds {}\n"
+ ),
+ "app/Jobs/NeverDispatched.php": "<?php\nnamespace App\\Jobs;\nclass NeverDispatched {}\n",
+ "database/factories/TicketFactory.php": (
+ "<?php\nnamespace Database\\Factories;\nclass TicketFactory {}\n"
+ ),
+ "config/ticketing.php": "<?php\nreturn ['hold_minutes' => 10];\n",
+}
+
+
+def _analyze(repo: Path) -> set[tuple[str, str]]:
+ builder = GraphBuilder(repo)
+ parser = ASTParser()
+ source_map: dict[str, bytes] = {}
+ for fi in FileTraverser(repo).traverse():
+ src = Path(fi.abs_path).read_bytes()
+ builder.add_file(parser.parse_file(fi, src))
+ source_map[fi.path] = src
+ builder.set_source_map(source_map)
+ builder.build()
+ builder.add_framework_edges([item.name for item in detect_tech_stack(repo)])
+ report = DeadCodeAnalyzer(
+ builder.graph(), {}, parsed_files=builder._parsed_files, source_map=source_map, repo_root=repo
+ ).analyze({"detect_unused_internals": False})
+ return {(f.kind.value, f.file_path) for f in report.findings}
+
+
+def _write_app(repo: Path, require: dict[str, str]) -> None:
+ (repo / "composer.json").write_text(
+ json.dumps(
+ {
+ "require": require,
+ "autoload": {
+ "psr-4": {"App\\": "app/", "Database\\Factories\\": "database/factories/"}
+ },
+ }
+ ),
+ encoding="utf-8",
+ )
+ for rel, text in _FILES.items():
+ path = repo / rel
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(text, encoding="utf-8")
+
+
+_CONVENTION_FILES = (
+ "routes/api.php",
+ "app/Providers/AppServiceProvider.php",
+ "app/Console/Commands/ReleaseHolds.php",
+ "database/factories/TicketFactory.php",
+ "config/ticketing.php",
+)
+
+
+def test_convention_files_are_live_and_an_undispatched_job_is_not(tmp_path: Path) -> None:
+ _write_app(tmp_path, {"laravel/framework": "^11.0"})
+
+ flagged = {path for _kind, path in _analyze(tmp_path)}
+
+ for live in (*_CONVENTION_FILES, "app/Http/Controllers/Controller.php"):
+ assert live not in flagged, live
+ # Code has to dispatch a job, so one nothing names really is unused.
+ assert "app/Jobs/NeverDispatched.php" in flagged
+
+
+def test_without_the_framework_the_same_files_are_flagged(tmp_path: Path) -> None:
+ # The control: nothing but the Laravel requirement keeps them alive.
+ _write_app(tmp_path, {"php": "^8.2"})
+
+ flagged = {path for _kind, path in _analyze(tmp_path)}
+
+ for dead in _CONVENTION_FILES:
+ assert dead in flagged, dead
+ # The base class is reached through its subclasses either way.
+ assert "app/Http/Controllers/Controller.php" not in flagged
diff --git a/tests/unit/ingestion/test_composer.py b/tests/unit/ingestion/test_composer.py
new file mode 100644
index 000000000..84d1a4eb3
--- /dev/null
+++ b/tests/unit/ingestion/test_composer.py
@@ -0,0 +1,154 @@
+"""The shared composer.json reader and the PHP framework facts it feeds."""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+from repowise.core.ingestion.composer import (
+ find_composer_manifests,
+ find_vendor_manifests,
+ parse_composer,
+ read_composer,
+)
+from repowise.core.ingestion.framework_facts import (
+ LARAVEL,
+ SYMFONY,
+ TYPO3,
+ detect_php_framework,
+)
+
+
+def _write(path: Path, data: dict) -> None:
+ path.mkdir(parents=True, exist_ok=True)
+ (path / "composer.json").write_text(json.dumps(data), encoding="utf-8")
+
+
+class TestParse:
+ def test_reads_every_field_repowise_uses(self) -> None:
+ manifest = parse_composer(
+ {
+ "name": "acme/billing",
+ "type": "library",
+ "require": {"php": "^8.2", "acme/core": "^1.0"},
+ "require-dev": {"phpunit/phpunit": "^11"},
+ "autoload": {
+ "psr-4": {"Billing\\": "src/"},
+ "psr-0": {"Legacy_": "lib/"},
+ "classmap": ["database/"],
+ },
+ "autoload-dev": {"psr-4": {"Billing\\Tests\\": ["tests/", "./spec"]}},
+ "repositories": [
+ {"type": "path", "url": "../core"},
+ {"type": "vcs", "url": "https://example.com/x.git"},
+ ],
+ "extra": {"laravel": {"providers": []}},
+ },
+ "packages/billing",
+ )
+ assert manifest is not None
+ assert manifest.name == "acme/billing"
+ assert manifest.type == "library"
+ assert manifest.requires == {
+ "php": "^8.2",
+ "acme/core": "^1.0",
+ "phpunit/phpunit": "^11",
+ }
+ assert manifest.psr4 == (
+ ("Billing\\", ("packages/billing/src",)),
+ ("Billing\\Tests\\", ("packages/billing/tests", "packages/billing/spec")),
+ )
+ assert manifest.psr0 == (("Legacy_", ("packages/billing/lib",)),)
+ assert manifest.classmap == ("packages/billing/database",)
+ assert manifest.path_repositories == ("../core",)
+ assert manifest.extra == {"laravel": {"providers": []}}
+
+ def test_keyed_repositories_form(self) -> None:
+ manifest = parse_composer({"repositories": {"core": {"type": "path", "url": "../core"}}})
+ assert manifest is not None and manifest.path_repositories == ("../core",)
+
+ def test_root_dir_autoload_is_the_repo_root(self) -> None:
+ manifest = parse_composer({"autoload": {"psr-4": {"App\\": ["", "./"]}}})
+ assert manifest is not None and manifest.psr4 == (("App\\", ("", "")),)
+
+ def test_non_object_and_bad_json(self, tmp_path: Path) -> None:
+ assert parse_composer(["not", "an", "object"]) is None
+ (tmp_path / "composer.json").write_text("{nope", encoding="utf-8")
+ assert read_composer(tmp_path / "composer.json") is None
+ assert read_composer(tmp_path / "missing.json") is None
+
+
+class TestFind:
+ def test_root_first_then_nested_sorted(self, tmp_path: Path) -> None:
+ _write(tmp_path, {"name": "acme/app"})
+ _write(tmp_path / "packages" / "b", {"name": "acme/b"})
+ _write(tmp_path / "packages" / "a", {"name": "acme/a"})
+ assert [m.rel_dir for m in find_composer_manifests(tmp_path)] == [
+ "",
+ "packages/a",
+ "packages/b",
+ ]
+
+ def test_depth_is_bounded(self, tmp_path: Path) -> None:
+ _write(tmp_path / "a" / "b" / "c", {"name": "x/three"})
+ _write(tmp_path / "a" / "b" / "c" / "d", {"name": "x/four"})
+ assert [m.name for m in find_composer_manifests(tmp_path)] == ["x/three"]
+
+ def test_skips_junk_hidden_and_nested_repos(self, tmp_path: Path) -> None:
+ _write(tmp_path / "node_modules" / "evil", {"name": "evil/pkg"})
+ _write(tmp_path / ".cache" / "pkg", {"name": "hidden/pkg"})
+ nested = tmp_path / "checkout"
+ _write(nested, {"name": "other/repo"})
+ (nested / ".git").mkdir()
+ assert find_composer_manifests(tmp_path) == []
+
+ def test_vendor_is_read_separately(self, tmp_path: Path) -> None:
+ _write(tmp_path, {"name": "acme/site"})
+ _write(tmp_path / "vendor" / "acme" / "ext", {"name": "acme/ext"})
+ assert [m.name for m in find_composer_manifests(tmp_path)] == ["acme/site"]
+ assert [m.rel_dir for m in find_vendor_manifests(tmp_path)] == ["vendor/acme/ext"]
+
+ def test_tests_fixtures_and_examples_are_not_packages(self, tmp_path: Path) -> None:
+ _write(tmp_path / "tests" / "Fixtures" / "app", {"name": "x/fixture"})
+ _write(tmp_path / "examples" / "demo", {"name": "x/demo"})
+ _write(tmp_path / "src" / "Testing", {"name": "x/testing"})
+ assert [m.name for m in find_composer_manifests(tmp_path)] == ["x/testing"]
+
+
+class TestFrameworkFacts:
+ def test_precedence_typo3_over_symfony(self) -> None:
+ manifest = parse_composer(
+ {
+ "type": "typo3-cms-extension",
+ "require": {"typo3/cms-core": "^13", "symfony/framework-bundle": "*"},
+ }
+ )
+ assert manifest is not None and detect_php_framework(manifest) is TYPO3
+
+ def test_each_framework_by_its_package(self) -> None:
+ for package, facts in (
+ ("laravel/framework", LARAVEL),
+ ("symfony/symfony", SYMFONY),
+ ("typo3/cms-core", TYPO3),
+ ):
+ manifest = parse_composer({"require-dev": {package: "*"}})
+ assert manifest is not None and detect_php_framework(manifest) is facts
+ plain = parse_composer({"require": {"guzzlehttp/guzzle": "^7"}})
+ assert plain is not None and detect_php_framework(plain) is None
+
+ def test_entry_files_are_scoped_to_the_app_root(self) -> None:
+ paths = {
+ "apps/api/routes/api.php",
+ "apps/api/app/Providers/AppServiceProvider.php",
+ "apps/api/app/Jobs/SendMail.php",
+ "routes/api.php",
+ }
+ assert LARAVEL.entry_files("apps/api", paths) == [
+ "apps/api/app/Providers/AppServiceProvider.php",
+ "apps/api/routes/api.php",
+ ]
+
+ def test_entry_root_is_literal_and_globs_are_case_sensitive(self) -> None:
+ paths = {"ext[old]/ext_localconf.php", "exto/ext_localconf.php", "configuration/TCA/x.php"}
+ assert TYPO3.entry_files("ext[old]", paths) == ["ext[old]/ext_localconf.php"]
+ assert TYPO3.entry_files("", paths) == []
diff --git a/tests/unit/ingestion/test_laravel_framework_edges.py b/tests/unit/ingestion/test_laravel_framework_edges.py
index 9ad30057d..644d05ebf 100644
--- a/tests/unit/ingestion/test_laravel_framework_edges.py
+++ b/tests/unit/ingestion/test_laravel_framework_edges.py
@@ -170,3 +170,66 @@ class TestLaravelGate:
ctx = _ctx(tmp_path, parsed)
count = add_framework_edges(graph, parsed, ctx, tech_stack=[])
assert count == 0
+
+
+class TestLaravelConventionFiles:
+ def _graph(self, repo: Path) -> nx.DiGraph:
+ parsed = _build_parsed(repo)
+ graph = nx.DiGraph()
+ graph.add_nodes_from(parsed)
+ add_framework_edges(graph, parsed, _ctx(repo, parsed), tech_stack=[])
+ return graph
+
+ def test_nested_app_found_from_its_manifest(self, tmp_path: Path) -> None:
+ # No routes/ at the repo root and no stack hint: the nested manifest
+ # requiring the framework is what places the app.
+ app = tmp_path / "apps" / "api"
+ app.mkdir(parents=True)
+ (app / "composer.json").write_text(json.dumps({"require": {"laravel/framework": "^11"}}))
+ for rel in (
+ "routes/api.php",
+ "app/Providers/AppServiceProvider.php",
+ "app/Console/Commands/Prune.php",
+ "database/factories/UserFactory.php",
+ "app/Jobs/SendMail.php",
+ ):
+ (app / rel).parent.mkdir(parents=True, exist_ok=True)
+ (app / rel).write_text("<?php\n")
+ graph = self._graph(tmp_path)
+ anchored = set(graph.successors("framework:laravel"))
+ assert anchored == {
+ "apps/api/routes/api.php",
+ "apps/api/app/Providers/AppServiceProvider.php",
+ "apps/api/app/Console/Commands/Prune.php",
+ "apps/api/database/factories/UserFactory.php",
+ }
+
+ def test_route_files_alone_anchor_no_convention_files(self, tmp_path: Path) -> None:
+ # A PHP app with routes/web.php but no Laravel requirement keeps its
+ # route edges and gets no framework anchors for config/ or providers.
+ (tmp_path / "composer.json").write_text(json.dumps({"require": {"slim/slim": "^4"}}))
+ for rel in ("routes/web.php", "config/app.php", "app/Providers/Boot.php"):
+ (tmp_path / rel).parent.mkdir(parents=True, exist_ok=True)
+ (tmp_path / rel).write_text("<?php\n")
+ graph = self._graph(tmp_path)
+ assert "framework:laravel" not in graph
+
+ def test_route_files_link_controllers_under_a_nested_root(self, tmp_path: Path) -> None:
+ app = tmp_path / "api"
+ (app / "routes").mkdir(parents=True)
+ (app / "composer.json").write_text(
+ json.dumps(
+ {"require": {"laravel/framework": "^11"}, "autoload": {"psr-4": {"App\\": "app/"}}}
+ )
+ )
+ (app / "routes" / "api.php").write_text(
+ "<?php\nuse App\\Http\\Controllers\\PingController;\n"
+ "Route::get('/ping', [PingController::class, 'show']);\n"
+ )
+ ctrl = app / "app" / "Http" / "Controllers"
+ ctrl.mkdir(parents=True)
+ (ctrl / "PingController.php").write_text(
+ "<?php\nnamespace App\\Http\\Controllers;\nclass PingController {}\n"
+ )
+ graph = self._graph(tmp_path)
+ assert graph.has_edge("api/routes/api.php", "api/app/Http/Controllers/PingController.php")
diff --git a/tests/unit/ingestion/test_php_resolver.py b/tests/unit/ingestion/test_php_resolver.py
index 37fba69a8..cb155698b 100644
--- a/tests/unit/ingestion/test_php_resolver.py
+++ b/tests/unit/ingestion/test_php_resolver.py
@@ -10,7 +10,7 @@ import networkx as nx
from repowise.core.ingestion.resolvers.context import ResolverContext
from repowise.core.ingestion.resolvers.php import resolve_php_import
from repowise.core.ingestion.resolvers.php_composer import (
- read_composer_psr4,
+ get_or_build_psr4_map,
resolve_via_psr4,
)
@@ -36,24 +36,40 @@ def _write_composer(repo: Path, autoload: dict, autoload_dev: dict | None = None
(repo / "composer.json").write_text(json.dumps(data))
-class TestComposerParsing:
+class TestPsr4Map:
def test_psr4_single_string_value(self, tmp_path: Path) -> None:
_write_composer(tmp_path, {"App\\": "src/"})
- psr4 = read_composer_psr4(tmp_path)
- assert psr4 == {"App\\": ["src"]}
+ assert get_or_build_psr4_map(_ctx(tmp_path, [])) == {"App\\": ["src"]}
def test_psr4_list_value(self, tmp_path: Path) -> None:
_write_composer(tmp_path, {"App\\": ["src/", "lib/"]})
- psr4 = read_composer_psr4(tmp_path)
- assert psr4 == {"App\\": ["src", "lib"]}
+ assert get_or_build_psr4_map(_ctx(tmp_path, [])) == {"App\\": ["src", "lib"]}
def test_psr4_merges_autoload_dev(self, tmp_path: Path) -> None:
_write_composer(tmp_path, {"App\\": "src/"}, autoload_dev={"Tests\\": "tests/"})
- psr4 = read_composer_psr4(tmp_path)
+ psr4 = get_or_build_psr4_map(_ctx(tmp_path, []))
assert psr4 == {"App\\": ["src"], "Tests\\": ["tests"]}
def test_missing_composer(self, tmp_path: Path) -> None:
- assert read_composer_psr4(tmp_path) == {}
+ assert get_or_build_psr4_map(_ctx(tmp_path, [])) == {}
+
+ def test_nested_manifest_is_rebased_after_the_root(self, tmp_path: Path) -> None:
+ _write_composer(tmp_path, {"App\\": "app/"})
+ pkg = tmp_path / "packages" / "billing"
+ pkg.mkdir(parents=True)
+ _write_composer(pkg, {"Billing\\": "src/", "App\\": "extra/"})
+ psr4 = get_or_build_psr4_map(_ctx(tmp_path, []))
+ assert psr4 == {
+ "App\\": ["app", "packages/billing/extra"],
+ "Billing\\": ["packages/billing/src"],
+ }
+
+ def test_vendor_packages_are_left_out(self, tmp_path: Path) -> None:
+ _write_composer(tmp_path, {"App\\": "app/"})
+ dep = tmp_path / "vendor" / "acme" / "lib"
+ dep.mkdir(parents=True)
+ _write_composer(dep, {"Acme\\": "src/"})
+ assert get_or_build_psr4_map(_ctx(tmp_path, [])) == {"App\\": ["app"]}
class TestPsr4Resolution:
@@ -68,6 +84,27 @@ class TestPsr4Resolution:
ctx = _ctx(tmp_path, ["src/Models/User.php"])
assert resolve_via_psr4("App\\Models\\User", ctx) == "src/Models/User.php"
+ def test_shorter_prefix_is_tried_when_the_longest_misses(self, tmp_path: Path) -> None:
+ # Composer's loader falls back to shorter prefixes; laravel/framework
+ # maps Illuminate\Support\ to four dirs and leaves Str to Illuminate\.
+ _write_composer(
+ tmp_path,
+ {
+ "Illuminate\\": "src/Illuminate/",
+ "Illuminate\\Support\\": ["src/Illuminate/Macroable/"],
+ },
+ )
+ ctx = _ctx(tmp_path, ["src/Illuminate/Support/Str.php", "types/Support/Str.php"])
+ got = resolve_via_psr4("Illuminate\\Support\\Str", ctx)
+ assert got == "src/Illuminate/Support/Str.php"
+
+ def test_nested_package_prefix_resolves(self, tmp_path: Path) -> None:
+ pkg = tmp_path / "packages" / "billing"
+ pkg.mkdir(parents=True)
+ _write_composer(pkg, {"Billing\\": "src/"})
+ ctx = _ctx(tmp_path, ["packages/billing/src/Invoice.php"])
+ assert resolve_via_psr4("Billing\\Invoice", ctx) == "packages/billing/src/Invoice.php"
+
def test_falls_through_when_no_match(self, tmp_path: Path) -> None:
_write_composer(tmp_path, {"App\\": "src/"})
ctx = _ctx(tmp_path, ["src/Foo.php"])
@@ -93,6 +130,43 @@ class TestPhpResolverIntegration:
result = resolve_php_import("Vendor\\Lib\\Missing", "src/Foo.php", ctx)
assert result == "external:Vendor\\Lib\\Missing"
+ def test_dependency_namespace_does_not_stem_match_a_local_file(self, tmp_path: Path) -> None:
+ # Illuminate\Http\Request is a vendor class; the local Request.php
+ # and config/request.php must not claim it.
+ _write_composer(tmp_path, {"App\\": "app/"})
+ ctx = _ctx(tmp_path, ["app/Http/Requests/Request.php", "config/request.php"])
+ result = resolve_php_import("Illuminate\\Http\\Request", "app/Http/Kernel.php", ctx)
+ assert result == "external:Illuminate\\Http\\Request"
+
+ def test_catch_all_prefix_claims_every_namespace(self, tmp_path: Path) -> None:
+ _write_composer(tmp_path, {"": "src/"})
+ ctx = _ctx(tmp_path, ["legacy/Mailer.php"])
+ got = resolve_php_import("Foo\\Mailer", "src/X.php", ctx)
+ assert got == "legacy/Mailer.php"
+
+ def test_classmapped_file_may_still_claim_an_unprefixed_class(self, tmp_path: Path) -> None:
+ (tmp_path / "composer.json").write_text(
+ json.dumps({"autoload": {"psr-4": {"App\\": "app/"}, "classmap": ["legacy/"]}})
+ )
+ ctx = _ctx(tmp_path, ["legacy/Mailer.php", "other/Request.php"])
+ assert resolve_php_import("Old\\Mailer", "app/X.php", ctx) == "legacy/Mailer.php"
+ assert resolve_php_import("Http\\Request", "app/X.php", ctx) == "external:Http\\Request"
+
+ def test_name_fallback_needs_the_whole_file_name(self, tmp_path: Path) -> None:
+ # No composer: the class-name fallback must not take LoginRequest.php
+ # for Request.
+ ctx = _ctx(tmp_path, ["app/Http/Requests/Auth/LoginRequest.php"])
+ got = resolve_php_import("Illuminate\\Http\\Request", "app/X.php", ctx)
+ assert got == "external:Illuminate\\Http\\Request"
+
+ def test_first_party_namespace_keeps_the_stem_fallback(self, tmp_path: Path) -> None:
+ # A claimed prefix whose directory layout PSR-4 cannot reach still
+ # falls back to the class name.
+ _write_composer(tmp_path, {"App\\": "app/"})
+ ctx = _ctx(tmp_path, ["legacy/Mailer.php"])
+ got = resolve_php_import("App\\Services\\Mailer", "app/X.php", ctx)
+ assert got == "legacy/Mailer.php"
+
class TestFileBasedRequires:
def test_importer_relative_require(self, tmp_path: Path) -> None:
@@ -162,3 +236,39 @@ class TestRequireExtraction:
assert modules == [
"/inc/auth.php", "/inc/db.php", "config/app.php", "lib/helpers.php",
]
+
+
+class TestUseDeclarations:
+ @staticmethod
+ def _imports(src: bytes) -> list[tuple[str, list[str]]]:
+ from datetime import datetime
+
+ from repowise.core.ingestion.models import FileInfo
+ from repowise.core.ingestion.parser import ASTParser
+
+ fi = FileInfo(
+ path="a.php", abs_path="/tmp/a.php", language="php",
+ size_bytes=1, git_hash="", last_modified=datetime.now(),
+ is_test=False, is_config=False, is_api_contract=False,
+ is_entry_point=False,
+ )
+ pf = ASTParser().parse_file(fi, src)
+ return [(i.module_path, i.imported_names) for i in pf.imports]
+
+ def test_grouped_use_becomes_one_import_per_class(self) -> None:
+ got = self._imports(b"<?php\nuse App\\Models\\{User, Http\\Post as P};\n")
+ assert got == [
+ ("App\\Models\\User", ["User"]),
+ ("App\\Models\\Http\\Post", ["P"]),
+ ]
+
+ def test_function_and_const_imports_bind_no_class(self) -> None:
+ got = self._imports(
+ b"<?php\nuse function App\\fmt;\nuse const App\\LIMIT;\n"
+ b"use App\\{function b, const C, D};\n"
+ )
+ assert got == [("App\\D", ["D"])]
+
+ def test_comma_separated_use_keeps_every_clause(self) -> None:
+ got = self._imports(b"<?php\nuse \\App\\X, App\\Y as Z;\n")
+ assert got == [("App\\X", ["X"]), ("App\\Y", ["Z"])]
diff --git a/tests/unit/ingestion/test_php_same_namespace.py b/tests/unit/ingestion/test_php_same_namespace.py
new file mode 100644
index 000000000..70a493bf1
--- /dev/null
+++ b/tests/unit/ingestion/test_php_same_namespace.py
@@ -0,0 +1,162 @@
+"""PHP class references no ``use`` names: same namespace and fully qualified."""
+
+from __future__ import annotations
+
+from datetime import datetime
+
+import networkx as nx
+
+from repowise.core.ingestion.cohesion import SAME_NAMESPACE_HINT
+from repowise.core.ingestion.languages.php_same_namespace import (
+ QUALIFIED_NAME_HINT,
+ resolve_php_same_namespace_refs,
+)
+from repowise.core.ingestion.models import FileInfo
+from repowise.core.ingestion.parser import ASTParser
+
+
+def _resolve(files: dict[str, str]) -> nx.DiGraph:
+ parser = ASTParser()
+ parsed = {}
+ for path, text in files.items():
+ fi = FileInfo(
+ path=path,
+ abs_path=path,
+ language="php",
+ size_bytes=len(text),
+ git_hash="",
+ last_modified=datetime.now(),
+ is_test=False,
+ is_config=False,
+ is_api_contract=False,
+ is_entry_point=False,
+ )
+ parsed[path] = parser.parse_file(fi, text.encode())
+ graph = nx.DiGraph()
+ graph.add_nodes_from(files)
+ resolve_php_same_namespace_refs(graph, parsed, files)
+ return graph
+
+
+CONTROLLER = "<?php\nnamespace App\\Http\\Controllers;\nabstract class Controller {}\n"
+
+
+def test_subclass_reaches_its_base_class_with_no_use() -> None:
+ graph = _resolve(
+ {
+ "app/Http/Controllers/Controller.php": CONTROLLER,
+ "app/Http/Controllers/TicketController.php": (
+ "<?php\nnamespace App\\Http\\Controllers;\n"
+ "class TicketController extends Controller {}\n"
+ ),
+ }
+ )
+ edge = graph.get_edge_data(
+ "app/Http/Controllers/TicketController.php", "app/Http/Controllers/Controller.php"
+ )
+ assert edge is not None
+ assert edge["hint_source"] == SAME_NAMESPACE_HINT
+ assert edge["imported_names"] == ["Controller"]
+
+
+def test_a_use_shadows_the_namespace_sibling() -> None:
+ graph = _resolve(
+ {
+ "app/Http/Controllers/Controller.php": CONTROLLER,
+ "app/Http/Controllers/Api.php": (
+ "<?php\nnamespace App\\Http\\Controllers;\n"
+ "use Illuminate\\Routing\\Controller;\n"
+ "class Api extends Controller {}\n"
+ ),
+ }
+ )
+ assert graph.number_of_edges() == 0
+
+
+def test_an_alias_leaves_the_original_name_to_the_namespace() -> None:
+ graph = _resolve(
+ {
+ "app/Http/Controllers/Controller.php": CONTROLLER,
+ "app/Http/Controllers/Api.php": (
+ "<?php\nnamespace App\\Http\\Controllers;\n"
+ "use Illuminate\\Routing\\{Controller as Base};\n"
+ "class Api extends Controller {}\n"
+ ),
+ }
+ )
+ assert graph.has_edge("app/Http/Controllers/Api.php", "app/Http/Controllers/Controller.php")
+
+
+def test_a_segment_of_a_qualified_name_is_not_a_bare_reference() -> None:
+ # Laravel's default User model imports a same-named class from the framework.
+ graph = _resolve(
+ {
+ "app/Models/User.php": "<?php\nnamespace App\\Models;\nclass User {}\n",
+ "app/Models/Admin.php": (
+ "<?php\nnamespace App\\Models;\n"
+ "use Illuminate\\Foundation\\Auth\\User as Authenticatable;\n"
+ "class Admin extends Authenticatable { public \\Other\\User $u; }\n"
+ ),
+ }
+ )
+ assert graph.number_of_edges() == 0
+
+
+def test_a_file_with_several_namespaces_is_left_alone() -> None:
+ graph = _resolve(
+ {
+ "lib/Two.php": (
+ "<?php\nnamespace A;\nclass X {}\nnamespace B;\nclass Y {}\n"
+ ),
+ "lib/UsesY.php": "<?php\nnamespace A;\nclass Z { public Y $y; }\n",
+ }
+ )
+ assert graph.number_of_edges() == 0
+
+
+def test_relative_qualified_names_resolve_from_the_namespace_or_an_alias() -> None:
+ graph = _resolve(
+ {
+ "src/View/Concerns/CompilesViews.php": (
+ "<?php\nnamespace App\\View\\Concerns;\ntrait CompilesViews {}\n"
+ ),
+ "src/View/Compiler.php": (
+ "<?php\nnamespace App\\View;\nclass Compiler { use Concerns\\CompilesViews; }\n"
+ ),
+ "src/Other.php": (
+ "<?php\nnamespace Lib;\nuse App\\View\\Concerns;\n"
+ "class Other { use Concerns\\CompilesViews; }\n"
+ ),
+ }
+ )
+ target = "src/View/Concerns/CompilesViews.php"
+ assert graph.get_edge_data("src/View/Compiler.php", target)["hint_source"] == QUALIFIED_NAME_HINT
+ assert graph.has_edge("src/Other.php", target)
+ assert graph.number_of_edges() == 2
+
+
+def test_other_namespaces_and_ambiguous_names_link_nothing() -> None:
+ graph = _resolve(
+ {
+ "a/Controller.php": CONTROLLER,
+ "b/Other.php": "<?php\nnamespace App\\Other;\nclass Other extends Controller {}\n",
+ "c/Dup1.php": "<?php\nnamespace App\\Dup;\nclass Twin {}\n",
+ "c/Dup2.php": "<?php\nnamespace App\\Dup;\nclass Twin {}\n",
+ "c/User.php": "<?php\nnamespace App\\Dup;\nclass User { public Twin $t; }\n",
+ }
+ )
+ assert graph.number_of_edges() == 0
+
+
+def test_fully_qualified_reference_from_a_global_file() -> None:
+ graph = _resolve(
+ {
+ "app/Actions/Login.php": "<?php\nnamespace App\\Actions;\nclass Login {}\n",
+ "config/auth.php": (
+ "<?php\nreturn ['pipeline' => [\\App\\Actions\\Login::class, \\Vendor\\Other::class]];\n"
+ ),
+ }
+ )
+ edge = graph.get_edge_data("config/auth.php", "app/Actions/Login.php")
+ assert edge is not None and edge["hint_source"] == QUALIFIED_NAME_HINT
+ assert graph.number_of_edges() == 1
diff --git a/tests/unit/ingestion/test_typo3_framework_edges.py b/tests/unit/ingestion/test_typo3_framework_edges.py
index 6683256db..ebca0030f 100644
--- a/tests/unit/ingestion/test_typo3_framework_edges.py
+++ b/tests/unit/ingestion/test_typo3_framework_edges.py
@@ -245,16 +245,17 @@ class TestTypo3JavaScriptModules:
" ],\n"
"];\n"
),
- "Resources/Public/JavaScript/Local.js": "// stub\n",
+ # Same relative path as the foreign reference, so only the
+ # extension-key check keeps the edge out.
+ "Resources/Public/JavaScript/Foo.js": "// stub\n",
},
)
parsed = _build_parsed(tmp_path)
graph = _graph_with_nodes(parsed)
ctx = _ctx(tmp_path, parsed)
add_framework_edges(graph, parsed, ctx, tech_stack=[])
- # No edge to a file under a foreign extension key.
- assert not any(
- target.startswith("EXT:other_ext/") for _, target in graph.edges()
+ assert not graph.has_edge(
+ "Configuration/JavaScriptModules.php", "Resources/Public/JavaScript/Foo.js"
)
diff --git a/tests/unit/workspace/test_cross_repo.py b/tests/unit/workspace/test_cross_repo.py
index fc1fdc686..2e5e61851 100644
--- a/tests/unit/workspace/test_cross_repo.py
+++ b/tests/unit/workspace/test_cross_repo.py
@@ -18,10 +18,10 @@ from repowise.core.workspace.cross_repo import (
_is_noise_path,
_parse_git_log,
detect_cross_repo_co_changes,
- detect_package_dependencies,
load_overlay,
save_overlay,
)
+from repowise.core.workspace.manifests import detect_package_dependencies
# ---------------------------------------------------------------------------
# _parse_git_log
diff --git a/tests/unit/workspace/test_dotnet_workspace.py b/tests/unit/workspace/test_dotnet_workspace.py
index 74dfb2284..4d717e519 100644
--- a/tests/unit/workspace/test_dotnet_workspace.py
+++ b/tests/unit/workspace/test_dotnet_workspace.py
@@ -17,13 +17,13 @@ from textwrap import dedent
import pytest
from repowise.core import fs_walk
-from repowise.core.workspace.cross_repo import (
- _index_csproj_files,
+from repowise.core.workspace.extractors.grpc import GrpcExtractor
+from repowise.core.workspace.extractors.http import HttpExtractor
+from repowise.core.workspace.manifests import (
+ _index_manifests,
_scan_csproj,
detect_package_dependencies,
)
-from repowise.core.workspace.extractors.grpc import GrpcExtractor
-from repowise.core.workspace.extractors.http import HttpExtractor
# ---------------------------------------------------------------------------
# HTTP — ASP.NET
@@ -271,7 +271,7 @@ class TestScanCsproj:
_csproj(assembly_name="Acme.Cached", packages=["Acme.Common"])
)
- index = _index_csproj_files(repos)
+ index = _index_manifests(repos)
deps = detect_package_dependencies(repos)
assert all("packages" not in path.parts for path in index.by_repo["api"])
@@ -289,7 +289,7 @@ class TestScanCsproj:
standalone = _scan_csproj(repos["api"], repos, alias="api")
shared = _scan_csproj(
- repos["api"], repos, alias="api", csproj_index=_index_csproj_files(repos)
+ repos["api"], repos, alias="api", csproj_index=_index_manifests(repos)
)
assert standalone == shared
@@ -325,6 +325,6 @@ class TestScanCsproj:
(other / "src" / "Api").mkdir(parents=True)
(other / "src" / "Api" / "Other.csproj").write_text(_csproj(packages=["Acme.Common"]))
# repo_paths["api"] holds no .csproj at all; the scanned tree does.
- deps = _scan_csproj(other, repos, alias="api", csproj_index=_index_csproj_files(repos))
+ deps = _scan_csproj(other, repos, alias="api", csproj_index=_index_manifests(repos))
assert [d.target_repo for d in deps] == ["shared"]
assert deps[0].source_manifest == "src/Api/Other.csproj"
diff --git a/tests/unit/workspace/test_manifests.py b/tests/unit/workspace/test_manifests.py
new file mode 100644
index 000000000..75ed401bd
--- /dev/null
+++ b/tests/unit/workspace/test_manifests.py
@@ -0,0 +1,155 @@
+"""The ecosystem table: package-name edges for npm and composer, and service markers."""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+from repowise.core.workspace.extractors.service_boundary import detect_service_boundaries
+from repowise.core.workspace.manifests import (
+ ECOSYSTEMS,
+ SERVICE_MARKERS,
+ detect_package_dependencies,
+)
+
+
+def _json(path: Path, data: dict) -> None:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(json.dumps(data), encoding="utf-8")
+
+
+def _deps(repos: dict[str, Path]) -> list[tuple[str, str, str, str, str]]:
+ return [
+ (d.source_repo, d.target_repo, d.source_manifest, d.kind, d.target_package)
+ for d in detect_package_dependencies(repos)
+ ]
+
+
+class TestNpmNames:
+ def test_dependency_on_a_sibling_workspace_member(self, tmp_path: Path) -> None:
+ lib, web = tmp_path / "lib", tmp_path / "web"
+ _json(lib / "package.json", {"name": "lib-root", "workspaces": ["packages/*"]})
+ _json(lib / "packages" / "ui" / "package.json", {"name": "@acme/ui"})
+ _json(web / "package.json", {"dependencies": {"@acme/ui": "^1.0.0", "react": "^18"}})
+
+ deps = detect_package_dependencies({"lib": lib, "web": web})
+
+ assert [(d.source_repo, d.target_repo, d.kind, d.target_package) for d in deps] == [
+ ("web", "lib", "npm_package", "@acme/ui")
+ ]
+ assert deps[0].target_manifest == "packages/ui/package.json"
+ assert deps[0].requested_version == "^1.0.0"
+
+ def test_non_member_copies_publish_nothing(self, tmp_path: Path) -> None:
+ # A vendored compiled copy and a fixture are not packages the repo ships.
+ lib, web = tmp_path / "lib", tmp_path / "web"
+ _json(lib / "package.json", {"name": "lib-root"})
+ _json(lib / "src" / "compiled" / "semver" / "package.json", {"name": "semver"})
+ _json(lib / "test" / "fixtures" / "app" / "package.json", {"name": "fixture-app"})
+ _json(web / "package.json", {"dependencies": {"semver": "^7", "fixture-app": "1"}})
+ assert _deps({"lib": lib, "web": web}) == []
+
+ def test_a_name_two_repos_publish_links_neither(self, tmp_path: Path) -> None:
+ a, b, web = tmp_path / "a", tmp_path / "b", tmp_path / "web"
+ _json(a / "package.json", {"name": "shared"})
+ _json(b / "package.json", {"name": "shared"})
+ _json(web / "package.json", {"dependencies": {"shared": "^1"}})
+ assert _deps({"a": a, "b": b, "web": web}) == []
+
+ def test_pnpm_members_publish_their_names(self, tmp_path: Path) -> None:
+ lib, web = tmp_path / "lib", tmp_path / "web"
+ (lib / "pnpm-workspace.yaml").parent.mkdir(parents=True)
+ (lib / "pnpm-workspace.yaml").write_text("packages:\n - libs/*\n", encoding="utf-8")
+ _json(lib / "package.json", {"name": "lib-root"})
+ _json(lib / "libs" / "money" / "package.json", {"name": "@acme/money"})
+ _json(web / "package.json", {"devDependencies": {"@acme/money": "^1"}})
+ assert _deps({"lib": lib, "web": web}) == [
+ ("web", "lib", "package.json", "npm_package", "@acme/money")
+ ]
+
+ def test_a_member_outside_the_repo_is_the_siblings_not_ours(self, tmp_path: Path) -> None:
+ shared, app, web = tmp_path / "shared", tmp_path / "app", tmp_path / "web"
+ _json(shared / "package.json", {"name": "shared", "dependencies": {"left-pad": "1"}})
+ _json(app / "package.json", {"name": "app", "workspaces": ["../shared"]})
+ _json(web / "package.json", {"dependencies": {"shared": "^1"}})
+ deps = _deps({"shared": shared, "app": app, "web": web})
+ assert ("web", "shared", "package.json", "npm_package", "shared") in deps
+ assert all(source != "app" or kind == "npm_workspace" for source, _t, _m, kind, _p in deps)
+
+ def test_workspace_protocol_is_never_a_sibling(self, tmp_path: Path) -> None:
+ lib, web = tmp_path / "lib", tmp_path / "web"
+ _json(lib / "package.json", {"name": "@acme/ui"})
+ _json(web / "package.json", {"dependencies": {"@acme/ui": "workspace:*"}})
+ assert _deps({"lib": lib, "web": web}) == []
+
+ def test_file_path_from_a_workspace_member_resolves_from_its_own_dir(
+ self, tmp_path: Path
+ ) -> None:
+ lib, web = tmp_path / "lib", tmp_path / "web"
+ _json(lib / "package.json", {"name": "lib"})
+ _json(web / "package.json", {"workspaces": ["apps/*"]})
+ _json(
+ web / "apps" / "site" / "package.json",
+ {"name": "site", "dependencies": {"x": "file:../../../lib"}},
+ )
+ assert _deps({"lib": lib, "web": web}) == [
+ ("web", "lib", "apps/site/package.json", "npm_local_path", "")
+ ]
+
+
+class TestComposer:
+ def test_require_of_a_sibling_package_name(self, tmp_path: Path) -> None:
+ core, api = tmp_path / "core", tmp_path / "api"
+ _json(core / "composer.json", {"name": "acme/core"})
+ _json(api / "composer.json", {"require": {"php": "^8.2", "acme/core": "^2.0"}})
+ assert _deps({"core": core, "api": api}) == [
+ ("api", "core", "composer.json", "composer_package", "acme/core")
+ ]
+
+ def test_path_repository_into_a_sibling(self, tmp_path: Path) -> None:
+ core, api = tmp_path / "core", tmp_path / "api"
+ _json(core / "packages" / "money" / "composer.json", {"name": "acme/money"})
+ _json(
+ api / "composer.json",
+ {"repositories": [{"type": "path", "url": "../core/packages/money"}]},
+ )
+ assert _deps({"core": core, "api": api}) == [
+ ("api", "core", "composer.json", "composer_path", "")
+ ]
+
+ def test_a_name_two_repos_publish_links_neither(self, tmp_path: Path) -> None:
+ a, b, api = tmp_path / "a", tmp_path / "b", tmp_path / "api"
+ _json(a / "composer.json", {"name": "acme/core"})
+ _json(b / "composer.json", {"name": "acme/core"})
+ _json(api / "composer.json", {"require-dev": {"acme/core": "*"}})
+ assert _deps({"a": a, "b": b, "api": api}) == []
+
+ def test_path_repository_inside_the_own_repo_is_not_an_edge(self, tmp_path: Path) -> None:
+ core, api = tmp_path / "core", tmp_path / "api"
+ _json(core / "composer.json", {"name": "acme/core"})
+ _json(api / "packages" / "money" / "composer.json", {"name": "acme/money"})
+ _json(api / "composer.json", {"repositories": [{"type": "path", "url": "packages/money"}]})
+ assert _deps({"core": core, "api": api}) == []
+
+ def test_nested_package_names_count_but_fixtures_do_not(self, tmp_path: Path) -> None:
+ core, api = tmp_path / "core", tmp_path / "api"
+ _json(core / "src" / "Money" / "composer.json", {"name": "acme/money"})
+ _json(core / "tests" / "fixtures" / "composer.json", {"name": "acme/fixture"})
+ _json(api / "composer.json", {"require": {"acme/money": "*", "acme/fixture": "*"}})
+ assert _deps({"core": core, "api": api}) == [
+ ("api", "core", "composer.json", "composer_package", "acme/money")
+ ]
+
+
+class TestServiceMarkers:
+ def test_markers_come_from_the_table(self) -> None:
+ assert {m for eco in ECOSYSTEMS for m in eco.markers} == SERVICE_MARKERS
+ assert "composer.json" in SERVICE_MARKERS
+
+ def test_a_composer_package_is_a_service_boundary(self, tmp_path: Path) -> None:
+ pkg = tmp_path / "services" / "billing"
+ _json(pkg / "composer.json", {"name": "acme/billing"})
+ (pkg / "Invoice.php").write_text("<?php\n", encoding="utf-8")
+ assert [b.service_path for b in detect_service_boundaries(tmp_path)] == [
+ "services/billing"
+ ]
diff --git a/tests/unit/workspace/test_maven_workspace.py b/tests/unit/workspace/test_maven_workspace.py
index 8c437c53d..94e7d3cfe 100644
--- a/tests/unit/workspace/test_maven_workspace.py
+++ b/tests/unit/workspace/test_maven_workspace.py
@@ -8,14 +8,12 @@ from pathlib import Path
import pytest
from repowise.core.ingestion.external_systems.maven_model import load_maven_reactor
-from repowise.core.workspace.cross_repo import (
- CrossRepoOverlay,
+from repowise.core.workspace.cross_repo import CrossRepoOverlay, load_overlay, save_overlay
+from repowise.core.workspace.manifests import (
CrossRepoPackageDep,
CrossRepoPackageDiagnostic,
detect_package_dependencies,
detect_package_dependencies_with_diagnostics,
- load_overlay,
- save_overlay,
)