Skip to content

Commit ab9e137

Browse files
authored
Handle versioned #:sdk being first (#49807)
1 parent 8e70150 commit ab9e137

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -642,13 +642,20 @@ public static void WriteProjectFile(
642642
var packageDirectives = directives.OfType<CSharpDirective.Package>();
643643
var projectDirectives = directives.OfType<CSharpDirective.Project>();
644644

645-
string sdkValue = "Microsoft.NET.Sdk";
645+
string firstSdkName;
646+
string? firstSdkVersion;
646647

647648
if (sdkDirectives.FirstOrDefault() is { } firstSdk)
648649
{
649-
sdkValue = firstSdk.ToSlashDelimitedString();
650+
firstSdkName = firstSdk.Name;
651+
firstSdkVersion = firstSdk.Version;
650652
processedDirectives++;
651653
}
654+
else
655+
{
656+
firstSdkName = "Microsoft.NET.Sdk";
657+
firstSdkVersion = null;
658+
}
652659

653660
if (isVirtualProject)
654661
{
@@ -672,13 +679,28 @@ public static void WriteProjectFile(
672679
</ItemGroup>
673680
674681
<!-- We need to explicitly import Sdk props/targets so we can override the targets below. -->
675-
<Import Project="Sdk.props" Sdk="{EscapeValue(sdkValue)}" />
676682
""");
683+
684+
if (firstSdkVersion is null)
685+
{
686+
writer.WriteLine($"""
687+
<Import Project="Sdk.props" Sdk="{EscapeValue(firstSdkName)}" />
688+
""");
689+
}
690+
else
691+
{
692+
writer.WriteLine($"""
693+
<Import Project="Sdk.props" Sdk="{EscapeValue(firstSdkName)}" Version="{EscapeValue(firstSdkVersion)}" />
694+
""");
695+
}
677696
}
678697
else
679698
{
699+
string slashDelimited = firstSdkVersion is null
700+
? firstSdkName
701+
: $"{firstSdkName}/{firstSdkVersion}";
680702
writer.WriteLine($"""
681-
<Project Sdk="{EscapeValue(sdkValue)}">
703+
<Project Sdk="{EscapeValue(slashDelimited)}">
682704
683705
""");
684706
}
@@ -843,7 +865,7 @@ public static void WriteProjectFile(
843865

844866
if (!sdkDirectives.Any())
845867
{
846-
Debug.Assert(sdkValue == "Microsoft.NET.Sdk");
868+
Debug.Assert(firstSdkName == "Microsoft.NET.Sdk" && firstSdkVersion == null);
847869
writer.WriteLine("""
848870
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
849871
""");
@@ -1205,11 +1227,6 @@ private Sdk() { }
12051227
Version = sdkVersion,
12061228
};
12071229
}
1208-
1209-
public string ToSlashDelimitedString()
1210-
{
1211-
return Version is null ? Name : $"{Name}/{Version}";
1212-
}
12131230
}
12141231

12151232
/// <summary>

test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,33 @@ public void Directives_Duplicate()
11691169
]);
11701170
}
11711171

1172+
[Fact] // https://github.com/dotnet/sdk/issues/49797
1173+
public void Directives_VersionedSdkFirst()
1174+
{
1175+
VerifyConversion(
1176+
inputCSharp: """
1177+
1178+
Console.WriteLine();
1179+
""",
1180+
expectedProject: $"""
1181+
<Project Sdk="Microsoft.NET.Sdk/9.0.0">
1182+
1183+
<PropertyGroup>
1184+
<OutputType>Exe</OutputType>
1185+
<TargetFramework>{ToolsetInfo.CurrentTargetFramework}</TargetFramework>
1186+
<ImplicitUsings>enable</ImplicitUsings>
1187+
<Nullable>enable</Nullable>
1188+
<PublishAot>true</PublishAot>
1189+
</PropertyGroup>
1190+
1191+
</Project>
1192+
1193+
""",
1194+
expectedCSharp: """
1195+
Console.WriteLine();
1196+
""");
1197+
}
1198+
11721199
private static void Convert(string inputCSharp, out string actualProject, out string? actualCSharp, bool force, string? filePath)
11731200
{
11741201
var sourceFile = new SourceFile(filePath ?? "/app/Program.cs", SourceText.From(inputCSharp, Encoding.UTF8));

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,21 @@ public void SdkReference()
15651565
.Should().Pass();
15661566
}
15671567

1568+
[Fact] // https://github.com/dotnet/sdk/issues/49797
1569+
public void SdkReference_VersionedSdkFirst()
1570+
{
1571+
var testInstance = _testAssetsManager.CreateTestDirectory();
1572+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
1573+
1574+
Console.WriteLine();
1575+
""");
1576+
1577+
new DotnetCommand(Log, "build", "Program.cs")
1578+
.WithWorkingDirectory(testInstance.Path)
1579+
.Execute()
1580+
.Should().Pass();
1581+
}
1582+
15681583
[Theory]
15691584
[InlineData("../Lib/Lib.csproj")]
15701585
[InlineData("../Lib")]

0 commit comments

Comments
 (0)