dotnet / sdk-container-builds

Libraries and build tooling to create container images from .NET projects using MSBuild
https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container
MIT License
175 stars 30 forks source link

Can't run `ContainerPublish` in solution with multi targeting #578

Open afscrome opened 1 week ago

afscrome commented 1 week ago

37072 was merged to allow you to run dotnet publish /t:ContainerPublish against a solution and have it ignore projects in which SDK Container Publishing was not enabled.

Whilt this works for many projects, if your solution has any multi targeted projects (e.g. <TargetFrameworks>net472;netstandard2.0;net6.0;net8.0</TargetFrameworks>), trying to publish the solution fails on the multi targeted project(s)

error MSB4057: The target "PublishContainer" does not exist in the project.

Looking at the bin log, the SDK declined to import Microsoft.NET.Build.Containers.targets. (although oddly it did import Microsoft.NET.Build.Containers.props)

image

Interestingly, whilst the Microsoft.NET.Build.Containers.targets wasn't imported, Microsoft.NET.Build.Containers.props was - I'd expect either both or neither of these files to be imported, and not just one.

NoImport: $ (_ContainersTargetsDir)Microsoft.NET.Build.Containers.targets at (64;3) false condition; (Exists('$(_ContainersTargetsDir)Microsoft.NET.Build.Containers.targets') AND '$(TargetFramework)' != '' was evaluated as Exists('C:\Program Files\dotnet\sdk\8.0.302\Sdks\Microsoft.NET.Sdk\Sdk......\Containers\build\Microsoft.NET.Build.Containers.targets') AND '' != '').

(Note a space was added in $(_ContainerTargetsDir) above as otherwise github tired to format it as a mathematical equation.

The problem seems to be that the target import includes a '$(TargetFramework)' != ''" check https://github.com/dotnet/sdk/blob/3cfb3ea0bf20d99ffba14e1cb56e07f0806ce1eb/src/Tasks/Microsoft.NET.Build.Tasks/sdk/Sdk.targets#L65

To be clear, if Publishing Containers doesn't work with multi targeting, that's fine by me. However I'm expecting the target to be there and be a no-op if IsPublishable == false or if the project doesn't have container configuration.