test(ci): add an import-graph check, green on the current tree (T41 net)
Nothing in this repo verifies that an import actually resolves. That matters
here more than usual: several modules are referenced only through
commented-out import lines, and 15 .nix files sit outside the import graph
entirely, so a stale reference is invisible until someone uncomments it or
nix evaluation happens to touch that file.
The check is deliberately narrow: it matches only two import shapes — a line
whose whole content (after an optional `#`) is a relative path, and
`import ../foo` / single-line `imports = [ ./a ./b ]`. Prose and shell-script
bodies are not matched, and anything containing `${...}` is skipped because it
cannot be resolved statically. Commented-out import lines ARE matched on
purpose: a comment naming a deleted file still misleads the next reader.
Baseline on the current tree: 132 .nix files, 120 import references, all
resolve. Wired in as check #8 of .ci/checks.sh; skips when python3 is absent.
`bash .ci/checks.sh --no-build` → PASS 3 / FAIL 0.
Not pushed.
This commit is contained in:
1 parent
262b966b1e
commit
1b188fbdb5
2 files changed
+125
No files matched your search
@@ -14,6 +14,7 @@
|
||||
# #2 nix flake check (all outputs evaluate)
|
||||
# #7 secrets/ files match .sops.yaml path_regex
|
||||
# #1 no `:latest` in container images (with R1.5 whitelist: 3x-ui is frozen on :latest)
|
||||
# #8 every import reference resolves (no dangling imports) — added 2026-10-24
|
||||
#
|
||||
# Not yet implemented (candidates from analysis-report.md §5):
|
||||
# #3 coredns domains ↔ nginx vhosts bidirectional match
|
||||
@@ -147,10 +148,24 @@ check_no_latest_images() {
|
||||
fi
|
||||
}
|
||||
|
||||
check_import_graph() {
|
||||
echo "Check #8: every import reference resolves (no dangling imports) ..."
|
||||
if ! command -v python3 >/dev/null 2>&1; then
|
||||
echo " SKIP: python3 not in PATH"
|
||||
return 0
|
||||
fi
|
||||
if python3 .ci/tests/import-graph.py . ; then
|
||||
report 0 "import graph"
|
||||
else
|
||||
report 1 "import graph"
|
||||
fi
|
||||
}
|
||||
|
||||
echo "=== nixos flake checks ==="
|
||||
check_nix_flake_check
|
||||
check_sops_path_regex
|
||||
check_no_latest_images
|
||||
check_import_graph
|
||||
echo "==="
|
||||
echo "PASS: $PASS FAIL: $FAIL"
|
||||
if [ "$FAIL" -gt 0 ]; then
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Import-graph integrity check for the nixos flake.
|
||||
|
||||
Fails when a .nix file references another .nix file or directory that does
|
||||
not exist. Two shapes are checked, because both are load-bearing:
|
||||
|
||||
1. import-list entries — a line whose only content (after an optional `#`)
|
||||
is a relative path. This is the dominant style in this repo, and it also
|
||||
covers commented-out imports, which still mislead the next reader when
|
||||
the file they name has been deleted.
|
||||
2. `import ../foo` expressions, and single-line `imports = [ ./a ./b ]`.
|
||||
|
||||
Deliberately conservative: targets containing a Nix interpolation (`${...}`)
|
||||
are skipped because they cannot be resolved statically, and paths that appear
|
||||
inside prose or shell-script bodies are not matched at all.
|
||||
|
||||
Usage: python3 .ci/tests/import-graph.py [repo-root]
|
||||
Exit: 0 = every import reference resolves, 1 = at least one dangling one.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
# A line that is nothing but an (optionally commented) relative path.
|
||||
PURE_PATH = re.compile(
|
||||
r"""^[ \t]*\#?[ \t]*((?:\.\.?/)[A-Za-z0-9_./+-]*[A-Za-z0-9_+-])[ \t]*(?:\#.*)?$"""
|
||||
)
|
||||
# `import ../foo` / `import ./foo.nix`
|
||||
IMPORT_EXPR = re.compile(r"""\bimport[ \t]+((?:\.\.?/)[A-Za-z0-9_./+-]*[A-Za-z0-9_+-])""")
|
||||
# single-line `imports = [ ./a ./b ]`
|
||||
INLINE_LIST = re.compile(r"""\bimports[ \t]*=[ \t]*\[([^\]]*)\]""")
|
||||
INLINE_PATH = re.compile(r"""(?:\.\.?/)[A-Za-z0-9_./+-]*[A-Za-z0-9_+-]""")
|
||||
|
||||
SKIP_DIRS = {".git", "archive", ".temp", ".omo", ".metaagent-director", "result"}
|
||||
|
||||
|
||||
def nix_files(root: str) -> list[str]:
|
||||
out = []
|
||||
for dirpath, dirnames, filenames in os.walk(root):
|
||||
dirnames[:] = sorted(
|
||||
d for d in dirnames if d not in SKIP_DIRS and not d.startswith(".")
|
||||
)
|
||||
for name in filenames:
|
||||
if name.endswith(".nix"):
|
||||
out.append(os.path.join(dirpath, name))
|
||||
return sorted(out)
|
||||
|
||||
|
||||
def references(path: str):
|
||||
"""(line number, target) for every statically resolvable import target."""
|
||||
found = []
|
||||
with open(path, encoding="utf-8", errors="ignore") as fh:
|
||||
for lineno, line in enumerate(fh, 1):
|
||||
if "${" in line:
|
||||
continue # dynamic import, not resolvable statically
|
||||
|
||||
m = PURE_PATH.match(line)
|
||||
if m:
|
||||
found.append((lineno, m.group(1)))
|
||||
continue
|
||||
|
||||
for m in IMPORT_EXPR.finditer(line):
|
||||
found.append((lineno, m.group(1)))
|
||||
|
||||
m = INLINE_LIST.search(line)
|
||||
if m:
|
||||
for p in INLINE_PATH.finditer(m.group(1)):
|
||||
found.append((lineno, p.group(0)))
|
||||
return found
|
||||
|
||||
|
||||
def resolves(base_file: str, target: str) -> bool:
|
||||
cand = os.path.normpath(os.path.join(os.path.dirname(base_file), target))
|
||||
if os.path.exists(cand):
|
||||
return True
|
||||
if not cand.endswith(".nix"):
|
||||
# a directory reference may mean `<dir>/default.nix` or `<name>.nix`
|
||||
if os.path.exists(os.path.join(cand, "default.nix")):
|
||||
return True
|
||||
if os.path.exists(cand + ".nix"):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def main() -> int:
|
||||
root = sys.argv[1] if len(sys.argv) > 1 else "."
|
||||
files = nix_files(root)
|
||||
dangling = []
|
||||
total = 0
|
||||
for path in files:
|
||||
for lineno, target in references(path):
|
||||
total += 1
|
||||
if not resolves(path, target):
|
||||
dangling.append((os.path.relpath(path, root), lineno, target))
|
||||
|
||||
if not dangling:
|
||||
print(f"import-graph: OK ({len(files)} .nix files, {total} import references resolve)")
|
||||
return 0
|
||||
|
||||
print("import-graph: DANGLING IMPORTS", file=sys.stderr)
|
||||
for rel, lineno, target in dangling:
|
||||
print(f" {rel}:{lineno} -> {target}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user