mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 18:28:19 +00:00
feat(contributor-scan): a scan says how many source files it left out under its runs directory (3.1, follow-up)
`56c4620` made a scan skip its runs directory when it lies inside the scanned path, and said nothing about it. By the maintainer's decision of 2026-11-25, recorded in the handover, the scan now prints one notice. The decision, as the handover records it: "When source files lie under the runs directory inside the scanned path, the scan prints one notice: line naming the path and the number left out, the way run retention does." The line is `notice: skipped <n> source files under the runs directory <path>` (`source file` at one). `ousast scan` prints it, the `openultrasast.scan` tool returns it among its notices, and `openultrasast.benchmark` returns the notices of the scan it runs. Nothing is added to the manifest or the report. The count follows the scanned tree's own rules, so it is the number the skip took: a source file that the target's root `.gitignore` already keeps out, or that lies under a directory the scan ignores, brings no notice, and a `.gitignore` inside the runs directory does not hide what was left out. To that end `enumerate_source_files` in src/openultrasast/preprocess.py and the new `count_skipped_source_files` share one private predicate, `_is_read_as_source`. The enumeration returns the lists it returned before (the skip test now comes first, so a path under the skipped directory is no longer stat'ed). The count is taken after the preprocess stage, so a tree whose ignore file cannot be read stops the scan inside that stage, as before. It costs one more read of the target's root `.gitignore` and, where there is one, one more `git ls-files`, and only when the runs directory lies inside the scanned path. Nothing is said when no source file is left out, when the runs directory lies outside the scanned path, or when it is the target or lies above it. Tests: the notice tests failed on the code as it was (6 failed) and pass after the change. Four more failed on a first cut that counted by the runs directory's own `.gitignore` and pass on this one: a `.gitignore` inside the runs directory does not hide what the scan left out, nothing is left out where the target already ignores the runs directory, nothing under a directory the scan ignores, and a directory named `.gitignore` costs the scan nothing. The test that a target inside the runs directory is read as it was now also pins that nothing is said; a count that covered that target failed it. The 23 test files around the change pass (241 passed); ruff, mypy, the three gates and compileall pass, and the gates print what they printed before the change. An independent reviewer approved the change in two rounds. Its first round found the count by the wrong rules, which this cut closes. Its second round named two things that are left open and recorded in the handover: were the target's root `.gitignore` to become unreadable between the preprocess stage and the count, the scan would stop with no failed stage recorded in its trace and no manifest; and no test pins that a tracked source file under an ignored runs directory is counted.
This commit is contained in:
@@ -55,7 +55,7 @@ from .pairs import (
|
||||
select_vendored,
|
||||
)
|
||||
from .policy import assert_rules_resolve, load_policy
|
||||
from .preprocess import FileTarget, preprocess_repository, write_preprocess_artifact
|
||||
from .preprocess import FileTarget, count_skipped_source_files, preprocess_repository, write_preprocess_artifact
|
||||
from .profiling import start as start_profiling
|
||||
from .provenance import fingerprint
|
||||
from .rank import rank_obligations, rank_targets, write_rankings
|
||||
@@ -598,7 +598,7 @@ def _run_scan(path: Path, config_path: Path, mode: str, fail_on: str) -> ScanOut
|
||||
)
|
||||
write_harness_config(config=config, path=run.root / "harness.json")
|
||||
runtime.start(mode=mode, target=run.target)
|
||||
policy, ruleset, targets, entry_points = _map_scan_targets(runtime, run, config, mode)
|
||||
policy, ruleset, targets, entry_points, left_out = _map_scan_targets(runtime, run, config, mode)
|
||||
ledger_path = run.target / CALIBRATION_DIR / "false_positive_learnings.json"
|
||||
provenance = runtime.run_stage("provenance", lambda: fingerprint(run.target))
|
||||
rankings = runtime.run_stage("rank", lambda: rank_targets(targets))
|
||||
@@ -731,7 +731,8 @@ def _run_scan(path: Path, config_path: Path, mode: str, fail_on: str) -> ScanOut
|
||||
for item in runtime.state["degradations"]
|
||||
if item.get("reason") == "language_not_covered"
|
||||
)
|
||||
+ prune_notices,
|
||||
+ prune_notices
|
||||
+ _skipped_files_notices(Path(config.runs_dir), left_out),
|
||||
)
|
||||
|
||||
|
||||
@@ -740,21 +741,23 @@ def _map_scan_targets(
|
||||
run: ScanRun,
|
||||
config: ResolvedConfig,
|
||||
mode: str,
|
||||
) -> tuple[dict[str, CwePolicy], tuple[PatternRule, ...], list[FileTarget], list[EntryPointRecord]]:
|
||||
"""The policy and ruleset the scan runs under, and the targets and entry points every later stage reads."""
|
||||
) -> tuple[dict[str, CwePolicy], tuple[PatternRule, ...], list[FileTarget], list[EntryPointRecord], int]:
|
||||
"""The policy and ruleset the scan runs under, the targets and entry points every later stage reads, and how
|
||||
many source files the skip left out."""
|
||||
policy = runtime.run_stage("policy_load", load_policy)
|
||||
rule_ledger_path = run.target / CALIBRATION_DIR / "rule_policy.json"
|
||||
ruleset = runtime.run_stage("ruleset_load", lambda: load_ruleset(DEFAULT_RULESET_DIR, rule_ledger_path))
|
||||
runtime.run_stage("policy_check", lambda: assert_rules_resolve(ruleset, policy))
|
||||
static_hints = runtime.run_stage("static_mapping", lambda: _load_static_hints(config.static_analysis.sarif_paths))
|
||||
write_static_hints(static_hints, run.root / "mapping" / "static_hints.json")
|
||||
runs_dir = Path(config.runs_dir)
|
||||
snapshot, targets = runtime.run_stage(
|
||||
"preprocess",
|
||||
lambda: preprocess_repository(
|
||||
run.target,
|
||||
run.root / "preprocess" / "file_targets.json",
|
||||
static_hints,
|
||||
skip=Path(config.runs_dir),
|
||||
skip=runs_dir,
|
||||
),
|
||||
)
|
||||
# A language nothing here covers is named, never a silent zero (pre-push 17.2, Requirement 9.2).
|
||||
@@ -772,7 +775,17 @@ def _map_scan_targets(
|
||||
write_entry_points(entry_points, run.root / "mapping" / "entry_points.json")
|
||||
targets = attach_reachability_hints(targets, entry_points)
|
||||
write_preprocess_artifact(snapshot, targets, run.root / "preprocess" / "file_targets.json")
|
||||
return policy, ruleset, targets, entry_points
|
||||
# After the preprocess stage, so a tree whose ignore file cannot be read stops the scan inside that stage, as before.
|
||||
left_out = count_skipped_source_files(run.target, runs_dir)
|
||||
return policy, ruleset, targets, entry_points, left_out
|
||||
|
||||
|
||||
def _skipped_files_notices(runs_dir: Path, left_out: int) -> tuple[str, ...]:
|
||||
"""What the scan says about the source files it left out under its runs directory: nothing when there are none."""
|
||||
if not left_out:
|
||||
return ()
|
||||
files = "file" if left_out == 1 else "files"
|
||||
return (f"skipped {left_out} source {files} under the runs directory {runs_dir}",)
|
||||
|
||||
|
||||
def _collect_findings(
|
||||
|
||||
@@ -105,20 +105,24 @@ def enumerate_source_files(root: Path, skip: Path | None = None) -> list[Path]:
|
||||
excluded = _excluded_directory(root, skip)
|
||||
paths: list[Path] = []
|
||||
for path in root.rglob("*"):
|
||||
if (
|
||||
path.is_symlink()
|
||||
or not path.is_file()
|
||||
or _has_ignored_dir(root, path)
|
||||
or _is_ignored(root, path, patterns, tracked)
|
||||
or _is_vendored(root, path)
|
||||
or (excluded is not None and path.is_relative_to(excluded))
|
||||
):
|
||||
if (excluded is not None and path.is_relative_to(excluded)) or not _is_read_as_source(root, path, patterns, tracked):
|
||||
continue
|
||||
if detect_language(path) != "unknown":
|
||||
paths.append(path)
|
||||
paths.append(path)
|
||||
return sorted(paths, key=lambda item: item.relative_to(root).as_posix())
|
||||
|
||||
|
||||
def _is_read_as_source(root: Path, path: Path, patterns: list[_IgnoreRule], tracked: set[str] | None) -> bool:
|
||||
"""Whether `root`'s rules let a scan read `path` as source."""
|
||||
kept = not (
|
||||
path.is_symlink()
|
||||
or not path.is_file()
|
||||
or _has_ignored_dir(root, path)
|
||||
or _is_ignored(root, path, patterns, tracked)
|
||||
or _is_vendored(root, path)
|
||||
)
|
||||
return kept and detect_language(path) != "unknown"
|
||||
|
||||
|
||||
def _excluded_directory(root: Path, skip: Path | None) -> Path | None:
|
||||
"""`skip` as a path under `root`, or None when there is nothing to skip: no `skip` was given, or it is
|
||||
`root` itself or lies above it, which leaves the tree read as it was."""
|
||||
@@ -130,6 +134,20 @@ def _excluded_directory(root: Path, skip: Path | None) -> Path | None:
|
||||
return root / named.relative_to(resolved)
|
||||
|
||||
|
||||
def count_skipped_source_files(root: Path, skip: Path | None) -> int:
|
||||
"""How many source files `skip` leaves out of `enumerate_source_files(root, skip)`, for a scan to say so.
|
||||
|
||||
The files under the skipped directory that `root`'s own rules let a scan read, since those are the ones
|
||||
the skip takes from it; 0 when `skip` keeps nothing out.
|
||||
"""
|
||||
excluded = _excluded_directory(root, skip)
|
||||
if excluded is None:
|
||||
return 0
|
||||
patterns = _load_ignore_patterns(root)
|
||||
tracked = _tracked_files(root) if patterns else None
|
||||
return sum(1 for path in excluded.rglob("*") if _is_read_as_source(root, path, patterns, tracked))
|
||||
|
||||
|
||||
def build_file_target(root: Path, path: Path) -> FileTarget:
|
||||
language = detect_language(path)
|
||||
text = _read_text(path)
|
||||
|
||||
Reference in New Issue
Block a user