Stream: t-compiler

Topic: x.py fmt and emacs don't agree


nikomatsakis (Mar 19 2020 at 17:55, on Zulip):

So, I have my rust-mode in emacs setup to rustfmt-on-save. I have no real idea what this is invoking, but it doesn't seem to agree with what x.py fmt does. I often get errors like this:

Diff in /home/nmatsakis/versioned/rust-3/src/librustc_span/lib.rs at line 403:
     pub fn source_callee(self) -> Option<ExpnData> {
         fn source_callee(expn_data: ExpnData) -> ExpnData {
             let next_expn_data = expn_data.call_site.ctxt().outer_expn_data();
-            if !next_expn_data.is_root() {
-                source_callee(next_expn_data)
-            } else {
-                expn_data
-            }
+            if !next_expn_data.is_root() { source_callee(next_expn_data) } else { expn_data }
         }
         let expn_data = self.ctxt().outer_expn_data();
-        if !expn_data.is_root() {
-            Some(source_callee(expn_data))
-        } else {
-            None
-        }
+        if !expn_data.is_root() { Some(source_callee(expn_data)) } else { None }
     }

     /// Checks if a span is "internal" to a macro in which `#[unstable]`
Diff in /home/nmatsakis/versioned/rust-3/src/librustc_span/lib.rs at line 1234:

         let line_index = lookup_line(&self.lines[..], pos);
         assert!(line_index < self.lines.len() as isize);
-        if line_index >= 0 {
-            Some(line_index as usize)
-        } else {
-            None
-        }
+        if line_index >= 0 { Some(line_index as usize) } else { None }
     }

Could this maybe be a result of some custom settings in our repo?

simulacrum (Mar 19 2020 at 18:06, on Zulip):

@nikomatsakis possible, but somewhat unlikely -- it's more likely that emacs and x.py fmt are using different rustfmts

simulacrum (Mar 19 2020 at 18:07, on Zulip):

do you know what emacs is using? I guess you said no

nikomatsakis (Mar 19 2020 at 18:07, on Zulip):

how often does rustfmt change?

nikomatsakis (Mar 19 2020 at 18:07, on Zulip):

I mean I can figure it out but it's surprising to me

simulacrum (Mar 19 2020 at 18:07, on Zulip):

once every 6 weeks, roughly

simulacrum (Mar 19 2020 at 18:07, on Zulip):

(in tree)

simulacrum (Mar 19 2020 at 18:07, on Zulip):

right now we're using rustfmt from nightly-2020-01-31

nikomatsakis (Mar 19 2020 at 18:07, on Zulip):

that's how often we cut a new release

nikomatsakis (Mar 19 2020 at 18:08, on Zulip):

but I guess what I meant is how often it changes behavior

simulacrum (Mar 19 2020 at 18:08, on Zulip):

right, yes, we try to bump the rustfmt we use from x.py fmt roughly at the same rate, though there's no real reason to do so

nikomatsakis (Mar 19 2020 at 18:08, on Zulip):

I know this difference has persisted for a while, but it's entirely possible that I've not updated the rustfmt I'm using in all that time :)

nikomatsakis (Mar 19 2020 at 18:08, on Zulip):

I'll dig a bit deeper

simulacrum (Mar 19 2020 at 18:08, on Zulip):

in theory it shouldn't change behavior I think -- not sure -- maybe just bugfixes?

simulacrum (Mar 19 2020 at 18:08, on Zulip):

to be honest I don't really know what rustfmt maintenance looks like these days

simulacrum (Mar 19 2020 at 18:09, on Zulip):

(in terms of rate of change etc)

nikomatsakis (Mar 19 2020 at 18:10, on Zulip):

OK, I think emacs is running ~/.cargo/bin/rustfmt

nikomatsakis (Mar 19 2020 at 18:10, on Zulip):

I can configure this

nikomatsakis (Mar 19 2020 at 18:10, on Zulip):

I probably want to write my own executable that will snoop and see if I'm in a rust directory...

simulacrum (Mar 19 2020 at 18:11, on Zulip):

I do let g:rustfmt_command = "/Users/mark/Edit/rust/build/x86_64-apple-darwin/stage0/bin/rustfmt"

simulacrum (Mar 19 2020 at 18:11, on Zulip):

(for vim)

nikomatsakis (Mar 19 2020 at 18:11, on Zulip):

that said, we do have a rustfmt.toml with

# Run rustfmt with this config (it should be picked up automatically).
version = "Two"
use_small_heuristics = "Max"
merge_derives = false
simulacrum (Mar 19 2020 at 18:11, on Zulip):

rustfmt should auto-read that, independent of which rustfmt runs, afaik

simulacrum (Mar 19 2020 at 18:11, on Zulip):

(and from where etc)

nikomatsakis (Mar 19 2020 at 18:11, on Zulip):

simulacrum said:

I do let g:rustfmt_command = "/Users/mark/Edit/rust/build/x86_64-apple-darwin/stage0/bin/rustfmt"

I have many directories

nikomatsakis (Mar 19 2020 at 18:11, on Zulip):

so I wouldn't want to hard-code a path

nikomatsakis (Mar 19 2020 at 18:12, on Zulip):

but I think I remember also that relative paths didn't work for some reason or something...

simulacrum (Mar 19 2020 at 18:12, on Zulip):

hm, this should be only for rust-lang/rust directories?

nikomatsakis (Mar 19 2020 at 18:12, on Zulip):

that said, I think I could use the "per directory" settings to configure this differently for each of my directories

simulacrum (Mar 19 2020 at 18:12, on Zulip):

i.e. you can just have one where you've run x.py recently in theory

nikomatsakis (Mar 19 2020 at 18:12, on Zulip):

I feel like I tried this and had a problem, I can't remember why

simulacrum (Mar 19 2020 at 18:12, on Zulip):

or install the same nightly toolchain as the one we use in x.py (from src/stage0.txt) and then use that

nikomatsakis (Mar 19 2020 at 18:12, on Zulip):

I'd rather have it do the right thing

nikomatsakis (Mar 19 2020 at 18:13, on Zulip):

than require some manual work from me every 6 weeks

nikomatsakis (Mar 19 2020 at 18:13, on Zulip):

but yes

simulacrum (Mar 19 2020 at 18:13, on Zulip):

do you have +stage1 set up? you could do the same for this in theory

nikomatsakis (Mar 19 2020 at 18:13, on Zulip):

I have e.g. +rust-1-stage1 etc

simulacrum (Mar 19 2020 at 18:13, on Zulip):

ah

nikomatsakis (Mar 19 2020 at 18:13, on Zulip):

one for each of my working directories :)

nikomatsakis (Mar 19 2020 at 18:14, on Zulip):

I used to use a custom wrapper around rustc that figured out which directory you were in...

nikomatsakis (Mar 19 2020 at 18:14, on Zulip):

...but that was a bit overkill, turns out

simulacrum (Mar 19 2020 at 18:16, on Zulip):

this is a bit crazy, but rustup run --install $(rg --replace '$1' 'rustfmt: (.*)$' src/stage0.txt) rustfmt

nikomatsakis (Mar 19 2020 at 18:17, on Zulip):

anyway setting the variable in what seemed to be the obvious per-directory way didn't work so...

mark-i-m (Mar 19 2020 at 18:20, on Zulip):

I actually have the same problem and I use vim

mark-i-m (Mar 19 2020 at 18:20, on Zulip):

I thought my rust.vim pluggin ought to just use the rustfmt.toml, but apparently it doesn't?

simulacrum (Mar 19 2020 at 18:27, on Zulip):

@mark-i-m you need the right version of rustfmt too, just the right config isn't enough I think

mark-i-m (Mar 20 2020 at 02:33, on Zulip):
$ rustfmt --version
rustfmt 1.4.11-stable (9eb4b56 2020-01-29)
mark-i-m (Mar 20 2020 at 02:33, on Zulip):

?

mark-i-m (Mar 20 2020 at 02:34, on Zulip):

Hmm... it doesn't seem that far from the nightly version timewise?

eddyb (Mar 23 2020 at 18:30, on Zulip):

so I misremembered this Zulip thread, but, now that I have a solution for my own problem, here it is in case anyone else needs it: VSCode format on save w/o RLS https://github.com/rust-lang/rustfmt/issues/4092#issuecomment-602765344

eddyb (Mar 23 2020 at 18:30, on Zulip):

and presumably this would work for any custom formatter command. and it works great with the undo log and instantly reloads, just like the real deal

Last update: Jun 04 2020 at 18:35UTC