Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail the code submission when user input is canceled #3703

Merged
merged 3 commits into from
Oct 8, 2024
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
27 changes: 27 additions & 0 deletions src/Microsoft.DotNet.Interactive.Tests/KeyValueStoreKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,33 @@ public async Task Completions_show_value_options()
.Contain("--name", "--from-url", "--from-file", "--mime-type");
}

[Fact]
public async Task Canceled_input_request_does_not_record_input_token_as_value()
{
using var kernel = CreateKernel();

kernel.RegisterCommandHandler<RequestInput>((requestInput, context) =>
{
context.Fail(requestInput);
return Task.CompletedTask;
});

kernel.SetDefaultTargetKernelNameForCommand(typeof(RequestInput), kernel.Name);

var result = await kernel.SubmitCodeAsync(
"""
#!value --from-value @input:{"type": "file"} --name file
""");

result.Events.Should().ContainSingle<CommandFailed>();

var keyValueStoreKernel = kernel.FindKernelByName("value");

var (_, valueInfosProduced) = await keyValueStoreKernel.TryRequestValueInfosAsync();

valueInfosProduced.ValueInfos.Should().BeEmpty();
}

private static CompositeKernel CreateKernel() =>
new()
{
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.DotNet.Interactive/KeyValueStoreKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ Task<KernelCommand> TryGetKernelCommandAsync(
ExpressionBindingResult expressionBindingResult,
Kernel keyValueStoreKernel)
{
if (expressionBindingResult.Diagnostics.FirstOrDefault(d => d.Severity == DiagnosticSeverity.Error) is { } diagnostic)
{
directiveNode.AddDiagnostic(diagnostic);
return Task.FromResult<KernelCommand>(null);
}

var parameterValues = directiveNode
.GetParameterValues(directive, expressionBindingResult.BoundValues)
.ToDictionary(t => t.Name, t => (t.Value, t.ParameterNode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ internal DirectiveExpressionNode(SourceText sourceText, SyntaxTree syntaxTree) :

public DirectiveExpressionParametersNode? ParametersNode { get; private set; }

public bool IsInputExpression =>
TypeNode?.Type is "input" or "password";

public void Add(DirectiveExpressionTypeNode node)
{
AddInternal(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ void AppendNode(SyntaxNode node)
case DirectiveParameterValueNode valueNode when subcommandNode is null:
directiveNode.Add(valueNode);
break;
case DirectiveParameterValueNode valueNode when subcommandNode is not null:
subcommandNode.Add(valueNode);
break;

case DirectiveParameterValueNode valueNode when subcommandNode is not null:
subcommandNode.Add(valueNode);
Expand Down
11 changes: 10 additions & 1 deletion src/Microsoft.DotNet.Interactive/Parsing/SubmissionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,8 @@ await RequestSingleValueOrInputAsync(
sourceCommand,
directiveNode.TargetKernelName);

if (expressionNode.Parent is { Parent: DirectiveParameterNode { NameNode.Text: { } parameterName } })
if (bindingResult?.IsSuccessful == true &&
expressionNode.Parent is { Parent: DirectiveParameterNode { NameNode.Text: { } parameterName } })
{
if (inputProduced is not null)
{
Expand All @@ -798,6 +799,14 @@ await RequestSingleValueOrInputAsync(
valuesProduced.Add(parameterName, valueProduced);
}
}
else
{
if (directiveNode.DescendantNodesAndTokens().OfType<DirectiveExpressionNode>().Any(node => node.IsInputExpression) &&
KernelInvocationContext.Current is { Command: SubmitCode } context)
{
context.Fail(sourceCommand, message: "Input not provided.");
}
}

return bindingResult;
});
Expand Down