dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.65k stars 1.05k forks source link

PublishAot failed with the NETSDK1183 error #28169

Open WeihanLi opened 1 year ago

WeihanLi commented 1 year ago

Describe the bug

Failed to publish aot

publish command:

dotnet publish -f net7.0 -c Release -p:AssemblyName=http -p:PublishAot=true -r linux-x64 -o /app/artifacts

The errors:

 error NETSDK1183: Unable to optimize assemblies for Ahead of time compilation: a valid runtime package was not found. Either set the PublishAot property to false, or use a supported runtime identifier when publishing. When targeting .NET 7 or higher, make sure to restore packages with the PublishAot property set to true. [/app/src/HTTPie/HTTPie.csproj::TargetFramework=net6.0]

To Reproduce

Code: https://github.com/WeihanLi/dotnet-httpie/tree/105a17b3e7e0a0c00c2c140498fe1c2ffa17ddeb Build the dockerfile for the docker image

FROM mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine AS base
LABEL Maintainer="WeihanLi"

FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build-env
# Install NativeAOT build prerequisites 
RUN apk update && apk add clang gcc lld musl-dev build-base zlib-dev

WORKDIR /app
COPY ./src/ ./src/
COPY ./build/ ./build/
COPY ./Directory.Build.props ./
COPY ./Directory.Build.targets ./
COPY ./Directory.Packages.props ./
WORKDIR /app/src/HTTPie/
RUN dotnet publish -f net7.0 -c Release -p:AssemblyName=http -p:PublishAot=true -r linux-x64 -o /app/artifacts

FROM base AS final
COPY --from=build-env /app/artifacts/http /root/.dotnet/tools/http
RUN ln -s /root/.dotnet/tools/http /root/.dotnet/tools/dotnet-http
ENV PATH="/root/.dotnet/tools:${PATH}"

CI: https://weihanli.visualstudio.com/Pipelines/_build/results?buildId=5025&view=logs&j=12f1170f-54f2-53f3-20dd-22fc7dff55f9&t=bd05475d-acb5-5619-3ccb-c46842dbc997

Exceptions (if any)

Further technical details

7.0.100-rc.1.22431.12

CI example: https://weihanli.visualstudio.com/Pipelines/_build/results?buildId=5025&view=logs&j=12f1170f-54f2-53f3-20dd-22fc7dff55f9&t=b939ce2a-e73e-5c6a-458a-4f58c0003587

JamesNK commented 1 year ago

I get this error when trying to AOT publish an app as well.

eerhardt commented 1 year ago

@WeihanLi - looking at your repro, I found the following:

  1. You are on an alpine Docker image, but you are trying to publish for -r linux-x64. Instead, you want to publish for -r linux-musl-x64.
  2. I think the bug comes from having <TargetFrameworks>net6.0;net7.0</TargetFrameworks> in your .csproj. For whatever reason, when you multi-target and pass -f net7.0 on the command line, the NativeAOT MSBuild logic is getting confused and erroring out.

To get your project to work, I fixed (1) in your Dockerfile. And then fixed (2) in the .csproj to just <TargetFramework>net7.0</TargetFramework>. After doing this, I was able to successfully build the Docker file.

JamesNK commented 1 year ago

I found this issue only happens when referencing a multitargeted project. Referencing a multitargeted NuGet package is fine. My fix:

  <!-- TODO: Workaround https://github.com/dotnet/sdk/issues/28169. Remove when fixed. -->
  <ItemGroup Condition="'$(PublishAot)'!='true'">
    <ProjectReference Include="..\..\..\src\Grpc.AspNetCore.Server\Grpc.AspNetCore.Server.csproj" />
    <ProjectReference Include="..\..\..\src\Grpc.AspNetCore.Web\Grpc.AspNetCore.Web.csproj" />
  </ItemGroup>
  <ItemGroup Condition="'$(PublishAot)'=='true'">
    <PackageReference Include="Grpc.AspNetCore.Server" Version="$(GrpcDotNetPackageVersion)" />
    <PackageReference Include="Grpc.AspNetCore.Web" Version="$(GrpcDotNetPackageVersion)" />
  </ItemGroup>
WeihanLi commented 1 year ago

Hi @eerhardt , thanks for the help, I can build it success with the following Dockerfile by following your steps:

FROM mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine AS base
LABEL Maintainer="WeihanLi"

FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build-env
# Install NativeAOT build prerequisites 
RUN apk update && apk add clang gcc lld musl-dev build-base zlib-dev

WORKDIR /app
COPY ./src/ ./src/
COPY ./build/ ./build/
COPY ./Directory.Build.props ./
COPY ./Directory.Build.targets ./
COPY ./Directory.Packages.props ./
WORKDIR /app/src/HTTPie/
RUN dotnet publish -f net7.0 -c Release --use-current-runtime -p:AssemblyName=http -p:PublishAot=true -p:TargetFrameworks=net7.0 -o /app/artifacts

FROM base AS final
COPY --from=build-env /app/artifacts/http /root/.dotnet/tools/http
RUN ln -s /root/.dotnet/tools/http /root/.dotnet/tools/dotnet-http
ENV PATH="/root/.dotnet/tools:${PATH}"

details: https://github.com/WeihanLi/dotnet-httpie/blob/8325008f7b43536efe7088b81436298b3d0448db/Dockerfile

The function did not work as expected even published success, I think it may belong to another problem that needs to be modified in the source code.

Is it a bug for the multi-target project? Feel free to close this if not a bug.

I found this issue only happens when referencing a project. Referencing a multitargeted NuGet package is fine

I had no reference for any project, so not sure if it's the same problem

hez2010 commented 10 months ago

This error can even be triggered by having a netstandard2.0 project reference. Is there any way to bypass this TFM check as it is producing a lot of false positives?