Skip to content
Open
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
3 changes: 1 addition & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@
<PackageVersion Include="SkiaSharp" Version="3.119.0" />
<PackageVersion Include="StreamJsonRpc" Version="2.21.69" />
<PackageVersion Include="System.Collections.Immutable" Version="9.0.6" />
<PackageVersion Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-rc.1.25413.101" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - I remember us removing all System.CommandLine dependencies (when you made the parser changes) but don't recall when we added it back...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.CommandLine had been used for parsing magic commands. That code was removed and a new, more fit-for-purpose parser was written. System.CommandLine was never removed for actual command line parsing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad - I thought it had been removed as part of the push to get all the interactive pieces to GA.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the driver, but also the magic command parser needed improvements that couldn't be implemented with System.CommandLine, while System.CommandLine was also having features removed to prepare it for GA.

<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="9.0.6" />
<PackageVersion Include="System.Drawing.Common" Version="9.0.6" />
Expand Down
4 changes: 4 additions & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<clear />
<add key="dotnet-public" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
<add key="dotnet-libraries" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-libraries/nuget/v3/index.json" />
</packageSources>
<packageSourceMapping>
<packageSource key="dotnet-eng">
Expand All @@ -22,6 +23,9 @@
<package pattern="Microsoft.SymbolUploader.Build.Task" />
<package pattern="sn" />
</packageSource>
<packageSource key="dotnet-libraries">
<package pattern="System.CommandLine" />
</packageSource>
<packageSource key="dotnet-public">
<package pattern="*" />
</packageSource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public void GetPosition_Should_Return_String_Without_Dollar_Signs()
public void GetPosition_Should_Return_Correct_Position()
{
var input = "some$$string";
string output;
MarkupTestFile.GetPosition(input, out output, out var position);
MarkupTestFile.GetPosition(input, out var output, out var position);
Assert.Equal(4, position);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<ProjectConfiguration>
<ProjectConfiguration>
<Settings>
<DefaultTestTimeout>20000</DefaultTestTimeout>
<IgnoredTests>
<NamedTestSelector>
<TestName>Microsoft.DotNet.Interactive.Http.Tests.HttpKernelTests+NamedRequest.body_content_produces_the_entirety_of_the_body_content("example.response.body.*")</TestName>
</NamedTestSelector>
<NamedTestSelector>
<TestName>Microsoft.DotNet.Interactive.Http.Tests.HttpKernelTests.traceparent_header_has_a_new_top_level_value_for_each_request</TestName>
</NamedTestSelector>
</IgnoredTests>
</Settings>
</ProjectConfiguration>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.DotNet.Interactive.Telemetry;
public sealed class StartupTelemetryEventBuilder
{
private readonly Func<string, string> _hash;
private readonly HashSet<string> _clearTextProperties = new(new[] { "frontend" });
private readonly HashSet<string> _clearTextProperties = ["frontend"];

public StartupTelemetryEventBuilder(Func<string, string> hash)
{
Expand Down Expand Up @@ -80,7 +80,7 @@ public IEnumerable<TelemetryEvent> GetTelemetryEventsFrom(ParseResult parseResul

var commandResult = parseResult.CommandResult;

var frontendName = GetFrontendName(parseResult.Directives, parseResult.CommandResult);
var frontendName = GetFrontendName(parseResult, parseResult.CommandResult);
entryItems.Add(new KeyValuePair<string, string>("frontend", frontendName));

foreach (var item in rule.Items)
Expand All @@ -94,7 +94,10 @@ public IEnumerable<TelemetryEvent> GetTelemetryEventsFrom(ParseResult parseResul
switch (item)
{
case OptionItem optItem:
var optionValue = commandResult.Children.OfType<OptionResult>().FirstOrDefault(o => o.Option.HasAlias(optItem.Option))?.GetValueOrDefault()?.ToString();
var optionResult = commandResult.Children.OfType<OptionResult>().FirstOrDefault(o => o.Option.Name == optItem.Option);

var optionValue = optionResult?.GetValue<object>(optItem.Option)?.ToString();

if (optionValue is not null && optItem.Values.Contains(optionValue))
{
entryItems.Add(new KeyValuePair<string, string>(optItem.EntryKey, optionValue));
Expand Down Expand Up @@ -259,23 +262,23 @@ private static CommandRuleItem Ignore(TokenType type, bool isOptional)
};

private static string GetFrontendName(
DirectiveCollection directives,
ParseResult parseResult,
CommandResult commandResult)
{
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("CODESPACES")))
{
return "gitHubCodeSpaces";
}

foreach (var directive in directives)
foreach (var directive in parseResult.Tokens.Where(t => t is { Type: TokenType.Directive }))
{
switch (directive.Key)
switch (directive.Value)
{
case "jupyter":
case "synapse":
case "vscode":
case "vs":
return directive.Key;
case "[jupyter]":
case "[synapse]":
case "[vscode]":
case "[vs]":
return directive.Value.Trim('[', ']');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics;
using System.Reflection;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.Interactive/KernelHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal KernelHost(
_kernel = kernel;
_defaultSender = sender;
_receiver = receiver;
_defaultConnector = async (name) =>
_defaultConnector = async name =>
{
var connector = new DefaultKernelConnector(
_defaultSender,
Expand Down
76 changes: 76 additions & 0 deletions src/dotnet-interactive.Tests/CommandLine/CommandExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Linq;
using Microsoft.DotNet.Interactive.App.Tests.Extensions;

namespace Microsoft.DotNet.Interactive.App.Tests.CommandLine;

public static class CommandExtensions
{
/// <summary>
/// Throws an exception if the parser configuration is ambiguous or otherwise not valid.
/// </summary>
/// <remarks>Due to the performance cost of this method, it is recommended to be used in unit testing or in scenarios where the parser is configured dynamically at runtime.</remarks>
/// <exception cref="InvalidOperationException">Thrown if the configuration is found to be invalid.</exception>
public static void ThrowIfInvalid(this Command command)
{
if (command.Parents.FlattenBreadthFirst(c => c.Parents).Any(ancestor => ancestor == command))
{
throw new InvalidOperationException($"Cycle detected in command tree. Command '{command.Name}' is its own ancestor.");
}

int count = command.Subcommands.Count + command.Options.Count;
for (var i = 0; i < count; i++)
{
Symbol symbol1 = GetChild(i, command, out HashSet<string> aliases1);

for (var j = i + 1; j < count; j++)
{
Symbol symbol2 = GetChild(j, command, out HashSet<string> aliases2);

if (symbol1.Name.Equals(symbol2.Name, StringComparison.Ordinal)
|| aliases1 is not null && aliases1.Contains(symbol2.Name))
{
throw new InvalidOperationException($"Duplicate alias '{symbol2.Name}' found on command '{command.Name}'.");
}
else if (aliases2 is not null && aliases2.Contains(symbol1.Name))
{
throw new InvalidOperationException($"Duplicate alias '{symbol1.Name}' found on command '{command.Name}'.");
}

if (aliases1 is not null && aliases2 is not null)
{
// take advantage of the fact that we are dealing with two hash sets
if (aliases1.Overlaps(aliases2))
{
foreach (string symbol2Alias in aliases2)
{
if (aliases1.Contains(symbol2Alias))
{
throw new InvalidOperationException($"Duplicate alias '{symbol2Alias}' found on command '{command.Name}'.");
}
}
}
}
}

if (symbol1 is Command childCommand)
{
childCommand.ThrowIfInvalid();
}
}

static Symbol GetChild(int index, Command command, out HashSet<string> aliases)
{
if (index < command.Subcommands.Count)
{
aliases = command.Subcommands[index].Aliases.ToHashSet();
return command.Subcommands[index];
}

aliases = command.Options[index - command.Subcommands.Count].Aliases.ToHashSet();
return command.Options[index - command.Subcommands.Count];
}
}
}
Loading
Loading