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/go: go mod tidy imports test dependencies of our dependencies #65789

Closed
AnomalRoil opened this issue Feb 19, 2024 · 1 comment
Closed

cmd/go: go mod tidy imports test dependencies of our dependencies #65789

AnomalRoil opened this issue Feb 19, 2024 · 1 comment

Comments

@AnomalRoil
Copy link

Go version

go version go1.21.7 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN='/home/anomalroil/go/bin'
GOCACHE='/home/anomalroil/.cache/go-build'
GOENV='/home/anomalroil/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/anomalroil/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/anomalroil/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/anomalroil/sdk/go1.21.7'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/anomalroil/sdk/go1.21.7/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.7'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/anomalroil/code/drand/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-build3941039239=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I'm importing a package, go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc, whose version v0.48.0 has an ambiguous import path issue with cloud.google.com/go/compute/metadata:

go: test imports
	go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc tested by
	go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.test imports
	google.golang.org/grpc/interop imports
	golang.org/x/oauth2/google imports
	cloud.google.com/go/compute/metadata: ambiguous import: found package cloud.google.com/go/compute/metadata in multiple modules:
	cloud.google.com/go v0.34.0 (/home/anomalroil/go/pkg/mod/cloud.google.com/go@v0.34.0/compute/metadata)
	cloud.google.com/go/compute/metadata v0.2.3 (/home/anomalroil/go/pkg/mod/cloud.google.com/go/compute/metadata@v0.2.3)

However, as you can see the offending package isn't go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc itself, but its named test package otelgrpc_test.

And yet, the following code allows to reproduce this:

package main

import (
	"fmt"

	grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
	"google.golang.org/grpc"
)

func main() {
	opts := append([]grpc.ServerOption{},
		grpc.StreamInterceptor(
			grpc_recovery.StreamServerInterceptor(),
		),
		grpc.StatsHandler(otelgrpc.NewServerHandler()),
	)

	fmt.Println(len(opts))
}

by running go mod tidy.

This looks like a go mod tidy bug:
why should the named test package dependencies be imported in my own go module if I'm only using the "parent", non-test package?

NB. the underlying ambiguous path issue was fixed in open-telemetry/opentelemetry-go-contrib#4897 where you can clearly see the issue stem only from named test packages and not from the package I'm actually importing in my program.

What did you see happen?

$ go get go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.48.0
$ go get github.com/grpc-ecosystem/go-grpc-middleware@v1.4.0
$ go mod tidy
go: mypackage imports
	go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc tested by
	go.open telemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.test imports
	google.golang.org/grpc/interop imports
	golang.org/x/oauth2/google imports
	cloud.google.com/go/compute/metadata: ambiguous import: found package cloud.google.com/go/compute/metadata in multiple modules:
	cloud.google.com/go v0.26.0 (/home/anomalroil/go/pkg/mod/cloud.google.com/go@v0.26.0/compute/metadata)
	cloud.google.com/go/compute/metadata v0.2.3 (/home/anomalroil/go/pkg/mod/cloud.google.com/go/compute/metadata@v0.2.3)

What did you expect to see?

When importing a package whose tests imports another package in a named test package (not same package as I'm importing), I would expect the test dependencies to be pruned and not causing any issues even in case of a ambiguous import path or whatnot since these are not packages I'm actually importing.

@seankhliao
Copy link
Member

https://go.dev/ref/mod#go-mod-tidy

go mod tidy works by loading all of the packages in the main module and all of the packages they import, recursively. This includes packages imported by tests (including tests in other modules).

the guiding idea has been it gets all the dependencies that allow you to run go test all.

Closing as working as intended.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants