fix newGopath set & target name

This commit is contained in:
tongjingran 2020-12-14 20:16:01 +08:00
parent d8cd308ce1
commit 1fb530c9ab
5 changed files with 13 additions and 70 deletions

View File

@ -133,8 +133,11 @@ func (b *Build) determineOutputDir(outputDir string) (string, error) {
targetName := "" targetName := ""
for _, pkg := range b.Pkgs { for _, pkg := range b.Pkgs {
if pkg.Name == "main" { if pkg.Name == "main" {
_, file := filepath.Split(pkg.Target) if pkg.Target != ""{
targetName = file targetName = filepath.Base(pkg.Target)
}else {
targetName = filepath.Base(pkg.Dir)
}
break break
} }
} }

View File

@ -17,14 +17,13 @@
package build package build
import ( import (
"os"
"path/filepath" "path/filepath"
"strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/otiai10/copy" "github.com/otiai10/copy"
"github.com/qiniu/goc/pkg/cover" "github.com/qiniu/goc/pkg/cover"
"strings"
) )
func (b *Build) cpLegacyProject() { func (b *Build) cpLegacyProject() {
@ -38,13 +37,11 @@ func (b *Build) cpLegacyProject() {
continue continue
} }
if err := b.copyDir(v, b.TmpDir); err != nil { if err := b.copyDir(v); err != nil {
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err) log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
} }
visited[src] = true visited[src] = true
//b.cpDepPackages(v, visited)
} }
if b.IsMod { if b.IsMod {
for _, v := range b.Pkgs { for _, v := range b.Pkgs {
@ -68,35 +65,7 @@ func (b *Build) cpLegacyProject() {
} }
} }
// only cp dependency in root(current gopath), func (b *Build) copyDir(pkg *cover.Package) error {
// skip deps in other GOPATHs
// only used for version before go 1.11.4
func (b *Build) cpDepPackages(pkg *cover.Package, visited map[string]bool) {
gopath := pkg.Root
for _, dep := range pkg.Deps {
src := filepath.Join(gopath, "src", dep)
// Check if copied
if _, ok := visited[src]; ok {
// Skip if already copied
continue
}
// Check if we can found in the root gopath
_, err := os.Stat(src)
if err != nil {
continue
}
dst := filepath.Join(b.TmpDir, "src", dep)
if err := copy.Copy(src, dst); err != nil {
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
}
visited[src] = true
}
}
func (b *Build) copyDir(pkg *cover.Package, tmpDir string) error {
fileList := []string{} fileList := []string{}
dir := pkg.Dir dir := pkg.Dir
fileList = append(fileList, pkg.GoFiles...) fileList = append(fileList, pkg.GoFiles...)
@ -120,7 +89,7 @@ func (b *Build) copyDir(pkg *cover.Package, tmpDir string) error {
root = pkg.Root root = pkg.Root
} }
src = strings.TrimPrefix(pkg.Dir, root) // get the relative path of the files src = strings.TrimPrefix(pkg.Dir, root) // get the relative path of the files
dst := filepath.Join(tmpDir, src, file) // it will adapt the case where src is "" dst := filepath.Join(b.TmpDir, src, file) // it will adapt the case where src is ""
if err := copy.Copy(p, dst); err != nil { if err := copy.Copy(p, dst); err != nil {
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err) log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
return err return err

View File

@ -17,7 +17,6 @@
package build package build
import ( import (
"path/filepath"
"strings" "strings"
"testing" "testing"
@ -47,36 +46,15 @@ func TestLegacyProjectCopyWithUnexistedDir(t *testing.T) {
assert.Equal(t, strings.Contains(output, "Failed to Copy"), true) assert.Equal(t, strings.Contains(output, "Failed to Copy"), true)
} }
// copy in cpDepPackages of invalid dst name
func TestDepPackagesCopyWithInvalidDir(t *testing.T) {
gopath := filepath.Join(baseDir, "../../tests/samples/simple_gopath_project")
pkg := &cover.Package{
Module: &cover.ModulePublic{
Dir: "not exied, ia mas duser",
},
Root: gopath,
Deps: []string{"qiniu.com", "ddfee 2344234"},
}
b := &Build{
TmpDir: "/", // "/" is invalid dst in Linux, it should fail
}
output := captureOutput(func() {
visited := make(map[string]bool)
b.cpDepPackages(pkg, visited)
})
assert.Equal(t, strings.Contains(output, "Failed to Copy"), true)
}
func TestCopyDir(t *testing.T) { func TestCopyDir(t *testing.T) {
wd, _ := os.Getwd() wd, _ := os.Getwd()
pkg := &cover.Package{Dir: wd, Root: wd, GoFiles: []string{"build.go", "legacy.go"}, CgoFiles: []string{"run.go"}} pkg := &cover.Package{Dir: wd, Root: wd, GoFiles: []string{"build.go", "legacy.go"}, CgoFiles: []string{"run.go"}}
tmpDir := "/tmp/test/" tmpDir := "/tmp/test/"
b := &Build{ b := &Build{
WorkingDir: "empty", WorkingDir: "empty",
TmpDir: tmpDir,
} }
assert.NoError(t, os.MkdirAll(tmpDir, os.ModePerm)) assert.NoError(t, os.MkdirAll(tmpDir, os.ModePerm))
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
assert.NoError(t, b.copyDir(pkg, tmpDir)) assert.NoError(t, b.copyDir(pkg))
} }

View File

@ -57,13 +57,6 @@ func (b *Build) MvProjectsToTmp() error {
} else { } else {
b.NewGOPATH = fmt.Sprintf("%v:%v", b.TmpDir, b.OriGOPATH) b.NewGOPATH = fmt.Sprintf("%v:%v", b.TmpDir, b.OriGOPATH)
} }
// fix #14: unable to build project not in GOPATH in legacy mode
// this kind of project does not have a pkg.Root value
// go 1.11, 1.12 has no pkg.Root,
// so add b.IsMod == false as secondary judgement
if b.Root == "" && b.IsMod == false {
b.NewGOPATH = b.OriGOPATH
}
log.Infof("New GOPATH: %v", b.NewGOPATH) log.Infof("New GOPATH: %v", b.NewGOPATH)
return nil return nil
} }

View File

@ -95,7 +95,7 @@ func TestLegacyProjectNotInGoPATH(t *testing.T) {
os.Setenv("GO111MODULE", "off") os.Setenv("GO111MODULE", "off")
b, _ := NewBuild("", []string{"."}, workingDir, "") b, _ := NewBuild("", []string{"."}, workingDir, "")
if b.OriGOPATH != b.NewGOPATH { if !strings.Contains(b.NewGOPATH,b.OriGOPATH) {
t.Fatalf("New GOPATH should be same with old GOPATH, for this kind of project. New: %v, old: %v", b.NewGOPATH, b.OriGOPATH) t.Fatalf("New GOPATH should be same with old GOPATH, for this kind of project. New: %v, old: %v", b.NewGOPATH, b.OriGOPATH)
} }