Skip to content

x/text: gotext nil dereferences when a const declaration is in another file #60555

@mraron

Description

@mraron

What version of Go are you using (go version)?

$ go version
go version go1.20.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/aron/.cache/go-build"
GOENV="/home/aron/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/aron/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/aron/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/snap/go/current"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/current/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="/usr/bin/gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/aron/text/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build922970473=/tmp/go-build -gno-record-gcc-switches"

What did you do?

login.go

package mvp

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

const alreadyLoggedInMessage = "You're already logged in"

func LoginStuff() {
	message.NewPrinter(language.MustParse("hu-HU")).Sprintf(alreadyLoggedInMessage)
}

register.go

package mvp

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

func RegisterStuff() {
	message.NewPrinter(language.MustParse("hu-HU")).Sprintf(alreadyLoggedInMessage)
}

Then I called gotext via gotext -srclang=en-US update -out=catalog.go -lang=en-US,hu-HU {package name}

I get the following panic:

"runtime error: invalid memory address or nil pointer dereference"
Stack:
	 4  0x00000000009975f9 in golang.org/x/text/message/pipeline.packageExtracter.handleCall
	     at /home/aron/text/message/pipeline/extract.go:600
	 5  0x0000000000996725 in golang.org/x/text/message/pipeline.(*extracter).extractMessages.func3
	     at /home/aron/text/message/pipeline/extract.go:545
	 6  0x000000000058791d in go/ast.inspector.Visit
	     at /snap/go/current/src/go/ast/walk.go:386
	 7  0x00000000005839ba in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:51
	 8  0x000000000058565b in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:206
	 9  0x0000000000583787 in go/ast.walkStmtList
	     at /snap/go/current/src/go/ast/walk.go:32
	10  0x0000000000585c53 in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:234
	11  0x000000000058703e in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:357
	12  0x00000000005838e7 in go/ast.walkDeclList
	     at /snap/go/current/src/go/ast/walk.go:38
	13  0x0000000000587169 in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:366
	14  0x00000000005879e5 in go/ast.Inspect
	     at /snap/go/current/src/go/ast/walk.go:397
	15  0x0000000000996427 in golang.org/x/text/message/pipeline.(*extracter).extractMessages
	     at /home/aron/text/message/pipeline/extract.go:542
	16  0x0000000000991c46 in golang.org/x/text/message/pipeline.Extract
	     at /home/aron/text/message/pipeline/extract.go:50
	17  0x00000000009aba9c in main.runUpdate
	     at /home/aron/text/cmd/gotext/update.go:35
	18  0x00000000009a90c3 in main.main
	     at /home/aron/text/cmd/gotext/main.go:149

As far as I understand it seems to happen because in extract.go ident.Obj is nil when extracting messages from register.go, since the the declaration is in the other file (according to #48141).

Indeed modifying the relevant code to something like:

		if obj := ident.Obj; obj != nil {
			if v, ok := obj.Decl.(*ast.ValueSpec); ok && v.Comment != nil {
				// TODO: get comment above ValueSpec as well
				comment = v.Comment.Text()
			}
		}

apparently fixes the problem.

What did you expect to see?

No panic.

What did you see instead?

A panic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions