rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.09k stars 1.57k forks source link

Unresolved import for std imports #6063

Closed lelongg closed 2 years ago

lelongg commented 3 years ago

Since last rust-analyzer I get unresolved import errors on every import of names from std, such as:

use std::{f64::consts::FRAC_PI_2, time::Duration};

I noticed this error is also triggered for the following import:

use uom::si::f64::*;

The problem exists also in nightly.

bjorn3 commented 3 years ago

Have you installed the rust-src rustup component?

lelongg commented 3 years ago

I'm using a nix-shell with rust-src enabled.

rust = (rustChannelOf {
    channel = "stable";
    date = "2020-08-27";
  }).rust.override { extensions = [ "rust-src" ]; };

I assume it's correctly installed since, without this extension explicitly enabled, rust-analyzer fails to start with:

rust-analyzer failed to load workspace: Failed to find sysroot for Cargo.toml file /home/sisyphe/Projects/easymov/sultan/Cargo.toml. Is rust-src installed?: can't load standard library from sysroot
/nix/store/npq8yxam827s5l8sw2yrvnkxr6wi0gfx-rust-1.46.0-2020-08-24-04488afe3
(discovered via `rustc --print sysroot`)
try running `rustup component add rust-src` or set `RUST_SRC_PATH`
bjorn3 commented 3 years ago

What is the full log of rust-analyzer when rust-src is installed? Please put it between <details> and </details> to make it collapsable.

<details>

log here


</details>
lelongg commented 3 years ago

Not sure if this this is the log you meant. If not, could you tell me where I can find it ?

``` INFO [9/23/2020, 1:14:26 PM]: Extension version: 0.3.322-nightly INFO [9/23/2020, 1:14:26 PM]: Using configuration { lruCapacity: null, files: { watcher: 'client', exclude: [] }, notifications: { cargoTomlNotFound: true }, cargo: { autoreload: true, noDefaultFeatures: false, allFeatures: false, features: [], loadOutDirsFromCheck: false, target: null }, rustfmt: { extraArgs: [], overrideCommand: null }, checkOnSave: { enable: true, extraArgs: [], command: 'clippy', overrideCommand: null, allTargets: true, noDefaultFeatures: null, allFeatures: null, features: null, target: null }, cargoRunner: null, runnableEnv: null, inlayHints: { enable: true, typeHints: true, chainingHints: true, parameterHints: true, maxLength: 20 }, completion: { addCallParenthesis: true, addCallArgumentSnippets: true, postfix: { enable: true } }, callInfo: { full: true }, updates: { channel: 'nightly', askBeforeDownload: true }, serverPath: null, trace: { server: 'off', extension: false }, procMacro: { enable: true }, debug: { engine: 'auto', sourceFileMap: { '/rustc/': '${env:USERPROFILE}/.rustup/toolchains//lib/rustlib/src/rust' }, openDebugPane: false, engineSettings: {} }, lens: { enable: true, run: true, debug: true, implementations: true }, hoverActions: { enable: true, implementations: true, run: true, debug: true, gotoTypeDef: true }, linkedProjects: null, withSysroot: true, diagnostics: { enable: true, enableExperimental: true, disabled: [], warningsAsInfo: [], warningsAsHint: [] }, assist: { importMergeBehaviour: 'full' } } INFO [9/23/2020, 1:14:26 PM]: PersistentState: { lastCheck: 1600846115986, releaseId: 31681454, serverVersion: '0.3.322-nightly' } INFO [9/23/2020, 1:14:26 PM]: Using server binary at /home/sisyphe/.config/Code/User/globalStorage/matklad.rust-analyzer/rust-analyzer-linux ```
bjorn3 commented 3 years ago

That is the log I meant. I don't see anything obviously wrong.

lelongg commented 3 years ago

Please let me know if I can provide additional information

Rizary commented 3 years ago

Got the same error. Running on WSL, with rust environment provided by nix. Here is the log:

``` INFO [10/5/2020, 9:38:29 AM]: Extension version: 0.2.328 INFO [10/5/2020, 9:38:29 AM]: Using configuration { lruCapacity: null, files: { watcher: 'client', exclude: [] }, notifications: { cargoTomlNotFound: true }, cargo: { autoreload: true, noDefaultFeatures: false, allFeatures: true, features: [], loadOutDirsFromCheck: false, target: null }, rustfmt: { extraArgs: [ '--config-path=backend/rust', '--config-path=common/rust', '--config-path=frontend/rust' ], overrideCommand: null }, checkOnSave: { enable: true, extraArgs: [], command: 'check', overrideCommand: null, allTargets: true, noDefaultFeatures: null, allFeatures: null, features: null, target: null }, cargoRunner: null, runnableEnv: null, inlayHints: { enable: true, typeHints: true, chainingHints: true, parameterHints: true, maxLength: 20 }, completion: { addCallParenthesis: true, addCallArgumentSnippets: true, postfix: { enable: true } }, callInfo: { full: true }, updates: { channel: 'stable', askBeforeDownload: true }, serverPath: null, trace: { server: 'off', extension: false }, procMacro: { enable: false }, debug: { engine: 'auto', sourceFileMap: { '/rustc/': '${env:USERPROFILE}/.rustup/toolchains//lib/rustlib/src/rust' }, openDebugPane: false, engineSettings: {} }, lens: { enable: true, run: true, debug: true, implementations: true }, hoverActions: { enable: true, implementations: true, run: true, debug: true, gotoTypeDef: true }, linkedProjects: [ 'backend/rust/Cargo.toml', 'common/rust/Cargo.toml', 'frontend/rust/Cargo.toml' ], withSysroot: true, diagnostics: { enable: true, enableExperimental: true, disabled: [], warningsAsInfo: [], warningsAsHint: [] }, assist: { importMergeBehaviour: 'full' } } INFO [10/5/2020, 9:38:29 AM]: PersistentState: { lastCheck: 1593567305567, releaseId: 28093846, serverVersion: '0.2.328' } INFO [10/5/2020, 9:38:29 AM]: Using server binary at /home/rizary/.vscode-server-insiders/data/User/globalStorage/matklad.rust-analyzer/rust-analyzer-linux ```
matklad commented 3 years ago

I've added some extra logging at https://github.com/rust-analyzer/rust-analyzer/pull/6230/files

Eroc33 commented 3 years ago

I was having a similar issue to this myself (std imports were errors, and no completions for std) and decided to take a look at the file structure in '${env:USERPROFILE}/.rustup/toolchains//lib/rustlib/src'. I found that the src directory hadn't been modified since last year, and after removing that directory it was recreated with a fairly different structure and rust-analyzer was resolving std again. I couldn't say what component is responsible, but I'd guess that the cause is overzealous caching.

Old src directory structure
rust
└───src
    ├───build_helper
    ├───liballoc
    │   ├───alloc
    │   ├───benches
    │   │   └───btree
    │   ├───collections
    │   │   ├───btree
    │   │   ├───linked_list
    │   │   └───vec_deque
    │   ├───prelude
    │   ├───raw_vec
    │   ├───rc
    │   ├───sync
    │   └───tests
    │       └───btree
    ├───libcore
    │   ├───benches
    │   │   ├───char
    │   │   ├───hash
    │   │   └───num
    │   │       ├───dec2flt
    │   │       └───flt2dec
    │   │           └───strategy
    │   ├───char
    │   ├───fmt
    │   │   └───rt
    │   ├───future
    │   ├───hash
    │   ├───iter
    │   │   ├───adapters
    │   │   └───traits
    │   ├───mem
    │   ├───num
    │   │   ├───dec2flt
    │   │   └───flt2dec
    │   │       └───strategy
    │   ├───ops
    │   ├───prelude
    │   ├───ptr
    │   ├───slice
    │   ├───str
    │   ├───sync
    │   ├───task
    │   ├───tests
    │   │   ├───fmt
    │   │   ├───hash
    │   │   └───num
    │   │       ├───dec2flt
    │   │       └───flt2dec
    │   │           └───strategy
    │   └───unicode
    ├───libpanic_abort
    ├───libpanic_unwind
    │   └───dwarf
    ├───libproc_macro
    │   └───bridge
    ├───libprofiler_builtins
    └───librustc
        ├───benches
        ├───cfg
        ├───dep_graph
        ├───hir
        │   ├───lowering
        │   └───map
        ├───ich
        ├───infer
        │   ├───canonical
        │   ├───error_reporting
        │   │   └───nice_region_error
        │   ├───higher_ranked
        │   ├───lexical_region_resolve
        │   ├───nll_relate
        │   ├───opaque_types
        │   ├───outlives
        │   └───region_constraints
        ├───lint
        ├───middle
        ├───mir
        │   └───interpret
        ├───query
        ├───session
        │   └───config
        ├───traits
        │   ├───codegen
        │   ├───query
        │   │   └───type_op
        │   └───specialize
        └───ty
            ├───inhabitedness
            ├───print
            └───query
New src directory structure
rust
├───library
│   ├───alloc
│   │   ├───benches
│   │   │   └───btree
│   │   ├───src
│   │   │   ├───alloc
│   │   │   ├───collections
│   │   │   │   ├───btree
│   │   │   │   │   ├───borrow
│   │   │   │   │   ├───map
│   │   │   │   │   ├───node
│   │   │   │   │   └───set
│   │   │   │   ├───linked_list
│   │   │   │   └───vec_deque
│   │   │   ├───prelude
│   │   │   ├───raw_vec
│   │   │   ├───rc
│   │   │   └───sync
│   │   └───tests
│   ├───backtrace
│   │   ├───.github
│   │   │   └───workflows
│   │   ├───benches
│   │   ├───ci
│   │   │   └───docker
│   │   │       ├───aarch64-linux-android
│   │   │       ├───aarch64-unknown-linux-gnu
│   │   │       ├───arm-linux-androideabi
│   │   │       ├───arm-unknown-linux-gnueabihf
│   │   │       ├───armv7-linux-androideabi
│   │   │       ├───armv7-unknown-linux-gnueabihf
│   │   │       ├───i586-unknown-linux-gnu
│   │   │       ├───i686-linux-android
│   │   │       ├───i686-unknown-linux-gnu
│   │   │       ├───powerpc64-unknown-linux-gnu
│   │   │       ├───x86_64-linux-android
│   │   │       ├───x86_64-pc-windows-gnu
│   │   │       ├───x86_64-unknown-linux-gnu
│   │   │       └───x86_64-unknown-linux-musl
│   │   ├───examples
│   │   ├───src
│   │   │   ├───backtrace
│   │   │   ├───print
│   │   │   └───symbolize
│   │   │       └───gimli
│   │   └───tests
│   │       └───accuracy
│   ├───core
│   │   ├───benches
│   │   │   ├───ascii
│   │   │   ├───char
│   │   │   ├───hash
│   │   │   └───num
│   │   │       ├───dec2flt
│   │   │       └───flt2dec
│   │   │           └───strategy
│   │   ├───src
│   │   │   ├───alloc
│   │   │   ├───array
│   │   │   ├───char
│   │   │   ├───convert
│   │   │   ├───fmt
│   │   │   │   └───rt
│   │   │   ├───future
│   │   │   ├───hash
│   │   │   ├───iter
│   │   │   │   ├───adapters
│   │   │   │   └───traits
│   │   │   ├───macros
│   │   │   ├───mem
│   │   │   ├───num
│   │   │   │   ├───dec2flt
│   │   │   │   ├───flt2dec
│   │   │   │   │   └───strategy
│   │   │   │   └───shells
│   │   │   ├───ops
│   │   │   ├───prelude
│   │   │   ├───ptr
│   │   │   ├───slice
│   │   │   │   └───iter
│   │   │   ├───str
│   │   │   ├───sync
│   │   │   ├───task
│   │   │   └───unicode
│   │   └───tests
│   │       ├───fmt
│   │       ├───hash
│   │       └───num
│   │           ├───dec2flt
│   │           └───flt2dec
│   │               └───strategy
│   ├───panic_abort
│   │   └───src
│   ├───panic_unwind
│   │   └───src
│   │       └───dwarf
│   ├───proc_macro
│   │   ├───src
│   │   │   └───bridge
│   │   └───tests
│   ├───profiler_builtins
│   │   └───src
│   ├───rtstartup
│   ├───rustc-std-workspace-alloc
│   ├───rustc-std-workspace-core
│   ├───rustc-std-workspace-std
│   ├───std
│   │   ├───benches
│   │   │   └───hash
│   │   ├───src
│   │   │   ├───backtrace
│   │   │   ├───collections
│   │   │   │   └───hash
│   │   │   │       ├───map
│   │   │   │       └───set
│   │   │   ├───env
│   │   │   ├───error
│   │   │   ├───f32
│   │   │   ├───f64
│   │   │   ├───ffi
│   │   │   │   ├───c_str
│   │   │   │   └───os_str
│   │   │   ├───fs
│   │   │   ├───io
│   │   │   │   ├───buffered
│   │   │   │   ├───cursor
│   │   │   │   ├───error
│   │   │   │   ├───impls
│   │   │   │   ├───stdio
│   │   │   │   └───util
│   │   │   ├───lazy
│   │   │   ├───memchr
│   │   │   ├───net
│   │   │   │   ├───addr
│   │   │   │   ├───ip
│   │   │   │   ├───parser
│   │   │   │   ├───tcp
│   │   │   │   └───udp
│   │   │   ├───num
│   │   │   ├───os
│   │   │   │   ├───android
│   │   │   │   ├───dragonfly
│   │   │   │   ├───emscripten
│   │   │   │   ├───fortanix_sgx
│   │   │   │   ├───freebsd
│   │   │   │   ├───fuchsia
│   │   │   │   ├───haiku
│   │   │   │   ├───illumos
│   │   │   │   ├───ios
│   │   │   │   ├───linux
│   │   │   │   ├───macos
│   │   │   │   ├───netbsd
│   │   │   │   ├───openbsd
│   │   │   │   ├───raw
│   │   │   │   ├───redox
│   │   │   │   ├───solaris
│   │   │   │   └───vxworks
│   │   │   ├───panic
│   │   │   ├───path
│   │   │   ├───prelude
│   │   │   ├───process
│   │   │   ├───sync
│   │   │   │   ├───barrier
│   │   │   │   ├───condvar
│   │   │   │   ├───mpsc
│   │   │   │   │   ├───mpsc_queue
│   │   │   │   │   └───spsc_queue
│   │   │   │   ├───mutex
│   │   │   │   ├───once
│   │   │   │   └───rwlock
│   │   │   ├───sys
│   │   │   │   ├───cloudabi
│   │   │   │   │   ├───abi
│   │   │   │   │   └───shims
│   │   │   │   ├───hermit
│   │   │   │   │   └───ext
│   │   │   │   ├───sgx
│   │   │   │   │   ├───abi
│   │   │   │   │   │   ├───tls
│   │   │   │   │   │   │   └───sync_bitset
│   │   │   │   │   │   └───usercalls
│   │   │   │   │   ├───ext
│   │   │   │   │   ├───rwlock
│   │   │   │   │   └───waitqueue
│   │   │   │   │       ├───spin_mutex
│   │   │   │   │       └───unsafe_list
│   │   │   │   ├───unix
│   │   │   │   │   ├───ext
│   │   │   │   │   │   ├───net
│   │   │   │   │   │   └───ucred
│   │   │   │   │   ├───fd
│   │   │   │   │   ├───os
│   │   │   │   │   └───process
│   │   │   │   │       └───process_common
│   │   │   │   ├───unsupported
│   │   │   │   ├───vxworks
│   │   │   │   │   └───process
│   │   │   │   ├───wasi
│   │   │   │   │   └───ext
│   │   │   │   ├───wasm
│   │   │   │   └───windows
│   │   │   │       ├───args
│   │   │   │       ├───ext
│   │   │   │       ├───os
│   │   │   │       ├───path
│   │   │   │       └───process
│   │   │   ├───sys_common
│   │   │   │   ├───bytestring
│   │   │   │   ├───condvar
│   │   │   │   ├───net
│   │   │   │   ├───remutex
│   │   │   │   ├───thread_local_key
│   │   │   │   ├───thread_parker
│   │   │   │   └───wtf8
│   │   │   ├───thread
│   │   │   │   └───local
│   │   │   └───time
│   │   └───tests
│   ├───stdarch
│   │   ├───.github
│   │   │   └───workflows
│   │   ├───ci
│   │   │   └───docker
│   │   │       ├───aarch64-linux-android
│   │   │       ├───aarch64-unknown-linux-gnu
│   │   │       ├───arm-linux-androideabi
│   │   │       ├───arm-unknown-linux-gnueabihf
│   │   │       ├───armv7-unknown-linux-gnueabihf
│   │   │       ├───i586-unknown-linux-gnu
│   │   │       ├───i686-unknown-linux-gnu
│   │   │       ├───mips-unknown-linux-gnu
│   │   │       ├───mips64-unknown-linux-gnuabi64
│   │   │       ├───mips64el-unknown-linux-gnuabi64
│   │   │       ├───mipsel-unknown-linux-musl
│   │   │       ├───nvptx64-nvidia-cuda
│   │   │       ├───powerpc-unknown-linux-gnu
│   │   │       ├───powerpc64-unknown-linux-gnu
│   │   │       ├───powerpc64le-unknown-linux-gnu
│   │   │       ├───s390x-unknown-linux-gnu
│   │   │       ├───wasm32-wasi
│   │   │       ├───x86_64-linux-android
│   │   │       ├───x86_64-unknown-linux-gnu
│   │   │       └───x86_64-unknown-linux-gnu-emulated
│   │   ├───crates
│   │   │   ├───assert-instr-macro
│   │   │   │   └───src
│   │   │   ├───core_arch
│   │   │   │   ├───src
│   │   │   │   │   ├───aarch64
│   │   │   │   │   │   └───neon
│   │   │   │   │   ├───acle
│   │   │   │   │   │   ├───barrier
│   │   │   │   │   │   └───registers
│   │   │   │   │   ├───arm
│   │   │   │   │   │   └───neon
│   │   │   │   │   ├───mips
│   │   │   │   │   │   └───msa
│   │   │   │   │   ├───nvptx
│   │   │   │   │   ├───powerpc
│   │   │   │   │   ├───powerpc64
│   │   │   │   │   ├───wasm32
│   │   │   │   │   ├───x86
│   │   │   │   │   └───x86_64
│   │   │   │   └───tests
│   │   │   ├───simd-test-macro
│   │   │   │   └───src
│   │   │   ├───stdarch-gen
│   │   │   │   └───src
│   │   │   ├───stdarch-test
│   │   │   │   └───src
│   │   │   ├───stdarch-verify
│   │   │   │   ├───src
│   │   │   │   └───tests
│   │   │   └───std_detect
│   │   │       ├───src
│   │   │       │   └───detect
│   │   │       │       ├───arch
│   │   │       │       ├───os
│   │   │       │       │   ├───freebsd
│   │   │       │       │   ├───linux
│   │   │       │       │   └───windows
│   │   │       │       └───test_data
│   │   │       └───tests
│   │   └───examples
│   ├───term
│   │   └───src
│   │       └───terminfo
│   │           ├───parm
│   │           ├───parser
│   │           │   └───compiled
│   │           └───searcher
│   ├───test
│   │   └───src
│   │       ├───formatters
│   │       ├───helpers
│   │       └───stats
│   └───unwind
│       └───src
└───src
    └───llvm-project
        └───libunwind
            ├───cmake
            │   └───Modules
            ├───docs
            ├───include
            │   └───mach-o
            ├───src
            └───test
                └───libunwind
                    └───test
lelongg commented 3 years ago

This seems to solve the issue for me but it isn't resolved for a coworker having the same issue. Moreover I still get unresolved import but for macro generated types only now.

castarco commented 3 years ago

It doesn't solve the issue for me either. I tried recreating the caches, and removing and adding the rust-src component again, but the problem persists.

In my case, the problem arises with std::os::unix (it's ok until std::os), and I'm working on Linux so these modules should be available (the compilation process works flawlessly).

lnicola commented 3 years ago

@castarco see https://github.com/rust-analyzer/rust-analyzer/issues/6038

Veykril commented 2 years ago

I believe this was either caused by an old rust-src structure or in case of std::os::unix cfg_if support.