diff --git a/cmd/cover.go b/cmd/cover.go index b139ff4..fef9874 100644 --- a/cmd/cover.go +++ b/cmd/cover.go @@ -18,8 +18,8 @@ package cmd import ( "fmt" - "github.com/spf13/viper" log "github.com/sirupsen/logrus" + "github.com/spf13/viper" "os" "strings" diff --git a/cmd/profile.go b/cmd/profile.go index 2817a2f..c1d0918 100644 --- a/cmd/profile.go +++ b/cmd/profile.go @@ -19,8 +19,8 @@ package cmd import ( "bytes" "fmt" - "io" log "github.com/sirupsen/logrus" + "io" "os" "github.com/qiniu/goc/pkg/cover" diff --git a/pkg/build/build.go b/pkg/build/build.go index cf80fcd..d40ea88 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -18,12 +18,13 @@ package build import ( "fmt" - "github.com/qiniu/goc/pkg/cover" - log "github.com/sirupsen/logrus" "os" "os/exec" "path/filepath" "strings" + + "github.com/qiniu/goc/pkg/cover" + log "github.com/sirupsen/logrus" ) // Build is to describe the building/installing process of a goc build/install @@ -51,8 +52,8 @@ func NewBuild(buildflags string, packages string, outputDir string) *Build { if false == b.validatePackageForBuild() { log.Fatalln("packages only support \".\"") } - b.Target = b.determineOutputDir(outputDir) b.MvProjectsToTmp() + b.Target = b.determineOutputDir(outputDir) return b } @@ -79,6 +80,9 @@ func (b *Build) Build() { // determineOutputDir, as we only allow . as package name, // the binary name is always same as the directory name of current directory func (b *Build) determineOutputDir(outputDir string) string { + if b.TmpDir == "" { + log.Fatalln("Can only be called after Build.MvProjectsToTmp().") + } curWorkingDir, err := os.Getwd() if err != nil { log.Fatalf("Cannot get current working directory, the err: %v.", err) @@ -86,8 +90,11 @@ func (b *Build) determineOutputDir(outputDir string) string { // if if outputDir == "" { _, last := filepath.Split(curWorkingDir) - // replace "_" with "-" in the import path - last = strings.ReplaceAll(last, "_", "-") + if b.IsMod { + // in mod, special rule + // replace "_" with "-" in the import path + last = strings.ReplaceAll(last, "_", "-") + } return filepath.Join(curWorkingDir, last) } abs, err := filepath.Abs(outputDir) diff --git a/pkg/build/gomodules.go b/pkg/build/gomodules.go index 493a252..f593c8d 100644 --- a/pkg/build/gomodules.go +++ b/pkg/build/gomodules.go @@ -19,7 +19,6 @@ package build import ( log "github.com/sirupsen/logrus" - "github.com/otiai10/copy" ) diff --git a/pkg/build/legacy.go b/pkg/build/legacy.go index 13a499f..ed6244a 100644 --- a/pkg/build/legacy.go +++ b/pkg/build/legacy.go @@ -48,7 +48,7 @@ func (b *Build) cpLegacyProject() { // only cp dependency in root(current gopath), // skip deps in other GOPATHs -func (b *Build) cpDepPackages( pkg *cover.Package, visited map[string]bool) { +func (b *Build) cpDepPackages(pkg *cover.Package, visited map[string]bool) { /* oriGOPATH := os.Getenv("GOPATH") if oriGOPATH == "" { diff --git a/pkg/cover/server.go b/pkg/cover/server.go index de96123..721ee05 100644 --- a/pkg/cover/server.go +++ b/pkg/cover/server.go @@ -19,9 +19,9 @@ package cover import ( "bytes" "fmt" + log "github.com/sirupsen/logrus" "io" "io/ioutil" - log "github.com/sirupsen/logrus" "net" "net/http" "net/url" diff --git a/tests/e2e/simple_project_test.go b/tests/e2e/simple_project_test.go index 7245c6c..370838f 100644 --- a/tests/e2e/simple_project_test.go +++ b/tests/e2e/simple_project_test.go @@ -55,7 +55,7 @@ var _ = Describe("E2E", func() { By("goc build") testProjDir := filepath.Join(TESTS_ROOT, "samples/simple_project") - cmd := exec.Command("goc", "build") + cmd := exec.Command("goc", "build", "--debuggoc") cmd.Dir = testProjDir out, err := cmd.CombinedOutput() @@ -84,7 +84,7 @@ var _ = Describe("E2E", func() { obj := filepath.Join(dir, "simple-project") fInfo, err := os.Lstat(obj) Expect(err).To(BeNil()) - Expect(startTime.Before(fInfo.ModTime())).To(Equal(true), "new binary should be generated, not the old one") + Expect(startTime.Before(fInfo.ModTime())).To(Equal(true), obj+"new binary should be generated, not the old one") cmd := exec.Command("go", "tool", "objdump", "simple-project") cmd.Dir = dir @@ -114,12 +114,13 @@ var _ = Describe("E2E", func() { GOPATH = testProjDir By("goc build") - cmd := exec.Command("goc", "build") + cmd := exec.Command("goc", "build", "--debuggoc") cmd.Dir = oriWorkingDir // use GOPATH mode to compile project cmd.Env = append(os.Environ(), fmt.Sprintf("GOPATH=%v", GOPATH), "GO111MODULE=off") out, err := cmd.CombinedOutput() + fmt.Println(string(out)) Expect(err).To(BeNil(), "goc build on this project should be successful", string(out), cmd.Dir) By("goc install") @@ -145,7 +146,7 @@ var _ = Describe("E2E", func() { By("check generated binary") objects := []string{GOPATH + "/bin", oriWorkingDir} for _, dir := range objects { - obj := filepath.Join(dir, "simple-gopath-project") + obj := filepath.Join(dir, "simple_gopath_project") fInfo, err := os.Lstat(obj) Expect(err).To(BeNil()) Expect(startTime.Before(fInfo.ModTime())).To(Equal(true), "new binary should be generated, not the old one")