Skip to content

Commit

Permalink
fix: load packages correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
y1yang0 committed Jan 21, 2025
1 parent 4485e2f commit fc33129
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 242 deletions.
6 changes: 3 additions & 3 deletions test/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func TestBuildProject(t *testing.T) {
RunGoBuild(t, "go", "build", "-o", "./cmd", "./cmd")
RunGoBuild(t, "go", "build", "cmd/foo.go")
RunGoBuild(t, "go", "build", "cmd/foo.go", "cmd/bar.go")
RunGoBuild(t, "go", "build", "cmd")
}

func TestBuildProject2(t *testing.T) {
Expand All @@ -36,6 +35,7 @@ func TestBuildProject2(t *testing.T) {

RunGoBuild(t, "go", "build", ".")
RunGoBuild(t, "go", "build", "")
RunGoBuild(t, "go", "build", "./...")
}

func TestBuildProject3(t *testing.T) {
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestBuildProject5(t *testing.T) {
// both test_fmt.json and default.json rules should be available
// because we always append new -rule to the default.json by default
ExpectPreprocessContains(t, util.DebugLogFile, "fmt")
ExpectPreprocessContains(t, util.DebugLogFile, "database/sql")
ExpectPreprocessContains(t, util.DebugLogFile, "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/http")
}

func TestBuildProject6(t *testing.T) {
Expand All @@ -84,5 +84,5 @@ func TestBuildProject6(t *testing.T) {
RunGoBuild(t, "go", "build", "m1")
// only test_fmt.json should be available because -disabledefault is set
ExpectPreprocessContains(t, util.DebugLogFile, "fmt")
ExpectPreprocessNotContains(t, util.DebugLogFile, "database/sql")
ExpectPreprocessNotContains(t, util.DebugLogFile, "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/http")
}
2 changes: 0 additions & 2 deletions tool/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ func fatal(err error) {
message += fmt.Sprintf("%-11s: %s\n", "Command", strings.Join(os.Args, " "))
message += fmt.Sprintf("%-11s: %s\n", "ErrorLog", util.GetLoggerPath())
message += fmt.Sprintf("%-11s: %s\n", "WorkDir", os.Getenv("PWD"))
path, _ := util.GetGoModPath()
message += fmt.Sprintf("%-11s: %s\n", "GoMod", path)
message += fmt.Sprintf("%-11s: %s, %s, %s\n", "Toolchain",
runtime.GOOS+"/"+runtime.GOARCH,
runtime.Version(), config.ToolVersion)
Expand Down
1 change: 1 addition & 0 deletions tool/errc/errcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
ErrNotModularized
ErrGetExecutable
ErrInstrument
ErrPreprocess
)

var errMessages = map[int]string{
Expand Down
7 changes: 4 additions & 3 deletions tool/preprocess/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ type moduleHolder struct {
Dir string // absolute path to cached source root directory
}

func fetchNetwork(path string) (string, error) {
func (dp *DepProcessor) fetchNetwork(path string) (string, error) {
// 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)
text, err := runCmdOutput(dp.getGoModDir(),
"go", "mod", "download", "-json", path)
if err != nil {
return "", err

Expand Down Expand Up @@ -125,7 +126,7 @@ func (dp *DepProcessor) fetchFrom(path string) (string, error) {
}

// Download the module to the local file system
dir, err := fetchNetwork(path)
dir, err := dp.fetchNetwork(path)
if err != nil {
return "", err
}
Expand Down
9 changes: 2 additions & 7 deletions tool/preprocess/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,7 @@ func findFlagValue(cmd []string, flag string) string {
return ""
}

func parseVendorModules() (map[string]string, error) {
util.Assert(util.IsVendorBuild(), "why not otherwise")
projDir, err := util.GetProjRootDir()
if err != nil {
return nil, err
}
func parseVendorModules(projDir string) (map[string]string, error) {
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 @@ -353,7 +348,7 @@ func (dp *DepProcessor) matchRules(compileCmds []string) error {
// If we are in vendor mode, we need to parse the vendor/modules.txt file
// to get the version of each module for future matching
if dp.vendorBuild {
modules, err := parseVendorModules()
modules, err := parseVendorModules(dp.getGoModDir())
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit fc33129

Please sign in to comment.