Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

new magic command parser fails if quoted parameter value doesn't contain valid JSON #3612

Description

@jonsequitur

In many cases, quoted strings don't contain JSON. In the previous version of the parser (using System.CommandLine), quoting a value was simply a way to denote a string that intentionally contains whitespace. This is still supported but the heuristic for determining which case to use was insufficient, resulting in errors like the following. Given a submission containing e.g...

#!import "C:\Program Files\Microsoft Visual Studio\2022\Main\Common7\IDE\Extensions\Microsoft\Web Tools\Languages\Formatters\HttpResponseFormatter"

...the following error is produced, because the Utf8JsonReader assumes the substring \P is an (invalid) escape sequence.

D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\KernelHost.cs:line 124 System.Text.Json.JsonReaderException: 'p' is an invalid escapable character within a JSON string. The string should be correctly escaped. LineNumber: 0 | BytePositionInLine: 4.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.ConsumeStringAndValidate(ReadOnlySpan`1 data, Int32 idx)
   at System.Text.Json.Utf8JsonReader.ConsumeString()
   at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker)
   at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte first)
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Utf8JsonWriter.WriteRawValueCore(ReadOnlySpan`1 utf8Json, Boolean skipInputValidation)
   at System.Text.Json.Utf8JsonWriter.TranscodeAndWriteRawValue(ReadOnlySpan`1 json, Boolean skipInputValidation)
   at System.Text.Json.Utf8JsonWriter.WriteRawValue(String json, Boolean skipInputValidation)
   at Microsoft.DotNet.Interactive.Parsing.DirectiveNode.<TryGetJsonAsync>g__WriteProperty|26_0(String propertyName, String value, <>c__DisplayClass26_0&) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\Parsing\DirectiveNode.cs:line 351
   at Microsoft.DotNet.Interactive.Parsing.DirectiveNode.TryGetJsonAsync(DirectiveBindingDelegate bind) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\Parsing\DirectiveNode.cs:line 319
   at Microsoft.DotNet.Interactive.Parsing.DirectiveNode.TryGetJsonAsync(DirectiveBindingDelegate bind) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\Parsing\DirectiveNode.cs:line 344
   at Microsoft.DotNet.Interactive.Parsing.SubmissionParser.<>c__DisplayClass7_0.<<SplitSubmission>g__CreateActionDirectiveCommand|7>d.MoveNext() in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\Parsing\SubmissionParser.cs:line 378
--- End of stack trace from previous location ---
   at Microsoft.DotNet.Interactive.Parsing.SubmissionParser.SplitSubmission(KernelCommand originalCommand, String code, CreateChildCommand createChildCommand) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\Parsing\SubmissionParser.cs:line 132
   at Microsoft.DotNet.Interactive.Parsing.SubmissionParser.SplitSubmission(SubmitCode submitCode) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\Parsing\SubmissionParser.cs:line 44
   at Microsoft.DotNet.Interactive.Kernel.TrySplitCommand(KernelCommand originalCommand, KernelInvocationContext context) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\Kernel.cs:line 152
   at Microsoft.DotNet.Interactive.Kernel.SendAsync(KernelCommand command, CancellationToken cancellationToken) in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\Kernel.cs:line 397
   at Microsoft.DotNet.Interactive.KernelHost.<>c__DisplayClass13_1.<<ConnectAsync>b__4>d.MoveNext() in D:\a\_work\1\s\src\Microsoft.DotNet.Interactive\KernelHost.cs:line 124

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions