dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.87k stars 781 forks source link

In FSI, an invalid string literal can break further parsing of string in that interactive session #14160

Open abonie opened 1 year ago

abonie commented 1 year ago

In FSI, inputting an interpolated string literal that opens interpolation expression with { but does not close it, e.g. $"{" breaks the session in a way that further valid string literals can no longer be parsed

Repro steps

In dotnet fsi try:

> $"{";;

followed by any(?) string literal, e.g. empty string:

> "";;

Full session example:

# dotnet fsi

Microsoft (R) F# Interactive version 12.0.5.0 for F# 7.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> $"{";;

  $"{";;
  ---^

stdin(1,4): error FS3373: Invalid interpolated string. Single quote or verbatim string literals may not be used in interpolated expressions in single quote or verbatim strings. Consider using an explicit 'let' binding for the interpolation expression or use a triple quote string as the outer string literal.

> "";;

  "";;
  ^

stdin(2,1): error FS3373: Invalid interpolated string. Single quote or verbatim string literals may not be used in interpolated expressions in single quote or verbatim strings. Consider using an explicit 'let' binding for the interpolation expression or use a triple quote string as the outer string literal.

  $"{";;
  -----^

stdin(1,6): error FS3373: Invalid interpolated string. Single quote or verbatim string literals may not be used in interpolated expressions in single quote or verbatim strings. Consider using an explicit 'let' binding for the interpolation expression or use a triple quote string as the outer string literal.

> > ;;
> "";;

  ;;
  ^

stdin(3,1): error FS3373: Invalid interpolated string. Single quote or verbatim string literals may not be used in interpolated expressions in single quote or verbatim strings. Consider using an explicit 'let' binding for the interpolation expression or use a triple quote string as the outer string literal.

  "";;
  --^

stdin(2,3): error FS3373: Invalid interpolated string. Single quote or verbatim string literals may not be used in interpolated expressions in single quote or verbatim strings. Consider using an explicit 'let' binding for the interpolation expression or use a triple quote string as the outer string literal.

> >

Note: the invalid input results in "error FS3373: Invalid interpolated string", however not all inputs that result in this particular error are followed by this buggy behavior, e.g. $"{""}" or $"{"}" result in error FS3373, but don't break the session.

Expected behavior

Valid string literals are parsed correctly, regardless of erroneous input that happened earlier in that same interactive session.

Actual behavior

Certain inputs make FSI unable to parse valid strings afterwards.

Known workarounds

Close FSI session and open a new one

Related information

Provide any related information (optional):

abonie commented 1 year ago

I think I incidentally stumbled upon a solution as I've been working on lexing recently, so I am looking into this issue.

No clue how to unit test this yet though.

abonie commented 1 year ago

Reopening as https://github.com/dotnet/fsharp/pull/14182 had to be reverted due to https://github.com/dotnet/fsharp/issues/14757

MrLuje commented 1 year ago

I have a similiar behavior (broker parsing) that doesn't involve "{", should I open a different issue ?

let cst = "Error creating bean with name 'org.sonarsource.scanner.api.internal.IsolatedClassloader@37d688b1-org.sonar.scanner.bootstrap.ScannerPluginRepository': Initialization of bean failed"

Send it 4 times to FSI

FSI

Microsoft (R) F# Interactive version 12.5.0.0 for F# 7.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> # silentCd @"/home/vince/src/github/mrluje/fs-gitlab-stats";;
- # 1 @"/home/vince/src/github/mrluje/fs-gitlab-stats/test.fsx"
- ;;
> let cst = "Error creating bean with name 'org.sonarsource.scanner.api.internal.IsolatedClassloader@37d688b1-org.sonar.scanner.bootstrap.ScannerPluginRepository': Initialization of bean failed";;
val cst: string =  "Error creating bean with name 'org.sonarsource.scanner.api.in"+[119 chars]

> let cst = "Error creating bean with name 'org.sonarsource.scanner.api.internal.IsolatedClassloader@37d688b1-org.sonar.scanner.bootstrap.ScannerPluginRepository': Initialization of bean failed";;
val cst: string =  "Error creating bean with name 'org.sonarsource.scanner.api.in"+[119 chars]

> ";; cst = "Error creating bean with name 'org.sonarsource.scanner.api.internal.IsolatedClassloader@37d688b1-org.sonar.scanner.bootstrap.ScannerPluginRepository': Initialization of bean faile
- let cst = "Error creating bean with name 'org.sonarsource.scanner.api.internal.IsolatedClassloader@37d688b1-org.sonar.scanner.bootstrap.ScannerPluginRepository': Initialization of bean faile

error FS0193: Operation is not supported on this platform.

> ";;
- 
abonie commented 1 year ago

@MrLuje how are you sending the input to FSI in your example? Because it looks like the input got mangled the third time it was sent. Does not seem to be the same issue.

MrLuje commented 1 year ago

@abonie with ionide extension (v7.7.0) on vscode with either "FSI: send selection" or "FSI: send line"

abonie commented 1 year ago

Hmm... It does not repro for me at least on the same version of ionide

MrLuje commented 1 year ago

I didn't check before but it looks like it is only failing from WSL

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.6 LTS"

$ uname -a
Linux HVS-PF346A2W 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

I can't reproduce it either from windows

T-Gro commented 1 month ago

@abonie : Is this still relevant?