racer-rust / emacs-racer

Racer support for Emacs
399 stars 48 forks source link

emacs-racer doesn't complete for external crates #120

Closed JuneKelly closed 5 years ago

JuneKelly commented 5 years ago

Follow on from an issue on the main racer repo, where we concluded that my issue was with emacs-racer rather than racer itself: https://github.com/racer-rust/racer/issues/1020#issuecomment-468424788

Emacs version: 25.2.2 emacs-racer version: 20190124.538

I'm using racer via the emacs-racer plugin that's included with spacemacs (https://github.com/syl20bnr/spacemacs/tree/master/layers/%2Blang/rust)

When I'm editing, (for example, in this demo repo: https://github.com/kngwyu/simple-crate.git), racer completions work for the standard library, but not for external crates like rand.

Output from racer-debug:

The last racer command was:

$ cd /home/shanek/tmp/simple-crate/
$ export CARGO_HOME=/home/shanek/.cargo
$ export RUST_SRC_PATH=/home/shanek/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src
$ /home/shanek/.cargo/bin/racer complete 3 21 /home/shanek/tmp/simple-crate/src/main.rs /tmp/racer8927IeQ

This command terminated with exit code 0.

stdout:

PREFIX 32,43,AsByteSlice
END

No output on stderr.

The temporary file will have been deleted. You should be
able to reproduce the same output from racer with the
following command:

$ CARGO_HOME=/home/shanek/.cargo RUST_SRC_PATH=/home/shanek/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src /home/shanek/.cargo/bin/racer complete 3 21 /home/shanek/tmp/simple-crate/src/main.rs

Please report bugs on GitHub.
JuneKelly commented 5 years ago

As a bit of debugging, I tried running the command that racer-debug prints out at the bottom, and it does seem to produce the correct completions:

shanek@skub:~/tmp/simple-crate$ CARGO_HOME=/home/shanek/.cargo RUST_SRC_PATH=/home/shanek/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src /home/shanek/.cargo/bin/racer complete 3 21 /home/shanek/tmp/simple-crate/src/main.rs
PREFIX 32,43,AsByteSlice
MATCH AsByteSliceMut,448,10,/home/shanek/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.6.5/src/lib.rs,Trait,pub trait AsByteSliceMut
END
JuneKelly commented 5 years ago

Some more debugging, I've put in some prints in the racer--call function, and got the following:

>>>> ----------------
>> src path /home/shanek/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src
>> cargo home /home/shanek/.cargo
>> dir /home/shanek/tmp/simple-crate/
>> env (RUST_SRC_PATH=/home/shanek/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src CARGO_HOME=/home/shanek/.cargo TERM=dumb IM_CONFIG_PHASE=2 LESSCLOSE=/usr/bin/lesspipe %s %s LESSOPEN=| /usr/bin/lesspipe %s LS_COLORS NVM_BIN=/home/shanek/.nvm/versions/node/v8.15.0/bin NVM_CD_FLAGS NVM_DIR=/home/shanek/.nvm _=/usr/bin/env  KDE_SESSION_UID=1000 XDG_CONFIG_DIRS=/etc/xdg/xdg-plasma:/etc/xdg:/usr/share/kubuntu-default-settings/kf5-settings XDG_DATA_DIRS=/usr/share/plasma:/usr/local/share:/usr/share:/var/lib/snapd/desktop PWD=/home/shanek XDG_VTNR=1 GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1 QT_ACCESSIBILITY=1 SHELL=/bin/bash SSH_AUTH_SOCK=/tmp/ssh-XPfQlorpcG0n/agent.2100 XAUTHORITY=/home/shanek/.Xauthority XDG_SESSION_DESKTOP=KDE XDG_CURRENT_DESKTOP=KDE LANG=en_GB.UTF-8 XCURSOR_THEME=breeze_cursors XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1 XDG_RUNTIME_DIR=/run/user/1000 PATH=/home/shanek/.nvm/versions/node/v8.15.0/bin:/home/shanek/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin XDG_SESSION_ID=3 XDG_SESSION_CLASS=user DEFAULTS_PATH=/usr/share/gconf/plasma.default.path QT_AUTO_SCREEN_SCALE_FACTOR=0 LOGNAME=shanek MANDATORY_PATH=/usr/share/gconf/plasma.mandatory.path DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus KDE_SESSION_VERSION=5 XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0 DESKTOP_SESSION=plasma HOME=/home/shanek SHLVL=1 SSH_AGENT_PID=2175 XDG_SESSION_TYPE=x11 XDG_SEAT=seat0 LANGUAGE=en_GB:en USER=shanek GS_LIB=/home/shanek/.fonts KDE_FULL_SESSION=true)
>> exit-code 0
>> stdout PREFIX 50,61,AsByteSlice
END

Process *async-racer* finished

>> stderr 
Process *async-racer* stderr finished

As we can see here, the process std-out genuinely get's no matches, but again, if I run racer-debug and run the command that's suggested at the end, it does get matches, so I think there's a bug somewhere in racer--shell-command function:

shanek@skub:~/tmp/simple-crate$ CARGO_HOME=/home/shanek/.cargo RUST_SRC_PATH=/home/shanek/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src /home/shanek/.cargo/bin/racer complete 4 21 /home/shanek/tmp/simple-crate/src/main.rs

PREFIX 50,61,AsByteSlice
MATCH AsByteSliceMut,448,10,/home/shanek/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.6.5/src/lib.rs,Trait,pub trait AsByteSliceMut
END
JuneKelly commented 5 years ago

I guess it's possible that this issue is related? https://github.com/racer-rust/emacs-racer/issues/115

kngwyu commented 5 years ago

Honestly I haven't met this kind of bug and feel really confusing :thinking: ... I'll try to debug it but please wait for a few days.

JuneKelly commented 5 years ago

Thanks!

JuneKelly commented 5 years ago

For what it's worth, I did manage to replicate the issue in a fresh install of Ubuntu (in a VM):

Just let me know if there's anything I can do to help out

Wilfred commented 5 years ago

Thanks for the great bug report and sample repository! :+1:

OK, I think this might be #115, which should be fixed by 9d8984e. In 780074b a macro was added that creates new temporary buffers using with-temp-buffer, which also changes the current buffer.

However, I didn't manage to reproduce this inside Emacs. I've observed that racer is sensitive to the value of CARGO_HOME:

$ CARGO_HOME=/ racer complete 3 21 /home/wilfred/tmp/simple-crate/src/main.rs
PREFIX 32,43,AsByteSlice
END

$ CARGO_HOME=~/.cargo racer complete 3 21 /home/wilfred/tmp/simple-crate/src/main.rs
PREFIX 32,43,AsByteSlice
MATCH AsByteSliceMut,448,10,/home/wilfred/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.6.5/src/lib.rs,Trait,pub trait AsByteSliceMut
END

This is consistent with the theory that CARGO_HOME was not being honoured inside racer--shell-command.

Could you please try the latest version?

JuneKelly commented 5 years ago

I've updated to the latest version, and it seems to work! Thanks!

braised-babbage commented 5 years ago

I had a similar issue to what ShaneKilkelly described, and what I found was that the behavior of racer depends on whether or not cargo is present in the current env PATH. The gotcha was that as a spacemacs user, an old value of PATH (without ~/.cargo/bin) was cached in .spacemacs.env (see https://github.com/syl20bnr/spacemacs/issues/11510) and so the behavior I observed when running racer from bash was different from what was observed when running racer through emacs.

ryanswilson59 commented 4 years ago

I am getting a similar issue in emacs 26.2 using racer 20190610.800 Evaluating the command provided by racer-debug also finds a match

ryanswilson59 commented 4 years ago

It is not an issue with cargo home, printing $CARGO_HOME before racer is run gives ~/.cargo