aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.52k stars 1.17k forks source link

sam build fails for HelloWorld dotnet 7 aot project in WSL2 #4981

Open bradthurber opened 1 year ago

bradthurber commented 1 year ago

Description:

sam build fails after creating the out-of-the-box HelloWorld dotnet 7 aot project [in a WSL2/Ubuntu/Bash environment but seems to work fine a pure Windows environment - Brad 4/10/2023].

I use SAM for Python projects, but this is my first-time trying SAM in the dotnet world. I don't know much about dotnet either - so keep that in mind.

Steps to reproduce:

  1. Open WSL2 Ubuntu 22 terminal
  2. Install prerequisites for a SAM dotnet 7 aot project
  3. run sam init to create a HelloWorld dotnet 7 aot project
  4. run sam build --debug and get an error: ... docker run: MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file..

Detailed sam build --debug log:

brad@brad13600:~/dev/dotnet/sam-native-aot$ sam build --debug
2023-04-08 19:16:34,936 | Config file location: /home/brad/dev/dotnet/sam-native-aot/samconfig.toml
2023-04-08 19:16:34,937 | Loading configuration values from [default.['build'].parameters] (env.command_name.section) in config file at '/home/brad/dev/dotnet/sam-native-aot/samconfig.toml'...
2023-04-08 19:16:34,941 | Configuration values successfully loaded.
2023-04-08 19:16:34,941 | Configuration values are: {'stack_name': 'sam-native-aot', 'cached': True, 'parallel': True}
2023-04-08 19:16:34,944 | Using SAM Template at /home/brad/dev/dotnet/sam-native-aot/template.yaml
2023-04-08 19:16:34,969 | Using config file: samconfig.toml, config environment: default
2023-04-08 19:16:34,969 | Expand command line arguments to:
2023-04-08 19:16:34,969 | --template_file=/home/brad/dev/dotnet/sam-native-aot/template.yaml --parallel --mount_with=READ --build_dir=.aws-sam/build --cache_dir=.aws-sam/cache --cached
2023-04-08 19:16:35,062 | 'build' command is called
2023-04-08 19:16:35,062 | Starting Build use cache
2023-04-08 19:16:35,065 | No Parameters detected in the template
2023-04-08 19:16:35,078 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2023-04-08 19:16:35,078 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2023-04-08 19:16:35,079 | 0 stacks found in the template
2023-04-08 19:16:35,079 | No Parameters detected in the template
2023-04-08 19:16:35,088 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2023-04-08 19:16:35,088 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2023-04-08 19:16:35,089 | 2 resources found in the stack
2023-04-08 19:16:35,089 | Found Serverless function with name='HelloWorldFunction' and CodeUri='./src/HelloWorld/'
2023-04-08 19:16:35,089 | --base-dir is not presented, adjusting uri ./src/HelloWorld/ relative to /home/brad/dev/dotnet/sam-native-aot/template.yaml
2023-04-08 19:16:35,092 | 2 resources found in the stack
2023-04-08 19:16:35,092 | Found Serverless function with name='HelloWorldFunction' and CodeUri='./src/HelloWorld/'
2023-04-08 19:16:35,092 | Instantiating build definitions
2023-04-08 19:16:35,094 | Same function build definition found, adding function (Previous: BuildDefinition(provided.al2, /home/brad/dev/dotnet/sam-native-aot/src/HelloWorld, Zip, , 7cc428f0-976a-49a3-be09-9cdb4d3781ab, {'BuildMethod': 'dotnet7'}, {}, x86_64, []), Current: BuildDefinition(provided.al2, /home/brad/dev/dotnet/sam-native-aot/src/HelloWorld, Zip, , 38d5f600-e61e-4a83-9f25-ae301ad9b350, {'BuildMethod': 'dotnet7'}, {}, x86_64, []), Function: Function(function_id='HelloWorldFunction', name='HelloWorldFunction', functionname='HelloWorldFunction', runtime='provided.al2', memory=256, timeout=10, handler='bootstrap', imageuri=None, packagetype='Zip', imageconfig=None, codeuri='/home/brad/dev/dotnet/sam-native-aot/src/HelloWorld', environment={'Variables': {'PARAM1': 'VALUE'}}, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api', 'Properties': {'Path': '/hello', 'Method': 'get', 'RestApiId': 'ServerlessRestApi'}}}, metadata={'BuildMethod': 'dotnet7', 'SamResourceId': 'HelloWorldFunction'}, inlinecode=None, codesign_config_arn=None, architectures=['x86_64'], function_url_config=None, stack_path='', runtime_management_config=None))
2023-04-08 19:16:35,095 | Async execution started
2023-04-08 19:16:35,095 | Invoking function functools.partial(<bound method ParallelBuildStrategy.build_single_function_definition of <samcli.lib.build.build_strategy.ParallelBuildStrategy object at 0x7fbe5c056bd0>>, <samcli.lib.build.build_graph.FunctionBuildDefinition object at 0x7fbe5bfb2e10>)
2023-04-08 19:16:35,095 | Running incremental build for runtime provided.al2 for following resources (HelloWorldFunction)
2023-04-08 19:16:35,096 | Waiting for async results
2023-04-08 19:16:35,451 | Cache is invalid, running build and copying resources for following functions (HelloWorldFunction)
2023-04-08 19:16:35,451 | Building codeuri: /home/brad/dev/dotnet/sam-native-aot/src/HelloWorld runtime: provided.al2 metadata: {'BuildMethod': 'dotnet7'} architecture: x86_64 functions: HelloWorldFunction
2023-04-08 19:16:35,451 | Building to following folder /home/brad/dev/dotnet/sam-native-aot/.aws-sam/build/HelloWorldFunction
2023-04-08 19:16:35,452 | Loading workflow module 'aws_lambda_builders.workflows'
2023-04-08 19:16:35,453 | Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
2023-04-08 19:16:35,454 | Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
2023-04-08 19:16:35,454 | Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
2023-04-08 19:16:35,455 | Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
2023-04-08 19:16:35,456 | Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
2023-04-08 19:16:35,456 | Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
2023-04-08 19:16:35,457 | Registering workflow 'NodejsNpmEsbuildBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm-esbuild', application_framework=None)'
2023-04-08 19:16:35,458 | Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2023-04-08 19:16:35,458 | Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
2023-04-08 19:16:35,459 | Registering workflow 'RustCargoLambdaBuilder' with capability 'Capability(language='rust', dependency_manager='cargo', application_framework=None)'
2023-04-08 19:16:35,459 | Found workflow 'DotnetCliPackageBuilder' to support capabilities 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
2023-04-08 19:16:35,565 | Running workflow 'DotnetCliPackageBuilder'
2023-04-08 19:16:35,565 | Running DotnetCliPackageBuilder:GlobalToolInstall
2023-04-08 19:16:35,565 | Entered synchronized block for updating Amazon.Lambda.Tools
2023-04-08 19:16:35,565 | Installing Amazon.Lambda.Tools Global Tool
2023-04-08 19:16:35,565 | executing dotnet: ['dotnet', 'tool', 'install', '-g', 'Amazon.Lambda.Tools', '--ignore-failed-sources']
2023-04-08 19:16:35,810 |
2023-04-08 19:16:35,810 | Error installing probably due to already installed. Attempt to update to latest version.
2023-04-08 19:16:35,810 | executing dotnet: ['dotnet', 'tool', 'update', '-g', 'Amazon.Lambda.Tools', '--ignore-failed-sources']
2023-04-08 19:16:37,566 | Tool 'amazon.lambda.tools' was reinstalled with the latest stable version (version '5.6.4').
2023-04-08 19:16:37,567 | DotnetCliPackageBuilder:GlobalToolInstall succeeded
2023-04-08 19:16:37,567 | Running DotnetCliPackageBuilder:RunPackageAction
2023-04-08 19:16:37,567 | Running `dotnet lambda package` in /home/brad/dev/dotnet/sam-native-aot/src/HelloWorld
2023-04-08 19:16:37,567 | executing dotnet: ['dotnet', 'lambda', 'package', '--output-package', '/home/brad/dev/dotnet/sam-native-aot/.aws-sam/build/HelloWorldFunction/HelloWorld.zip', '--msbuild-parameters', '--runtime linux-x64']
2023-04-08 19:16:40,301 | Amazon Lambda Tools for .NET Core applications (5.6.4)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Found /etc/os-release
Architecture not provided, defaulting to x86_64 for container build image.
Executing publish command
Starting container for native AOT build using build image: public.ecr.aws/sam/build-dotnet7:latest-x86_64.
... invoking 'docker run --name tempLambdaBuildContainer-48ed7f21-d7d1-4f01-ac89-3d916d1fc1b0 --rm --volume "/home/brad/dev/dotnet/sam-native-aot/src/HelloWorld":/tmp/source/ -i -u 1000:1000 -e DOTNET_CLI_HOME=/tmp/dotnet -e XDG_DATA_HOME=/tmp/xdg public.ecr.aws/sam/build-dotnet7:latest-x86_64 dotnet publish "/tmp/source/" --output "/tmp/source/bin/Release/net7.0/publish" --configuration "Release" --framework "net7.0" --runtime linux-x64 /p:GenerateRuntimeConfigurationFiles=true --self-contained True  /p:StripSymbols=true' from directory /home/brad/dev/dotnet/sam-native-aot/src/HelloWorld
... docker run: MSBuild version 17.5.0+6f08c67f3 for .NET
... docker run: MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
ERROR: Container build returned 1
Failed to create application package
2023-04-08 19:16:40,301 | DotnetCliPackageBuilder:RunPackageAction failed
Traceback (most recent call last):
  File "aws_lambda_builders/workflows/dotnet_clipackage/actions.py", line 108, in execute
  File "aws_lambda_builders/workflows/dotnet_clipackage/dotnetcli.py", line 66, in run
aws_lambda_builders.workflows.dotnet_clipackage.dotnetcli.DotnetCLIExecutionError: Dotnet CLI Failed:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "aws_lambda_builders/workflow.py", line 369, in run
  File "aws_lambda_builders/workflows/dotnet_clipackage/actions.py", line 115, in execute
aws_lambda_builders.actions.ActionFailedError: Dotnet CLI Failed:
2023-04-08 19:16:40,303 | Exception raised during the execution

There is a /src/HelloWorld/HelloWorld.csproj file that is created by sam init.

I noticed the docker run command in the log - so I tried to run that manually as a troubleshooting step - and that seems to work - which is odd:

brad@brad13600:~/dev/dotnet/sam-native-aot$ docker run --name tempLambdaBuildContainer-48ed7f21-d7d1-4f01-ac89-3d916d1fc1b0 --rm --volume "/home/brad/dev/dotnet/sam-native-aot/src/HelloWorld":/tmp/source/ -i -u 1000:1000 -e DOTNET_CLI_HOME=/tmp/dotnet -e XDG_DATA_HOME=/tmp/xdg public.ecr.aws/sam/build-dotnet7:latest-x86_64 dotnet publish "/tmp/source/" --output "/tmp/source/bin/Release/net7.0/publish" --configuration "Release" --framework "net7.0" --runtime linux-x64 /p:GenerateRuntimeConfigurationFiles=true --self-contained True  /p:StripSymbols=true
MSBuild version 17.5.0+6f08c67f3 for .NET
  Determining projects to restore...
  Restored /tmp/source/HelloWorld.csproj (in 7.03 sec).
  HelloWorld -> /tmp/source/bin/Release/net7.0/linux-x64/bootstrap.dll
  HelloWorld -> /tmp/source/bin/Release/net7.0/publish/

docker run also works if I cd into the .src/HelloWorld folder first:

brad@brad13600:~/dev/dotnet/sam-native-aot/src$ cd /home/brad/dev/dotnet/sam-native-aot/src/HelloWorld
brad@brad13600:~/dev/dotnet/sam-native-aot/src/HelloWorld$ docker run --name tempLambdaBuildContainer-48ed7f21-d7d1-4f01-ac89-3d916d1fc1b0 --rm --volume "/home/brad/dev/dotnet/sam-native-aot/src/HelloWorld":/tmp/source/ -i -u 1000:1000 -e DOTNET_CLI_HOME=/tmp/dotnet -e XDG_DATA_HOME=/tmp/xdg public.ecr.aws/sam/build-dotnet7:latest-x86_64 dotnet publish "/tmp/source/" --output "/tmp/source/bin/Release/net7.0/publish" --configuration "Release" --framework "net7.0" --runtime linux-x64 /p:GenerateRuntimeConfigurationFiles=true --self-contained True  /p:StripSymbols=true
MSBuild version 17.5.0+6f08c67f3 for .NET
  Determining projects to restore...
  Restored /tmp/source/HelloWorld.csproj (in 7.21 sec).
  HelloWorld -> /tmp/source/bin/Release/net7.0/linux-x64/bootstrap.dll
  HelloWorld -> /tmp/source/bin/Release/net7.0/publish/

Observed result:

sam build fails

Expected result:

sam build should succeed

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

{
  "version": "1.79.0",
  "system": {
    "python": "3.7.10",
    "os": "Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-debian-bookworm-sid"
  },
  "additional_dependencies": {
    "docker_engine": "20.10.23",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  }
}
bradthurber commented 1 year ago

I decided to try sam build in Windows instead of WSL2 and it works there. Updating title of issue to note WSL2 as it seems to work fine in straight windows.

PS C:\Users\bradt\Documents\temporary\dotnet7-aot-win> sam build
Starting Build use cache
Cache is invalid, running build and copying resources for following functions (HelloWorldFunction)
Building codeuri: C:\Users\bradt\Documents\temporary\dotnet7-aot-win\src\HelloWorld runtime: provided.al2 metadata: {'BuildMethod': 'dotnet7'} architecture: x86_64 functions: HelloWorldFunction
Running DotnetCliPackageBuilder:GlobalToolInstall

Tool 'amazon.lambda.tools' was reinstalled with the latest stable version (version '5.6.4').
Running DotnetCliPackageBuilder:RunPackageAction
Amazon Lambda Tools for .NET Core applications (5.6.4)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Architecture not provided, defaulting to x86_64 for container build image.
Executing publish command
Starting container for native AOT build using build image: public.ecr.aws/sam/build-dotnet7:latest-x86_64.
... invoking 'docker run --name tempLambdaBuildContainer-6efb532a-4c1a-4b77-8434-05f5515c1714 --rm --volume "C:\Users\bradt\Documents\temporary\dotnet7-aot-win\src\HelloWorld":/tmp/source/ -i public.ecr.aws/sam/build-dotnet7:latest-x86_64 dotnet publish "/tmp/source/" --output "/tmp/source\bin\Release\net7.0\publish" --configuration "Release" --framework "net7.0" --runtime linux-x64 /p:GenerateRuntimeConfigurationFiles=true --self-contained True  /p:StripSymbols=true' from directory C:\Users\bradt\Documents\temporary\dotnet7-aot-win\src\HelloWorld
... docker run: MSBuild version 17.5.0+6f08c67f3 for .NET
... docker run:   Determining projects to restore...
... docker run:   Restored /tmp/source/HelloWorld.csproj (in 6.77 sec).
... docker run: /tmp/source/Function.cs(53,24): warning IL3050: Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [/tmp/source/HelloWorld.csproj]
... docker run: /tmp/source/Function.cs(53,24): warning IL2026: Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [/tmp/source/HelloWorld.csproj]
... docker run:   HelloWorld -> /tmp/source/bin/Release/net7.0/linux-x64/bootstrap.dll
... docker run:   Generating native code
... docker run: /tmp/source/Function.cs(51): Trim analysis warning IL2026: HelloWorld.Function.<FunctionHandler>d__3.MoveNext(): Using member 'System.Text.Json.JsonSerializer.Serialize<Dictionary`2<String,String>>(Dictionary`2<String,String>,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [/tmp/source/HelloWorld.csproj]
... docker run: /tmp/source/Function.cs(51): AOT analysis warning IL3050: HelloWorld.Function.<FunctionHandler>d__3.MoveNext(): Using member 'System.Text.Json.JsonSerializer.Serialize<Dictionary`2<String,String>>(Dictionary`2<String,String>,JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [/tmp/source/HelloWorld.csproj]
... docker run: /tmp/dotnet/.nuget/packages/amazon.lambda.serialization.systemtextjson/2.3.0/lib/net6.0/Amazon.Lambda.Serialization.SystemTextJson.dll : warning IL2104: Assembly 'Amazon.Lambda.Serialization.SystemTextJson' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [/tmp/source/HelloWorld.csproj]
... docker run: /tmp/dotnet/.nuget/packages/amazon.lambda.serialization.systemtextjson/2.3.0/lib/net6.0/Amazon.Lambda.Serialization.SystemTextJson.dll : warning IL3053: Assembly 'Amazon.Lambda.Serialization.SystemTextJson' produced AOT analysis warnings. [/tmp/source/HelloWorld.csproj]
... docker run: /tmp/dotnet/.nuget/packages/amazon.lambda.runtimesupport/1.8.2/lib/net6.0/Amazon.Lambda.RuntimeSupport.dll : warning IL2104: Assembly 'Amazon.Lambda.RuntimeSupport' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [/tmp/source/HelloWorld.csproj]
... docker run: /tmp/dotnet/.nuget/packages/runtime.linux-x64.microsoft.dotnet.ilcompiler/7.0.4/framework/System.Linq.Expressions.dll : warning IL3053: Assembly 'System.Linq.Expressions' produced AOT analysis warnings. [/tmp/source/HelloWorld.csproj]
... docker run:   HelloWorld -> /tmp/source/bin/Release/net7.0/publish/
Missing deps.json file. Skipping flattening runtime folder because  is an unrecognized format
Zipping publish folder C:\Users\bradt\Documents\temporary\dotnet7-aot-win\src\HelloWorld\bin\Release\net7.0\publish to C:\Users\bradt\Documents\temporary\dotnet7-aot-win\.aws-sam\build\HelloWorldFunction\HelloWorld.zip
Creating directory C:\Users\bradt\Documents\temporary\dotnet7-aot-win\.aws-sam\build\HelloWorldFunction
... zipping: bootstrap
Created publish archive (C:\Users\bradt\Documents\temporary\dotnet7-aot-win\.aws-sam\build\HelloWorldFunction\HelloWorld.zip).
Lambda project successfully packaged: C:\Users\bradt\Documents\temporary\dotnet7-aot-win\.aws-sam\build\HelloWorldFunction\HelloWorld.zip

Build Succeeded

Built Artifacts  : .aws-sam\build
Built Template   : .aws-sam\build\template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided
PS C:\Users\bradt\Documents\temporary\dotnet7-aot-win>
qingchm commented 1 year ago

Thanks for opening the issue, just wondering for the sam installation for the WSL2 system, which installer did you use?

bradthurber commented 1 year ago

for the sam installation for the WSL2 system, which installer did you use?

Hi. Thank you for reviewing this issue. I use this bash script to install and update SAM at the Ubuntu Linux prompt:

#!/bin/bash -e

# https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-linux.html#cliv2-linux-install
# https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-completion.html

echo "make sure OS is up-to-date"
sudo apt update && sudo apt upgrade -y
sudo apt install unzip -y

echo "* Switching to new temporary tmp folder..."
pushd "$(mktemp -d)"
echo
echo "* Downloading the AWS SAM CLI installer for linux"
wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip -O aws-sam-cli-linux-x86_64.zip
echo
echo "* Unzipping it..."
unzip -q aws-sam-cli-linux-x86_64.zip
echo
if [ -f /usr/local/aws-sam-cli/current/bin/sam ]; then
    echo "* Current version:"
    /usr/local/aws-sam-cli/current/bin/sam --version
    echo "* Updating..."
    sudo ./install --update
else
    echo "* Installing..."
    sudo ./install
fi
echo
echo "* Installed version:"
sam --version
echo
echo "Resuming at original directory"
popd
franramvel commented 3 months ago

Does exist any solution for this? I'm having exact same issue