rust-analyzer: RA does not work at all when offline

I tried to do a bit of coding on rustc while on a plane without internet, and noticed that RA handles this situation quite badly. cargo metadata fails because it cannot refresh the index, and then basically nothing works, even though everything needed should be available offline. In particular, not even “check on save” does anything – I had to run ./x.py check by hand. That worked perfectly fine, demonstrating that RA could also have run x.py as usual and show the diagnostics in the editor.

rust-analyzer version: ad6810e90 2022-06-06 stable

rustc version: rustc 1.63.0-nightly (e09449220 2022-05-31)

relevant settings:

{
    "rust-analyzer.checkOnSave.enable": true,
    "rust-analyzer.checkOnSave.overrideCommand": [
        "./x.py",
        "check",
        "--json-output",
        "library/std",
        "compiler/rustc",
    ],
    "rust-analyzer.rustfmt.overrideCommand": [
        "./build/x86_64-unknown-linux-gnu/stage0/bin/rustfmt",
        "--edition=2021"
    ],
    "rust-analyzer.procMacro.enable": false,
    "editor.formatOnSave": false,
    "files.watcherExclude": {
        "*rustc*/src/llvm-project/**": true,
        "*rustc*/build/**": true,
    },
    "files.exclude": {
        "src/llvm-project/**": true,
        "build/**": true
    },
    "rust-analyzer.cargo.buildScripts.enable": false,
    "rust-analyzer.rustc.source": "./Cargo.toml",
    "rust-analyzer.hover.actions.references.enable": true,
}

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 10
  • Comments: 21 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Realized there is a --no-deps flag which prevents metadata from accessing the registry entirely, so with https://github.com/rust-lang/rust-analyzer/pull/17915 the workspace itself will at least load, while no crates-io dependencies will resolve. Not ideal, but better than not working at all

FWIW, I ran into this yesterday on an airplane as well. I managed to work around this in the following way:

  • Quit the editor (in my case, VS Code)
  • Kill any “stuck” RA processes
  • Run cargo check --all-targets --offline in a terminal to update the Cargo.lock file
  • Then restart the editor

(Separately, editing dependencies required some manual dexterity, but that’s more of a Cargo issue.)

In my mind the ideal solution here would be that RA uses some connectivity canary (like a trivial DNS request to Google or Cloudflare DNS servers) to assess whether it should try applying --offline/CARGO_NET_OFFLINE.

That’s not only “I’m on an airplane” case, but also “I work in airgapped environment and would like to use Rust”. We do build docker image with all cargo dependencies on a networked host and then move it inside environment without network access.

I believe analyzer worked ok about 9 months ago, but we could have used older RA back then too.

Should there be an x.py metadata? 😄

This might be related to https://github.com/rust-lang/cargo/issues/10096, where cargo metadata insists to do some work that is IMO entirely unnecessary.

I see two orthogonal issues here:

  • cargo metadata failed when it possibly should not have
  • RA completely bails when cargo metadata fails, even though I would think it can still run ./x.py check and render the resulting diagnostics in the editor

That only changes the error message to

rust-analyzer failed to load workspace: Failed to read Cargo metadata from Cargo.toml file /home/r/src/rust/rustc/Cargo.toml, cargo 1.63.0-nightly (39ad1039d 2022-05-25): Failed to run `"cargo" "metadata" "--format-version" "1" "--manifest-path" "/home/r/src/rust/rustc/Cargo.toml" "--filter-platform" "x86_64-unknown-linux-gnu"`: `cargo metadata` exited with an error: error: failed to download `minifier v0.2.1`

Caused by:
  attempting to make an HTTP request, but --offline was specified