Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: the execution order of init functions from different packages is incorrect #65033

Closed
wingrez opened this issue Jan 9, 2024 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@wingrez
Copy link
Contributor

wingrez commented Jan 9, 2024

Go version

go version devel go1.22-b7c630dc3a

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Wingrez\AppData\Local\go-build
set GOENV=C:\Users\Wingrez\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\workspace\my\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=*
set GOOS=windows
set GOPATH=D:\workspace\my\go
set GOPRIVATE=
set GOPROXY=http://cmc-cd-mirror.rnd.huawei.com/goproxy,direct
set GOROOT=D:\workspace\git\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\workspace\git\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=devel go1.22-b7c630dc3a Tue Jan 9 01:36:54 2024 +0000
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Wingrez\AppData\Local\Temp\go-build3833231449=/tmp/go-build -gno-record-gcc-switches

What did you do?

I compiled the same code using go1.22 and go1.19.3, but the results were different.
This is my code:

main.go

package main

import (
    _ "demo/pkg2"
    _ "demo/pkg1"
)

func init() {
    println("init-main")
}

func main() {
    println("main")
}

pkg1/init.go

package pkg1

func init() {
    println("init from pkg1")
}

pkg2/init.go

package pkg2

func init() {
    println("init from pkg2")
}

What did you see happen?

Using go1.19.3, the result is:

init from pkg2
init from pkg1
init-main     
main

Using go1.22, the result is:

init from pkg1
init from pkg2
init-main     
main

What did you expect to see?

I think the result from go1.19.3 is correct. Not sure if Go's specifications have changed.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 9, 2024
@ALTree
Copy link
Member

ALTree commented Jan 9, 2024

Yes, this was changed recently. From the 1.21 release notes:

Package initialization order is now specified more precisely. The new algorithm is:

Sort all packages by import path.
Repeat until the list of packages is empty:

  • Find the first package in the list for which all imports are already initialized.
  • Initialize that package and remove it from the list.

This may change the behavior of some programs that rely on a specific initialization ordering that was not expressed by explicit imports. The behavior of such programs was not well defined by the spec in past releases. The new rule provides an unambiguous definition.

Closing as this is working as intended.

@ALTree ALTree closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants