New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code completion not working for external crates? #2793

Closed
typoon opened this Issue Aug 24, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@typoon

typoon commented Aug 24, 2018

Environment

I have tried this with both CLion 2018.2.2 and IDEA Ultimate version 2018.2.2.
The Rust plugin version is 0.2.0.2103-182

Tried with both Rust 1.28 and nightly (1.30 at this time).

Operating System is Ubuntu 17.10.

Problem description

I have added an external crate to the project (rand), but can't get any code completin from it.
I added the "rand" dependency in the Cargo.toml file and it was succesfully installed.

When I add "extern crate rand;" to the code, the IDE understands it fine.

If I type "use rand::<ctrl+space>" to see the completions, I only see some defaults but none of the expected ones. See image below:

Code completion

I expected to see for example the "Rng" module being suggested there.

Steps to reproduce

Create a new Rust project.
Add the following line to the Cargo.toml file:

rand = "0.3"

Compile the project.

Edit the main.rs file and add the following code to it:

extern crate rand;

use rand::<ctrl+space>

fn main() {
}
@Undin

This comment has been minimized.

Show comment
Hide comment
@Undin

Undin Aug 25, 2018

Member

Looks like that your project model is not updated after changes in Cargo.toml
Try to sync project explicitly via sync button in cargo toolwindow. Also check that option Watch Cargo.toml is enabled - it should update your project model automatically

Member

Undin commented Aug 25, 2018

Looks like that your project model is not updated after changes in Cargo.toml
Try to sync project explicitly via sync button in cargo toolwindow. Also check that option Watch Cargo.toml is enabled - it should update your project model automatically

@typoon

This comment has been minimized.

Show comment
Hide comment
@typoon

typoon Aug 25, 2018

The project is being updated. The watch Cargo.toml option is checked. I tried synchronizing again, and still the same issue. I tried restarting the IDE, and still the issue persists. This happens both in IDEA and CLion.

Any other ideas what it could be?

typoon commented Aug 25, 2018

The project is being updated. The watch Cargo.toml option is checked. I tried synchronizing again, and still the same issue. I tried restarting the IDE, and still the issue persists. This happens both in IDEA and CLion.

Any other ideas what it could be?

@typoon

This comment has been minimized.

Show comment
Hide comment
@typoon

typoon Aug 25, 2018

Alright, I made some progress. When I include rand 0.3, Cargo will compile and install both rand 0.3.22 and 0.4.3 (at this time). The Cargo.toml looks like this:

[dependencies]
rand = "0.3.22"

And a cargo build will generate this output:

 $ cargo build
   Compiling libc v0.2.43                                                                                                                                                                                           
   Compiling rand v0.4.3
   Compiling rand v0.3.22
   Compiling bla v0.1.0 (file:///home/henrique/tmp/rust/bla)
   Finished dev [unoptimized + debuginfo] target(s) in 2.32s

So there are 2 rand crates in the project. (Well, only 1, but 2 different versions)

If I create a new project and instead tell it to use rand 0.4:

[dependencies]
rand = "0.4"

Then cargo build will install just one rand crate, and the auto complete feature works.

So it seems there is some sort of bug where Intellij-rust doesn't know what to do if there are two versions of a crate in the project.

typoon commented Aug 25, 2018

Alright, I made some progress. When I include rand 0.3, Cargo will compile and install both rand 0.3.22 and 0.4.3 (at this time). The Cargo.toml looks like this:

[dependencies]
rand = "0.3.22"

And a cargo build will generate this output:

 $ cargo build
   Compiling libc v0.2.43                                                                                                                                                                                           
   Compiling rand v0.4.3
   Compiling rand v0.3.22
   Compiling bla v0.1.0 (file:///home/henrique/tmp/rust/bla)
   Finished dev [unoptimized + debuginfo] target(s) in 2.32s

So there are 2 rand crates in the project. (Well, only 1, but 2 different versions)

If I create a new project and instead tell it to use rand 0.4:

[dependencies]
rand = "0.4"

Then cargo build will install just one rand crate, and the auto complete feature works.

So it seems there is some sort of bug where Intellij-rust doesn't know what to do if there are two versions of a crate in the project.

@alexander-irbis

This comment has been minimized.

Show comment
Hide comment
@alexander-irbis

alexander-irbis Aug 27, 2018

Hm, rand 0.3 just reexports the content of rand 0.4.

alexander-irbis commented Aug 27, 2018

Hm, rand 0.3 just reexports the content of rand 0.4.

@vlad20012

This comment has been minimized.

Show comment
Hide comment
@vlad20012

vlad20012 Aug 27, 2018

Collaborator

Duplicate #2439, I guess

Collaborator

vlad20012 commented Aug 27, 2018

Duplicate #2439, I guess

bors bot added a commit that referenced this issue Sep 3, 2018

Merge #2820
2820: RES: fix extern crate resolution with several versions of the same crate in dependencies r=Undin a=Undin

Some crates depend on another version of the same crate. For example, `rand 0.3` and `num-traits 0.1`.
And currently, name resolution of `extern crate` item for such transitive dependencies returns two items: `lib.rs` of the current crate (it's wrong) and `lib.rs` of transitive dependency (correct resolution). It leads to broken resolution and completion for a user because such crates actively use reexports from their transitive dependencies which we fail to resolve.

These changes fix this bug. Now we don't add a lib target of the current crate to resolve results while extern crate resolution inside the lib target, i.e. we ignore `lib.rs` of `rand 0.3` while resolution `extern crate rand` inside `lib.rs` of `rand 0.3` because it refers to `lib.rs` of `rand 0.4` (transitive dependency of `rand 0.3`)

Fixes #2439
Fixes #2793
Fixes resolution/completion part of  #2817

Co-authored-by: Arseniy Pendryak <a.pendryak@yandex.ru>

@bors bors bot closed this in #2820 Sep 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment