Skip to content

Static message analyzer fails to observe unexported constant in other file in same package #92

@mark-rushakoff

Description

@mark-rushakoff

As a minimal test case, we can use main.go:

package main

import (
	"log/slog"
)

const thisFileConstMsg = "Hello from main.go"

const localCopy = otherFileConstMsg

func main() {
	slog.Info(thisFileConstMsg)
	slog.Info(otherFileConstMsg)
	slog.Info(localCopy)
}

and message.go, in the same directory as main.go:

package main

const otherFileConstMsg = "Hello from message.go"

If I go run the directory, I get the expected three log lines:

$ go run .
2025/07/09 14:28:15 INFO Hello from main.go
2025/07/09 14:28:15 INFO Hello from message.go
2025/07/09 14:28:15 INFO Hello from message.go

But if I try to analyze the file:

$ sloglint -static-msg=true ./_test/
.../main.go:13:12: message should be a string literal or a constant
exit status 3

If I comment or remove the slog.Info(otherFileConstMsg), analysis succeeds.

From a quick inspection of sloglint source, I see that in isStaticMsg:

	case *ast.Ident: // e.g. const msg = "msg"; slog.Info(msg)
		return msg.Obj != nil && msg.Obj.Kind == ast.Con

we observe msg.Obj.Kind to be nil when the analysis fails.

I've observed this on the current main commit (0a750b1) and in tag v0.11.0 which is bundled in the current release of golang-ci.

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