[PR #6641] fix: prevent log file deletion when using --log-level DEBUG #12015

Open
opened 2026-02-16 18:16:57 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6641

State: open
Merged: No


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

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6641 **State:** open **Merged:** No --- ## 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
yindo added the pull-request label 2026-02-16 18:16:57 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12015