Skip to content

Commit

Permalink
Filter out main modules (#17)
Browse files Browse the repository at this point in the history
This fixes an issue with multi-module projects that accidentially
vendored main modules, causing already-exists errors when symlinking.

The same strategy is employed by go in the `go mod vendor` command, see
https://github.com/golang/go/blob/81efd7b347dd6d7f12fd49c6eee0274005734c71/src/cmd/go/internal/modcmd/vendor.go#L100
  • Loading branch information
adracus authored Nov 2, 2022
1 parent e7a74a1 commit 1829a8f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
26 changes: 20 additions & 6 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ func BuildModuleNodes(modules []Module) ([]Node, error) {
}

type Module struct {
Path string
Dir string
Path string
Dir string
Version string
Main bool
}

type moduleReader struct {
Expand Down Expand Up @@ -181,10 +183,6 @@ func ParseModules(r io.Reader) ([]Module, error) {
return nil, err
}

// Don't include indirect modules without directory.
if mod.Dir == "" {
continue
}
mods = append(mods, mod)
}
return mods, nil
Expand All @@ -205,6 +203,20 @@ func ReadModules() ([]Module, error) {
return mods, nil
}

func FilterVendorModules(modules []Module) []Module {
var res []Module
for _, module := range modules {
// Don't vendor modules without paths / main modules.
if module.Dir == "" || module.Version == "" && module.Main {
continue
}

res = append(res, module)
}

return res
}

type Options struct {
SkipGoBin bool
SkipGoSrc bool
Expand Down Expand Up @@ -268,6 +280,8 @@ func LinkGoSrc(dstDir string) error {
return fmt.Errorf("error reading modules: %w", err)
}

mods = FilterVendorModules(mods)

nodes, err := BuildModuleNodes(mods)
if err != nil {
return fmt.Errorf("error building module tree: %w", err)
Expand Down
17 changes: 14 additions & 3 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

var _ = Describe("Internal", func() {
var (
tmpDir string
moduleA, moduleB, moduleB1, moduleB11, moduleB2, moduleC Module
tmpDir string
moduleA, moduleB, moduleB1, moduleB11, moduleB2, moduleC, moduleD Module
)
BeforeEach(func() {
var err error
Expand All @@ -41,6 +41,7 @@ var _ = Describe("Internal", func() {
moduleA = Module{
Path: "a",
Dir: "/tmp/a",
Main: true,
}
moduleB = Module{
Path: "example.org/b",
Expand All @@ -62,6 +63,9 @@ var _ = Describe("Internal", func() {
Path: "example.org/user/c",
Dir: "/tmp/example.org/user/c",
}
moduleD = Module{
Path: "example.org/d",
}
})
AfterEach(func() {
if tmpDir != "" {
Expand Down Expand Up @@ -146,7 +150,14 @@ var _ = Describe("Internal", func() {
mods, err := ParseModules(bytes.NewReader(data))
Expect(err).NotTo(HaveOccurred())

Expect(mods).To(Equal([]Module{moduleA, moduleB}))
Expect(mods).To(Equal([]Module{moduleA, moduleB, moduleD}))
})
})

Describe("FilterVendorModules", func() {
It("should correctly filter the modules", func() {
mods := FilterVendorModules([]Module{moduleA, moduleB, moduleD})
Expect(mods).To(Equal([]Module{moduleB}))
})
})

Expand Down
3 changes: 2 additions & 1 deletion internal/testdata/modules.json.stream
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Path": "a",
"Dir": "/tmp/a"
"Dir": "/tmp/a",
"Main": true
}
{
"Path": "example.org/b",
Expand Down

0 comments on commit 1829a8f

Please sign in to comment.