Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion documentation/general/dotnet-run-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Other directives result in a warning, reserving them for future use.
#:sdk Microsoft.NET.Sdk.Web
#:property TargetFramework net11.0
#:property LangVersion preview
#:package System.CommandLine 2.0.0-*
#:package System.CommandLine@2.0.0-*
```

The value must be separated from the name of the directive by white space and any leading and trailing white space is not considered part of the value.
Expand Down
11 changes: 8 additions & 3 deletions src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#nullable enable

using System.Buffers;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Security;
Expand Down Expand Up @@ -814,9 +815,11 @@ public static CSharpDirective Parse(SourceFile sourceFile, TextSpan span, string
};
}

private static (string, string?) ParseOptionalTwoParts(SourceFile sourceFile, TextSpan span, string directiveKind, string directiveText)
private static (string, string?) ParseOptionalTwoParts(SourceFile sourceFile, TextSpan span, string directiveKind, string directiveText, SearchValues<char>? separators = null)
{
var i = directiveText.IndexOf(' ', StringComparison.Ordinal);
var i = separators != null
? directiveText.AsSpan().IndexOfAny(separators)
: directiveText.IndexOf(' ', StringComparison.Ordinal);
var firstPart = checkFirstPart(i < 0 ? directiveText : directiveText[..i]);
var secondPart = i < 0 ? [] : directiveText.AsSpan((i + 1)..).TrimStart();
if (i < 0 || secondPart.IsWhiteSpace())
Expand Down Expand Up @@ -912,14 +915,16 @@ private Property() { }
/// </summary>
public sealed class Package : CSharpDirective
{
private static readonly SearchValues<char> s_separators = SearchValues.Create(' ', '@');

private Package() { }

public required string Name { get; init; }
public string? Version { get; init; }

public static new Package Parse(SourceFile sourceFile, TextSpan span, string directiveKind, string directiveText)
{
var (packageName, packageVersion) = ParseOptionalTwoParts(sourceFile, span, directiveKind, directiveText);
var (packageName, packageVersion) = ParseOptionalTwoParts(sourceFile, span, directiveKind, directiveText, s_separators);

return new Package
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ public void Directives_Separators()
#:sdk Third 3.0=a/b
#:package P1 1.0/a=b
#:package P2 2.0/a=b
#:package P3@1.0 ab
""",
expectedProject: $"""
<Project Sdk="First/1.0=a/b">
Expand All @@ -429,6 +430,7 @@ public void Directives_Separators()
<ItemGroup>
<PackageReference Include="P1" Version="1.0/a=b" />
<PackageReference Include="P2" Version="2.0/a=b" />
<PackageReference Include="P3" Version="1.0 ab" />
</ItemGroup>

</Project>
Expand Down
Loading