Skip to content

Commit

Permalink
Merge pull request #1294 from 99designs/fix-init
Browse files Browse the repository at this point in the history
Allow rewriter to work on empty but potentially importable packages
  • Loading branch information
lwc authored Aug 18, 2020
2 parents 08eee0f + a87c54a commit eb424a2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
3 changes: 3 additions & 0 deletions internal/code/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func TestImportPathForDir(t *testing.T) {
// directory does not exist
assert.Equal(t, "github.com/99designs/gqlgen/dos", ImportPathForDir(filepath.Join(wd, "..", "..", "dos")))

// out of module
assert.Equal(t, "", ImportPathForDir(filepath.Join(wd, "..", "..", "..")))

if runtime.GOOS == "windows" {
assert.Equal(t, "", ImportPathForDir("C:/doesnotexist"))
} else {
Expand Down
14 changes: 6 additions & 8 deletions internal/rewrite/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"

"github.com/99designs/gqlgen/internal/code"
"golang.org/x/tools/go/packages"
)

Expand All @@ -19,7 +20,11 @@ type Rewriter struct {
copied map[ast.Decl]bool
}

func New(importPath string) (*Rewriter, error) {
func New(dir string) (*Rewriter, error) {
importPath := code.ImportPathForDir(dir)
if importPath == "" {
return nil, fmt.Errorf("import path not found for directory: %q", dir)
}
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedSyntax | packages.NeedTypes,
}, importPath)
Expand All @@ -29,13 +34,6 @@ func New(importPath string) (*Rewriter, error) {
if len(pkgs) == 0 {
return nil, fmt.Errorf("package not found for importPath: %s", importPath)
}
if len(pkgs[0].Errors) != 0 {
for _, e := range pkgs[0].Errors {
if e.Kind == packages.ListError {
return nil, e
}
}
}

return &Rewriter{
pkg: pkgs[0],
Expand Down
2 changes: 1 addition & 1 deletion internal/rewrite/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestRewriter(t *testing.T) {
t.Run("default", func(t *testing.T) {
r, err := New("github.com/99designs/gqlgen/internal/rewrite/testdata")
r, err := New("testdata")
require.NoError(t, err)

body := r.GetMethodBody("Foo", "Method")
Expand Down
2 changes: 1 addition & 1 deletion plugin/resolvergen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (m *Plugin) generateSingleFile(data *codegen.Data) error {
}

func (m *Plugin) generatePerSchema(data *codegen.Data) error {
rewriter, err := rewrite.New(data.Config.Resolver.ImportPath())
rewriter, err := rewrite.New(data.Config.Resolver.Dir())
if err != nil {
return err
}
Expand Down

0 comments on commit eb424a2

Please sign in to comment.