Skip to content

Commit

Permalink
People who use gqlgen as a library get errors following #2598 (#3431)
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Coffman <[email protected]>
  • Loading branch information
StevenACoffman authored Dec 10, 2024
1 parent 26220f6 commit 7ba0197
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
2 changes: 2 additions & 0 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (c *Config) Init() error {
if c.Packages == nil {
c.Packages = code.NewPackages(
code.WithBuildTags(c.GoBuildTags...),
code.PackagePrefixToCache("github.com/99designs/gqlgen/graphql"),
)
}

Expand Down Expand Up @@ -856,6 +857,7 @@ func (c *Config) LoadSchema() error {
if c.Packages != nil {
c.Packages = code.NewPackages(
code.WithBuildTags(c.GoBuildTags...),
code.PackagePrefixToCache("github.com/99designs/gqlgen/graphql"),
)
}

Expand Down
48 changes: 23 additions & 25 deletions internal/code/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime/debug"
"strings"
"sync"

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

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

var mode = packages.NeedName |
packages.NeedFiles |
packages.NeedTypes |
Expand All @@ -30,10 +23,11 @@ type (
// Packages is a wrapper around x/tools/go/packages that maintains a (hopefully prewarmed) cache of packages
// that can be invalidated as writes are made and packages are known to change.
Packages struct {
packages map[string]*packages.Package
importToName map[string]string
loadErrors []error
buildFlags []string
packages map[string]*packages.Package
importToName map[string]string
loadErrors []error
buildFlags []string
packagesToCachePrefix string

numLoadCalls int // stupid test steam. ignore.
numNameCalls int // stupid test steam. ignore.
Expand All @@ -42,13 +36,21 @@ type (
Option func(p *Packages)
)

// WithBuildTags adds build tags to the packages.Load call
// WithBuildTags option for NewPackages adds build tags to the packages.Load call
func WithBuildTags(tags ...string) func(p *Packages) {
return func(p *Packages) {
p.buildFlags = append(p.buildFlags, "-tags", strings.Join(tags, ","))
}
}

// PackagePrefixToCache option for NewPackages
// will not reset gqlgen packages in packages.Load call
func PackagePrefixToCache(prefixPath string) func(p *Packages) {
return func(p *Packages) {
p.packagesToCachePrefix = prefixPath
}
}

// NewPackages creates a new packages cache
// It will load all packages in the current module, and any packages that are passed to Load or LoadAll
func NewPackages(opts ...Option) *Packages {
Expand All @@ -60,27 +62,23 @@ func NewPackages(opts ...Option) *Packages {
}

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 {
if p.packagesToCachePrefix == "" {
// Cleanup all packages if we don't know which ones to keep
p.packages = nil
} else {
// Don't clean up github.com/99designs/gqlgen prefixed packages,
// they haven't changed and do not need to be reloaded
// if you are using a fork, then you need to have customized
// the prefix using PackagePrefixToCache
var toRemove []string
for k := range p.packages {
if !strings.HasPrefix(k, modInfo.Main.Path) {
if !strings.HasPrefix(k, p.packagesToCachePrefix) {
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
}
}

Expand Down

0 comments on commit 7ba0197

Please sign in to comment.