Repository: DioxusLabs/dioxus (2026-04-08T11:38:38Z)

Issue:
dx fmt breaks indentation of commented-out RSX blocks (regression since v0.7.3)
Since v0.7.4 (commit b6793dc2b, PR #5257), `dx fmt` mangles the indentation of commented-out RSX blocks. Two variants:

**Variant 1: First line keeps indentation, rest gets dedented**

Idempotency test with this input fails:

```diff
 rsx! {
     div {
         h2 { "Title" }
         // div { class: "wrapper",
-        //     p { "A" }
-        //     p { "B" }
-        // }
+    //     p { "A" }
+    //     p { "B" }
+    // }
     }
 }
```

**Variant 2: Entire block gets dedented (nested rsx!)**

```diff
 rsx! {
     Component {
         field: rsx! {
             div {
                 h2 { "Title" }
-                // div { class: "wrapper",
-                //     // nested comment
-                //     p { "A" }
-                // }
+            // div { class: "wrapper",
+            //     // nested comment
+            //     p { "A" }
+            // }
             }
         },
     }
 }
```

Root cause is the rewritten comment-handling in `packages/autofmt/src/writer.rs` (PR #5257). Commented-out RSX blocks aren't covered by existing tests.

Happy to submit a PR with the test cases (and a fix) if that's helpful.


Discussion:

Patch:
diff --git a/packages/autofmt/src/writer.rs b/packages/autofmt/src/writer.rs
index d9dfbd3ec8..462a4ba83d 100644
--- a/packages/autofmt/src/writer.rs
+++ b/packages/autofmt/src/writer.rs
@@ -426,7 +426,13 @@ impl<'a> Writer<'a> {
         {
             let comments = self.accumulate_full_line_comments(brace.span.span().end());
             if !comments.is_empty() {
+                // Undo the tab from tabbed_line(). It positioned for the closing
+                // brace, but trailing comments need child-level indentation
+                let tab_width = self.out.indent.indent_str().len() * self.out.indent_level;
+                self.out.buf.truncate(self.out.buf.len() - tab_width);
+                self.out.indent_level += 1;
                 self.apply_line_comments(comments)?;
+                self.out.indent_level -= 1;
                 self.out.tab()?;
             }
         }


Test patch:
diff --git a/packages/autofmt/tests/samples.rs b/packages/autofmt/tests/samples.rs
index 7a8257f735..d38cfef931 100644
--- a/packages/autofmt/tests/samples.rs
+++ b/packages/autofmt/tests/samples.rs
@@ -69,6 +69,11 @@ twoway![
     blank_lines,
     blank_lines_preserved,
     forloop_tuple,
+    commented_rsx_block,
+    commented_rsx_block_nested,
+    commented_rsx_block_only,
+    commented_rsx_block_between,
+    commented_rsx_block_deep,
 ];
 
 fn assert_idempotent(src: &str) {
diff --git a/packages/autofmt/tests/samples/commented_rsx_block.rsx b/packages/autofmt/tests/samples/commented_rsx_block.rsx
new file mode 100644
index 0000000000..5de4d232dd
--- /dev/null
+++ b/packages/autofmt/tests/samples/commented_rsx_block.rsx
@@ -0,0 +1,9 @@
+rsx! {
+    div {
+        h2 { "Title" }
+        // div { class: "wrapper",
+        //     p { "A" }
+        //     p { "B" }
+        // }
+    }
+}
diff --git a/packages/autofmt/tests/samples/commented_rsx_block_between.rsx b/packages/autofmt/tests/samples/commented_rsx_block_between.rsx
new file mode 100644
index 0000000000..a547417f55
--- /dev/null
+++ b/packages/autofmt/tests/samples/commented_rsx_block_between.rsx
@@ -0,0 +1,9 @@
+rsx! {
+    div {
+        h1 { "Before" }
+        // div { class: "commented",
+        //     p { "A" }
+        // }
+        h2 { "After" }
+    }
+}
diff --git a/packages/autofmt/tests/samples/commented_rsx_block_deep.rsx b/packages/autofmt/tests/samples/commented_rsx_block_deep.rsx
new file mode 100644
index 0000000000..3affb9327a
--- /dev/null
+++ b/packages/autofmt/tests/samples/commented_rsx_block_deep.rsx
@@ -0,0 +1,12 @@
+rsx! {
+    div {
+        div {
+            div {
+                h2 { "Title" }
+                // div { class: "deep",
+                //     p { "A" }
+                // }
+            }
+        }
+    }
+}
diff --git a/packages/autofmt/tests/samples/commented_rsx_block_nested.rsx b/packages/autofmt/tests/samples/commented_rsx_block_nested.rsx
new file mode 100644
index 0000000000..1da97f33ea
--- /dev/null
+++ b/packages/autofmt/tests/samples/commented_rsx_block_nested.rsx
@@ -0,0 +1,13 @@
+rsx! {
+    Component {
+        field: rsx! {
+            div {
+                h2 { "Title" }
+                // div { class: "wrapper",
+                //     // nested comment
+                //     p { "A" }
+                // }
+            }
+        },
+    }
+}
diff --git a/packages/autofmt/tests/samples/commented_rsx_block_only.rsx b/packages/autofmt/tests/samples/commented_rsx_block_only.rsx
new file mode 100644
index 0000000000..bbbc099f51
--- /dev/null
+++ b/packages/autofmt/tests/samples/commented_rsx_block_only.rsx
@@ -0,0 +1,6 @@
+rsx! {
+    div {
+        // p { "A" }
+        // p { "B" }
+    }
+}