dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

dx fmt deletes comments

Open magikmw opened this issue 1 year ago • 8 comments

Problem

Using dx fmt on source files is deleting comments, both in rsx macros and standard rust when using --all-code.
This is especially egregious because standard rustfmt formatter is really messing up any files with rsx in them, so dx fmt is the only viable alternative.

Steps To Reproduce

Steps to reproduce the behavior:

  • Put any comments into a file - either inside rsx or elsewhere
  • Run dx fmt -f yourfile.rs
  • Comments deleted

Expected behavior

Comments should be left alone.
I'm not entirely sure how rustfmt does it, but with my limited experience with it, it seems to keep comments intact and relatively in the relevelant place after formatting.

Screenshots

n/a

Environment:

  • Dioxus version: 0.5.1
  • Rust version: rustc 1.77.2 (25ef9e3d8 2024-04-09)
  • OS info: Fedora 40 beta
  • App platform: n/a (but I'm making a fullstack app)

Questionnaire

  • [x] I'm interested in fixing this myself but don't know where to start
  • [ ] I would like to fix and I have a solution
  • [x] I don't have time to fix this right now, but maybe later

magikmw avatar Apr 19 '24 08:04 magikmw

I am unable to reproduce this using the git version of the CLI. Can you test the git version of the CLI on your end?

You can install the git version with:

cargo install --git https://github.com/DioxusLabs/dioxus dioxus-cli

DogeDark avatar Apr 20 '24 05:04 DogeDark

I did run the command to install git version and checked again:

-                // TODO: Implement event handling here -> listen to eve
nts in child
                 let accept_keys = [Key::Enter, Key::Character(" ".to_string())];
-                if accept_keys.contains(&evt.key()){
+                if accept_keys.contains(&evt.key()) {
                     log::info!("{} pressed in {}", evt.key(), "{props.labelText}");
                     log::info!("{} pressed in {}", evt.key(), "{props.labelText}");
                 }
             },
             class: "flex flex-1 h-full w-3/4 items-center justify-center btn btn-outline my-1 py-1",
-            div { class: "flex-none py-1", // TODO: Refactor into separate component
+            div { class: "flex-none py-1",
                 Icon { width: 24, height: 24, icon: props.icon, title: "{props.iconTitle}" }
-            },
+            }
             "{props.labelText}"
-            input { // TODO: Refactor into separate component
+            input {

dx -V says:

dioxus 0.5.2

magikmw avatar Apr 22 '24 07:04 magikmw

I can reproduce it now. The formatter doesn't like inline comments and removes them completely:

rsx! {
    div {   // this comment will be removed
        "hi",
    }
}

DogeDark avatar Apr 24 '24 23:04 DogeDark

this also seems to affect the VSCode Dioxus extension. Block-level comments (/* .. */) are also affected; doc comments (///) however are not.

spookyvision avatar May 02 '24 03:05 spookyvision

Adding to the list.

All comments are removed in event handlers (except doc comments):

rsx! {
    div {
        // also occurs in move || async move
        onclick: move |_| {
            // is removed
            /* is removed */
        }
    }
}

DogeDark avatar May 02 '24 03:05 DogeDark

I think it comes from here: https://github.com/DioxusLabs/dioxus/blob/472031d1f51557601797d1cb469f09e4f3e0ca71/packages/cli/src/cli/autoformat.rs#L249-L253

syn does not keep comments.

https://crates.io/crates/rustfmt-wrapper (or spawning rustfmt directly) could be used instead. This won't help in non-rust code, but I think it's a start.

I don't know if Dioxus developers would agree with this solution, but if they do, I would be happy to do it.

OlivierLemoine avatar May 03 '24 14:05 OlivierLemoine

We try our best to preserve comments by writing out whitepace on rsx indents, but there's no way to preserve whitespace with prettyplease. I'd be interested in supporting the rustfmt wrapper approach, but it is convenient for autoformatting to not rely on rustfmt so we can use it in things like wasm.

jkelleyrtp avatar May 03 '24 17:05 jkelleyrtp

What if it was behind a target cfg? Wasi support spawning new tasks, so rustfmt would work. But it's true it's not an ideal solution.

OlivierLemoine avatar May 06 '24 12:05 OlivierLemoine