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

#2298: fix gqlgen extracting module name from comment line #2299

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion internal/code/imports.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package code

import (
"bufio"
"fmt"
"go/build"
"go/parser"
"go/token"
Expand Down Expand Up @@ -74,7 +76,7 @@ func goModuleRoot(dir string) (string, bool) {
}

if content, err := os.ReadFile(filepath.Join(modDir, "go.mod")); err == nil {
moduleName := string(modregex.FindSubmatch(content)[1])
moduleName := extractModuleName(content)
result = goModuleSearchResult{
path: moduleName,
goModPath: modDir,
Expand Down Expand Up @@ -126,6 +128,27 @@ func goModuleRoot(dir string) (string, bool) {
return res.path, true
}

func extractModuleName(content []byte) string {
for {
advance, tkn, err := bufio.ScanLines(content, false)
if err != nil {
panic(fmt.Errorf("error parsing mod file: %w", err))
}
if advance == 0 {
break
}
s := strings.Trim(string(tkn), " \t")
if len(s) != 0 && !strings.HasPrefix(s, "//") {
break
}
if advance <= len(content) {
content = content[advance:]
}
}
moduleName := string(modregex.FindSubmatch(content)[1])
return moduleName
}

// ImportPathForDir takes a path and returns a golang import path for the package
func ImportPathForDir(dir string) (res string) {
dir, err := filepath.Abs(dir)
Expand Down
12 changes: 12 additions & 0 deletions internal/code/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ import (
"github.com/stretchr/testify/require"
)

func TestExtractModuleName(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)

modDir := filepath.Join(wd, "..", "..", "testdata")
content, err := os.ReadFile(filepath.Join(modDir, "gomod-with-leading-comments.mod"))
require.NoError(t, err)
assert.Equal(t, "github.com/99designs/gqlgen", extractModuleName(content))
}

func TestImportPathForDir(t *testing.T) {
wd, err := os.Getwd()

require.NoError(t, err)

assert.Equal(t, "github.com/99designs/gqlgen/internal/code", ImportPathForDir(wd))

assert.Equal(t, "github.com/99designs/gqlgen/internal/code", ImportPathForDir(wd))
assert.Equal(t, "github.com/99designs/gqlgen/api", ImportPathForDir(filepath.Join(wd, "..", "..", "api")))

Expand Down
7 changes: 7 additions & 0 deletions testdata/gomod-with-leading-comments.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// main module of gqlgen

// and another module to test stripping of comment lines

module github.com/99designs/gqlgen // replace it for new project

go 1.16