Skip to content

Conversation

@AungMyoKyaw
Copy link

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

The init() function was calling cleanup() before creating new
log file, which caused cleanup logic to delete existing log
files including one from a previous run.

Move cleanup() call to after new log file is created and
truncated to ensure current log is never deleted during
initialization.

Fixes anomalyco#6583
The init() function was calling cleanup() before creating new
log file, which caused cleanup logic to delete existing log
files including one from a previous run.

Move cleanup() call to after new log file is created and
truncated to ensure current log is never deleted during
initialization.

Added test case to verify log preservation across reinitializations.

Fixes anomalyco#6583
@AungMyoKyaw AungMyoKyaw marked this pull request as ready for review January 2, 2026 13:29
Copilot AI review requested due to automatic review settings January 2, 2026 13:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where the --log-level DEBUG flag caused existing log files to be deleted. The fix reorders operations in the Log.init() function to ensure the new log file is created before running the cleanup routine that deletes old log files.

Key Changes:

  • Moved cleanup() call from before log file creation (line 60) to after (line 73)
  • Added comprehensive tests to verify log file preservation and rotation behavior

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 12 comments.

File Description
packages/opencode/src/util/log.ts Relocated cleanup call to execute after new log file initialization, preventing deletion of the current log file
packages/opencode/test/util/log.test.ts Added two test cases to verify log file preservation during reinitialization and proper log rotation behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@djnawara
Copy link

djnawara commented Jan 7, 2026

Hi @AungMyoKyaw! I ran into the same issue and did some investigation. I opened #7245 which addresses this with a similar approach (awaiting cleanup), but also adds .sort() before .slice(0, -10) to fix a related bug where Bun.Glob.scan() returns files in arbitrary filesystem order - meaning the current code deletes random files instead of the oldest ones.

Happy to coordinate if it makes sense to merge efforts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--log-level DEBUG removes log file

2 participants