From 2b20fbe98217a904ff464b6d6cd24d028adb50dc Mon Sep 17 00:00:00 2001 From: elhoim Date: Sun, 30 Aug 2026 23:25:39 +0000 Subject: [PATCH] Fix cache loss when markdown report write fails Finding #10 (markdown-write-loses-cache, high): the markdown write in main() was the only unguarded file I/O and ran before save_cache. A bad --markdown-output path (e.g. a missing parent directory) raised an unhandled FileNotFoundError that escaped main() as a traceback, discarding every fresh cache entry gathered during that run (potentially paid/rate-limited lookups). Fix: persist the cache immediately after the query loop, before the markdown write, and additionally wrap the markdown file write in try/except OSError so a bad output path degrades to an error message and exit code 1 instead of crashing and losing data. --- bin/cli.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/bin/cli.py b/bin/cli.py index 92267b1..50a39be 100644 --- a/bin/cli.py +++ b/bin/cli.py @@ -1069,6 +1069,13 @@ def main() -> int: "response": {"error": str(e)}, }) + if cache_dirty: + try: + save_cache(args.cache_file, cache) + except Exception as e: + print(f"[!] Unable to save cache file {args.cache_file}: {e}", file=sys.stderr) + return 1 + if args.unified_output and not suppress_standard_json_output: print(json.dumps(merged_output, indent=2, sort_keys=True)) @@ -1083,16 +1090,13 @@ def main() -> int: if args.markdown_output == "-": print(markdown_report) else: - with open(args.markdown_output, "w", encoding="utf-8") as f: - f.write(markdown_report) - log(f"Wrote markdown report to {args.markdown_output}") - - if cache_dirty: - try: - save_cache(args.cache_file, cache) - except Exception as e: - print(f"[!] Unable to save cache file {args.cache_file}: {e}", file=sys.stderr) - return 1 + try: + with open(args.markdown_output, "w", encoding="utf-8") as f: + f.write(markdown_report) + log(f"Wrote markdown report to {args.markdown_output}") + except OSError as e: + print(f"[!] Unable to write markdown report to {args.markdown_output}: {e}", file=sys.stderr) + return 1 if not any_queried: log("\nNo module query was executed successfully.")