sam build includes absolute paths in Golang binaries
Description:
When you run sam build and sam package in two instances of the same projects with different absolute paths and upload to the same S3 bucket, the compiled binaries will be uploaded both times, even though the binaries are identical.
According to #2110 this has been fixed, but I can still reproduce the issue on Windows.
Steps to reproduce:
- Clone this repository into a directory on your local Windows machine.
cdinto the directory.- Run
sam build. - Run
sam package --s3-bucket YOUR_BUCKETNAME - Clone the same repository into a second directory on your local windows machine.
- Copy the second directory into a third directory using
xcopy seconddirectory thirddirectory /s /e. cdinto the second directory.- Run
sam build. - Run
sam package --s3-bucket YOUR_BUCKETNAME cdinto the third directory.- Run
sam build. - Run
sam package --s3-bucket YOUR_BUCKETNAME
Observed result:
The calculated hashes change, when the parent directory changes. This results in the compiled binaries being re-uploaded for every directory, although no files have been modified.
Expected result:
Hashes should not depend on the parent directory. Compiled binaries should only be uploaded, when they have been modified.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: Windows 10 Pro Version 20H2 (Build 19042.1165)
sam --version: 1.31.0- AWS region: eu-central-1
Further notes:
I can imagine this is also the cause for the issue #2872, where running sam package in an ephemeral container results in the files being uploaded every time.
I've played around with the source code and it seems the hashes are correct, but instead the compiled binaries are not identical. But I have not yet identified, why they differ.
I believe I have identified the issue. When I build my binaries using sam build, the resulting binary is identical to go build. But when I use go build -trimpath the binary is different, yet the binaries in both directories are now identical. I'm looking into what other metadata is stored in the binaries, that could let to the binaries differing between two CodeBuild runs.
I solved the issue successfully for both local builds and builds generated by CodeBuild. As my last post suggested, the issue came down to go build embedding absolute paths into the binary. So to resolve the issue I did the following:
To have sam build use the -trimpath flag, I setup makefiles for every Lambda function and added these lines to each function:
Metadata:
BuildMethod: makefile
Then I added a Makefile file to each directory listed as CodeUri with the following content:
build-Logical-Resource-Id:
go build -trimpath -o "$(ARTIFACTS_DIR)/your-handler-name"
For more information on how to use a makefile, see the documentation. A complete example can be found in the resolved branch of my repo.
Going forward, it might be interesting to consider using the -trimpath by default. I do not know how important it is to keep the information in the binaries and which variant more commonly would lead to issues.
Updated title to reflect the found issue.
@suroh1994 Thanks for the info, it is very valuable. We will consider adding -trimpath by default if that avoid such issue
Any update on if trimpath can be included by default? My group is running into this issue atm.
@jaglade This will be a welcome contribution to the aws-lambda-builders golang build workflow and we can make this feature opt-in (atleast at this point, I have not considered the ramifications of the change in behavior causing breaks).
@sriram-mv I have opened a pull request in aws-lambda-builders allowing this option to be passed in.
Once this PR is reviewed and merged I believe this project can be updated here to something along the lines of:
_build_options: Dict = {
"go": {
"artifact_executable_name": handler,
"trim_go_path": build_props.get("TrimGoPath", False),
},
"provided": {"build_logical_id": function_name},
"nodejs": {"use_npm_ci": build_props.get("UseNpmCi", False)},
}
I believe this will allow SAM template users to define functions like the following:
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/hello-world/
Handler: main
Runtime: go1.x
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Metadata:
BuildProperties:
TrimGoPath: True
Which fixes the issue described above by @suroh1994 and @jaglade
For anyone landing here from a search: TrimGoPath fixes the absolute-path half, but since Go 1.18 — March 2022, five months before this was closed — go build also stamps vcs.revision, vcs.time and vcs.modified into the binary by default. The hash still changes on every commit, and differs between a dirty working tree and a clean CI checkout, even with TrimGoPath: True.
Measured on go1.26.5, same source, new commit:
default flags: fd31d00604a89035 → c233fbe928fd8c24 (differs)
-buildvcs=false: 6c4aad78bdfce5d1 → 6c4aad78bdfce5d1 (identical)
There is no BuildProperties knob for it — go_modules/builder.py only ever appends -trimpath, and otherwise inherits the process environment — so it has to go through GOFLAGS before sam build:
export GOFLAGS="-trimpath -buildvcs=false"
We built a committed-binary-plus-hash-check workaround around this exact symptom before finding the flag, so it seemed worth leaving here.