opencode
opencode copied to clipboard
fix: prevent log file deletion when using --log-level DEBUG
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.tsto 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:
- New log file exists before cleanup runs
- Cleanup deletes only old files (keeps last 10)
- Current log file is never deleted
- Expected behavior of keeping last 10 log files is maintained
Testing
Manual testing confirms this resolves issue #6583:
- Run
opencode→ Creates log file2026-01-01T152627.log - Run
opencode --log-level DEBUG→ Previous log is PRESERVED ✅ - New DEBUG log file created successfully
Fixes #6583