diff --git a/core/chaincode/platforms/golang/platform.go b/core/chaincode/platforms/golang/platform.go index 736b42462f0..d3812f624f1 100644 --- a/core/chaincode/platforms/golang/platform.go +++ b/core/chaincode/platforms/golang/platform.go @@ -290,7 +290,7 @@ func DescribeCode(path string) (*CodeDescriptor, error) { func describeGopath(importPath string) (*CodeDescriptor, error) { output, err := exec.Command("go", "list", "-f", "{{.Dir}}", importPath).Output() if err != nil { - return nil, err + return nil, wrapExitErr(err, "'go list' failed") } sourcePath := filepath.Clean(strings.TrimSpace(string(output))) @@ -336,7 +336,7 @@ func moduleInfo(path string) (*ModuleInfo, error) { cmd.Env = append(os.Environ(), "GO111MODULE=on") output, err := cmd.Output() if err != nil { - return nil, errors.Wrap(err, "failed to determine module root") + return nil, wrapExitErr(err, "failed to determine module root") } modExists, err := regularFileExists(strings.TrimSpace(string(output))) @@ -445,6 +445,10 @@ func findSource(cd *CodeDescriptor) (SourceMap, error) { case cd.Module: name = filepath.Join("src", name) default: + // skip top level go.mod and go.sum when not in module mode + if name == "go.mod" || name == "go.sum" { + return nil + } name = filepath.Join("src", cd.Path, name) } diff --git a/core/chaincode/platforms/golang/platform_test.go b/core/chaincode/platforms/golang/platform_test.go index 64cd292182b..e77cd56c0ee 100644 --- a/core/chaincode/platforms/golang/platform_test.go +++ b/core/chaincode/platforms/golang/platform_test.go @@ -177,14 +177,17 @@ func getGopath() (string, error) { func Test_findSource(t *testing.T) { t.Run("Gopath", func(t *testing.T) { source, err := findSource(&CodeDescriptor{ + Module: false, Source: filepath.FromSlash("testdata/src/chaincodes/noop"), MetadataRoot: filepath.FromSlash("testdata/src/chaincodes/noop/META-INF"), Path: "chaincodes/noop", }) require.NoError(t, err, "failed to find source") - assert.Len(t, source, 2) assert.Contains(t, source, "src/chaincodes/noop/chaincode.go") assert.Contains(t, source, "META-INF/statedb/couchdb/indexes/indexOwner.json") + assert.NotContains(t, source, "src/chaincodes/noop/go.mod") + assert.NotContains(t, source, "src/chaincodes/noop/go.sum") + assert.Len(t, source, 2) }) t.Run("Module", func(t *testing.T) { @@ -463,6 +466,24 @@ func TestDescribeCode(t *testing.T) { }) } +func TestRegularFileExists(t *testing.T) { + t.Run("RegularFile", func(t *testing.T) { + ok, err := regularFileExists("testdata/ccmodule/go.mod") + assert.NoError(t, err) + assert.True(t, ok) + }) + t.Run("MissingFile", func(t *testing.T) { + ok, err := regularFileExists("testdata/missing.file") + assert.NoError(t, err) + assert.False(t, ok) + }) + t.Run("Directory", func(t *testing.T) { + ok, err := regularFileExists("testdata") + assert.NoError(t, err) + assert.False(t, ok) + }) +} + func TestMain(m *testing.M) { viper.SetConfigName("core") viper.SetEnvPrefix("CORE") diff --git a/core/chaincode/platforms/golang/testdata/src/chaincodes/noop/go.mod b/core/chaincode/platforms/golang/testdata/src/chaincodes/noop/go.mod new file mode 100644 index 00000000000..95aab407e14 --- /dev/null +++ b/core/chaincode/platforms/golang/testdata/src/chaincodes/noop/go.mod @@ -0,0 +1,5 @@ +module github.com/hyperledger/fabric/core/chaincode/platforms/golang/testdata/src/chaincodes/noop + +go 1.13 + +// This should not get included in packages that were created from a GOPATH diff --git a/core/chaincode/platforms/golang/testdata/src/chaincodes/noop/go.sum b/core/chaincode/platforms/golang/testdata/src/chaincodes/noop/go.sum new file mode 100644 index 00000000000..275cb67d0ef --- /dev/null +++ b/core/chaincode/platforms/golang/testdata/src/chaincodes/noop/go.sum @@ -0,0 +1,2 @@ +ignore/me v0.0.1 h1:0badc0de0badcode +ignore/me v0.0.1/go.mod