Skip to content

cmd/go: missing version diagnostic when a package in the main module imports a package from a newer release #48966

Closed
@andig

Description

@andig

Given

module example.com/M

go 1.16

and using embed as part of that module, I'm experiencing a lot of user's questions where compilation fails due to go versions below 1.16. Having declared dependency on go1.16 syntax/libraries as part of go.mod it would be nice if the compiler would fail if the module version of the main module or it's dependencies are not met during build. It could be optional behaviour behind a flag.

I realise this might cause breakage for modules who's authors use the highes available go version as part of their go.mod to be "compatible", but that approach seems flawed anyway.

Activity

bcmills

bcmills commented on Oct 14, 2021

@bcmills
Contributor

What kind of errors are your users reporting? When the build fails, they should be getting an explicit notice about the version mismatch already.

bcmills

bcmills commented on Oct 14, 2021

@bcmills
Contributor

(Also compare #46136.)

andig

andig commented on Oct 14, 2021

@andig
ContributorAuthor

What kind of errors are your users reporting? When the build fails, they should be getting an explicit notice about the version mismatch already.

From evcc-io/evcc#1303:

pi@evcc:/opt/repos/evcc/evcc $ go build ./...
build github.com/andig/evcc: cannot find module for path embed 

For an end user that tries to self-compile its impossible to relate this error in a mismatching go version.

bcmills

bcmills commented on Oct 14, 2021

@bcmills
Contributor

That report is from

go version go1.11.6 linux/arm

That version of Go is way out of date: per the Go project's release policy, the current supported versions of the toolchain are 1.16.9 and 1.17.2.

Any change we would make at this point would only affect 1.18 and later, and I posit that the supported versions of the toolchain already produce a much more useful diagnostic in this case.

bcmills

bcmills commented on Oct 14, 2021

@bcmills
Contributor

Hmm... It is the case that go1.15.15 (the last release that did not include the embed package) still produces a pretty useless error here:

/tmp/evcc$ go1.15.15 build ./...
main.go:4:2: package embed is not in GOROOT (/usr/local/google/home/bcmills/sdk/go1.15/src/embed)
/usr/local/google/home/bcmills/pkg/mod/github.com/evcc-io/eebus@v0.0.0-20211004203943-949a5847f2ea/cert/cert.go:16:2: package io/fs is not in GOROOT (/usr/local/google/home/bcmills/sdk/go1.15/src/io/fs)

It looks like the failure in module resolution is before the failure due to the Go version.

And if we construct a similar situation with some new 1.18 package using Go 1.17.2, we still get a bad error message:

$ go1.17.2 build .
main.go:3:8: package constraints is not in GOROOT (/usr/local/google/home/bcmills/sdk/go1.17.2/src/constraints)

-- go.mod --
module example

go 1.18
-- main.go --
package main

import _ "constraints"

func main() {}

So there is a bug to be fixed here — but it's just a bug, it doesn't need to go through the proposal process.

changed the title [-]go/build: proposal to fail if go.mod version is higher than go compiler version[/-] [+]cmd/go: poor diagnostic when the main module specifies a newer Go version and imports a newly-added package[/+] on Oct 14, 2021
changed the title [-]cmd/go: poor diagnostic when the main module specifies a newer Go version and imports a newly-added package[/-] [+]cmd/go: missing version diagnostic when the main module specifies a newer Go version and imports a newly-added package[/+] on Oct 14, 2021
changed the title [-]cmd/go: missing version diagnostic when the main module specifies a newer Go version and imports a newly-added package[/-] [+]cmd/go: missing version diagnostic when a package in the main module imports a package from a newer release[/+] on Oct 14, 2021
added this to the Backlog milestone on Oct 14, 2021
andig

andig commented on Oct 26, 2021

@andig
ContributorAuthor

Note https://github.com/theckman/goconstraint/ for a go-only compile time solution.

mzacho

mzacho commented on Jan 10, 2022

@mzacho

I'd like to look into this issue - @bcmills do you have some pointers on how to fix it?

bcmills

bcmills commented on Jan 10, 2022

@bcmills
Contributor

@zacho314, the error message that we see is from an ImportMissingError:
https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/modload/import.go;l=54-56;drc=master

Generally when we see an ImportMissingError we attribute that to the package being imported, but in this case we want to also attribute the error to the importing package, since we can't tell whether the error is due to a missing standard-library package or a missing package to be provided by a replace directive.

I suspect that the point where we're reporting the error today is here:
https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/modload/load.go;l=1205;drc=master

What we need to propagate this error correctly is something like:
For each package pkg, if all of:

  • pkg imports a package with an ImportMissingError for which isStd == true
  • pkg.mod specifies a higher Go version than the current version,
  • and pkg.err is nil,
    then set pkg.err to an error that indicates the mismatched version.

We already have a “for each package pkg” loop that also iterates over imports today in (*loader).buildStacks; that might be a decent place to put the above logic as well.

lazerkat

lazerkat commented on Sep 15, 2022

@lazerkat
Contributor

I plan on looking into this if @mzacho isn't actively working on it.

gopherbot

gopherbot commented on Sep 20, 2022

@gopherbot
Contributor

Change https://go.dev/cl/432075 mentions this issue: modload: provide a clearer compiler error for standard library packages from newer releases

added a commit that references this issue on Nov 3, 2022
2dbd8cf
locked and limited conversation to collaborators on Oct 7, 2023
added a commit that references this issue on Feb 20, 2024
40c7e94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @andig@lazerkat@bcmills@gopherbot@mzacho

        Issue actions

          cmd/go: missing version diagnostic when a package in the main module imports a package from a newer release · Issue #48966 · golang/go