Skip to content

fix: report the real error when the log target cannot be opened - #670

Open
l1a wants to merge 1 commit into
creativeprojects:masterfrom
l1a:fix/log-target-error-upstream
Open

fix: report the real error when the log target cannot be opened#670
l1a wants to merge 1 commit into
creativeprojects:masterfrom
l1a:fix/log-target-error-upstream

Conversation

@l1a

@l1a l1a commented Jul 30, 2026

Copy link
Copy Markdown

Problem

When a log target cannot be opened, the error message discards the actual reason:

cannot open log target: %!s(<nil>)

I hit this with a scheduled profile whose log pointed into a directory that did not exist. The message gave me nothing to go on, and the real cause (no such file or directory) was never printed.

Cause

In main.go, err is declared in the if statement's init:

if closer, options, err := setupTargetLogger(flags, terminal, logTarget, commandOutput); err == nil {
    ...
    return
}
// fallback to a console logger
setupConsoleLogger(flags)
clog.Errorf("cannot open log target: %s", err)

Go scopes that err to the if statement, so it is out of scope by the time clog.Errorf runs. The call therefore resolves to main()'s outer var err error (main.go:39), which is still nil at that point — hence %!s(<nil>). It compiles precisely because that outer variable exists.

Fix

Hoist the call out of the if init so the error is in scope where it is logged. Two lines, no behaviour change beyond the message.

Verification

Same command, before and after:

# before
$ resticprofile --log /nonexistent-dir/test.log -n profile show
cannot open log target: %!s(<nil>)

# after
$ resticprofile --log /nonexistent-dir/test.log -n profile show
cannot open log target: open /nonexistent-dir/test.log: no such file or directory

make build and make lint pass (0 issues on darwin, linux and windows). make test shows no new failures.

I did not add a unit test: the affected code is a closure inside main(), so covering it would require refactoring setupLogging out of main — happy to do that in this PR if you would prefer it.

The inner `err` from the `if closer, options, err := setupTargetLogger(...)`
statement is scoped to that `if` only, so the `clog.Errorf` call after the
block referenced `main()`'s outer `var err error` (main.go:39), which is
still nil at that point. The result was that any failure to open a log
target printed the useless

    cannot open log target: %!s(<nil>)

and discarded the real reason. Hoist the call out of the `if` init so the
actual error is in scope where it is logged.

Before: cannot open log target: %!s(<nil>)
After:  cannot open log target: open /nonexistent/test.log: no such file
        or directory

Assisted-By: Claude Opus 5
l1a added a commit to l1a/resticprofile that referenced this pull request Jul 30, 2026
- PRs 1, 3 and 5 are now open upstream as creativeprojects#670, creativeprojects#672 and creativeprojects#671. Note that
  they are opened with ghpub, since the default fine-grained PAT cannot
  create pull requests on repositories we do not own.
- Flag that creativeprojects#672 conflicts with our own master: the fork already fixed
  that same test differently in 1282c8d (before/after count rather than
  filtering). Records how to resolve it on the next merge.
- Quote issue creativeprojects#331, where the maintainer states linger is not something
  resticprofile should set up automatically and drop-in files are the
  sanctioned mechanism. Wave 4A must not be opened as-is; notes the
  warning-instead-of-enforcement slice that could land.
- Correct the mock drift note: mockery v3.7.0 vs upstream's pinned
  v3.7.1 is the likelier cause, and mark the check as unconfirmed.
- Keep PR 3 and 5 bodies alongside PR 1's in upstream-prs/.

Assisted-By: Claude Opus 5
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.

1 participant