import json
import os
import subprocess
import sys
TOOLCHAIN = os.environ.get("RUST_TOOLCHAIN_NIGHTLY", "nightly")
def cargo_metadata():
out = subprocess.run(
["cargo", "metadata", "--format-version", "1", "--no-deps"],
capture_output=True, text=True, check=True,
)
return json.loads(out.stdout)
def docsrs_meta(pkg):
return ((pkg.get("metadata") or {}).get("docs") or {}).get("rs") or {}
def docsrs_cmd(pkg, target_dir, override_rustflags=None):
meta = docsrs_meta(pkg)
cmd = ["cargo", f"+{TOOLCHAIN}", "doc", "--no-deps", "-p", pkg["name"]]
if meta.get("all-features"):
cmd.append("--all-features")
else:
if meta.get("no-default-features"):
cmd.append("--no-default-features")
feats = meta.get("features") or []
if feats:
cmd += ["--features", ",".join(feats)]
if meta.get("cargo-args"):
print(f" WARNING: {pkg['name']} has cargo-args (not emulated): {meta['cargo-args']}")
rustflags = " ".join(meta.get("rustc-args") or [])
if override_rustflags is not None:
rustflags = override_rustflags
rustdocflags = " ".join(meta.get("rustdoc-args") or [])
if "--cfg docsrs" not in rustdocflags:
rustdocflags = ("--cfg docsrs " + rustdocflags).strip()
env = dict(os.environ)
env["RUSTFLAGS"] = rustflags
env["RUSTDOCFLAGS"] = rustdocflags
env["CARGO_TARGET_DIR"] = target_dir
return cmd, env, rustflags, rustdocflags
def run_pkg(pkg, target_dir, override_rustflags=None, tag=""):
cmd, env, rf, rdf = docsrs_cmd(pkg, target_dir, override_rustflags)
print(f"==> {pkg['name']}{tag} [RUSTFLAGS='{rf}'] [RUSTDOCFLAGS='{rdf}']", flush=True)
r = subprocess.run(cmd, env=env, capture_output=True, text=True)
if r.returncode != 0:
tail = "\n".join(r.stderr.splitlines()[-60:])
print(f"❌ FAIL: {pkg['name']}{tag}\n{tail}\n", flush=True)
return False
print(f"✅ PASS: {pkg['name']}{tag}", flush=True)
return True
def main():
md = cargo_metadata()
target_dir = os.environ.get(
"CARGO_TARGET_DIR", os.path.join(md["workspace_root"], "target", "docsrs-check")
)
pkgs = [p for p in md["packages"] if p.get("publish") is None]
pkgs.sort(key=lambda p: (bool(docsrs_meta(p).get("rustc-args")), p["name"]))
print(f"publishable crates ({len(pkgs)}): {' '.join(p['name'] for p in pkgs)}\n", flush=True)
control = next(p for p in pkgs if p["name"] == "rama-dns")
print("--- positive control: rama-dns with empty RUSTFLAGS (pre-#1058 conditions) ---", flush=True)
control_failed = not run_pkg(control, target_dir, override_rustflags="", tag=" [control]")
print(
"--- control "
+ ("reproduced the rc.1 failure (harness models docs.rs)" if control_failed
else "DID NOT FAIL: harness untrustworthy, investigate")
+ " ---\n",
flush=True,
)
failures = [pkg["name"] for pkg in pkgs if not run_pkg(pkg, target_dir)]
print("\n================ SUMMARY ================", flush=True)
print(f"control (expected FAIL): {'failed as expected' if control_failed else '❌ UNEXPECTEDLY PASSED'}")
if failures:
print(f"❌ FAILED ({len(failures)}): {' '.join(failures)}")
else:
print(f"✅ all {len(pkgs)} crates pass under docs.rs-equivalent flags")
sys.exit(0 if (control_failed and not failures) else 1)
if __name__ == "__main__":
main()