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

Crash when using --keepNewlineAfter #513

Closed
knocte opened this issue Oct 15, 2019 · 4 comments
Closed

Crash when using --keepNewlineAfter #513

knocte opened this issue Oct 15, 2019 · 4 comments

Comments

@knocte
Copy link
Contributor

knocte commented Oct 15, 2019

Description

I've tried to run fantomas 3.0 in a project inside my solution, and it crashed with:

$ fantomas . --recurse --keepNewlineAfter
./Presentation.fs has been written.
The following exception occurs while formatting stdin: Fantomas.FormatConfig+FormatException: Parsing failed with errors: [|/tmp.fsx (46,90)-(46,92) parse error Unexpected keyword 'if' in binding. Expected incomplete structured construct at or before this point or other token.;
  /tmp.fsx (61,1)-(61,4) parse error Incomplete structured construct at or before this point in binding. Expected incomplete structured construct at or before this point or other token.;
  /tmp.fsx (38,1)-(38,4) parse error Incomplete value or function definition. If this is in an expression, the body of the expression must be indented to the same column as the 'let' keyword.|]
And options: { SourceFiles = [|"/tmp.fsx"|]
  ConditionalCompilationDefines = []
  ErrorSeverityOptions = { WarnLevel = 3
                           GlobalWarnAsError = false
                           WarnOff = []
                           WarnOn = []
                           WarnAsError = []
                           WarnAsWarn = [] }
  IsInteractive = false
  LightSyntax = None
  CompilingFsLib = false
  IsExe = false }
   at Fantomas.CodeFormatterImpl.parse@61-2.Invoke(FSharpParseFileResults _arg2) in C:\Users\nojaf\Projects\fantomas\src\Fantomas\CodeFormatterImpl.fs:line 66
   at Microsoft.FSharp.Control.AsyncPrimitives.CallThenInvokeNoHijackCheck[a,b](AsyncActivation`1 ctxt, FSharpFunc`2 userCode, b result1) in E:\A\_work\130\s\src\fsharp\FSharp.Core\async.fs:line 417
   at <StartupCode$FSharp-Compiler-Service>.$Service.ParseFile@424-3.Invoke(AsyncActivation`1 ctxt)
   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in E:\A\_work\130\s\src\fsharp\FSharp.Core\async.fs:line 109
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.FSharp.Control.AsyncResult`1.Commit() in E:\A\_work\130\s\src\fsharp\FSharp.Core\async.fs:line 350
   at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronouslyInCurrentThread[a](CancellationToken cancellationToken, FSharpAsync`1 computation) in E:\A\_work\130\s\src\fsharp\FSharp.Core\async.fs:line 882
   at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronously[T](CancellationToken cancellationToken, FSharpAsync`1 computation, FSharpOption`1 timeout) in E:\A\_work\130\s\src\fsharp\FSharp.Core\async.fs:line 890
   at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T](FSharpAsync`1 computation, FSharpOption`1 timeout, FSharpOption`1 cancellationToken) in E:\A\_work\130\s\src\fsharp\FSharp.Core\async.fs:line 1154
   at Fantomas.Cmd.Program.processSourceString(Boolean isFsiFile, String s, FSharpChoice`2 tw, FormatConfig config) in C:\Users\nojaf\Projects\fantomas\src\Fantomas.CoreGlobalTool\Program.fs:line 85
   at Fantomas.Cmd.Program.stringToFile@196(FSharpRef`1 force, FSharpRef`1 profile, String s, String outFile, FormatConfig config) in C:\Users\nojaf\Projects\fantomas\src\Fantomas.CoreGlobalTool\Program.fs:line 204
Force writing original contents to ./Program.fs.
./UserInteraction.fs has been written.
./Properties/AssemblyInfo.fs has been written.

Repro code

  1. Clone https://github.com/knocte/geewallet
  2. Switch to the 'stable' branch.
  3. cd src\GWallet.Frontend.Console
  4. Run fantomas . --recurse --keepNewlineAfter

Current results: crash.
Expected results: shouldn't crash.

@VelocityRa
Copy link

The issue is not a crash, but an error when formatting.

The first time you run fantomas, it indents these lines incorrectly (appends 84 spaces to each one): https://github.com/knocte/geewallet/blob/stable/src/GWallet.Frontend.Console/Program.fs#L43-L52

And then after this if you run it again, you get the crash because the code is invalid.

After a bit of testing, the formatting issue happens when any comment (even an empty one) appears right after this line like so:

    let signedTransaction = Account.LoadSignedTransactionFromFile fileToReadFrom.FullName
    //

(has to be right after, no newlines)

@VelocityRa
Copy link

VelocityRa commented Oct 18, 2019

Reduced down to

let foo() =
    let bar = 6
    //
    baz
    baz2

which gets formatted to

let foo() =
    let bar = 6
               //
               baz
               baz2

@smoothdeveloper
Copy link
Contributor

smoothdeveloper commented Oct 20, 2019

It doesn't seem related to (edit: only) single line comments, just an empty line causes it to glitch too

let a () =
    let q = 1
    
    q
    b

turns into

let a() =
    let q = 1

             q
             b

you need a sequence of expressions for it to happen, when arriving there:

| SequentialSimple es -> atCurrentColumn (colAutoNlnSkip0 sepSemiNln es (genExpr astContext))

the ctx.Writer.Column is 9 (in above sample) and should probably be reset before handling printing of trivia and AST nodes. I'm not familiar with the codebase to know where and how to handle this transformation of the Context.

edit: There is easy fix (#526) for this case without comment but it still fails the same with the comment.

nojaf pushed a commit that referenced this issue Oct 21, 2019
…ix for issue #513) (#526)

* minimal fix for now

* fixing other case with single line comment

* Address code review (LetBindingTests.fs) and fix/complement a comment (CodePrinter.fs).
@smoothdeveloper
Copy link
Contributor

@knocte you may give a shot at version in master, it looks like it still compiles after running that version.

@nojaf nojaf closed this as completed Jan 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants