Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for possible move to go modules #970

Merged
merged 2 commits into from
Apr 1, 2020

Conversation

sykesm
Copy link
Contributor

@sykesm sykesm commented Apr 1, 2020

These changes isolate GOPATH dependencies and enable a possible move to go modules. These changes are focused on two areas:

Discovery of the sampleconfig resources during test

If the build is run in module mode, the sampleconfig directory is calculated as relative to the module root of the test. If the build is run in GOPATH mode, the directory is calculated as relative to the GOPATH

Use "simple" chaincode instead of a module in most cases

There were a number of places in the integration tests where we would attempt to build a chaincode module from an import path reference. This works in GOPATH mode but fails in module mode. These changes replace the use of the module chaincode with simple chaincode where appropriate as simple chaincode could be built within the context of the module hosting the integration tests.

@sykesm sykesm requested a review from a team as a code owner April 1, 2020 00:45
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
Change-Id: I44a43db4bc848f27cf9598fce8189328ac2c2c90
The module path should be exercised for package and install but is not
needed when building chaincode with the external binary builder.

- Replace most use of chaincode/module with chaincode/simple
- Move signal handling from chaincode/module to chaincode/simple

Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
Change-Id: I241f3cf9dce1812bc03205a59960ab35b2fcd8b5
Copy link
Contributor

@MHBauer MHBauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the gomod/gopath determination for finding the file is elegant and correct for this situation.

I can't speak to the simplecc change. Glancing at the directory in the location, I don't know how to figure out what it is.The directory could use a Readme or a comment to explain it's existence in the location.

Agree as to being a separable change in preparation. 👍

Comment on lines +46 to +53
path, err := gomodDevConfigDir()
if err != nil {
path, err = gopathDevConfigDir()
if err != nil {
panic(err)
}
}
return path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The barest of nits. This reads better straight line with no indentation. I don't think the single exit point helps in a function this short. I think it's clearer in terms of, use gomod first, use gopath second.

	path, err := gomodDevConfigDir()
	if err == nil {
		return path
	}
	path, err = gopathDevConfigDir()
	if err == nil {
		return path
	}
	panic(err)

It can even be collapsed with the assignable-if

	var err error
	if path, err := gomodDevConfigDir(); err == nil {
		return path
	}
	if path, err := gopathDevConfigDir(); err == nil {
		return path
	}
	panic(err)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about what you suggested above. But, we need three return path statement and especially the last return path which comes after panic(err) is a dead code. IMO, the current structure looks good and no changes required.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MHBauer If the panic(err) can be avoided, what you suggested is applicable and makes the code more readable.


func gomodDevConfigDir() (string, error) {
buf := bytes.NewBuffer(nil)
cmd := exec.Command("go", "env", "GOMOD")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me and seems more flexible.

@@ -27,7 +27,7 @@ if [ -f "$SOURCE/src/go.mod" ]; then
cd "$SOURCE/src"
go build -v -mod=readonly -o "$OUTPUT/chaincode" "$GO_PACKAGE_PATH"
else
GO111MODULE=off go build -v -o "$OUTPUT/chaincode" "$GO_PACKAGE_PATH"
GOPATH="$SOURCE" GO111MODULE=off go build -v -o "$OUTPUT/chaincode" "$GO_PACKAGE_PATH"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So before this gopath was completely unset, and was the default set depending on go version, and the fact that it built was because everything was already checked out in the gopath. I think this makes sense.

@@ -38,15 +43,55 @@ func dirExists(path string) bool {
// maintained with the source tree. This should only be used in a
// test/development context.
func GetDevConfigDir() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the structure -- highly readable.

Just one minor suggestion:

In the formatting rules section, I have specified the following:

  1. Closely related concepts should not be kept far within a file; Should never be kept in a different file.
  2. If one function calls another, they should be vertically close. The caller should be placed above the callee.
  3. Certain bits of code which has a conceptual affinity towards other code, they should be kept together. Affinity might be caused because a group of functions performs similar operations.
  4. The most important concept first and the low-level details in the bottom.

While the current code structure satisfies these rules somewhat, it would be nice to have gomodDevConfigDir() immediately after the GetDevConfigDir() followed by gopathDevConfigDir(). The structure can look similar to how various function calls are made in the top-level function that is GetDevConfigDir()

When I read the top-level function, I searched for gomodDevConfigDir and had to browse the code a bit more to find out where it is located.

As these points are highly debatable, I will leave the decision to you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say feel free to create a change on top of this if you like @cendhu

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is simple to do. Will push a PR.

}
}

func main() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the top-level function in this file. IMO, this should be at the beginning of this file. The reason is that, first, I read handleSignals, then I read handleSigTerm followed by main(). Finally, I had to again read handleSignals and handleSigTerm by keeping main() in the context. Probably, I need to improve my code reading skills. May be the rearrangement might help me.

Copy link
Contributor

@mastersingh24 mastersingh24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like getting started down the path to Go modules

@mastersingh24 mastersingh24 merged commit 11469c1 into hyperledger:master Apr 1, 2020
@MHBauer MHBauer mentioned this pull request Apr 15, 2020
3 tasks
@sykesm sykesm deleted the prep-for-modules branch February 20, 2021 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants