question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Build cache should exclude `.aws-sam` in md5 calculation

See original GitHub issue

Description:

Running sam build --cached always reports that the cache is invalid despite no code changes.

Steps to reproduce:

Run sam build --cached twice. The second run doesn’t use the cache.

Observed result:

The cache is deemed invalid and not used despite no code changes.

Expected result:

The cache is used when the code isn’t changed between builds.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: OS X 10.15.7 (Catalina), Windows 10
  2. sam --version: SAM CLI, version 1.23.0
  3. AWS region: eu-west-1

Problem

The CLI stores the directory hash in .aws-sam/build.toml. This file is also used when calculating the directory hash. Consider this scenario:

  • The developer writes some code. The code directory is now in State 1.
  • A build is run and it finds that the directory hash in build.toml doesn’t equal hash(State 1)
  • SAM deems the cache invalid and rebuilds
  • At the end of the build SAM saves hash(State 1) in build.toml
  • Saving the hash means that the directory is no longer in State 1 but State 2
  • The developer changes no code and runs another build
  • SAM compares the hash in build.toml - which is hash(State 1) - and finds it is not equal to the hash of the current state, State 2
  • SAM rebuilds again and saves hash(State 2) in build.toml, which is now in State 3 due to this update
  • And so on

Workaround

This issue can be worked around by specifying -b, --build-dir DIRECTORY when calling sam build. Specifying a directory outside of the code directory results in SAM using the cache.

Possible Solutions

How do we solve this issue? Would we be able to ignore .aws-sam/build.toml when calculating the hash?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
aahungcommented, May 13, 2021

Oh that’s why, thanks for providing the information. As a workaround, you can continue to use -b option or put the source code into a subfolder and set a CodeUri to <sub-folder-name>.

0reactions
github-actions[bot]commented, Jun 2, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sam build - AWS Serverless Application Model
Enable or disable cached builds. Use this option to reuse build artifacts that haven't changed from previous builds. AWS SAM evaluates whether you've...
Read more >
Working with layers - AWS Serverless Application Model
The AWS SAM CLI generates the names of the images it builds, as well as the LayerVersions that are held in the cache....
Read more >
MD5 function - Amazon Redshift - AWS Documentation
The MD5 function returns a 32-character string that is a text representation of the hexadecimal value of a 128-bit checksum. Examples. The following...
Read more >
Accelerating serverless development with AWS SAM Accelerate
Building a serverless application changes the way developers think about testing their code. Previously, developers would emulate the ...
Read more >
Api - AWS Serverless Application Model - AWS Documentation
Creates a collection of Amazon API Gateway resources and methods that can be invoked through HTTPS endpoints.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found