Skip to content

Commit a48d94a

Browse files
authored
Allow empty file-level directive values (#51078)
1 parent cf932ba commit a48d94a

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,8 +1755,8 @@ public readonly struct ParseContext
17551755

17561756
private static (string, string?)? ParseOptionalTwoParts(in ParseContext context, char separator)
17571757
{
1758-
var i = context.DirectiveText.IndexOf(separator, StringComparison.Ordinal);
1759-
var firstPart = (i < 0 ? context.DirectiveText : context.DirectiveText.AsSpan(..i)).TrimEnd();
1758+
var separatorIndex = context.DirectiveText.IndexOf(separator, StringComparison.Ordinal);
1759+
var firstPart = (separatorIndex < 0 ? context.DirectiveText : context.DirectiveText.AsSpan(..separatorIndex)).TrimEnd();
17601760

17611761
string directiveKind = context.DirectiveKind;
17621762
if (firstPart.IsWhiteSpace())
@@ -1770,12 +1770,20 @@ private static (string, string?)? ParseOptionalTwoParts(in ParseContext context,
17701770
return context.Diagnostics.AddError<(string, string?)?>(context.SourceFile, context.Info.Span, string.Format(CliCommandStrings.InvalidDirectiveName, directiveKind, separator));
17711771
}
17721772

1773-
var secondPart = i < 0 ? [] : context.DirectiveText.AsSpan((i + 1)..).TrimStart();
1774-
if (i < 0 || secondPart.IsWhiteSpace())
1773+
if (separatorIndex < 0)
17751774
{
17761775
return (firstPart.ToString(), null);
17771776
}
17781777

1778+
var secondPart = context.DirectiveText.AsSpan((separatorIndex + 1)..).TrimStart();
1779+
if (secondPart.IsWhiteSpace())
1780+
{
1781+
Debug.Assert(secondPart.Length == 0,
1782+
"We have trimmed the second part, so if it's white space, it should be actually empty.");
1783+
1784+
return (firstPart.ToString(), string.Empty);
1785+
}
1786+
17791787
return (firstPart.ToString(), secondPart.ToString());
17801788
}
17811789

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,50 @@ public void Directives_EmptyName(
11541154
expectedWildcardPattern: RunFileTests.DirectiveError("/app/Program.cs", 1, CliCommandStrings.MissingDirectiveName, directive));
11551155
}
11561156

1157+
[Theory]
1158+
[InlineData("")]
1159+
[InlineData(" ")]
1160+
public void Directives_EmptyValue(string value)
1161+
{
1162+
VerifyConversion(
1163+
inputCSharp: $"""
1164+
#:property TargetFramework={value}
1165+
#:property Prop1={value}
1166+
#:sdk First@{value}
1167+
#:sdk Second@{value}
1168+
#:package P1@{value}
1169+
""",
1170+
expectedProject: """
1171+
<Project Sdk="First/">
1172+
1173+
<Sdk Name="Second" Version="" />
1174+
1175+
<PropertyGroup>
1176+
<OutputType>Exe</OutputType>
1177+
<ImplicitUsings>enable</ImplicitUsings>
1178+
<Nullable>enable</Nullable>
1179+
<PublishAot>true</PublishAot>
1180+
<PackAsTool>true</PackAsTool>
1181+
<TargetFramework></TargetFramework>
1182+
<Prop1></Prop1>
1183+
</PropertyGroup>
1184+
1185+
<ItemGroup>
1186+
<PackageReference Include="P1" Version="" />
1187+
</ItemGroup>
1188+
1189+
</Project>
1190+
1191+
""",
1192+
expectedCSharp: "");
1193+
1194+
VerifyConversionThrows(
1195+
inputCSharp: $"""
1196+
#:project{value}
1197+
""",
1198+
expectedWildcardPattern: RunFileTests.DirectiveError("/app/Program.cs", 1, CliCommandStrings.MissingDirectiveName, "project"));
1199+
}
1200+
11571201
[Fact]
11581202
public void Directives_MissingPropertyValue()
11591203
{

0 commit comments

Comments
 (0)