Skip to content

dx fmt corrupts source files with rsx inline if/else expressions #5096

@rapporian

Description

@rapporian

Problem

Summary

dx fmt corrupts source code when a file contains a top-level comment and an inline if/else expression within an RSX attribute.

The formatter incorrectly duplicates the file's leading comment and injects it at the end of the if/else expression, right before the element's closing brace.

The specific failure chain is:

  1. dx fmt copies the text from line 1 (e.g., // A comment...).

  2. It pastes this text immediately after the else block inside the RSX.

  3. Since the pasted text is a line comment (//), it comments out the closing brace } that follows it.

  4. This leaves the RSX element unclosed, resulting in a compiler error


Minimal Reproduction

Step 1: Create this file (sample.rs)

// A comment about this file

use dioxus::prelude::*;

#[component]
pub fn Sample() -> Element {
    let is_active = use_signal(|| false);

    rsx! {
        div { class: if is_active() { "active" } else { "inactive" },
            div { class: if is_active() { "a" } else { "b" }}
        }
    }
}

Step 2: Run the formatter

dx fmt --file sample.rs

Step 3: Observe corrupted output

// A comment about this file

use dioxus::prelude::*;

#[component]
pub fn Sample() -> Element {
    let is_active = use_signal(|| false);

    rsx! {
        div { class: if is_active() { "active" } else { "inactive" },
            div { class: if is_active() { "a" } else { "b" } // A comment about this file }
        }
    }
}

The bug: The comment from line 1 (// A comment about this file) has been appended and the closing } has been commented out.

Workaround

  • Remove all top level comments
  • Wrap the rsx with if/else in an extra layer of {} brackets

Environment

  • Dioxus CLI version: 0.7.2
  • Rust version: 1.85.1
  • OS: macOS 15.6.1 (arm64)

Metadata

Metadata

Assignees

No one assigned

    Labels

    autoformattingRelated to the autofmt cratebugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions