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

List of packages include "replace" package #184

Closed
rhcarvalho opened this issue Mar 23, 2020 · 4 comments · Fixed by #199
Closed

List of packages include "replace" package #184

rhcarvalho opened this issue Mar 23, 2020 · 4 comments · Fixed by #199

Comments

@rhcarvalho
Copy link
Contributor

Among packages from go.mod, package list is also showing a replace entry, looks wrong:

image

Here's the code that needs improvement:

sentry-go/integrations.go

Lines 70 to 104 in a0f55ee

func getModulesFromMod() (map[string]string, error) {
modules := make(map[string]string)
file, err := os.Open("go.mod")
if err != nil {
return nil, fmt.Errorf("unable to open mod file")
}
defer file.Close()
areModulesPresent := false
scanner := bufio.NewScanner(file)
for scanner.Scan() {
splits := strings.Split(scanner.Text(), " ")
if splits[0] == "require" {
areModulesPresent = true
// Mod file has only 1 dependency
if len(splits) > 2 {
modules[strings.TrimSpace(splits[1])] = splits[2]
return modules, nil
}
} else if areModulesPresent && splits[0] != ")" && splits[0] != "" {
modules[strings.TrimSpace(splits[0])] = splits[1]
}
}
if scannerErr := scanner.Err(); scannerErr != nil {
return nil, scannerErr
}
return modules, nil
}

@wingyplus
Copy link
Contributor

wingyplus commented Apr 3, 2020

I prototype it using golang.org/x/mod module:

package main

import (
	"fmt"
	"log"

	"golang.org/x/mod/modfile"
)

func main() {
	mod := []byte(`module github.com/wingyplus/sentry-exp

go 1.14

require (
	github.com/getsentry/sentry-go v0.0.0
	github.com/gin-gonic/gin v1.6.2
)

replace github.com/getsentry/sentry-go v0.0.0 => ./pkg/sentry-go
`)
	f, err := modfile.Parse("go.mod", mod, nil)
	if err != nil {
		log.Fatal(err)
	}

	for _, req := range f.Require {
		fmt.Println(req.Mod.Path, req.Mod.Version)
	}
}

Result will be:

github.com/getsentry/sentry-go v0.0.0
github.com/gin-gonic/gin v1.6.2

@rhcarvalho
Copy link
Contributor Author

FTR for binaries built in Module Mode, there is https://golang.org/pkg/runtime/debug/#BuildInfo.
No dependencies required.

@wingyplus
Copy link
Contributor

@rhcarvalho Nice! it's works!

@wingyplus
Copy link
Contributor

@rhcarvalho I opened PR to fix this problem. It's good to see your feedback. :)

rhcarvalho pushed a commit to wingyplus/sentry-go that referenced this issue Apr 17, 2020
rhcarvalho added a commit that referenced this issue Apr 17, 2020
runtime/debug.ReadBuildInfo is more reliable than manually parsing a `go.mod`
file, specially without assistance from golang.org/x/mod/modfile. Another
advantage is that it doesn't require `go.mod` and source code to be available.
We also now include the current module (the module with package main) in the
list of modules.

runtime/debug.ReadBuildInfo requires go 1.12, which is already the minimum
supported Go version. Thus bump the language version in go.mod.

Drop support for reading dependencies from govendor's `vendor/vendor.json`.

Fixes #184.
Fixes #197.

Co-authored-by: Rodolfo Carvalho <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants