Skip to content

fix: variable shadowing in log CLI, rename find_oldest_log to find_latest_log#137

Open
fanxing11 wants to merge 1 commit into
ActivityWatch:masterfrom
fanxing11:fix/cli-log-shadowed-path-and-naming
Open

fix: variable shadowing in log CLI, rename find_oldest_log to find_latest_log#137
fanxing11 wants to merge 1 commit into
ActivityWatch:masterfrom
fanxing11:fix/cli-log-shadowed-path-and-naming

Conversation

@fanxing11
Copy link
Copy Markdown
Contributor

Found two small issues in aw_cli:

1. Variable shadowing bug in print_oldest_log

path gets reassigned to the return value of find_oldest_log(), so when no log is found the error message prints None instead of the original directory:

# before: prints "No logfile found in None"
# after:  prints "No logfile found in /home/user/.local/share/activitywatch/log/aw-server-rust"

Fixed by using a separate logfile variable.

2. Misleading function name

find_oldest_log sorts by st_mtime and takes [-1] — that is the newest file, not the oldest. Renamed to find_latest_log to match what the code actually does.

…d_latest_log

Two issues in aw_cli:

1. print_oldest_log() shadows the `path` parameter by reassigning it to
   the return value of find_oldest_log(). When no log file is found, the
   error message prints `None` instead of the original directory path.
   Fix: use a separate `logfile` variable.

2. find_oldest_log() actually returns the most recently modified log file
   (sorts by st_mtime ascending, takes [-1]). Renamed to find_latest_log()
   to match what the code does.
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 27, 2026

Greptile Summary

This PR fixes two small bugs in aw_cli: a variable shadowing issue that caused unhelpful error messages, and a misleading function name that contradicted what the code actually does.

  • Variable shadowing fix: print_latest_log (formerly print_oldest_log) now stores the find_latest_log result in a separate logfile variable instead of overwriting path, so the "No logfile found in {path}" error message now correctly shows the searched directory rather than None.
  • Function rename: find_oldest_logfind_latest_log to accurately reflect that the function sorts by st_mtime and returns logfiles[-1] — the newest file. All references across both files are updated consistently and no stale usages remain.

Confidence Score: 4/5

Both fixes are correct and targeted; the only remaining nit is a stale return type annotation on the renamed function.

The variable shadowing fix is straightforward and correct — the original directory is now preserved for the error message. The rename is accurate: the function returns the most-recently-modified file, so find_latest_log is the right name. The one loose end is that find_latest_log is still annotated -> Path despite having two code paths that return None implicitly; callers like print_latest_log already handle None correctly at runtime, so this is a type-checking gap rather than a functional bug.

aw_cli/log.py — the return type annotation on find_latest_log should be Optional[Path]

Important Files Changed

Filename Overview
aw_cli/main.py Fixes variable shadowing bug in print_latest_log (renamed from print_oldest_log): path argument is no longer overwritten with the find_latest_log result, so the "No logfile found in {path}" message now prints the original directory correctly. All call sites updated consistently.
aw_cli/log.py Renames find_oldest_log to find_latest_log, correctly reflecting that the function returns the file with the highest mtime (newest, not oldest). Return type annotation -> Path is inaccurate — the function has two implicit None return paths and should be annotated -> Optional[Path].

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[aw logs command] --> B{module_name provided?}
    B -- Yes --> C[print_latest_log\nlogdir / module_name]
    B -- No --> D[Iterate sorted subdirs]
    D --> E[print_latest_log\nsubdir]
    C --> F[find_latest_log\npath, testing]
    E --> F
    F --> G{path.is_dir?}
    G -- No --> H[return None]
    G -- Yes --> I[Filter .log files\nby testing flag]
    I --> J{logfiles empty?}
    J -- Yes --> H
    J -- No --> K[Sort by st_mtime\ntake last = newest]
    K --> L[return logfile]
    H --> M{logfile is None?}
    L --> M
    M -- Yes --> N[print: No logfile found in path]
    M -- No --> O[print_log logfile]
Loading

Reviews (1): Last reviewed commit: "fix: correct variable shadowing in print..." | Re-trigger Greptile

Comment thread aw_cli/log.py


def find_oldest_log(path: Path, testing=False) -> Path:
def find_latest_log(path: Path, testing=False) -> Path:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 The return type annotation says Path but the function can return None via two implicit returns (when path is not a directory or when logfiles is empty). Since the PR is already touching the function signature, this is a good moment to fix the annotation to Optional[Path].

Suggested change
def find_latest_log(path: Path, testing=False) -> Path:
def find_latest_log(path: Path, testing=False) -> Optional[Path]:

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.

2 participants