You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Comment without code after it in the current scope should retain their "parent" indentation #2286
Hello, this is a known problem.
I would most welcome a fix for this, alas it is not an easy problem to tackle.
Are you invested in submitting a PR for this?
I investigated this with the current Fantomas main branch and the bug is still present. Here's the root cause.
Root Cause
The problem is in how Fantomas assigns comment trivia to Oak nodes.
Consider the input:
moduleTestletgenerateBinding()=iftruethen// Update the Femto metadata()// Otherwise, do nothing
```
When Fantomas builds the Oak tree, the `BindingNode` for `generateBinding` has range `(4,0)–(7,10)` — it ends where the `()` expression ends (line 7, column 10). The `// Otherwise, do nothing` comment is at line 9, column 4, which is **outside** this range.
Running `scripts/oak.fsx` reveals:
```
Oak((2,0--9,28)
ModuleOrNamespaceNode((2,0--7,10)
BindingNode((4,0--7,10)
ExprIfThenNode((5,4--7,10)...))
Newline(range:(8,0--8,0))
CommentOnSingleLine(range:(9,4--9,28),"// Otherwise, do nothing")))
The comment ends up as ContentAfter of the ModuleOrNamespaceNode (the smallest Oak node whose range contains the comment's range is the Oak root, which has range (2,0)–(9,28); simpleTriviaToTriviaInstruction then places it after the module's last child). When printed, the ModuleOrNamespaceNode is at indent level 0, so the comment is emitted at column 0 instead of column 4.
When a following declaration exists (e.g., let anotherFunc () = ()), the comment is placed as ContentBefore of that next binding — still at module-level column 0.
Why it's Hard
The F# parser does not extend binding ranges to include trailing comments. The comment is at column 4, which visually "belongs" to the generateBinding function body, but the AST has no way to express this. Fantomas' trivia attribution in Trivia.fs uses AST range containment, not column indentation heuristics.
Potential Fix Direction
In Trivia.fs → addToTree, after finding smallestNodeThatContainsTrivia, one could check whether there is a descendant node whose range ends before the comment but whose column indentation matches the comment's column. If such a node exists, the comment could be attributed as ContentAfter of that node instead of the parent.
This is non-trivial because:
Column matching alone is not a reliable heuristic
findNodeWhereRangeFitsIn is pure range-containment, with no column check
It would need careful handling to avoid regressions in cases where trailing-comment-at-column-0 is correct (e.g., a comment really is at module level)
This issue is related to #932 and the general challenge of indentation-based scope attribution for trivia.
Activity
nojaf commentedon Jun 3, 2022
Hello, this is a known problem.
I would most welcome a fix for this, alas it is not an easy problem to tackle.
Are you invested in submitting a PR for this?
MangelMaxime commentedon Jun 9, 2022
Hello @nojaf,
I can't really invest the time needed for submitting a PR, I don't have the time for that right now.
github-actions commentedon Mar 30, 2026
🤖 This is an automated response from Repo Assist.
I investigated this with the current Fantomas
mainbranch and the bug is still present. Here's the root cause.Root Cause
The problem is in how Fantomas assigns comment trivia to Oak nodes.
Consider the input:
The comment ends up as
ContentAfterof theModuleOrNamespaceNode(the smallest Oak node whose range contains the comment's range is theOakroot, which has range(2,0)–(9,28);simpleTriviaToTriviaInstructionthen places it after the module's last child). When printed, theModuleOrNamespaceNodeis at indent level 0, so the comment is emitted at column 0 instead of column 4.When a following declaration exists (e.g.,
let anotherFunc () = ()), the comment is placed asContentBeforeof that next binding — still at module-level column 0.Why it's Hard
The F# parser does not extend binding ranges to include trailing comments. The comment is at column 4, which visually "belongs" to the
generateBindingfunction body, but the AST has no way to express this. Fantomas' trivia attribution inTrivia.fsuses AST range containment, not column indentation heuristics.Potential Fix Direction
In
Trivia.fs→addToTree, after findingsmallestNodeThatContainsTrivia, one could check whether there is a descendant node whose range ends before the comment but whose column indentation matches the comment's column. If such a node exists, the comment could be attributed asContentAfterof that node instead of the parent.This is non-trivial because:
findNodeWhereRangeFitsInis pure range-containment, with no column checkThis issue is related to #932 and the general challenge of indentation-based scope attribution for trivia.
Preserve blank lines before indented comments, fixes fsprojects#2286
Bump the dotnet-sidecars group with 6 updates (#257)
Bump the packages group with 5 updates (#106)
Bump the packages group with 6 updates (#84)