Skip to content

Conversation

@scrofungulus
Copy link
Contributor

This PR addresses the issue described here in the AWS SAM CLI repository.

Description of changes:
Adds a trim_go_path option to the GoModulesBuilder. Directly from the go help build command from the Go CLI:

-trimpath
                remove all file system paths from the resulting executable.
		Instead of absolute file system paths, the recorded file names
		will begin either a module path@version (when using modules),
		or a plain import path (when using the standard library, or GOPATH).

Ultimately, this addresses an issue that occurs when using sam build. Without -trimpath the compiled binaries computed hashes are different each time forcing CloudFormation to redeploy a Lambda function without code changes.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions github-actions bot added pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. area/workflow/go_modules labels Oct 6, 2022
@scrofungulus scrofungulus marked this pull request as ready for review October 6, 2022 02:43
@sriram-mv
Copy link
Contributor

Overall, this looks good. These options will need to be passed in from AWS SAM CLI as well here: https://github.com/aws/aws-sam-cli/blob/develop/samcli/lib/build/app_builder.py#L715

@sriram-mv sriram-mv self-requested a review October 6, 2022 19:57
Copy link
Contributor

@sriram-mv sriram-mv left a comment

Choose a reason for hiding this comment

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

I am happy to approve with a small test change.

pathname = Path(self.artifacts_dir, "no-deps-main-arm64")
self.assertEqual(get_executable_arch(pathname), "AArch64")

def test_builds_with_trimpath(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a test to show the difference between when trim path is enabled vs not enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sriram-mv @mndeveci I have added this test case. I wrote a utility that will return the md5 hexdigest of the built binaries. I created a copy of the no-deps go project in testdata. By building an exact copy of that project with -trimpath enabled we can assert that their md5 hex digests are the same. Similarly, we can assert that they will not be equal without the -trimpath flag. This was the most straightforward way that I could think of. Hopefully it is sufficient.

Copy link
Contributor

@mndeveci mndeveci left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! I think changes looks good to me. Can we add an intergration test to validate this case? Integration test file can be found here; https://github.com/aws/aws-lambda-builders/blob/develop/tests/integration/workflows/go_modules/test_go.py#L18

Copy link
Contributor

@sriram-mv sriram-mv left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes!

Copy link
Contributor

@mndeveci mndeveci left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution, looks good to me as well!

@mndeveci mndeveci merged commit 94030f0 into aws:develop Oct 11, 2022
@sriram-mv
Copy link
Contributor

@scrofungulus to use this effectively, the next step is to make changes in AWS SAM CLI as well. since it passes in the options based on the builder that is chosen. Those changes are showcased here: https://github.com/aws/aws-sam-cli/blob/develop/samcli/lib/build/app_builder.py#L714

Would you be open to contributing those changes there as well?

@scrofungulus
Copy link
Contributor Author

@sriram-mv I'm definitely open to contributing! Looks straightforward.

@scrofungulus
Copy link
Contributor Author

scrofungulus commented Oct 12, 2022

@sriram-mv pull request number 4300 addresses your above comment! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/workflow/go_modules maintainer/need-followup pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants