From b829628d3186975544ee375cc23bf5cb778965a5 Mon Sep 17 00:00:00 2001 From: Viktor Stanchev Date: Tue, 26 Nov 2019 15:06:53 +0300 Subject: [PATCH 1/2] shortcut QualifyPackagePath in go module mode --- internal/code/imports.go | 23 +++++++++++++++++++---- internal/code/util.go | 5 +++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/code/imports.go b/internal/code/imports.go index ad62f7c56b..b9369f0369 100644 --- a/internal/code/imports.go +++ b/internal/code/imports.go @@ -51,21 +51,21 @@ func NameForDir(dir string) string { return SanitizePackageName(filepath.Base(dir)) } -// ImportPathForDir takes a path and returns a golang import path for the package -func ImportPathForDir(dir string) (res string) { +// GoModuleRoot returns the root of the current go module if there is a go.mod file in the directory tree +// If not, it returns false +func GoModuleRoot(dir string) (string, bool) { dir, err := filepath.Abs(dir) if err != nil { panic(err) } dir = filepath.ToSlash(dir) - modDir := dir assumedPart := "" for { f, err := ioutil.ReadFile(filepath.Join(modDir, "go.mod")) if err == nil { // found it, stop searching - return string(modregex.FindSubmatch(f)[1]) + assumedPart + return string(modregex.FindSubmatch(f)[1]) + assumedPart, true } assumedPart = "/" + filepath.Base(modDir) + assumedPart @@ -80,6 +80,21 @@ func ImportPathForDir(dir string) (res string) { } modDir = parentDir } + return "", false +} + +// ImportPathForDir takes a path and returns a golang import path for the package +func ImportPathForDir(dir string) (res string) { + dir, err := filepath.Abs(dir) + if err != nil { + panic(err) + } + dir = filepath.ToSlash(dir) + + modDir, ok := GoModuleRoot(dir) + if ok { + return modDir + } for _, gopath := range gopaths { if len(gopath) < len(dir) && strings.EqualFold(gopath, dir[0:len(gopath)]) { diff --git a/internal/code/util.go b/internal/code/util.go index 2be83a23ce..56442bf3ff 100644 --- a/internal/code/util.go +++ b/internal/code/util.go @@ -41,6 +41,11 @@ func NormalizeVendor(pkg string) string { func QualifyPackagePath(importPath string) string { wd, _ := os.Getwd() + // in go module mode, the import path doesn't need fixing + if _, ok := GoModuleRoot(wd); ok { + return importPath + } + pkg, err := build.Import(importPath, wd, 0) if err != nil { return importPath From 4db0e6eccc8745ed765f3863d221ea13c57f0bd1 Mon Sep 17 00:00:00 2001 From: Viktor Stanchev Date: Tue, 26 Nov 2019 17:27:43 +0300 Subject: [PATCH 2/2] keep function private --- internal/code/imports.go | 6 +++--- internal/code/util.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/code/imports.go b/internal/code/imports.go index b9369f0369..3660079633 100644 --- a/internal/code/imports.go +++ b/internal/code/imports.go @@ -51,9 +51,9 @@ func NameForDir(dir string) string { return SanitizePackageName(filepath.Base(dir)) } -// GoModuleRoot returns the root of the current go module if there is a go.mod file in the directory tree +// goModuleRoot returns the root of the current go module if there is a go.mod file in the directory tree // If not, it returns false -func GoModuleRoot(dir string) (string, bool) { +func goModuleRoot(dir string) (string, bool) { dir, err := filepath.Abs(dir) if err != nil { panic(err) @@ -91,7 +91,7 @@ func ImportPathForDir(dir string) (res string) { } dir = filepath.ToSlash(dir) - modDir, ok := GoModuleRoot(dir) + modDir, ok := goModuleRoot(dir) if ok { return modDir } diff --git a/internal/code/util.go b/internal/code/util.go index 56442bf3ff..cbe40858e2 100644 --- a/internal/code/util.go +++ b/internal/code/util.go @@ -42,7 +42,7 @@ func QualifyPackagePath(importPath string) string { wd, _ := os.Getwd() // in go module mode, the import path doesn't need fixing - if _, ok := GoModuleRoot(wd); ok { + if _, ok := goModuleRoot(wd); ok { return importPath }