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 83604b9  fix: hybird compilation issue on windows (#179)
83604b9 is described below

commit 83604b96a62c27ee92df2c42989aa09d69931e28
Author: Shyunn <[email protected]>
AuthorDate: Mon Apr 15 21:10:01 2024 +0800

    fix: hybird compilation issue on windows (#179)
---
 tools/go-agent/instrument/agentcore/instrument.go  | 26 +++++++++++++++++++++-
 tools/go-agent/instrument/instrument.go            |  2 +-
 tools/go-agent/instrument/plugins/instrument.go    |  5 ++++-
 .../go-agent/instrument/plugins/rewrite/import.go  |  4 +++-
 tools/go-agent/instrument/reporter/instrument.go   |  8 +++++--
 tools/go-agent/tools/copy.go                       |  7 ++++--
 tools/go-agent/tools/dst.go                        |  5 +++++
 7 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/tools/go-agent/instrument/agentcore/instrument.go 
b/tools/go-agent/instrument/agentcore/instrument.go
index b46abec..bca365f 100644
--- a/tools/go-agent/instrument/agentcore/instrument.go
+++ b/tools/go-agent/instrument/agentcore/instrument.go
@@ -18,10 +18,12 @@
 package agentcore
 
 import (
+       "fmt"
        "html"
        "io/fs"
        "path/filepath"
        "regexp"
+       "runtime"
        "strings"
 
        "github.com/apache/skywalking-go/plugins/core"
@@ -66,7 +68,21 @@ func (i *Instrument) FilterAndEdit(path string, curFile 
*dst.File, cursor *dstut
        }
        targetDir := filepath.Dir(path)
        for _, sub := range CopiedSubPackages {
-               if regexp.MustCompile(filepath.Join(CopiedBasePackage, sub) + 
"$").MatchString(targetDir) {
+               // On os=windows, we need to escape the characters,
+               // and we can't just use "filepath.Join",
+               // which would cause the regex to throw an error
+               var prefix string
+               if runtime.GOOS == "windows" {
+                       prefix = strings.ReplaceAll(CopiedBasePackage, `\/`, 
`\\`)
+                       if sub != "" {
+                               prefix = fmt.Sprintf(`%s\\%s`, prefix, sub)
+                       }
+               } else {
+                       prefix = filepath.Join(CopiedBasePackage, sub)
+               }
+
+               res := prefix + "$"
+               if regexp.MustCompile(res).MatchString(targetDir) {
                        i.needsCopyDir = sub
                        i.hasCopyPath = true
                        return true
@@ -94,6 +110,14 @@ func (i *Instrument) WriteExtraFiles(dir string) ([]string, 
error) {
                pkgUpdates[filepath.Join(EnhanceFromBasePackage, p)] = 
filepath.Join(EnhanceBasePackage, p)
        }
        pkgUpdates[filepath.Join(EnhanceFromBasePackage, 
ReporterFromBasePackage)] = filepath.Join(ProjectBasePackage, 
ReporterBasePackage)
+
+       for k, v := range pkgUpdates {
+               newKey := strings.ReplaceAll(k, `\`, `/`)
+               newVal := strings.ReplaceAll(v, `\`, `/`)
+
+               pkgUpdates[newKey] = newVal
+       }
+
        copiedFiles, err := tools.CopyGoFiles(core.FS, sub, dir, 
i.buildDSTDebugInfo, func(file *dst.File) {
                tools.ChangePackageImportPath(file, pkgUpdates)
        })
diff --git a/tools/go-agent/instrument/instrument.go 
b/tools/go-agent/instrument/instrument.go
index 97a0e84..a0fe334 100644
--- a/tools/go-agent/instrument/instrument.go
+++ b/tools/go-agent/instrument/instrument.go
@@ -134,7 +134,7 @@ func instrumentFiles(buildDir string, inst api.Instrument, 
args []string) error
        for _, updateFileSrc := range instrumentedFiles {
                info := parsedFiles[updateFileSrc]
                filename := filepath.Base(updateFileSrc)
-               dest := filepath.Join(buildDir, filename)
+               dest := strings.ReplaceAll(filepath.Join(buildDir, filename), 
`\`, `/`)
                debugInfo, err := tools.BuildDSTDebugInfo(updateFileSrc, nil)
                if err != nil {
                        return err
diff --git a/tools/go-agent/instrument/plugins/instrument.go 
b/tools/go-agent/instrument/plugins/instrument.go
index 0cce03e..7a288e7 100644
--- a/tools/go-agent/instrument/plugins/instrument.go
+++ b/tools/go-agent/instrument/plugins/instrument.go
@@ -358,7 +358,10 @@ func (i *Instrument) copyOperatorsFS(context 
*rewrite.Context, baseDir, packageN
                        if strings.HasSuffix(entry.Name(), "_test.go") || 
strings.HasSuffix(entry.Name(), "_test_base.go") {
                                continue
                        }
-                       file, err1 := fs.ReadFile(core.FS, filepath.Join(dir, 
entry.Name()))
+
+                       // Force the use of '/' delimiter on all platforms
+                       path := strings.ReplaceAll(filepath.Join(dir, 
entry.Name()), `\`, `/`)
+                       file, err1 := fs.ReadFile(core.FS, path)
                        if err1 != nil {
                                return nil, err1
                        }
diff --git a/tools/go-agent/instrument/plugins/rewrite/import.go 
b/tools/go-agent/instrument/plugins/rewrite/import.go
index 94e6cd9..fb10c96 100644
--- a/tools/go-agent/instrument/plugins/rewrite/import.go
+++ b/tools/go-agent/instrument/plugins/rewrite/import.go
@@ -21,6 +21,7 @@ import (
        "fmt"
        "go/token"
        "path/filepath"
+       "strings"
 
        "github.com/apache/skywalking-go/tools/go-agent/instrument/agentcore"
 
@@ -33,7 +34,8 @@ func (c *Context) Import(imp *dst.ImportSpec, cursor 
*dstutil.Cursor) {
        operatorCode := false
        // delete the original import("agent/core/xxx")
        for _, subPackageName := range OperatorDirs {
-               if imp.Path.Value == fmt.Sprintf("%q", 
filepath.Join(agentcore.EnhanceFromBasePackage, subPackageName)) {
+               path := 
strings.ReplaceAll(filepath.Join(agentcore.EnhanceFromBasePackage, 
subPackageName), `\`, `/`)
+               if imp.Path.Value == fmt.Sprintf("%q", path) {
                        shouldRemove = true
                        operatorCode = true
                        break
diff --git a/tools/go-agent/instrument/reporter/instrument.go 
b/tools/go-agent/instrument/reporter/instrument.go
index 2d83bb7..989299f 100644
--- a/tools/go-agent/instrument/reporter/instrument.go
+++ b/tools/go-agent/instrument/reporter/instrument.go
@@ -21,6 +21,7 @@ import (
        "html"
        "io/fs"
        "path/filepath"
+       "strings"
 
        "github.com/apache/skywalking-go/plugins/core"
        "github.com/apache/skywalking-go/tools/go-agent/config"
@@ -76,7 +77,8 @@ func (i *GRPCInstrument) WriteExtraFiles(dir string) 
([]string, error) {
        results = append(results, copiedFiles...)
 
        // copy reporter implementations
-       reporterDirName := filepath.Join("reporter", "grpc")
+       // Force the use of '/' delimiter on all platforms
+       reporterDirName := strings.ReplaceAll(filepath.Join("reporter", 
"grpc"), `\`, `/`)
        copiedFiles, err = tools.CopyGoFiles(core.FS, reporterDirName, dir, 
func(entry fs.DirEntry, f *dst.File) (*tools.DebugInfo, error) {
                if i.compileOpts.DebugDir == "" {
                        return nil, nil
@@ -87,7 +89,9 @@ func (i *GRPCInstrument) WriteExtraFiles(dir string) 
([]string, error) {
                file.Name = dst.NewIdent("reporter")
                pkgUpdates := make(map[string]string)
                for _, p := range agentcore.CopiedSubPackages {
-                       
pkgUpdates[filepath.Join(agentcore.EnhanceFromBasePackage, p)] = 
filepath.Join(agentcore.EnhanceBasePackage, p)
+                       key := 
strings.ReplaceAll(filepath.Join(agentcore.EnhanceFromBasePackage, p), `\`, `/`)
+                       val := 
strings.ReplaceAll(filepath.Join(agentcore.EnhanceBasePackage, p), `\`, `/`)
+                       pkgUpdates[key] = val
                }
                tools.ChangePackageImportPath(file, pkgUpdates)
                tools.DeletePackageImports(file, 
"github.com/apache/skywalking-go/plugins/core/reporter")
diff --git a/tools/go-agent/tools/copy.go b/tools/go-agent/tools/copy.go
index fa63b93..9604c68 100644
--- a/tools/go-agent/tools/copy.go
+++ b/tools/go-agent/tools/copy.go
@@ -33,6 +33,7 @@ func CopyGoFiles(fromFS fs.ReadDirFS, fromDir, targetDir 
string,
        debugInfoBuilder func(entry fs.DirEntry, file *dst.File) (*DebugInfo, 
error),
        peek func(file *dst.File)) ([]string, error) {
        results := make([]string, 0)
+
        files, err := fromFS.ReadDir(fromDir)
        if err != nil {
                return nil, err
@@ -45,7 +46,9 @@ func CopyGoFiles(fromFS fs.ReadDirFS, fromDir, targetDir 
string,
                        continue
                }
 
-               readFile, err := fs.ReadFile(fromFS, filepath.Join(fromDir, 
f.Name()))
+               // Force the use of '/' delimiter on all platforms
+               join := strings.ReplaceAll(filepath.Join(fromDir, f.Name()), 
`\`, `/`)
+               readFile, err := fs.ReadFile(fromFS, join)
                if err != nil {
                        return nil, err
                }
@@ -65,7 +68,7 @@ func CopyGoFiles(fromFS fs.ReadDirFS, fromDir, targetDir 
string,
                }
 
                peek(parse)
-               copiedFilePath := filepath.Join(targetDir, f.Name())
+               copiedFilePath := strings.ReplaceAll(filepath.Join(targetDir, 
f.Name()), `\`, `/`)
                if err := WriteDSTFile(copiedFilePath, parse, debugInfo); err 
!= nil {
                        return nil, err
                }
diff --git a/tools/go-agent/tools/dst.go b/tools/go-agent/tools/dst.go
index 5d65554..4147a96 100644
--- a/tools/go-agent/tools/dst.go
+++ b/tools/go-agent/tools/dst.go
@@ -27,6 +27,7 @@ import (
        "os"
        "path/filepath"
        "regexp"
+       "runtime"
        "strings"
 
        "github.com/dave/dst"
@@ -172,6 +173,10 @@ func BuildDSTDebugInfo(srcPath string, file *dst.File) 
(*DebugInfo, error) {
 }
 
 func WriteDSTFile(path string, file *dst.File, debug *DebugInfo) error {
+       if runtime.GOOS == "windows" {
+               path = strings.ReplaceAll(path, `/`, `\`)
+       }
+
        output, err := os.Create(path)
        if err != nil {
                return err

Reply via email to