Skip to content

Commit

Permalink
Omit go.{mod,sum} from pkg when not in module mode
Browse files Browse the repository at this point in the history
In certain environments, it's possible to package chaincode that
structured as a module from an active GOPATH. This often happens when
the path provided to the package command is an import path resolvable
from the GOPATH instead of a file system path.

If the package could be successfully built in the packaging environment
using the import path, the chaincode dependencies would be calculated
and packaged from the GOPATH for compilation as a traditional go
package.

In this scenario, the `go.mod` would be included in the chaincode
package as packaging always includes all non-hidden files in the top
level folder of the import path.

On the server, the presence of the `go.mod` implies that the build
process should execute in module mode. When the dependencies have been
vendored in the module, the build uses `-mod=vendor` flag to indicate
the module requirements should be satisfied from the vendor folder.
Unfortunately, since the chaincode dependencies were packaged using
GOPATH mode instead of module mode, there are some metadata files
missing from the vendor folder that are expected by the module mode
build process.

To help prevent this from occuring, we will explicitly omit go.mod and
go.sum from top level folder of chaincode that is not packaged in module
mode.

FAB-17725

Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
Change-Id: I5253205ab9c7471beddffec7a11dab987c3acebd
  • Loading branch information
sykesm authored and Brett Logan committed Apr 9, 2020
1 parent d0d4a3f commit 08df2e1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 6 additions & 2 deletions core/chaincode/platforms/golang/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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)
}

Expand Down
23 changes: 22 additions & 1 deletion core/chaincode/platforms/golang/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore/me v0.0.1 h1:0badc0de0badcode
ignore/me v0.0.1/go.mod

0 comments on commit 08df2e1

Please sign in to comment.