diff --git a/src/apply_fixes/apply_fixes.py b/src/apply_fixes/apply_fixes.py index 29df64d6..9611f332 100644 --- a/src/apply_fixes/apply_fixes.py +++ b/src/apply_fixes/apply_fixes.py @@ -44,9 +44,12 @@ def get_workspace(main_args: Namespace) -> Path | None: return Path(workspace_root) -def get_bazel_bin_dir(main_args: Namespace, workspace_root: Path) -> Path: - if main_args.bazel_bin: - return Path(main_args.bazel_bin) +def get_reports_search_dir(main_args: Namespace, workspace_root: Path) -> Path: + """ + Unless a dedicated search directory is provided, try to deduce the 'bazel-bin' dir. + """ + if main_args.search_path: + return Path(main_args.search_path) if main_args.use_bazel_info: process = execute_and_capture( @@ -61,12 +64,12 @@ def get_bazel_bin_dir(main_args: Namespace, workspace_root: Path) -> Path: return bazel_bin_link.resolve() -def gather_reports(bazel_bin: Path) -> list[Path]: +def gather_reports(search_path: Path) -> list[Path]: """ We explicitly use os.walk() as it has better performance than Path.glob() in large and deeply nested file trees. """ reports = [] - for root, _, files in walk(bazel_bin): + for root, _, files in walk(search_path): for file in files: if file.endswith("_dwyu_report.json"): reports.append(Path(root) / file) # noqa: PERF401 @@ -223,10 +226,10 @@ def main(args: Namespace) -> int: return 1 logging.debug(f"Workspace: '{workspace}'") - bin_dir = get_bazel_bin_dir(main_args=args, workspace_root=workspace) - logging.debug(f"Bazel-bin directory: '{bin_dir}'") + reports_search_dir = get_reports_search_dir(main_args=args, workspace_root=workspace) + logging.debug(f"Reports search directory: '{reports_search_dir}'") - reports = gather_reports(bin_dir) + reports = gather_reports(reports_search_dir) if not reports: logging.fatal( """ diff --git a/src/apply_fixes/main.py b/src/apply_fixes/main.py index 10c22392..cc71a484 100644 --- a/src/apply_fixes/main.py +++ b/src/apply_fixes/main.py @@ -50,12 +50,13 @@ def cli() -> Namespace: naming scheme or do not point to the Bazel output directory containing the DWYU reports.""", ) parser.add_argument( - "--bazel-bin", + "--search-path", metavar="PATH", help=""" - Path to the bazel-bin directory inside which the DWYU reports are located. + Path to the directory below which the DWYU reports are located. Using this option is recommended if neither the convenience symlinks nor the 'bazel info' command are suited to - deduce the Bazel output directory containing the DWYU report files.""", + deduce the Bazel output directory containing the DWYU report files. Or if you want to search only in a sub tree + of te Bazel output directories.""", ) parser.add_argument( "--fix-unused-deps", diff --git a/test/apply_fixes/tool_cli/test_use_custom_output_base.py b/test/apply_fixes/tool_cli/test_use_custom_output_base.py index a7acc2dd..d98eb5f2 100644 --- a/test/apply_fixes/tool_cli/test_use_custom_output_base.py +++ b/test/apply_fixes/tool_cli/test_use_custom_output_base.py @@ -21,7 +21,7 @@ def windows_compatible(self) -> bool: def execute_test_logic(self) -> Result: with TemporaryDirectory() as output_base: self._create_reports(startup_args=[f"--output_base={output_base}"]) - self._run_automatic_fix(extra_args=["--fix-unused", f"--bazel-bin={output_base}"]) + self._run_automatic_fix(extra_args=["--fix-unused", f"--search-path={output_base}"]) target_deps = self._get_target_attribute(target=self.test_target, attribute="deps") if (expected := set()) != target_deps: # type: ignore[var-annotated]