diff --git a/syft/pkg/url.go b/syft/pkg/url.go index a2d2d4b8e26..19fd1e9c869 100644 --- a/syft/pkg/url.go +++ b/syft/pkg/url.go @@ -47,8 +47,10 @@ func URL(p Package, release *linux.Release) string { case p.Type == GoModulePkg: re := regexp.MustCompile(`(/)[^/]*$`) fields := re.Split(p.Name, -1) - namespace = fields[0] - name = strings.TrimPrefix(p.Name, namespace+"/") + if len(fields) > 1 { + namespace = fields[0] + name = strings.TrimPrefix(p.Name, namespace+"/") + } case p.Type == NpmPkg: fields := strings.SplitN(p.Name, "/", 2) if len(fields) > 1 { diff --git a/syft/pkg/url_test.go b/syft/pkg/url_test.go index 9e33acd31be..1e68ee5d8a7 100644 --- a/syft/pkg/url_test.go +++ b/syft/pkg/url_test.go @@ -25,6 +25,15 @@ func TestPackageURL(t *testing.T) { }, expected: "pkg:golang/github.com/anchore/syft@v0.1.0", }, + { + name: "golang short name", + pkg: Package{ + Name: "go.opencensus.io", + Version: "v0.23.0", + Type: GoModulePkg, + }, + expected: "pkg:golang/go.opencensus.io@v0.23.0", + }, { name: "pub", pkg: Package{ @@ -237,7 +246,7 @@ func TestPackageURL(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - if test.pkg.Type != "" { + if test.pkg.Type != "" && !contains(pkgTypes, string(test.pkg.Type)) { pkgTypes = append(pkgTypes, string(test.pkg.Type)) } actual := URL(test.pkg, test.distro) @@ -250,3 +259,13 @@ func TestPackageURL(t *testing.T) { } assert.ElementsMatch(t, expectedTypes.List(), pkgTypes, "missing one or more package types to test against (maybe a package type was added?)") } + +func contains(values []string, val string) bool { + for _, v := range values { + if val == v { + return true + } + } + + return false +}