Skip to content

Commit

Permalink
fix: stop loading package dependencies (#2988)
Browse files Browse the repository at this point in the history
  • Loading branch information
agratta-silo authored Apr 6, 2024
1 parent d0a1aec commit 99d7d88
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 26 deletions.
20 changes: 2 additions & 18 deletions internal/code/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ var (

var mode = packages.NeedName |
packages.NeedFiles |
packages.NeedImports |
packages.NeedTypes |
packages.NeedSyntax |
packages.NeedTypesInfo |
packages.NeedModule |
packages.NeedDeps
packages.NeedModule

type (
// Packages is a wrapper around x/tools/go/packages that maintains a (hopefully prewarmed) cache of packages
Expand Down Expand Up @@ -135,11 +133,6 @@ func (p *Packages) LoadAll(importPaths ...string) []*packages.Package {
func (p *Packages) addToCache(pkg *packages.Package) {
imp := NormalizeVendor(pkg.PkgPath)
p.packages[imp] = pkg
for _, imp := range pkg.Imports {
if _, found := p.packages[NormalizeVendor(imp.PkgPath)]; !found {
p.addToCache(imp)
}
}
}

// Load works the same as LoadAll, except a single package at a time.
Expand Down Expand Up @@ -220,18 +213,9 @@ func (p *Packages) NameForPackage(importPath string) string {
return pkg.Name
}

// Evict removes a given package import path from the cache, along with any packages that depend on it. Further calls
// to Load will fetch it from disk.
// Evict removes a given package import path from the cache. Further calls to Load will fetch it from disk.
func (p *Packages) Evict(importPath string) {
delete(p.packages, importPath)

for _, pkg := range p.packages {
for _, imported := range pkg.Imports {
if imported.PkgPath == importPath {
p.Evict(pkg.PkgPath)
}
}
}
}

func (p *Packages) ModTidy() error {
Expand Down
8 changes: 0 additions & 8 deletions internal/code/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ func TestPackages(t *testing.T) {
require.Equal(t, 2, p.numLoadCalls)
})

t.Run("evicting a package also evicts its dependencies", func(t *testing.T) {
p := initialState(t)
p.Evict("github.com/99designs/gqlgen/internal/code/testdata/a")
require.Equal(t, "a", p.Load("github.com/99designs/gqlgen/internal/code/testdata/a").Name)
require.Equal(t, 2, p.numLoadCalls)
require.Equal(t, "b", p.Load("github.com/99designs/gqlgen/internal/code/testdata/b").Name)
require.Equal(t, 3, p.numLoadCalls)
})
t.Run("able to load private package with build tags", func(t *testing.T) {
p := initialState(t, WithBuildTags("private"))
p.Evict("github.com/99designs/gqlgen/internal/code/testdata/a")
Expand Down

0 comments on commit 99d7d88

Please sign in to comment.