-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Fix rust analyzer #911
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
Closed
Closed
Fix rust analyzer #911
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
726844b
Fix rust analyzer
jackos f0bcb4a
Don't change author format
jackos a831aa7
Move logic into main binary
jackos 979f778
Fix skipping both errors for rust-analyzer not existing
jackos 27c1be0
Remove check for rust analyzer, always generate rust-project.json
jackos aba47a5
Remove default run
jackos 2a586d7
Remove rust-project.json and add to .gitignore
jackos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ exercises/clippy/Cargo.lock | |
.idea | ||
.vscode | ||
*.iml | ||
rust-project.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/// because `rustlings` is a special type of project where we don't have a | ||
/// cargo.toml linking to each exercise, we need a way to tell `rust-analyzer` | ||
/// how to parse the exercises. This functionality is built into rust-analyzer | ||
/// by putting a `rust-project.json` at the root of the repository. This module generates | ||
/// that file by finding the default toolchain used and looping through each exercise | ||
/// to build the configuration in a way that allows rust-analyzer to work with the exercises. | ||
use glob::glob; | ||
use serde::{Deserialize, Serialize}; | ||
use std::error::Error; | ||
use std::process::Command; | ||
|
||
/// Contains the structure of resulting rust-project.json file | ||
/// and functions to build the data required to create the file | ||
#[derive(Serialize, Deserialize)] | ||
pub struct RustAnalyzerProject { | ||
sysroot_src: String, | ||
crates: Vec<Crate>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct Crate { | ||
root_module: String, | ||
edition: String, | ||
deps: Vec<String>, | ||
} | ||
|
||
impl RustAnalyzerProject { | ||
pub fn new() -> RustAnalyzerProject { | ||
RustAnalyzerProject { | ||
sysroot_src: String::new(), | ||
crates: Vec::new(), | ||
} | ||
} | ||
|
||
/// Write rust-project.json to disk | ||
pub fn write_to_disk(&self) -> Result<(), std::io::Error> { | ||
std::fs::write( | ||
"./rust-project.json", | ||
serde_json::to_vec(&self).expect("Failed to serialize to JSON"), | ||
)?; | ||
Ok(()) | ||
} | ||
|
||
/// If path contains .rs extension, add a crate to `rust-project.json` | ||
fn path_to_json(&mut self, path: String) { | ||
if let Some((_, ext)) = path.split_once(".") { | ||
if ext == "rs" { | ||
self.crates.push(Crate { | ||
root_module: path, | ||
edition: "2021".to_string(), | ||
deps: Vec::new(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
/// Parse the exercises folder for .rs files, any matches will create | ||
/// a new `crate` in rust-project.json which allows rust-analyzer to | ||
/// treat it like a normal binary | ||
pub fn exercies_to_json(&mut self) -> Result<(), Box<dyn Error>> { | ||
let glob = glob("./exercises/**/*")?; | ||
for e in glob { | ||
let path = e?.to_string_lossy().to_string(); | ||
self.path_to_json(path); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Use `rustup` command to determine the default toolchain, if it exists | ||
/// it will be put in RustAnalyzerProject.sysroot_src, otherwise an error will be returned | ||
pub fn get_sysroot_src(&mut self) -> Result<(), Box<dyn Error>> { | ||
let mut sysroot_src = home::rustup_home()?.to_string_lossy().to_string(); | ||
|
||
let output = Command::new("rustup").arg("default").output()?; | ||
|
||
let toolchain = String::from_utf8_lossy(&output.stdout).to_string(); | ||
|
||
sysroot_src += "/toolchains/"; | ||
sysroot_src += toolchain | ||
.split_once(' ') | ||
.ok_or("Unable to determine toolchain")? | ||
.0; | ||
sysroot_src += "/lib/rustlib/src/rust/library"; | ||
println!( | ||
"Determined toolchain to use with Rustlings: {}", | ||
sysroot_src | ||
); | ||
self.sysroot_src = sysroot_src; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn parses_exercises() { | ||
let mut rust_project = RustAnalyzerProject::new(); | ||
rust_project | ||
.exercies_to_json() | ||
.expect("Failed to parse exercises"); | ||
assert_eq!(rust_project.crates.len() > 0, true); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"sysroot_src":"/home/jacko/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library","crates":[]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"sysroot_src":"/home/jacko/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library","crates":[]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"sysroot_src":"/home/jacko/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library","crates":[]} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
rustc --print sysroot
instead. This also works when you don't have rustup installed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks I changed this in new pr: #1026