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

fix: find vendor/modules.txt from project root #282

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions tool/preprocess/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ import (
"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util"
)

func runModDownload(path string) (string, error) {
return util.RunCmdOutput("go", "mod", "download", "-json", path)
}

type moduleHolder struct {
Error string // error loading module
Dir string // absolute path to cached source root directory
}

func fetchNetwork(path string) (string, error) {
text, err := runModDownload(path)
// Note we dont use CombinedOutput here because some unexpected output may
// be printed to stderr, which is not what we want, we only care about the
// json output and handle the error when the command fails
text, err := util.RunCmdOutput("go", "mod", "download", "-json", path)
if err != nil {
return "", err

Expand Down
12 changes: 9 additions & 3 deletions tool/preprocess/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ func findFlagValue(cmd []string, flag string) string {

func parseVendorModules() (map[string]string, error) {
util.Assert(util.IsVendorBuild(), "why not otherwise")
vendorFile := filepath.Join("vendor", "modules.txt")
projDir, err := util.GetProjRootDir()
if err != nil {
return nil, err
}
vendorFile := filepath.Join(projDir, "vendor", "modules.txt")
if util.PathNotExists(vendorFile) {
return nil, errc.New(errc.ErrNotExist, "vendor/modules.txt not found")
}
Expand Down Expand Up @@ -329,8 +333,10 @@ func parseVendorModules() (map[string]string, error) {
}
}
}
if err = scanner.Err(); err != nil {
return nil, err
err = scanner.Err()
if err != nil {
return nil, errc.New(errc.ErrParseCode,
"cannot parse vendor/modules.txt")
}
return vendorModules, nil
}
Expand Down
20 changes: 11 additions & 9 deletions tool/preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (dp *DepProcessor) restoreBackupFiles() error {
func getCompileCommands() ([]string, error) {
dryRunLog, err := os.Open(util.GetLogPath(DryRunLog))
if err != nil {
return nil, err
return nil, errc.New(errc.ErrOpenFile, err.Error())
}
defer func(dryRunLog *os.File) {
err := dryRunLog.Close()
Expand All @@ -219,8 +219,9 @@ func getCompileCommands() ([]string, error) {
compileCmds = append(compileCmds, line)
}
}
if err = scanner.Err(); err != nil {
return nil, err
err = scanner.Err()
if err != nil {
return nil, errc.New(errc.ErrParseCode, "cannot parse dry run log")
}
return compileCmds, nil
}
Expand Down Expand Up @@ -457,6 +458,7 @@ func runDryBuild(goBuildCmd []string) error {
// The full build command is: "go build -a -x -n {...}"
args := []string{"go", "build", "-a", "-x", "-n"}
args = append(args, goBuildCmd[2:]...)
util.AssertGoBuild(goBuildCmd)
util.AssertGoBuild(args)

// Run the dry build
Expand All @@ -476,31 +478,31 @@ func runDryBuild(goBuildCmd []string) error {
}

func runModTidy() error {
out, err := util.RunCmdOutput("go", "mod", "tidy")
out, err := util.RunCmdCombinedOutput("go", "mod", "tidy")
util.Log("Run go mod tidy: %v", out)
return err
}

func runModVendor() error {
out, err := util.RunCmdOutput("go", "mod", "vendor")
out, err := util.RunCmdCombinedOutput("go", "mod", "vendor")
util.Log("Run go mod vendor: %v", out)
return err
}

func runGoGet(dep string) error {
out, err := util.RunCmdOutput("go", "get", dep)
out, err := util.RunCmdCombinedOutput("go", "get", dep)
util.Log("Run go get %v: %v", dep, out)
return err
}

func runGoModDownload(path string) error {
out, err := util.RunCmdOutput("go", "mod", "download", path)
out, err := util.RunCmdCombinedOutput("go", "mod", "download", path)
util.Log("Run go mod download %v: %v", path, out)
return err
}

func runGoModEdit(require string) error {
out, err := util.RunCmdOutput("go", "mod", "edit", "-require="+require)
out, err := util.RunCmdCombinedOutput("go", "mod", "edit", "-require="+require)
util.Log("Run go mod edit %v: %v", require, out)
return err
}
Expand Down Expand Up @@ -548,7 +550,7 @@ func runBuildWithToolexec(goBuildCmd []string) error {
util.Log("Run go build with args %v in toolexec mode", args)
}
util.AssertGoBuild(args)
out, err := util.RunCmdOutput(args...)
out, err := util.RunCmdCombinedOutput(args...)
util.Log("Run go build with toolexec: %v", out)
return err
}
Expand Down
4 changes: 3 additions & 1 deletion tool/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/errc"
"github.com/alibaba/opentelemetry-go-auto-instrumentation/tool/util"
)

Expand All @@ -31,6 +32,7 @@ func listFiles(fs embed.FS, dir string) ([]string, error) {
}
var files []string
for _, item := range list {
// embedded file path is always / rather than os.PathSeparator
path := dir + "/" + item.Name()
if item.IsDir() {
subFiles, err := listFiles(fs, path)
Expand Down Expand Up @@ -68,7 +70,7 @@ func CopyPkgTo(target string) error {
target := filepath.Join(target, file)
err = os.MkdirAll(filepath.Dir(target), os.ModePerm)
if err != nil {
return err
return errc.New(errc.ErrMkdirAll, err.Error())
}
text := string(t)
_, err = util.WriteFile(target, text)
Expand Down
3 changes: 3 additions & 0 deletions tool/util/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func GetGoModPath() (string, error) {
// If module-aware mode is enabled, but there is no go.mod, GOMOD will be
// os.DevNull ("/dev/null" on Unix-like systems, "NUL" on Windows).
// If module-aware mode is disabled, GOMOD will be the empty string.
// The rationale on why using Output instead of CombinedOutput variant is
// that we only care about the output of the command, and we can handle the
// error when the command fails.
out, err := RunCmdOutput("go", "env", "GOMOD")
if err != nil {
return "", err
Expand Down
12 changes: 12 additions & 0 deletions tool/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ func RunCmd(args ...string) error {
}

func RunCmdOutput(args ...string) (string, error) {
path := args[0]
args = args[1:]
cmd := exec.Command(path, args...)
out, err := cmd.Output()
if err != nil {
return "", errc.New(errc.ErrRunCmd, string(out)).
With("command", fmt.Sprintf("%v", args))
}
return string(out), nil
}

func RunCmdCombinedOutput(args ...string) (string, error) {
path := args[0]
args = args[1:]
cmd := exec.Command(path, args...)
Expand Down
Loading