opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix: prevent log file deletion when using --log-level DEBUG

Open AungMyoKyaw opened this issue 2 weeks ago • 1 comments

Summary

  • Fixed bug where --log-level DEBUG flag removes existing log files
  • Moved cleanup() call to execute after new log file initialization
  • Ensures current log file is never deleted during reinitialization

Changes

  • Modified packages/opencode/src/util/log.ts to reorder initialization
  • Line 60: Removed premature cleanup() call
  • Line 73: Added cleanup() call after writer setup

Root Cause

The init() function was calling cleanup(Global.Path.log) before setting up new log file. This caused the cleanup logic to delete log files including ones created in a previous run, because the new file path wasn't set yet when cleanup ran.

Fix Details

By moving the cleanup() call to execute after the new log file is created, initialized with fs.truncate(), and the writer is configured:

  1. New log file exists before cleanup runs
  2. Cleanup deletes only old files (keeps last 10)
  3. Current log file is never deleted
  4. Expected behavior of keeping last 10 log files is maintained

Testing

Manual testing confirms this resolves issue #6583:

  1. Run opencode → Creates log file 2026-01-01T152627.log
  2. Run opencode --log-level DEBUG → Previous log is PRESERVED
  3. New DEBUG log file created successfully

Fixes #6583

AungMyoKyaw avatar Jan 02 '26 12:01 AungMyoKyaw