Skip to content

Commit 6942fe7

Browse files
authored
Merge pull request #28628 from nagilson/nagilson-exe-rids
Allow Implicit RID Opt Out + Don't Infer RID for non EXE Type Projects
2 parents 63b3654 + d3173a2 commit 6942fe7

File tree

7 files changed

+121
-9
lines changed

7 files changed

+121
-9
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace newc;
2+
3+
static class Program
4+
{
5+
static void Main()
6+
{
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace lib;
2+
public class Class1
3+
{
4+
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<PublishSingleFile>true</PublishSingleFile>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net7.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<UseWindowsForms>true</UseWindowsForms>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<PublishSingleFile>true</PublishSingleFile>
10+
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="lib\lib.csproj" />
15+
</ItemGroup>
16+
17+
</Project>

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,21 @@ Copyright (c) .NET Foundation. All rights reserved.
6161
<RuntimeIdentifier Condition="'$(PlatformTarget)' == 'x86' or '$(PlatformTarget)' == ''">win7-x86</RuntimeIdentifier>
6262
</PropertyGroup>
6363

64-
<PropertyGroup Condition="'$(UseCurrentRuntimeIdentifier)' == 'true' or
65-
(
64+
<PropertyGroup Condition="'$(UseCurrentRuntimeIdentifier)' == ''">
65+
<UseCurrentRuntimeIdentifier Condition="
6666
'$(RuntimeIdentifier)' == '' and
6767
'$(RuntimeIdentifiers)' == '' and
68-
(
69-
'$(SelfContained)' == 'true' or
70-
'$(PublishReadyToRun)' == 'true' or
71-
'$(PublishSingleFile)' == 'true' or
72-
'$(PublishAot)' == 'true'
73-
)
74-
)">
68+
'$(_IsExecutable)' == 'true' and '$(IsTestProject)' != 'true' and
69+
'$(IsRidAgnostic)' != 'true' and
70+
(
71+
'$(SelfContained)' == 'true' or
72+
'$(PublishReadyToRun)' == 'true' or
73+
'$(PublishSingleFile)' == 'true' or
74+
'$(PublishAot)' == 'true'
75+
)">true</UseCurrentRuntimeIdentifier>
76+
</PropertyGroup>
77+
78+
<PropertyGroup Condition="'$(UseCurrentRuntimeIdentifier)' == 'true'">
7579
<RuntimeIdentifier>$(NETCoreSdkPortableRuntimeIdentifier)</RuntimeIdentifier>
7680
</PropertyGroup>
7781

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.IO;
5+
using FluentAssertions;
6+
using Microsoft.NET.TestFramework;
7+
using Microsoft.NET.TestFramework.Assertions;
8+
using Microsoft.NET.TestFramework.Commands;
9+
using Xunit;
10+
using Xunit.Abstractions;
11+
12+
namespace Microsoft.NET.Publish.Tests
13+
{
14+
public class GivenThatWeWantToPublishASingleFileLibrary : SdkTest
15+
{
16+
public GivenThatWeWantToPublishASingleFileLibrary(ITestOutputHelper log) : base(log)
17+
{
18+
}
19+
20+
[WindowsOnlyFact]
21+
// Tests regression on https://github.com/dotnet/sdk/pull/28484
22+
public void ItPublishesSuccessfullyWithRIDAndPublishSingleFileLibrary()
23+
{
24+
var targetFramework = ToolsetInfo.CurrentTargetFramework;
25+
var testAsset = _testAssetsManager
26+
.CopyTestAsset("AppWithLibrarySDKStyleThatPublishesSingleFile")
27+
.WithTargetFramework(targetFramework)
28+
.WithSource();
29+
30+
var publishCommand = new PublishCommand(testAsset);
31+
publishCommand.Execute()
32+
.Should()
33+
.Pass();
34+
35+
// It would be better if we could somehow check the library binlog or something for a RID instead.
36+
var exeFolder = publishCommand.GetOutputDirectory(targetFramework: targetFramework);
37+
// Parent: RID, then TFM, then Debug, then bin, then the test folder
38+
var ridlessLibraryDllPath = Path.Combine(exeFolder.Parent.Parent.Parent.Parent.FullName, "lib", "bin", "Debug", targetFramework, "lib.dll");
39+
Assert.True(File.Exists(ridlessLibraryDllPath));
40+
}
41+
42+
}
43+
44+
}

src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ public void PublishWithRuntimeIdentifier(bool publishNoBuild)
193193
}
194194
}
195195

196+
[Fact]
197+
public void ImplicitRuntimeIdentifierOptOutCorrecltyOptsOut()
198+
{
199+
var targetFramework = ToolsetInfo.CurrentTargetFramework;
200+
var runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
201+
var testProject = new TestProject()
202+
{
203+
IsExe = true,
204+
TargetFrameworks = targetFramework
205+
};
206+
testProject.AdditionalProperties["SelfContained"] = "true";
207+
testProject.AdditionalProperties["UseCurrentRuntimeIdentifier"] = "false";
208+
209+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
210+
211+
var publishCommand = new PublishCommand(testAsset);
212+
publishCommand
213+
.Execute()
214+
.Should()
215+
.Fail()
216+
.And
217+
.HaveStdOutContaining("NETSDK1191");
218+
}
219+
196220
[Fact]
197221
public void DuplicateRuntimeIdentifiers()
198222
{

0 commit comments

Comments
 (0)