diff --git a/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs b/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs index bf80f0b8c9b6..5a34e59fbb1f 100644 --- a/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs +++ b/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs @@ -578,13 +578,20 @@ public static void WriteProjectFile( var packageDirectives = directives.OfType(); var projectDirectives = directives.OfType(); - string sdkValue = "Microsoft.NET.Sdk"; + string firstSdkName; + string? firstSdkVersion; if (sdkDirectives.FirstOrDefault() is { } firstSdk) { - sdkValue = firstSdk.ToSlashDelimitedString(); + firstSdkName = firstSdk.Name; + firstSdkVersion = firstSdk.Version; processedDirectives++; } + else + { + firstSdkName = "Microsoft.NET.Sdk"; + firstSdkVersion = null; + } if (isVirtualProject) { @@ -607,13 +614,28 @@ public static void WriteProjectFile( - """); + + if (firstSdkVersion is null) + { + writer.WriteLine($""" + + """); + } + else + { + writer.WriteLine($""" + + """); + } } else { + string slashDelimited = firstSdkVersion is null + ? firstSdkName + : $"{firstSdkName}/{firstSdkVersion}"; writer.WriteLine($""" - + """); } @@ -776,7 +798,7 @@ public static void WriteProjectFile( if (!sdkDirectives.Any()) { - Debug.Assert(sdkValue == "Microsoft.NET.Sdk"); + Debug.Assert(firstSdkName == "Microsoft.NET.Sdk" && firstSdkVersion == null); writer.WriteLine(""" """); @@ -1136,11 +1158,6 @@ private Sdk() { } Version = sdkVersion, }; } - - public string ToSlashDelimitedString() - { - return Version is null ? Name : $"{Name}/{Version}"; - } } /// diff --git a/test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs b/test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs index e07855c873eb..0f2f9b045ee2 100644 --- a/test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs +++ b/test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs @@ -1156,6 +1156,33 @@ public void Directives_Duplicate() ]); } + [Fact] // https://github.com/dotnet/sdk/issues/49797 + public void Directives_VersionedSdkFirst() + { + VerifyConversion( + inputCSharp: """ + #:sdk Microsoft.NET.Sdk@9.0.0 + Console.WriteLine(); + """, + expectedProject: $""" + + + + Exe + {ToolsetInfo.CurrentTargetFramework} + enable + enable + true + + + + + """, + expectedCSharp: """ + Console.WriteLine(); + """); + } + private static void Convert(string inputCSharp, out string actualProject, out string? actualCSharp, bool force, string? filePath) { var sourceFile = new SourceFile(filePath ?? "/app/Program.cs", SourceText.From(inputCSharp, Encoding.UTF8)); diff --git a/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs b/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs index 98b62c75dd06..044a46cec47f 100644 --- a/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs +++ b/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs @@ -1555,6 +1555,21 @@ public void SdkReference() .Should().Pass(); } + [Fact] // https://github.com/dotnet/sdk/issues/49797 + public void SdkReference_VersionedSdkFirst() + { + var testInstance = _testAssetsManager.CreateTestDirectory(); + File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """ + #:sdk Microsoft.NET.Sdk@9.0.0 + Console.WriteLine(); + """); + + new DotnetCommand(Log, "build", "Program.cs") + .WithWorkingDirectory(testInstance.Path) + .Execute() + .Should().Pass(); + } + [Theory] [InlineData("../Lib/Lib.csproj")] [InlineData("../Lib")]