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

js share by reference formatted values #3032

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,43 @@ public async Task value_option_is_required()
.Which.Message.Should().Be("Option '--value' is required.");
}

[Fact]
public async Task ProxyKernels_can_share_values()
Copy link
Contributor

Choose a reason for hiding this comment

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

This test name should be more specific.

Copy link
Member Author

Choose a reason for hiding this comment

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

this ProxyKernels_sharing_values_receives_them_as_deserialized_values

{
using var localCompositeKernel = new CompositeKernel
{
(new CSharpKernel()).UseValueSharing()
};
using var remoteCompositeKernel = new CompositeKernel
{
(new FSharpKernel()).UseValueSharing()
};

ConnectHost.ConnectInProcessHost(
localCompositeKernel,
remoteCompositeKernel);

await localCompositeKernel
.Host
.ConnectProxyKernelOnDefaultConnectorAsync(
"fsharp",
new Uri("kernel://remote/fsharp"));

await remoteCompositeKernel.SendAsync(new SubmitCode("let x = 123"));

var result = await localCompositeKernel.SendAsync(new SubmitCode("""
#!set --name x --value @fsharp:x
x
""", targetKernelName: "csharp"));

result.Events.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.Value
.Should()
.Be(123);
}

private static Kernel CreateKernel(Language language)
{
return language switch
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.Interactive/Connection/ProxyKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ private Task HandleByForwardingToRemoteAsync(KernelCommand command, KernelInvoca
return Task.CompletedTask;
}
}

var targetKernelName = command.TargetKernelName;
if (command.TargetKernelName == Name)
{
Expand Down Expand Up @@ -128,6 +127,7 @@ private Task HandleByForwardingToRemoteAsync(KernelCommand command, KernelInvoca
{
command.TargetKernelName = targetKernelName;


if (te.Result is CommandFailed cf)
{
context.Fail(command, cf.Exception, cf.Message);
Expand Down
60 changes: 20 additions & 40 deletions src/Microsoft.DotNet.Interactive/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,51 +173,29 @@ private static async Task HandleSetMagicCommand<T>(

var valueOptionResult = cmdLineContext.ParseResult.GetValueForOption(valueOption);

// FIX: (HandleSetMagicCommand)

switch (events.Count)
{
case 0: break;
case 1: break;
case 2: break;
default: break;
}

if (valueOptionResult is { })
{
if (valueOptionResult.Kernel is { } sourceKernelName)
{
switch (sourceKernelName)
{
case "input": break;
case "password": break;
default: break;
}
}
else
{
}

if (valueOptionResult.Kernel is { } sourceValueName)
{
}
else
{
}
var sourceKernel = Kernel.Root.FindKernelByName(valueOptionResult.Kernel);

ValueProduced valueProduced;
if (
sourceKernel?.KernelInfo.IsProxy == false
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe KernelInfo can ever be null.

Copy link
Member Author

Choose a reason for hiding this comment

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

if the kernel is not found. that is the case when using @input:x

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah right.

&& valueOptionResult is { Name: var sourceValueName, Kernel: var sourceKernelName }
&& sourceKernelName != "input")
{
valueProduced = events.SingleOrDefault(e =>
e.Name == sourceValueName && e.Command.TargetKernelName == sourceKernelName);
}else if (sourceKernel?.KernelInfo.IsProxy == true
&& valueOptionResult is { Name: var sourceValueName1 })
{
var destinationUri = sourceKernel?.KernelInfo.RemoteUri;

valueProduced = events.SingleOrDefault(e =>
e.Name == sourceValueName1 && e.Command.DestinationUri == destinationUri);
}
else
{
valueProduced = null;
}

var valueProduced = valueOptionResult switch
{
{ Name: var sourceValueName, Kernel: var sourceKernelName } when
// !string.IsNullOrWhiteSpace(sourceKernelName) &&
sourceKernelName != "input" => events.SingleOrDefault(
e => e.Name == sourceValueName && e.Command.TargetKernelName == sourceKernelName),
_ => null
};

if (valueProduced is { })
{
var referenceValue = isByref ? valueProduced.Value : null;
Expand Down Expand Up @@ -383,6 +361,8 @@ ValueOptionResult ParseValueOption(ArgumentResult argResult)
isByref = false;
}

var valueSourceKernel = destinationKernel.RootKernel.FindKernelByName(sourceValueName);
Copy link
Contributor

Choose a reason for hiding this comment

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

No longer needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

ops


var result = destinationKernel.RootKernel.SendAsync(requestValue).GetAwaiter().GetResult();

if (result.Events.LastOrDefault() is CommandFailed failed)
Expand Down