aws / aws/aws-sam-cli

Bug: Cached build doesn't work on .NET

Open
#4,749 4 comments 1 reaction 0 assignees View on GitHub
type/feature
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:
I'm using SAM for building a template that include .NET lambda functions. To reduce the time it takes to make changes I'm trying to use the cached build feature. I noticed that while python functions are cached and reused I never noticed the .NET lambdas to be cached but are always reported as invalid and rebuilt (and they are also the slowest to rebuild) even I didn't made any change.

I looked into the code and I noticed that caching is done by walking through the project folder and making an hash of all the files. The problem is that in .NET projects the build artifacts are generally stored inside the project folder together with the code. So every compilation will modify the hash so there will be never a match, as far as I understand.

A normal .NET project folder has this structure more or less, with code at the top level and artifacts inside bin and obj folders:

```
MyProject\Project.csproj
MyProject\Project.csproj.user
MyProject\Logger.cs
MyProject\Functions.cs
...
MyProject\bin\Release\net7.0\linux-x64\bootstrap
MyProject\bin\Release\net7.0\linux-x64\bootstrap.deps.json
MyProject\bin\Release\net7.0\linux-x64\bootstrap.pdb
MyProject\bin\Release\net7.0\linux-x64\libcoreclr.so
...
MyProject\bin\x64\Debug\net7.0\win-x64\bootstrap.dll
MyProject\bin\x64\Debug\net7.0\win-x64\bootstrap.exe
MyProject\bin\x64\Debug\net7.0\win-x64\bootstrap.pdb
MyProject\bin\x64\Debug\net7.0\win-x64\bootstrap.runtimeconfig.json
MyProject\bin\x64\Debug\net7.0\win-x64\hostfxr.dll
MyProject\bin\x64\Debug\net7.0\win-x64\hostpolicy.dll
...
MyProject\obj\Debug\net7.0\win-x64\.NETCoreApp,Version=v7.0.AssemblyAttributes.cs
MyProject\obj\Debug\net7.0\win-x64\MyProject.AssemblyInfo.cs
...
```

I tried modifying the line in [CachedBuildStrategy](https://github.com/aws/aws-sam-cli/blob/97803bc379971021b88f2b79518f0c201976058b/samcli/lib/build/build_strategy.py#L272) from

`source_hash = dir_checksum(code_dir, ignore_list=[".aws-sam"], hash_generator=hashlib.sha256())`

to

`source_hash = dir_checksum(code_dir, ignore_list=[".aws-sam","bin","obj"], hash_generator=hashlib.sha256())`

to make it ignore the artifact directories inside the code and now caching works. I cannot say if that can have effects on other languages so I'm not suggesting to put that as a fix as it is, but it was just to demonstrate where the problem is located (more or less). A real fix would need to ignore those directory only on .NET builds. Also, putting artifacts in different folder is not possible or ideal, all common .NET tools including both Visual Studio and AWS Lambda Tools work that way.

### Steps to reproduce:
Calling `sam build --template serverless.template --cached`

### Observed result:
No caching of .NET lambdas

### Expected result:
.NET lambda functions are cached like it does with others (like Python).

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

{
"version": "1.72.0",
"system": {
"python": "3.8.8",
"os": "Windows-10-10.0.19041-SP0"
},
"additional_dependencies": {
"docker_engine": "20.10.22",
"aws_cdk": "Not available",
"terraform": "Not available"
}
}

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.