Skip to content

Commit 58bc4f2

Browse files
authored
[release/10.0.1xx-preview7] Handle versioned #:sdk being first (#49899)
2 parents 3af01eb + 112ca2e commit 58bc4f2

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
@@ -631,13 +631,20 @@ public static void WriteProjectFile(
631631
var packageDirectives = directives.OfType<CSharpDirective.Package>();
632632
var projectDirectives = directives.OfType<CSharpDirective.Project>();
633633

634-
string sdkValue = "Microsoft.NET.Sdk";
634+
string firstSdkName;
635+
string? firstSdkVersion;
635636

636637
if (sdkDirectives.FirstOrDefault() is { } firstSdk)
637638
{
638-
sdkValue = firstSdk.ToSlashDelimitedString();
639+
firstSdkName = firstSdk.Name;
640+
firstSdkVersion = firstSdk.Version;
639641
processedDirectives++;
640642
}
643+
else
644+
{
645+
firstSdkName = "Microsoft.NET.Sdk";
646+
firstSdkVersion = null;
647+
}
641648

642649
if (isVirtualProject)
643650
{
@@ -660,13 +667,28 @@ public static void WriteProjectFile(
660667
</ItemGroup>
661668
662669
<!-- We need to explicitly import Sdk props/targets so we can override the targets below. -->
663-
<Import Project="Sdk.props" Sdk="{EscapeValue(sdkValue)}" />
664670
""");
671+
672+
if (firstSdkVersion is null)
673+
{
674+
writer.WriteLine($"""
675+
<Import Project="Sdk.props" Sdk="{EscapeValue(firstSdkName)}" />
676+
""");
677+
}
678+
else
679+
{
680+
writer.WriteLine($"""
681+
<Import Project="Sdk.props" Sdk="{EscapeValue(firstSdkName)}" Version="{EscapeValue(firstSdkVersion)}" />
682+
""");
683+
}
665684
}
666685
else
667686
{
687+
string slashDelimited = firstSdkVersion is null
688+
? firstSdkName
689+
: $"{firstSdkName}/{firstSdkVersion}";
668690
writer.WriteLine($"""
669-
<Project Sdk="{EscapeValue(sdkValue)}">
691+
<Project Sdk="{EscapeValue(slashDelimited)}">
670692
671693
""");
672694
}
@@ -829,7 +851,7 @@ public static void WriteProjectFile(
829851

830852
if (!sdkDirectives.Any())
831853
{
832-
Debug.Assert(sdkValue == "Microsoft.NET.Sdk");
854+
Debug.Assert(firstSdkName == "Microsoft.NET.Sdk" && firstSdkVersion == null);
833855
writer.WriteLine("""
834856
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
835857
""");
@@ -1189,11 +1211,6 @@ private Sdk() { }
11891211
Version = sdkVersion,
11901212
};
11911213
}
1192-
1193-
public string ToSlashDelimitedString()
1194-
{
1195-
return Version is null ? Name : $"{Name}/{Version}";
1196-
}
11971214
}
11981215

11991216
/// <summary>

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,33 @@ public void Directives_Duplicate()
11561156
]);
11571157
}
11581158

1159+
[Fact] // https://github.com/dotnet/sdk/issues/49797
1160+
public void Directives_VersionedSdkFirst()
1161+
{
1162+
VerifyConversion(
1163+
inputCSharp: """
1164+
#:sdk Microsoft.NET.Sdk@9.0.0
1165+
Console.WriteLine();
1166+
""",
1167+
expectedProject: $"""
1168+
<Project Sdk="Microsoft.NET.Sdk/9.0.0">
1169+
1170+
<PropertyGroup>
1171+
<OutputType>Exe</OutputType>
1172+
<TargetFramework>{ToolsetInfo.CurrentTargetFramework}</TargetFramework>
1173+
<ImplicitUsings>enable</ImplicitUsings>
1174+
<Nullable>enable</Nullable>
1175+
<PublishAot>true</PublishAot>
1176+
</PropertyGroup>
1177+
1178+
</Project>
1179+
1180+
""",
1181+
expectedCSharp: """
1182+
Console.WriteLine();
1183+
""");
1184+
}
1185+
11591186
private static void Convert(string inputCSharp, out string actualProject, out string? actualCSharp, bool force, string? filePath)
11601187
{
11611188
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
@@ -1555,6 +1555,21 @@ public void SdkReference()
15551555
.Should().Pass();
15561556
}
15571557

1558+
[Fact] // https://github.com/dotnet/sdk/issues/49797
1559+
public void SdkReference_VersionedSdkFirst()
1560+
{
1561+
var testInstance = _testAssetsManager.CreateTestDirectory();
1562+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """
1563+
#:sdk Microsoft.NET.Sdk@9.0.0
1564+
Console.WriteLine();
1565+
""");
1566+
1567+
new DotnetCommand(Log, "build", "Program.cs")
1568+
.WithWorkingDirectory(testInstance.Path)
1569+
.Execute()
1570+
.Should().Pass();
1571+
}
1572+
15581573
[Theory]
15591574
[InlineData("../Lib/Lib.csproj")]
15601575
[InlineData("../Lib")]

0 commit comments

Comments
 (0)