golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.28k stars 17.7k forks source link

x/mod/modfile: ParseLax and toolchain #67132

Open jimmyfrasche opened 6 months ago

jimmyfrasche commented 6 months ago

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

$ go version
go version go1.22.0 linux/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/jimmy/.cache/go-build'
GOENV='/home/jimmy/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/jimmy/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/jimmy/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/jimmy/code/autoreadme/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 -ffile-prefix-map=/tmp/go-build368045690=/tmp/go-build -gno-record-gcc-switches'
uname -sr: Linux 6.6.15-amd64
Distributor ID: Debian
Description:    Debian GNU/Linux trixie/sid
Release:    n/a
Codename:   trixie
/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Debian GLIBC 2.37-18) stable release version 2.37.
gdb --version: GNU gdb (Debian 13.2-1+b1) 13.2

What did you do?

https://go.dev/play/p/n_SQJPM3hh1

What did you expect to see?

I expected ParseLax to record the toolchain as it is not an "unknown statement"

What did you see instead?

Using Parse, the toolchain info is recorded but, using ParseLax, it's ignored.

I'm not sure if this is a problem with the code or documentation. The documentation states ParseLax is intended for non-main modules which do not need the toolchain directive, but it also implies that it should be identical to Parse except that an unknown directive in the go.mod does not cause an error.

I see two valid fixes

  1. change the documentation for ParseLax to specify that it will ignore known directives that are only applicable to main modules
  2. change the implementation so ParseLax records known directives that are irrelevant to non-main modules

2 would be preferable in my case as I

jimmyfrasche commented 6 months ago

cc @matloob and @samthanawalla per owners