Skip to content

Commit 527b129

Browse files
zzstoatzzclaude
andauthored
fix: exclude empty modules from nav in --include-inheritance path (#43)
The --include-inheritance code path ignored generate_mdx's return value and always appended the module to generated_modules, so empty modules were skipped on disk but still emitted into navigation — producing dead links (e.g. Prefect's regenerated docs.json pointed at deleted leaf-module pages). Capture the return value and skip empty modules from generated_modules in this path too, matching the parallel worker path fixed in 0.2.40. Adds an end-to-end regression test (parametrized over the default and --include-inheritance paths) asserting empty modules appear in neither the generated files nor the navigation. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 1f9da00 commit 527b129

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/mdxify/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def main():
419419
else:
420420
output_file = args.output_dir / f"{module_name.replace('.', '-')}.{ext}"
421421

422-
generate_mdx(
422+
wrote = generate_mdx(
423423
module_info,
424424
output_file,
425425
repo_url=repo_url,
@@ -430,6 +430,13 @@ def main():
430430
source_prefix=args.source_prefix,
431431
)
432432

433+
if not wrote:
434+
# Empty module: no page generated, so keep it out of
435+
# navigation as well.
436+
if args.verbose:
437+
print(f"Skipping {module_name} (empty module)")
438+
continue
439+
433440
generated_modules.append(module_name)
434441
if args.verbose:
435442
print(f"Processing {module_name}... done (with inheritance)")

tests/test_end_to_end.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,66 @@ def format_output(text: str, width: int = 80) -> str:
5454
return text[:width]
5555
'''))
5656

57+
# Create an empty leaf module (only private/internal content, no public API)
58+
(pkg_dir / "constants.py").write_text(dedent('''
59+
_PRIVATE = 1
60+
'''))
61+
5762
# Add the test package to Python path
5863
sys.path.insert(0, str(tmp_path))
5964
yield pkg_dir
6065
sys.path.remove(str(tmp_path))
6166

6267

68+
def _pages_to_strings(pages):
69+
"""Flatten a Mintlify pages list (strings and {group, pages} dicts) to strings."""
70+
out = []
71+
for page in pages:
72+
if isinstance(page, dict):
73+
out.extend(_pages_to_strings(page.get("pages", [])))
74+
else:
75+
out.append(str(page))
76+
return out
77+
78+
79+
@pytest.mark.parametrize("extra_args", [[], ["--include-inheritance"]])
80+
def test_empty_module_excluded_from_files_and_nav(test_package, tmp_path, extra_args):
81+
"""Empty modules must not be generated as files OR left in navigation.
82+
83+
Regression test for empty leaf modules surviving in docs.json — covers both
84+
the parallel worker path and the --include-inheritance path.
85+
"""
86+
docs_dir = tmp_path / "docs"
87+
docs_dir.mkdir()
88+
docs_json = docs_dir / "docs.json"
89+
docs_json.write_text(json.dumps({
90+
"navigation": {
91+
"anchors": [
92+
{"anchor": "SDK Reference", "pages": [{"$mdxify": "generated"}]}
93+
]
94+
}
95+
}, indent=2))
96+
97+
result = subprocess.run(
98+
[sys.executable, "-m", "mdxify", "--all", "--root-module", "mypkg",
99+
"--output-dir", str(docs_dir / "python-sdk")] + extra_args,
100+
cwd=tmp_path,
101+
capture_output=True,
102+
text=True,
103+
)
104+
assert result.returncode == 0, f"mdxify failed: {result.stderr}"
105+
106+
# No file generated for the empty module.
107+
assert not (docs_dir / "python-sdk" / "mypkg-constants.mdx").exists()
108+
109+
# And no dangling navigation entry pointing at it.
110+
pages = json.loads(docs_json.read_text())["navigation"]["anchors"][0]["pages"]
111+
flat = _pages_to_strings(pages)
112+
assert not any("mypkg-constants" in p for p in flat), flat
113+
# Sanity: the non-empty module is present.
114+
assert any("mypkg-core" in p for p in flat), flat
115+
116+
63117
def test_cli_default_output_directory(test_package, tmp_path):
64118
"""Test that CLI uses docs/python-sdk as default output directory."""
65119
# Run mdxify on the test package

0 commit comments

Comments
 (0)