From d8fe1d54cedd960efa01720c580df8508c57bad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Arg=C3=BCello?= Date: Fri, 17 Jan 2025 11:25:16 +0100 Subject: [PATCH] [v2] tools: fix fixmodules script (#3090) --- contrib/envoyproxy/go-control-plane/go.mod | 6 +-- contrib/envoyproxy/go-control-plane/go.sum | 2 + contrib/julienschmidt/httprouter/go.mod | 2 - go.mod | 2 - internal/apps/go.mod | 3 +- internal/apps/go.sum | 2 - internal/setup-smoke-test/go.mod | 4 +- internal/traceprof/traceproftest/go.mod | 6 --- internal/traceprof/traceproftest/go.sum | 2 + tools/fixmodules/go.mod | 2 + tools/fixmodules/go.sum | 2 + tools/fixmodules/main.go | 55 ++++++++++++++++++---- 12 files changed, 59 insertions(+), 29 deletions(-) diff --git a/contrib/envoyproxy/go-control-plane/go.mod b/contrib/envoyproxy/go-control-plane/go.mod index 0ea1cf806a..777f0a7a0f 100644 --- a/contrib/envoyproxy/go-control-plane/go.mod +++ b/contrib/envoyproxy/go-control-plane/go.mod @@ -84,8 +84,6 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/DataDog/dd-trace-go/v2 => ../../.. - -replace github.com/DataDog/dd-trace-go/contrib/google.golang.org/grpc/v2 => ../../../contrib/google.golang.org/grpc +replace github.com/DataDog/dd-trace-go/contrib/google.golang.org/grpc/v2 => ../../google.golang.org/grpc -replace github.com/DataDog/dd-trace-go/instrumentation/testutils/grpc/v2 => ../../../instrumentation/testutils/grpc +replace github.com/DataDog/dd-trace-go/v2 => ../../.. diff --git a/contrib/envoyproxy/go-control-plane/go.sum b/contrib/envoyproxy/go-control-plane/go.sum index c5a5a6cc33..8dafab0029 100644 --- a/contrib/envoyproxy/go-control-plane/go.sum +++ b/contrib/envoyproxy/go-control-plane/go.sum @@ -14,6 +14,8 @@ github.com/DataDog/datadog-agent/pkg/util/scrubber v0.59.0 h1:p4uZow1IE/ve590aKq github.com/DataDog/datadog-agent/pkg/util/scrubber v0.59.0/go.mod h1:krOxbYZc4KKE7bdEDu10lLSQBjdeSFS/XDSclsaSf1Y= github.com/DataDog/datadog-go/v5 v5.5.0 h1:G5KHeB8pWBNXT4Jtw0zAkhdxEAWSpWH00geHI6LDrKU= github.com/DataDog/datadog-go/v5 v5.5.0/go.mod h1:K9kcYBlxkcPP8tvvjZZKs/m1edNAUFzBbdpTUKfCsuw= +github.com/DataDog/dd-trace-go/instrumentation/testutils/grpc/v2 v2.0.0-rc.2 h1:KCoJVEpXp3+W//IIexHvUJSE7CGdPCZB2DCeEjscENs= +github.com/DataDog/dd-trace-go/instrumentation/testutils/grpc/v2 v2.0.0-rc.2/go.mod h1:A+sBDq2Q9FVx5fWSyHHcjv46FIuzL+f3qpOWtbNxd6s= github.com/DataDog/go-libddwaf/v3 v3.5.1 h1:GWA4ln4DlLxiXm+X7HA/oj0ZLcdCwOS81KQitegRTyY= github.com/DataDog/go-libddwaf/v3 v3.5.1/go.mod h1:n98d9nZ1gzenRSk53wz8l6d34ikxS+hs62A31Fqmyi4= github.com/DataDog/go-runtime-metrics-internal v0.0.4-0.20241206090539-a14610dc22b6 h1:bpitH5JbjBhfcTG+H2RkkiUXpYa8xSuIPnyNtTaSPog= diff --git a/contrib/julienschmidt/httprouter/go.mod b/contrib/julienschmidt/httprouter/go.mod index 02a2d593d7..553f05b818 100644 --- a/contrib/julienschmidt/httprouter/go.mod +++ b/contrib/julienschmidt/httprouter/go.mod @@ -80,6 +80,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/DataDog/dd-trace-go/contrib/net/http/v2 => ../../net/http - replace github.com/DataDog/dd-trace-go/v2 => ../../.. diff --git a/go.mod b/go.mod index c5d80a4418..7074b8666d 100644 --- a/go.mod +++ b/go.mod @@ -100,5 +100,3 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) - -replace github.com/DataDog/dd-trace-go/v2/internal/setup-smoke-test => ./internal/setup-smoke-test diff --git a/internal/apps/go.mod b/internal/apps/go.mod index cfb2cdc73a..7248355438 100644 --- a/internal/apps/go.mod +++ b/internal/apps/go.mod @@ -88,5 +88,6 @@ require ( google.golang.org/protobuf v1.35.1 // indirect ) -// use local version of dd-trace-go +replace github.com/DataDog/dd-trace-go/contrib/net/http/v2 => ../../contrib/net/http + replace github.com/DataDog/dd-trace-go/v2 => ../.. diff --git a/internal/apps/go.sum b/internal/apps/go.sum index 9d971993cf..4775930aa6 100644 --- a/internal/apps/go.sum +++ b/internal/apps/go.sum @@ -14,8 +14,6 @@ github.com/DataDog/datadog-agent/pkg/util/scrubber v0.59.0 h1:p4uZow1IE/ve590aKq github.com/DataDog/datadog-agent/pkg/util/scrubber v0.59.0/go.mod h1:krOxbYZc4KKE7bdEDu10lLSQBjdeSFS/XDSclsaSf1Y= github.com/DataDog/datadog-go/v5 v5.5.0 h1:G5KHeB8pWBNXT4Jtw0zAkhdxEAWSpWH00geHI6LDrKU= github.com/DataDog/datadog-go/v5 v5.5.0/go.mod h1:K9kcYBlxkcPP8tvvjZZKs/m1edNAUFzBbdpTUKfCsuw= -github.com/DataDog/dd-trace-go/contrib/net/http/v2 v2.0.0-rc.2 h1:r7EFr5heje76eWideUVHqp91AVyl56Q4F+1ltLlSv+I= -github.com/DataDog/dd-trace-go/contrib/net/http/v2 v2.0.0-rc.2/go.mod h1:cF3IsBtzzII+PH3ZbCsc77Kf4uxoVYUwu6e3mYeH03o= github.com/DataDog/go-libddwaf/v3 v3.5.1 h1:GWA4ln4DlLxiXm+X7HA/oj0ZLcdCwOS81KQitegRTyY= github.com/DataDog/go-libddwaf/v3 v3.5.1/go.mod h1:n98d9nZ1gzenRSk53wz8l6d34ikxS+hs62A31Fqmyi4= github.com/DataDog/go-runtime-metrics-internal v0.0.4-0.20241206090539-a14610dc22b6 h1:bpitH5JbjBhfcTG+H2RkkiUXpYa8xSuIPnyNtTaSPog= diff --git a/internal/setup-smoke-test/go.mod b/internal/setup-smoke-test/go.mod index 43dd2bab06..af99eb6905 100644 --- a/internal/setup-smoke-test/go.mod +++ b/internal/setup-smoke-test/go.mod @@ -80,6 +80,6 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect ) -replace github.com/DataDog/dd-trace-go/v2 => ../.. - replace github.com/DataDog/dd-trace-go/contrib/net/http/v2 => ../../contrib/net/http + +replace github.com/DataDog/dd-trace-go/v2 => ../.. diff --git a/internal/traceprof/traceproftest/go.mod b/internal/traceprof/traceproftest/go.mod index 0425bfa8a5..c6a8de3ceb 100644 --- a/internal/traceprof/traceproftest/go.mod +++ b/internal/traceprof/traceproftest/go.mod @@ -83,14 +83,8 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace github.com/DataDog/dd-trace-go/instrumentation/testutils/grpc/v2 => ../../../instrumentation/testutils/grpc - replace github.com/DataDog/dd-trace-go/contrib/google.golang.org/grpc/v2 => ../../../contrib/google.golang.org/grpc replace github.com/DataDog/dd-trace-go/contrib/julienschmidt/httprouter/v2 => ../../../contrib/julienschmidt/httprouter -replace github.com/DataDog/dd-trace-go/contrib/database/sql/v2 => ../../../contrib/database/sql - -replace github.com/DataDog/dd-trace-go/contrib/net/http/v2 => ../../../contrib/net/http - replace github.com/DataDog/dd-trace-go/v2 => ../../.. diff --git a/internal/traceprof/traceproftest/go.sum b/internal/traceprof/traceproftest/go.sum index fde880a3f4..60fe089120 100644 --- a/internal/traceprof/traceproftest/go.sum +++ b/internal/traceprof/traceproftest/go.sum @@ -14,6 +14,8 @@ github.com/DataDog/datadog-agent/pkg/util/scrubber v0.59.0 h1:p4uZow1IE/ve590aKq github.com/DataDog/datadog-agent/pkg/util/scrubber v0.59.0/go.mod h1:krOxbYZc4KKE7bdEDu10lLSQBjdeSFS/XDSclsaSf1Y= github.com/DataDog/datadog-go/v5 v5.5.0 h1:G5KHeB8pWBNXT4Jtw0zAkhdxEAWSpWH00geHI6LDrKU= github.com/DataDog/datadog-go/v5 v5.5.0/go.mod h1:K9kcYBlxkcPP8tvvjZZKs/m1edNAUFzBbdpTUKfCsuw= +github.com/DataDog/dd-trace-go/instrumentation/testutils/grpc/v2 v2.0.0-rc.2 h1:KCoJVEpXp3+W//IIexHvUJSE7CGdPCZB2DCeEjscENs= +github.com/DataDog/dd-trace-go/instrumentation/testutils/grpc/v2 v2.0.0-rc.2/go.mod h1:A+sBDq2Q9FVx5fWSyHHcjv46FIuzL+f3qpOWtbNxd6s= github.com/DataDog/go-libddwaf/v3 v3.5.1 h1:GWA4ln4DlLxiXm+X7HA/oj0ZLcdCwOS81KQitegRTyY= github.com/DataDog/go-libddwaf/v3 v3.5.1/go.mod h1:n98d9nZ1gzenRSk53wz8l6d34ikxS+hs62A31Fqmyi4= github.com/DataDog/go-runtime-metrics-internal v0.0.4-0.20241206090539-a14610dc22b6 h1:bpitH5JbjBhfcTG+H2RkkiUXpYa8xSuIPnyNtTaSPog= diff --git a/tools/fixmodules/go.mod b/tools/fixmodules/go.mod index ed0d1842e4..cd2835ceb0 100644 --- a/tools/fixmodules/go.mod +++ b/tools/fixmodules/go.mod @@ -1,3 +1,5 @@ module github.com/DataDog/dd-trace-go/v2/scripts/fixmodules go 1.22.0 + +require golang.org/x/mod v0.22.0 diff --git a/tools/fixmodules/go.sum b/tools/fixmodules/go.sum index e69de29bb2..36066b89e4 100644 --- a/tools/fixmodules/go.sum +++ b/tools/fixmodules/go.sum @@ -0,0 +1,2 @@ +golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= +golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= diff --git a/tools/fixmodules/main.go b/tools/fixmodules/main.go index 1649f4294d..f358ba8b05 100644 --- a/tools/fixmodules/main.go +++ b/tools/fixmodules/main.go @@ -13,18 +13,18 @@ import ( "go/ast" "go/parser" "go/token" + "golang.org/x/mod/modfile" "io/fs" "log" "os" "os/exec" + "path" "path/filepath" "slices" "sort" "strings" ) -const goVersion = "1.21" - func init() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s [fix-dir]\n", os.Args[0]) @@ -101,6 +101,8 @@ func main() { fixDir = resolved } + goVersion := getProjectGoVersion(root) + log.Printf("using go version globally %s\n", goVersion) log.Printf("finding modules recursively from %s\n", fixDir) fixModules, err := findModules(fixDir) @@ -160,9 +162,11 @@ func main() { return cmp.Compare(a.Old.Path, b.Old.Path) }) - log.Printf("fixing module: %s", modPath) - log.Printf(" need replaces: %v", replaces) - if err := fixModule(allModules, mod, replaces); err != nil { + log.Printf("adding module replaces %q\n", modPath) + for _, r := range replaces { + log.Printf(" %s => %s\n", r.Old.Path, r.New.Path) + } + if err := fixModule(allModules, mod, goVersion, replaces); err != nil { log.Fatal(err) } } @@ -176,7 +180,9 @@ func getLocalReplace(mods map[string]GoMod, mod, require string) Replace { if err != nil { log.Fatal(err) } - + if !strings.HasPrefix(rel, "./") && !strings.HasPrefix(rel, "../") { + rel = "./" + rel + } return Replace{ Old: Module{ Path: require, @@ -202,7 +208,7 @@ func readModule(path string) (GoMod, error) { return m, nil } -func fixModule(mods map[string]GoMod, mod GoMod, replaces []Replace) error { +func fixModule(mods map[string]GoMod, mod GoMod, goVersion string, replaces []Replace) error { // first, clean previous local replaces for _, replace := range mod.Replace { if _, ok := mods[replace.Old.Path]; ok { @@ -289,11 +295,13 @@ func findModules(root string) (map[string]GoMod, error) { func getModGoFiles(mod GoMod) ([]string, error) { var goFiles []string + var nestedMods []string foundGoMod := false - err := filepath.WalkDir(mod.dir, func(path string, d fs.DirEntry, err error) error { - if d.Name() == "go.mod" { + err := filepath.WalkDir(mod.dir, func(path string, f fs.DirEntry, err error) error { + if f.Name() == "go.mod" { if foundGoMod { + nestedMods = append(nestedMods, filepath.Dir(path)) return fs.SkipAll } foundGoMod = true @@ -306,7 +314,22 @@ func getModGoFiles(mod GoMod) ([]string, error) { if err != nil { return nil, err } - return goFiles, nil + + shouldInclude := func(path string) bool { + for _, nm := range nestedMods { + if strings.HasPrefix(path, nm) { + return false + } + } + return true + } + var filtered []string + for _, f := range goFiles { + if shouldInclude(f) { + filtered = append(filtered, f) + } + } + return filtered, nil } func findImports(goFiles []string) ([]string, error) { @@ -338,3 +361,15 @@ func findImports(goFiles []string) ([]string, error) { sort.Strings(imports) return imports, nil } + +func getProjectGoVersion(root string) string { + b, err := os.ReadFile(path.Join(root, "go.mod")) + if err != nil { + log.Fatal(err) + } + f, err := modfile.Parse("go.mod", b, nil) + if err != nil { + panic(err) + } + return f.Go.Version +}