Skip to content

Commit a92269b

Browse files
dandydarshanDarshan M Uclaude
authored
Don't analyse CodeWiki's own output in the docs folder as repo code (#15)
* Don't analyse CodeWiki's own output in the docs folder as repo code `generate` copies validate_module_tree_ownership.py into the docs folder, and the next run parsed it as the user's code: about 17 extra functions in the graph (34 of 38 components in a small test repo), which could even get their own docs page. Caches under docs/temp were in scope too. The dependency analysis now skips <docs>/temp and <docs>/<validator> when the docs folder is inside the repo. Only those: the docs folder may hold the user's own files, and the script's real source elsewhere is kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CXRqeijND7XPq2srGHndRd * Use a stand-in helper script in the analysis-scope test The wheel jobs run the tests from a copy outside the repo, where tests/../codewiki/templates/... does not exist (FileNotFoundError on Linux and Windows). The test is about which paths are skipped, so it now writes a small script under the helper's name instead of copying it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CXRqeijND7XPq2srGHndRd --------- Co-authored-by: Darshan M U <darshan_mu@yahoo.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a166cfb commit a92269b

4 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎codewiki/cli/html_generator.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
from codewiki.cli.utils.errors import FileSystemError
1212
from codewiki.cli.utils.fs import safe_write, safe_read
1313
from codewiki.src.be.search_index import build_search_index
14-
from codewiki.src.config import DEFAULT_QA_PORT, QA_PORT_COUNT, SEARCH_INDEX_FILENAME
14+
from codewiki.src.config import (
15+
DEFAULT_QA_PORT,
16+
MODULE_TREE_OWNERSHIP_VALIDATOR,
17+
QA_PORT_COUNT,
18+
SEARCH_INDEX_FILENAME,
19+
)
1520
from codewiki.src.language import language_tag
1621

1722
logger = logging.getLogger(__name__)
1823

1924
DOCS_TEMPLATES_DIR = Path(__file__).parent.parent / "templates" / "docs"
20-
MODULE_TREE_OWNERSHIP_VALIDATOR = "validate_module_tree_ownership.py"
2125

2226

2327
class HTMLGenerator:

‎codewiki/src/be/dependency_analyzer/dependency_graphs_builder.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
build_graph_from_components,
1313
get_leaf_nodes,
1414
)
15-
from codewiki.src.config import Config
15+
from codewiki.src.config import MODULE_TREE_OWNERSHIP_VALIDATOR, Config
1616
from codewiki.src.utils import file_manager
1717

1818
logger = logging.getLogger(__name__)
@@ -24,6 +24,22 @@ class DependencyGraphBuilder:
2424
def __init__(self, config: Config):
2525
self.config = config
2626

27+
def _own_output_patterns(self) -> list[str]:
28+
"""Repo-relative patterns for what CodeWiki itself writes into the docs folder.
29+
30+
Re-analysing them on the next run added the helper script's ~17 functions (and the
31+
caches under temp/) to the user's code, and could even give them a page. Only these
32+
are skipped: the docs folder may also hold the user's own files.
33+
"""
34+
docs_dir = getattr(self.config, "docs_dir", None)
35+
if not docs_dir:
36+
return []
37+
rel = os.path.relpath(os.path.abspath(docs_dir), os.path.abspath(self.config.repo_path))
38+
if rel == "." or rel.startswith(".."):
39+
return [] # docs written to the repo root or outside it
40+
rel = rel.replace(os.sep, "/")
41+
return [f"{rel}/temp", f"{rel}/{MODULE_TREE_OWNERSHIP_VALIDATOR}"]
42+
2743
def build_dependency_graph(self) -> tuple[dict[str, Any], list[str]]:
2844
"""
2945
Build and save dependency graph, returning components and leaf nodes.
@@ -61,6 +77,7 @@ def build_dependency_graph(self) -> tuple[dict[str, Any], list[str]]:
6177
# Get custom include/exclude patterns from config
6278
include_patterns = self.config.include_patterns if self.config.include_patterns else None
6379
exclude_patterns = self.config.exclude_patterns if self.config.exclude_patterns else None
80+
exclude_patterns = (exclude_patterns or []) + self._own_output_patterns() or None
6481

6582
artifact_options = ArtifactOptions(
6683
enabled=getattr(self.config, "artifacts_enabled", True),

‎codewiki/src/config.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
MODULE_TREE_FILENAME = "module_tree.json"
1818
OVERVIEW_FILENAME = "overview.md"
1919
SEARCH_INDEX_FILENAME = "search_index.json"
20+
# Helper script `generate` copies into the docs folder (generated output, never analysed)
21+
MODULE_TREE_OWNERSHIP_VALIDATOR = "validate_module_tree_ownership.py"
2022
# `codewiki serve` listens on the first free port of DEFAULT_QA_PORT..+QA_PORT_COUNT-1 (one per
2123
# repo served at once); the docs site looks for its own repo's server on those ports.
2224
DEFAULT_QA_PORT = 8765

‎tests/test_updater_graph_store.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,45 @@ def test_saved_graph_holds_no_absolute_paths(tmp_path):
302302

303303
save_graph(components, str(tmp_path / "copy.json")) # the updater's writer too
304304
assert str(tmp_path) not in (tmp_path / "copy.json").read_text(encoding="utf-8")
305+
306+
307+
def test_codewikis_own_output_is_not_analysed_as_repo_code(tmp_path):
308+
"""A second run used to add the copied helper script (~17 functions) to the user's code."""
309+
310+
from codewiki.src.be.dependency_analyzer import DependencyGraphBuilder
311+
from codewiki.src.config import MODULE_TREE_OWNERSHIP_VALIDATOR, Config
312+
313+
def analysed_files(repo, docs_dir):
314+
config = Config(
315+
repo_path=str(repo),
316+
output_dir=str(tmp_path / "out"),
317+
dependency_graph_dir=str(tmp_path / "out" / "graphs"),
318+
docs_dir=str(docs_dir),
319+
max_depth=2,
320+
llm_base_url="x",
321+
llm_api_key="x",
322+
main_model="x",
323+
cluster_model="x",
324+
)
325+
components, _ = DependencyGraphBuilder(config).build_dependency_graph()
326+
return {n.relative_path for n in components.values()}
327+
328+
repo = tmp_path / "repo"
329+
(repo / "pkg").mkdir(parents=True)
330+
(repo / "pkg" / "a.py").write_text("def f():\n return 1\n")
331+
(repo / "docs" / "temp").mkdir(parents=True)
332+
# A stand-in with the helper's name: the tests may run from a copy outside the repo.
333+
script = "def check_tree(tree):\n return tree\n\n\ndef main():\n return check_tree({})\n"
334+
(repo / "docs" / MODULE_TREE_OWNERSHIP_VALIDATOR).write_text(script) # as `generate` copies it
335+
(repo / "pkg" / MODULE_TREE_OWNERSHIP_VALIDATOR).write_text(script) # a real source copy
336+
(repo / "docs" / "temp" / "cache.py").write_text("def cached():\n return 1\n")
337+
(repo / "docs" / "conf.py").write_text("def setup(app):\n return app\n") # the user's own
338+
339+
files = analysed_files(repo, repo / "docs")
340+
assert "pkg/a.py" in files and "docs/conf.py" in files
341+
assert f"pkg/{MODULE_TREE_OWNERSHIP_VALIDATOR}" in files
342+
assert f"docs/{MODULE_TREE_OWNERSHIP_VALIDATOR}" not in files
343+
assert not any(f.startswith("docs/temp/") for f in files)
344+
345+
files = analysed_files(repo, repo) # docs written to the repo root: exclude nothing
346+
assert "pkg/a.py" in files and "docs/conf.py" in files

0 commit comments

Comments
 (0)