You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/11/20 14:32:21 UTC
(skywalking-go) branch main updated: Fix cannot enhance the vendor management project (#151)
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-go.git
The following commit(s) were added to refs/heads/main by this push:
new 7135bd2 Fix cannot enhance the vendor management project (#151)
7135bd2 is described below
commit 7135bd2790cfbab7abd2b7ba458835f9b69127ea
Author: mrproliu <74...@qq.com>
AuthorDate: Mon Nov 20 22:32:15 2023 +0800
Fix cannot enhance the vendor management project (#151)
---
.licenserc.yaml | 1 +
CHANGES.md | 3 +-
tools/go-agent/Makefile | 2 +-
tools/go-agent/instrument/plugins/instrument.go | 26 ++++++++++
tools/go-agent/tools/testdata/multi-modules.txt | 6 +++
tools/go-agent/tools/testdata/no-modules.txt | 0
tools/go-agent/tools/testdata/single-modules.txt | 4 ++
tools/go-agent/tools/testdata/wrong-modules.txt | 6 +++
tools/go-agent/tools/vendor.go | 57 ++++++++++++++++++--
tools/go-agent/tools/vendor_test.go | 66 +++++++++++++++++++++++-
10 files changed, 164 insertions(+), 7 deletions(-)
diff --git a/.licenserc.yaml b/.licenserc.yaml
index 76adc8b..d3c7bbd 100644
--- a/.licenserc.yaml
+++ b/.licenserc.yaml
@@ -34,6 +34,7 @@ header:
- '.gitignore'
- 'dist/LICENSE.tpl'
- '**/*.tmpl'
+ - 'tools/go-agent/tools/testdata/*'
comment: on-failure
diff --git a/CHANGES.md b/CHANGES.md
index 66da76b..948383f 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -12,7 +12,8 @@ Release Notes.
#### Documentation
#### Bug Fixes
-* Fix users can not use async api in toolkit-trace
+* Fix users can not use async api in toolkit-trace.
+* Fix cannot enhance the vendor management project.
#### Issues and PR
- All issues are [here](https://github.com/apache/skywalking/milestone/197?closed=1)
diff --git a/tools/go-agent/Makefile b/tools/go-agent/Makefile
index 3de51a1..ff7cdbf 100644
--- a/tools/go-agent/Makefile
+++ b/tools/go-agent/Makefile
@@ -34,7 +34,7 @@ GO_GET = $(GO) get
PLATFORMS := linux darwin windows
os = $(word 1, $@)
-ARCH ?= amd64
+ARCH ?= $(shell $(GO) env GOARCH)
SHELL = /bin/bash
diff --git a/tools/go-agent/instrument/plugins/instrument.go b/tools/go-agent/instrument/plugins/instrument.go
index f0024a2..0cce03e 100644
--- a/tools/go-agent/instrument/plugins/instrument.go
+++ b/tools/go-agent/instrument/plugins/instrument.go
@@ -559,6 +559,14 @@ func (i *Instrument) tryToFindThePluginVersion(opts *api.CompileOptions, ins ins
// see: https://go.dev/ref/mod
escapedBasePkg, _ := module.EscapePath(basePkg)
+ // trying to get the module version from vendor
+ vendor, err := i.tryToGetVersionByVendor(arg, escapedBasePkg)
+ if err != nil {
+ return "", err
+ } else if vendor != "" {
+ return vendor, nil
+ }
+
// arg example: github.com/!shopify/sarama@1.34.1/acl.go
_, afterPkg, found := strings.Cut(arg, escapedBasePkg)
if !found {
@@ -579,3 +587,21 @@ func (i *Instrument) tryToFindThePluginVersion(opts *api.CompileOptions, ins ins
}
return "", nil
}
+
+func (i *Instrument) tryToGetVersionByVendor(goFilePath, pkgName string) (string, error) {
+ before, _, found := strings.Cut(goFilePath, tools.VendorDir+pkgName)
+ if !found {
+ return "", nil
+ }
+
+ modulePath := before + tools.VendorDir + "modules.txt"
+ modules, err := tools.ParseVendorModule(modulePath)
+ if err != nil {
+ return "", err
+ }
+ mod := modules[pkgName]
+ if mod == nil {
+ return "", fmt.Errorf("cannot found the current module in vendor: %s, module path: %s", pkgName, modulePath)
+ }
+ return mod.Version, nil
+}
diff --git a/tools/go-agent/tools/testdata/multi-modules.txt b/tools/go-agent/tools/testdata/multi-modules.txt
new file mode 100644
index 0000000..88a111e
--- /dev/null
+++ b/tools/go-agent/tools/testdata/multi-modules.txt
@@ -0,0 +1,6 @@
+# github.com/apache/skywalking-go v0.3.0
+## explicit; go 1.18
+github.com/apache/skywalking-go
+github.com/apache/skywalking-go/agent/core
+# github.com/bytedance/sonic v1.9.1
+github.com/bytedance/sonic
\ No newline at end of file
diff --git a/tools/go-agent/tools/testdata/no-modules.txt b/tools/go-agent/tools/testdata/no-modules.txt
new file mode 100644
index 0000000..e69de29
diff --git a/tools/go-agent/tools/testdata/single-modules.txt b/tools/go-agent/tools/testdata/single-modules.txt
new file mode 100644
index 0000000..2676274
--- /dev/null
+++ b/tools/go-agent/tools/testdata/single-modules.txt
@@ -0,0 +1,4 @@
+# github.com/apache/skywalking-go v0.3.0
+## explicit; go 1.18
+github.com/apache/skywalking-go
+github.com/apache/skywalking-go/log
\ No newline at end of file
diff --git a/tools/go-agent/tools/testdata/wrong-modules.txt b/tools/go-agent/tools/testdata/wrong-modules.txt
new file mode 100644
index 0000000..6fa7792
--- /dev/null
+++ b/tools/go-agent/tools/testdata/wrong-modules.txt
@@ -0,0 +1,6 @@
+github.com/apache/skywalking-go
+github.com/apache/skywalking-go/agent/core
+github.com/apache/skywalking-go/agent/core/operator
+github.com/apache/skywalking-go/agent/core/tracing
+github.com/apache/skywalking-go/agent/reporter
+github.com/apache/skywalking-go/log
\ No newline at end of file
diff --git a/tools/go-agent/tools/vendor.go b/tools/go-agent/tools/vendor.go
index ce1408d..16d5bba 100644
--- a/tools/go-agent/tools/vendor.go
+++ b/tools/go-agent/tools/vendor.go
@@ -17,15 +17,64 @@
package tools
-import "strings"
+import (
+ "bytes"
+ "fmt"
+ "os"
+ "strings"
+)
-const vendorDir = "/vendor/"
+const VendorDir = "/vendor/"
+
+type VendorModule struct {
+ Name string
+ Version string
+}
+
+type VendorModules map[string]*VendorModule
// UnVendor removes the vendor directory from the path.
func UnVendor(path string) string {
- i := strings.Index(path, vendorDir)
+ i := strings.Index(path, VendorDir)
if i == -1 {
return path
}
- return path[i+len(vendorDir):]
+ return path[i+len(VendorDir):]
+}
+
+func ParseVendorModule(path string) (VendorModules, error) {
+ moduleContent, err := os.ReadFile(path)
+ if err != nil {
+ return nil, err
+ }
+ modules := make(VendorModules)
+ var module *VendorModule
+ for _, moduleData := range bytes.Split(moduleContent, []byte("\n")) {
+ moduleString := strings.TrimSpace(string(moduleData))
+ if strings.HasPrefix(moduleString, "# ") {
+ // module
+ moduleInfo := strings.SplitAfterN(moduleString, " ", 3)
+ if len(moduleInfo) != 3 {
+ return nil, fmt.Errorf("module data cannot be analyzed")
+ }
+ module = &VendorModule{
+ Name: moduleInfo[1],
+ Version: moduleInfo[2],
+ }
+ continue
+ } else if strings.HasPrefix(moduleString, "#") {
+ // go version required, ignore
+ continue
+ } else if moduleString == "" {
+ // empty data, ignore
+ continue
+ }
+
+ // otherwise, it should be the module package path
+ if module == nil {
+ return nil, fmt.Errorf("cannot found previous module data")
+ }
+ modules[moduleString] = module
+ }
+ return modules, nil
}
diff --git a/tools/go-agent/tools/vendor_test.go b/tools/go-agent/tools/vendor_test.go
index c2e897b..4a3e0af 100644
--- a/tools/go-agent/tools/vendor_test.go
+++ b/tools/go-agent/tools/vendor_test.go
@@ -17,7 +17,10 @@
package tools
-import "testing"
+import (
+ "fmt"
+ "testing"
+)
func TestUnVendor(t *testing.T) {
tests := []struct {
@@ -45,3 +48,64 @@ func TestUnVendor(t *testing.T) {
}
}
}
+
+func TestParseVendorModule(t *testing.T) {
+ tests := []struct {
+ name string
+ path string
+ hasError bool
+ packageCount int
+ validate func(modules VendorModules) error
+ }{
+ {
+ path: "./testdata/no-modules.txt",
+ hasError: false,
+ packageCount: 0,
+ },
+ {
+ path: "./testdata/single-modules.txt",
+ hasError: false,
+ packageCount: 2,
+ validate: func(modules VendorModules) error {
+ if m := modules["github.com/apache/skywalking-go/log"]; m == nil {
+ return fmt.Errorf("module missing")
+ } else if m.Version != "v0.3.0" {
+ return fmt.Errorf("version not correct")
+ }
+ return nil
+ },
+ },
+ {
+ path: "./testdata/wrong-modules.txt",
+ hasError: true,
+ },
+ {
+ path: "./testdata/multi-modules.txt",
+ hasError: false,
+ packageCount: 3,
+ validate: func(modules VendorModules) error {
+ if m := modules["github.com/bytedance/sonic"]; m == nil {
+ return fmt.Errorf("module missing")
+ } else if m.Version != "v1.9.1" {
+ return fmt.Errorf("version not correct")
+ }
+ return nil
+ },
+ },
+ }
+
+ for _, test := range tests {
+ module, err := ParseVendorModule(test.path)
+ if hasError := err != nil; test.hasError != hasError {
+ t.Errorf("test %s has error, excepted: %t, got: %t", test.name, test.hasError, hasError)
+ }
+ if moduleCount := len(module); test.packageCount != moduleCount {
+ t.Errorf("test %s module count, excepted: %d, got: %d", test.name, test.packageCount, moduleCount)
+ }
+ if test.validate != nil {
+ if err := test.validate(module); err != nil {
+ t.Errorf("test %s validate failure: %s", test.name, err)
+ }
+ }
+ }
+}