rust-lang / rust-analyzer

A Rust compiler front-end for IDEs

Home Page:https://rust-analyzer.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow opening rust-analyzer on a single file

jyn514 opened this issue · comments

commented

Currently, code x.rs (where x.rs is any self-contained source file) gives the error Cannot activate rust-analyzer: no folder is opened. It would be nice to have this work if x.rs doesn't require any external dependencies. That would allow running rust-analyzer even on projects not using cargo.

Note that there is a way to run rust-analyzer on projects not using cargo, but it's probably not easier than making a Cargo.toml: https://rust-analyzer.github.io/manual.html#non-cargo-based-projects.

commented

Those docs are intended for using some build system other than cargo, like x.py or bazel or something. I'm asking for support for very small projects that have no dependencies, only a single rust file.

Another use case would be for org-babel the emacs + org-mode literate programming environment that creates files in temp and enables users to write short snippets. Adding rust-analyzer support for individual files would be great for this as well.

Can rust-analyzer not just default to the directory containing Cargo.toml as cargo would?

commented

@jomala this is for projects not using cargo, where there is no Cargo.toml file.

The error I was receiving was: [rust-analyzer unlinked-file] [E] file not included in module tree, and due to this linting was not working while trying to edit a single main.rs file in my home directory.

I resolved the issue by creating a file named rust-project.json alongside my main.rs file with the contents:

{
    "sysroot_src": "/home/<YOUR-USER>/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library",
    "crates": [
        {
            "root_module": "main.rs",
            "edition": "2018",
            "deps": []
        }
    ]
}

@zzzachzzz this works for me without a rust-project.json with the exception of editing new files.