Skip to content

Space inserted before final > in type parameter #2860

Closed

Description

@cmeeren

Issue created from fantomas-online

Code

{| payload with reason = reason |}
|> Json.serialize<{|
    reason: string
    old: bool
    ``new``: bool
|}>

Result

{| payload with reason = reason |}
|> Json.serialize<{|
    reason: string
    old: bool
    ``new``: bool
|} >

Problem description

See the final line: |} >

There should not be a space before >. (There is no space when using Cramped, and it should be consistent with the opening <{|.)

Extra information

  • The formatted result breaks my code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.

Options

Fantomas main branch at 1/1/1990

    { config with
                MultilineBracketStyle = stroustrup }

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

Activity

  1. github-actions commented on Mar 5, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    Thanks for the clear reproduction! I've investigated the root cause.

    Root Cause

    The extra space (or newline) before > originates in the | Type.AppPrefix node -> handler in src/Fantomas.Core/CodePrinter.fs (around line 3254):

    +> leadingExpressionIsMultiline (colGenericTypeParameters node.Arguments) (fun isMultiline ->
        onlyIf isMultiline (!-" "))
    +> genSingleTextNode node.GreaterThan

    colGenericTypeParameters formats type arguments in a "long" form when they don't fit on one line:

    let long = indentSepNlnUnindent (genParameters (sepComma +> sepNln)) +> sepNln

    Note the final +> sepNln — this emits a newline after the closing |}. Because a WriteLine event was emitted, leadingExpressionIsMultiline returns true, and onlyIf isMultiline (!-" ") then inserts a space before >.

    The intent of the space was to produce readable spacing like:

    Foo<
        int,
        string
     >

    …but when the last type argument is an anonymous record, the |} already provides a natural visual delimiter — the extra space/line is unnecessary and breaks the output.

    In current main the rendered output is:

    {| payload with reason = reason |}
    |> Json.serialize<
        {|
            reason: string
            old: bool
            ``new``: bool
        |}
        >      // ← stray newline + space before >

    Possible Fix

    Skip the !-" " when the last type argument ends with a closing bracket that provides its own separation (e.g. |}, }, ]). Something along the lines of:

    +> leadingExpressionIsMultiline (colGenericTypeParameters node.Arguments) (fun isMultiline ->
        match List.tryLast node.Arguments with
        | Some (Type.AnonRecord _) -> sepNone   // |} already acts as delimiter
        | _ -> onlyIf isMultiline (!-" "))

    This is related to #3069 which also concerns the Type.AppPrefix + anonymous record + Stroustrup combination.

    If you'd like to contribute a fix, the relevant code is | Type.AppPrefix node -> in src/Fantomas.Core/CodePrinter.fs and colGenericTypeParameters (~line 2352). The online tooling and the scripts/oak.fsx / scripts/writer-events.fsx diagnostic scripts are very helpful for tracing exactly which events produce the stray character.

    Generated by Repo Assist · ◷

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@10f087607af87e4e89439161f1e5d4724235f396
    
  2. added a commit that references this issue on Oct 7, 2026
    d04aece
  3. added a commit that references this issue on Oct 21, 2026
    78a0d8c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions