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(gomod): prefer version specified by toolchain directive when present #565

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions extractor/filesystem/language/golang/gomod/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) ([]
}
}

// Give the toolchain version priority, if present
if parsedLockfile.Toolchain != nil && parsedLockfile.Toolchain.Name != "" {
version, _, _ := strings.Cut(parsedLockfile.Toolchain.Name, "-")

packages[mapKey{name: "stdlib"}] = &extractor.Inventory{
Name: "stdlib",
Version: strings.TrimPrefix(version, "go"),
Locations: []string{input.Path},
}
}

// The map values might have changed after replacement so we need to run another
// deduplication pass.
dedupedPs := map[mapKey]*extractor.Inventory{}
Expand Down
36 changes: 36 additions & 0 deletions extractor/filesystem/language/golang/gomod/gomod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,42 @@ func TestExtractor_Extract(t *testing.T) {
},
},
},
{
Name: "toolchain",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/toolchain.mod",
},
WantInventory: []*extractor.Inventory{
{
Name: "github.com/BurntSushi/toml",
Version: "1.0.0",
Locations: []string{"testdata/toolchain.mod"},
},
{
Name: "stdlib",
Version: "1.23.6",
Locations: []string{"testdata/toolchain.mod"},
},
},
},
{
Name: "toolchain with suffix",
InputConfig: extracttest.ScanInputMockConfig{
Path: "testdata/toolchain-with-suffix.mod",
},
WantInventory: []*extractor.Inventory{
{
Name: "github.com/BurntSushi/toml",
Version: "1.0.0",
Locations: []string{"testdata/toolchain-with-suffix.mod"},
},
{
Name: "stdlib",
Version: "1.23.6",
Locations: []string{"testdata/toolchain-with-suffix.mod"},
},
},
},
{
Name: "indirect packages",
InputConfig: extracttest.ScanInputMockConfig{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module my-library

go 1.23.5

toolchain go1.23.6-xyz

require (
github.com/BurntSushi/toml v1.0.0
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module my-library

go 1.23.5

toolchain go1.23.6

require (
github.com/BurntSushi/toml v1.0.0
)
Loading