annotate-snippets-rs icon indicating copy to clipboard operation
annotate-snippets-rs copied to clipboard

__ gets lost from labels

Open ndmitchell opened this issue 3 years ago • 5 comments

The starlark-rust library uses this code, specifically at https://github.com/facebookexperimental/starlark-rust/blob/13bec61a44dd2ec268403e1d6da4401ea76a3f12/starlark/src/errors/mod.rs#L205-L219. The relevant fragment is:

    let snippet = Snippet {
        title: Some(Annotation {
            label: Some(annotation_label),
            id: None,
            annotation_type: AnnotationType::Error,
        }),
        footer: Vec::new(),
        slices: slice.map(|s| vec![s]).unwrap_or_default(),
        opt: FormatOptions {
            color,
            ..Default::default()
        },
    };

    DisplayList::from(snippet)

However, if we have a label containing __, then that text gets lost from the label. The reason is that format_label in https://github.com/rust-lang/annotate-snippets-rs/blob/542e41e9a767c1c294564d549c46b2c3974b6481/src/display_list/from_snippet.rs#L56 splits by __ to apply italics to alternate parts of the label. That behaviour was super surprising. I can't find it documented. There seems to be no way to turn it off, and no way to apply escaping to stop it happening. Can you suggest how I might get it so that errors don't get changed as they are rendered?

ndmitchell avatar Nov 24 '21 18:11 ndmitchell

ha, interesting! Seems like this is my error in https://github.com/rust-lang/annotate-snippets-rs/commit/fe63d9ee72f0e1db154119c6efde15839b674a03

I'm curious no other user encountered it so far!

Can you provide a minimized test to reproduce?

zbraniecki avatar Nov 24 '21 19:11 zbraniecki

let s = Snippet {
    title: Some(Annotation {
        label: Some("hello__world"),
        id: None,
        annotation_type: AnnotationType::Error,
    }),
    footer: Vec::new(),
    slices: Vec::new(),
    opt: FormatOptions::default(),
};
eprintln!("{:?}", DisplayList::from(s).to_string());

Prints out "error: helloworld" would I would expect it to print "error: hello__world".

ndmitchell avatar Nov 24 '21 19:11 ndmitchell

I see! thank you. I don't have time right now to architect a solution to the problem, so marking as helpwanted.

zbraniecki avatar Nov 30 '21 18:11 zbraniecki

I don't see the reason for parsing __ in the labels at all, so if that's all that's required, I'm happy to send up a patch. More generally, if you can outline the kind of solution you expect, I can either fix it, or find someone who will.

ndmitchell avatar Dec 01 '21 14:12 ndmitchell

I've added a PR to remove the feature in #54, since the feature was undocumented, and I couldn't figure out what its purpose was. I'd be keen to get something merged, as we use this library in the Rust Starlark library, and it means if a user makes a mistake like typing __foo__ then the error message comes out as merely foo, which is super confusing.

ndmitchell avatar Mar 05 '22 11:03 ndmitchell