Skip to content

Commit

Permalink
[One .NET] fix 'dotnet publish' with no arguments
Browse files Browse the repository at this point in the history
Fixes: dotnet/maui#15696
Context: dotnet/sdk#30038

In .NET 8 Preview 5, there are various changes to default values:

* `dotnet publish` is now `Release` by default

* Builds that provide a `$(RuntimeIdentifier)` are no longer "self
  contained" by default.

The result of this change is the problem:

    dotnet new android
    dotnet publish
    Microsoft.NET.RuntimeIdentifierInference.targets(212,5): error NETSDK1191: A runtime identifier for the property 'SelfContained' couldn't be inferred. Specify a rid explicitly.

While these commands all work:

    dotnet build
    dotnet build -c Release
    dotnet publish -c Debug
    dotnet publish -r android-arm64

`Debug` configurations work fine, because trimming is not involved.
`dotnet build` works fine, because the offending default appears to be:

https://github.com/dotnet/sdk/blob/540b197311bc8d1c2a991fed1b935b094a4d7b2d/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets#L64-L76

`Microsoft.NET.RuntimeIdentifierInference.targets` has a lot of MSBuild
validation logic, that its main job is to emit nice error messages for
incorrect combinations of MSBuild properties/settings.

Android is kind of the odd one out when you compare to .NET console
apps, NativeAOT, etc.:

* We default to `RuntimeIdentifiers=android-arm;android-arm64;android-x86;android-x64`.

* We do our own "inner build" for each `RuntimeIdentifier`.

* Inside our build we force `$(SelfContained)` to `true` no matter what.
  As there is no such thing as a system-wide .NET on Android.

The fix appears to be to default `PublishSelfContained=false` and let
our existing logic force `SelfContained=true`.

I added a new test for this scenario. Our existing test didn't work
because it was passing a `RuntimeIdentifier`.
  • Loading branch information
jonathanpeppers committed Jun 20, 2023
1 parent f1d5918 commit 04341e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<UseCurrentRuntimeIdentifier>false</UseCurrentRuntimeIdentifier>
<PublishTrimmed Condition=" '$(PublishTrimmed)' == '' and ('$(AndroidLinkMode)' == 'SdkOnly' or '$(AndroidLinkMode)' == 'Full') ">true</PublishTrimmed>
<PublishTrimmed Condition=" '$(PublishTrimmed)' == '' and '$(Configuration)' == 'Release' and '$(AndroidLinkMode)' != 'None' ">true</PublishTrimmed>
<!--
This prevents an early error message during 'dotnet publish'.
We handle $(SelfContained) in a custom way where it is forced to be true.
-->
<PublishSelfContained Condition=" '$(PublishSelfContained)' == '' ">false</PublishSelfContained>
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(PublishTrimmed)' == 'true' ">SdkOnly</AndroidLinkMode>
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' ">None</AndroidLinkMode>
<!-- For compat with user code not marked trimmable, only trim opt-in by default. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,18 @@ static bool IsPreviewFrameworkVersion (string targetFramework)
&& XABuildConfig.AndroidLatestUnstableApiLevel != XABuildConfig.AndroidLatestStableApiLevel);
}

[Test]
public void DotNetPublishDefaultValues([Values (false, true)] bool isRelease)
{
var proj = new XamarinAndroidApplicationProject {
IsRelease = isRelease
};
var builder = CreateDllBuilder ();
builder.Save (proj);
var dotnet = new DotNetCLI (Path.Combine (Root, builder.ProjectDirectory, proj.ProjectFilePath));
Assert.IsTrue (dotnet.Publish (), "`dotnet publish` should succeed");
}

[Test]
public void DotNetPublish ([Values (false, true)] bool isRelease, [ValueSource(nameof(DotNetTargetFrameworks))] object[] data)
{
Expand Down

0 comments on commit 04341e6

Please sign in to comment.