Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(ingestion): read every module of a Groovy settings.gradle include (#3626)
Susheonnnc554f297327d
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py b/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.pyindex e5b76343b..9e2663146 100644--- a/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py+++ b/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py@@ -33,9 +33,23 @@ log = structlog.get_logger(__name__)# Regex patterns# ----------------------------------------------------------------------------_INCLUDE_RE = re.compile(r'include\s*\(\s*((?:"[^"]+"\s*,?\s*)+)\)')+# One quoted module name, in either quote style: 'core' or "core".+_QUOTED = r"""(?:'[^'\n]+'|"[^"\n]+")"""+# A whole ``include`` statement, in any of the forms Gradle accepts:+# include("core", "api") (Kotlin DSL / Groovy, parenthesised)+# include('core', 'api') (Groovy, parenthesised, single quotes)+# include 'core', 'api', 'web' (Groovy, space form)+# include 'core',+# 'api' (Groovy, list continued over lines)+# ``\b`` after ``include`` keeps ``includeBuild`` out. Comments are stripped+# before matching, so one between two items does not end the list.+_INCLUDE_RE = re.compile(+ rf"\binclude\b\s*(?:\(\s*(?P<paren>[^)]*)\)|(?P<bare>{_QUOTED}(?:\s*,\s*{_QUOTED})*))"+)+# Each quoted name inside a matched ``include`` statement (group 1 or 2).+_INCLUDE_MODULE_RE = re.compile(r"""'([^'\n]+)'|"([^"\n]+)\"""")+# Double-quoted items of a ``srcDirs(...)`` list._INCLUDE_ITEM_RE = re.compile(r'"([^"]+)"')-_INCLUDE_LINE_RE = re.compile(r'include\s+([\'"])([^\'"]+)\1')_INCLUDE_BUILD_RE = re.compile(r"""includeBuild\s*\(?\s*(["'])([^"']+)\1""")_PROJECT_DIR_RE = re.compile(r'project\s*\(\s*"([^"]+)"\s*\)\s*\.\s*projectDir\s*=\s*file\s*\(\s*"([^"]+)"\s*\)'@@ -147,12 +161,18 @@ def parse_settings(settings_file: Path) -> tuple[list[str], dict[str, str]]:except Exception:return [], {}+ # Drop comments first: a comment between two items would otherwise end+ # the list, and a commented-out include would still register.+ include_text = _GRADLE_BLOCK_COMMENT_RE.sub(+ lambda m: "\n" * m.group(0).count("\n"), text+ )+ include_text = _GRADLE_LINE_COMMENT_RE.sub("", include_text)+modules: list[str] = []- for match in _INCLUDE_RE.finditer(text):- for item in _INCLUDE_ITEM_RE.findall(match.group(1)):- modules.append(item.lstrip(":"))- for match in _INCLUDE_LINE_RE.finditer(text):- modules.append(match.group(2).lstrip(":"))+ for match in _INCLUDE_RE.finditer(include_text):+ items = match.group("paren") if match.group("paren") is not None else match.group("bare")+ for single, double in _INCLUDE_MODULE_RE.findall(items):+ modules.append((single or double).lstrip(":"))# Dedupe preserving orderseen: set[str] = set()diff --git a/tests/unit/ingestion/test_jvm_gradle.py b/tests/unit/ingestion/test_jvm_gradle.pyindex 814bfb402..ac6685a45 100644--- a/tests/unit/ingestion/test_jvm_gradle.py+++ b/tests/unit/ingestion/test_jvm_gradle.py@@ -10,6 +10,7 @@ import pytestfrom repowise.core.ingestion.resolvers.context import ResolverContextfrom repowise.core.ingestion.resolvers.jvm_gradle import (build_jvm_gradle_index,+ parse_settings,)@@ -256,3 +257,84 @@ class TestJvmGradleIndex:assert is_publishing_gradle_project(JvmGradleProject(id="lib_comment", root_dir="lib_comment", plugins=lib_comment_plugins))+++class TestParseSettingsInclude:+ """``parse_settings`` reads every module of an ``include`` statement (#3605)."""++ @pytest.mark.parametrize(+ ("text", "expected"),+ [+ # Groovy space form, several modules on one line.+ ("include 'core', 'api', 'web'\n", ["core", "api", "web"]),+ # Groovy space form, list continued on the next line.+ ("include 'core',\n 'api'\n", ["core", "api"]),+ # Groovy parenthesised form with single quotes.+ ("include('core', 'api')\n", ["core", "api"]),+ # Kotlin DSL form (already worked; must keep working).+ ('include("core", "api")\n', ["core", "api"]),+ ],+ )+ def test_reads_every_module(self, tmp_path: Path, text: str, expected: list[str]) -> None:+ settings = tmp_path / "settings.gradle"+ settings.write_text(text)++ modules, _ = parse_settings(settings)+ assert modules == expected++ def test_mixed_quote_styles_and_leading_colon(self, tmp_path: Path) -> None:+ settings = tmp_path / "settings.gradle"+ settings.write_text("include ':app', \":lib:core\", 'web'\n")++ modules, _ = parse_settings(settings)+ assert modules == ["app", "lib:core", "web"]++ def test_include_build_is_not_a_module(self, tmp_path: Path) -> None:+ settings = tmp_path / "settings.gradle"+ settings.write_text("includeBuild(\"build-logic\")\ninclude 'a'\ninclude 'b', 'c'\n")++ modules, _ = parse_settings(settings)+ assert modules == ["a", "b", "c"]++ @pytest.mark.parametrize(+ "text",+ [+ # Line comment after a trailing comma, list continued on the next line.+ "include 'core', // the core module\n 'api'\n",+ # Block comment between two items.+ "include 'core', /* the API */ 'api'\n",+ # Block comment spanning lines inside a parenthesised list.+ 'include(\n "core", /* the\n API */\n "api"\n)\n',+ ],+ )+ def test_comment_between_items_keeps_the_list(self, tmp_path: Path, text: str) -> None:+ settings = tmp_path / "settings.gradle"+ settings.write_text(text)++ modules, _ = parse_settings(settings)+ assert modules == ["core", "api"]++ def test_commented_out_include_is_not_a_module(self, tmp_path: Path) -> None:+ settings = tmp_path / "settings.gradle"+ settings.write_text("include 'core'\n// include 'legacy'\n/* include 'old' */\n")++ modules, _ = parse_settings(settings)+ assert modules == ["core"]++ def test_project_dir_override_applies_to_multi_name_include(self, tmp_path: Path) -> None:+ (tmp_path / "settings.gradle").write_text(+ "include 'core', 'api'\n"+ 'project(":api").projectDir = file("modules/api")\n'+ )+ (tmp_path / "build.gradle").write_text("// root\n")+ _make_java_class(tmp_path, "core", "com.example.core", "Core")+ api_path = _make_java_class(tmp_path, "modules/api", "com.example.api", "Api")++ modules, overrides = parse_settings(tmp_path / "settings.gradle")+ assert modules == ["core", "api"]+ assert overrides == {"api": "modules/api"}++ index = build_jvm_gradle_index(tmp_path)+ assert "core" in index.modules+ assert "api" in index.modules+ assert api_path in index.lookup_class("com.example.api.Api")