Skip to content

Commit

Permalink
[v2] tools: fix fixmodules script (#3090)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF authored Jan 17, 2025
1 parent bb4979d commit d8fe1d5
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 29 deletions.
6 changes: 2 additions & 4 deletions contrib/envoyproxy/go-control-plane/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../../..
2 changes: 2 additions & 0 deletions contrib/envoyproxy/go-control-plane/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 0 additions & 2 deletions contrib/julienschmidt/httprouter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../../..
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion internal/apps/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../..
2 changes: 0 additions & 2 deletions internal/apps/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions internal/setup-smoke-test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../..
6 changes: 0 additions & 6 deletions internal/traceprof/traceproftest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../../..
2 changes: 2 additions & 0 deletions internal/traceprof/traceproftest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions tools/fixmodules/go.mod
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions tools/fixmodules/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
55 changes: 45 additions & 10 deletions tools/fixmodules/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <root> [fix-dir]\n", os.Args[0])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

0 comments on commit d8fe1d5

Please sign in to comment.