Engine split step 6: rewrite only files a crate target compiles

compiled_files walks each crate's targets (standard paths and Cargo.toml target paths) through
mod lines, inline modules included. tools/analysis_ui.rs, which no module reaches, keeps main's text.
This commit is contained in:
aiongg committed 2026-11-23 03:38:36 +00:00
1 parent cf80a283f2
commit 62263bc7b8
4 files changed
+52 -32

No files matched your search

+47 -6
View File
@@ -5,16 +5,20 @@ the same offsets. `test_regions` finds a crate's test code: whole files reached
`#[cfg(test)]` (or `#[cfg(all(test, ...))]`) `mod x;`, and the `#[cfg(test)]` items and bare `#[test]`
functions of other files (the test harness compiles `#[test]` functions only in test builds).
`#[cfg(any(test, ...))]` items are not test code: other builds compile them too.
`compiled_files` lists the files some crate target's module tree reaches; the rewrites edit only
those, since a file no module reaches never compiles and CI's unreached-rs guard flags any edit to it.
`edited_definitions` finds a branch's edits to definitions a step moved or changed, for the steps'
`branch` modes.
"""
import os, re, subprocess
import glob, os, re, subprocess
CFG_TEST = re.compile(r"#\[cfg\((?:test|all\(test\b[^\]]*\))\)\]")
MOD_DECL = re.compile(r"((?:#\[[^\]]*\]\s*)*)(?:pub(?:\([a-z]+\))?\s+)?mod\s+([a-z0-9_]+)\s*;")
PATH_ATTR = re.compile(r'#\[path\s*=\s*"([^"]+)"\]')
TEST_ATTR = re.compile(r"#\[test\]")
TEST_FN = re.compile(r"#\[test\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([a-z]+\))?\s+)?(?:async\s+)?fn\s+([a-z0-9_]+)")
INLINE_MOD = re.compile(r"\bmod\s+([a-z0-9_]+)\s*\{")
TARGET_PATH = re.compile(r'^\s*path\s*=\s*"([^"]+\.rs)"', re.M)
IDENT_BEFORE = re.compile(r"[A-Za-z0-9_]")
@@ -93,20 +97,30 @@ def block_end(masked, start):
return i
def module_file(parent, name, attrs):
"""The file of `mod name;` declared in `parent` with attributes `attrs`, or None."""
def module_file(parent, name, attrs, inline=()):
"""The file of `mod name;` declared in `parent` with attributes `attrs`, inside the inline
modules `inline` (outermost first), or None."""
d = os.path.dirname(parent)
if os.path.basename(parent) not in ("lib.rs", "mod.rs", "main.rs"):
d = os.path.join(d, os.path.basename(parent)[:-3])
d = os.path.join(d, *inline)
p = PATH_ATTR.search(attrs)
if p:
if not inline:
d = os.path.dirname(parent)
f = os.path.normpath(os.path.join(d, p.group(1)))
return f if os.path.isfile(f) else None
base = d if os.path.basename(parent) in ("lib.rs", "mod.rs", "main.rs") else os.path.join(d, os.path.basename(parent)[:-3])
for f in (os.path.join(base, name + ".rs"), os.path.join(base, name, "mod.rs")):
for f in (os.path.join(d, name + ".rs"), os.path.join(d, name, "mod.rs")):
if os.path.isfile(f):
return f
return None
def inline_mods(masked, pos):
"""Names of the inline `mod x { .. }` blocks around `pos`, outermost first."""
return [m.group(1) for m in INLINE_MOD.finditer(masked, 0, pos) if block_end(masked, m.start()) > pos]
def test_regions(root_file):
"""{file: None} for a test file, {file: [(start, end)]} with its test items' ranges otherwise."""
regions, todo = {}, [(root_file, False)]
@@ -119,7 +133,7 @@ def test_regions(root_file):
regions[f] = None if is_test else []
for d in MOD_DECL.finditer(m):
attrs = src[d.start(1) : d.end(1)]
child = module_file(f, d.group(2), attrs)
child = module_file(f, d.group(2), attrs, inline_mods(m, d.start()))
if child:
todo.append((child, is_test or bool(CFG_TEST.search(attrs))))
if not is_test:
@@ -127,6 +141,33 @@ def test_regions(root_file):
return regions
def compiled_files(roots):
"""Every .rs file under `roots` that a target root of a crate there reaches through `mod` lines."""
targets = []
for root in roots:
for d, dirs, files in os.walk(root):
dirs[:] = [x for x in dirs if x not in ("target", "vendor")]
if "Cargo.toml" not in files:
continue
targets += [os.path.join(d, f) for f in ("build.rs", "src/lib.rs", "src/main.rs")]
targets += [os.path.join(d, p) for p in TARGET_PATH.findall(open(os.path.join(d, "Cargo.toml")).read())]
for sub in ("src/bin", "tests", "benches", "examples"):
targets += glob.glob(os.path.join(d, sub, "*.rs")) + glob.glob(os.path.join(d, sub, "*", "main.rs"))
seen, todo = set(), [os.path.normpath(t) for t in targets if os.path.isfile(t)]
while todo:
f = todo.pop()
if f in seen:
continue
seen.add(f)
src = open(f).read()
m = mask(src)
for d in MOD_DECL.finditer(m):
child = module_file(f, d.group(2), src[d.start(1) : d.end(1)], inline_mods(m, d.start()))
if child:
todo.append(os.path.normpath(child))
return seen
def item_ranges(masked):
"""The ranges of a non-test file's test items: `#[cfg(test)]` items and bare `#[test]` functions."""
return [(a.start(), block_end(masked, a.end())) for a in (*CFG_TEST.finditer(masked), *TEST_ATTR.finditer(masked))]
+2 -12
View File
@@ -25,7 +25,7 @@ every run.
import argparse, os, re, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from rust_source import edited_definitions, git # noqa: E402
from rust_source import compiled_files, edited_definitions, git # noqa: E402
FIELDS = ["record_tools", "recording", "take", "playing", "playback_stack", "playback_steps", "budget_shared", "batch_depth", "script_depth", "script_steps"]
RENAMED = {"recording": "target"}
@@ -108,18 +108,8 @@ def rewrite(path):
return False
def rust_files(roots):
for root in roots:
for d, _, files in os.walk(root):
if "/target" in d or "/vendor" in d:
continue
for name in files:
if name.endswith(".rs"):
yield os.path.join(d, name)
def split():
n = sum(rewrite(p) for p in rust_files(["crates", "apps"]))
n = sum(rewrite(p) for p in sorted(compiled_files(["crates", "apps"])))
print(f"session_state split: {n} files rewritten")
+2 -12
View File
@@ -41,7 +41,7 @@ Running any mode twice gives the same result. What it cannot do is printed at th
import argparse, json, os, re, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from rust_source import block_end, edited_definitions, git, mask # noqa: E402
from rust_source import block_end, compiled_files, edited_definitions, git, mask # noqa: E402
CORE = "crates/engine-core/src/"
# The engine and the crates that use it; nothing below the engine has a session.
@@ -338,16 +338,6 @@ def rewrite(path):
return False
def rust_files(roots):
for root in roots:
for d, _, files in os.walk(root):
if "/target" in d or "/vendor" in d or d.startswith("vendor"):
continue
for name in files:
if name.endswith(".rs"):
yield os.path.join(d, name)
# ---------------------------------------------------------------- fix mode
ACCESSOR_READ = re.compile(r"\.(" + "|".join(SLOTS) + r")\(\)")
@@ -505,7 +495,7 @@ def main():
os.chdir(git("rev-parse", "--show-toplevel").strip())
MEMBERS = members()
if args.mode == "split":
n = sum(rewrite(p) for p in rust_files(ROOTS))
n = sum(rewrite(p) for p in sorted(compiled_files(ROOTS)))
print(f"slot_access split: {n} files rewritten")
elif args.mode == "branch":
changed = git("diff", "--name-only", f"{args.base}...{args.head}").split()