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

Cleanup only non-gqlgen packages when reloading all packages #2598

Merged
merged 1 commit into from
Apr 7, 2023
Merged
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
36 changes: 35 additions & 1 deletion internal/code/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"strings"
"sync"

"golang.org/x/tools/go/packages"
)

var once = sync.Once{}
var modInfo *debug.BuildInfo

var mode = packages.NeedName |
packages.NeedFiles |
packages.NeedImports |
Expand All @@ -31,10 +37,38 @@ type Packages struct {
numNameCalls int // stupid test steam. ignore.
}

func (p *Packages) CleanupUserPackages() {
once.Do(func() {
var ok bool
modInfo, ok = debug.ReadBuildInfo()
if !ok {
modInfo = nil
}
})

// Don't cleanup github.com/99designs/gqlgen prefixed packages, they haven't changed and do not need to be reloaded
if modInfo != nil {
var toRemove []string
for k := range p.packages {
if !strings.HasPrefix(k, modInfo.Main.Path) {
toRemove = append(toRemove, k)
}
}

for _, k := range toRemove {
delete(p.packages, k)
}
} else {
p.packages = nil // Cleanup all packages if we don't know for some reason which ones to keep
}
}

// ReloadAll will call LoadAll after clearing the package cache, so we can reload
// packages in the case that the packages have changed
func (p *Packages) ReloadAll(importPaths ...string) []*packages.Package {
p.packages = nil
if p.packages != nil {
p.CleanupUserPackages()
}
return p.LoadAll(importPaths...)
}

Expand Down