Skip to content

fix: nil logger panic if command invoked without Cobra hook chain #222

@jmeridth

Description

@jmeridth

What

c.logger is only initialized inside persistentPreRun, which Cobra calls before each command. If a command is invoked programmatically (e.g., runCmd.Run(runCmd, []string{}) directly), c.logger remains nil and calls like c.logger.Trace(...) panic with a nil pointer dereference.

Why

This is a pre-existing issue from before the dependency injection refactor — the package-level logger global had the same nil exposure. The refactor preserved the behavior but did not add a guard.

Suggested fix

Initialize c.logger to a null logger in NewCLI so it's never nil:

c := &CLI{
    logger: hclog.NewNullLogger(),
    // ...
}

persistentPreRun would still overwrite it with the real logger from config.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions