]> git.lizzy.rs Git - rust.git/commitdiff
Merge #3309
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Sat, 29 Feb 2020 15:36:03 +0000 (15:36 +0000)
committerGitHub <noreply@github.com>
Sat, 29 Feb 2020 15:36:03 +0000 (15:36 +0000)
3309: Find cargo toml up the fs r=matklad a=not-much-io

Currently rust-analyzer will look for Cargo.toml in the root of the project and if failing that then go down the filesystem until root.

This unfortunately wouldn't work automatically with (what I imagine is) a fairly common project structure. As an example with multiple languages like:
```
js/
  ..
rust/
  Cargo.toml
  ...
```

Added this small change so rust-analyzer would glance one level up if not found in root or down the filesystem.

## Why not go deeper?

Could be problematic with large project vendored dependencies etc.

## Why not add a Cargo.toml manual setting option?

Loosely related and a good idea, however the convenience of having this automated also is hard to pass up.

## Testing?

Build a binary with various logs and checked it in a project with such a structure:

```
[ERROR ra_project_model] find_cargo_toml()
[ERROR ra_project_model] find_cargo_toml_up_the_fs()
[ERROR ra_project_model] entities: ReadDir("/workspaces/my-project")
[ERROR ra_project_model] candidate: "/workspaces/my-project/rust/Cargo.toml", exists: true
```

## Edge Cases?

If you have multiple Cargo.toml files one level deeper AND not in the root, will get whatever comes first (order undefined), example:
```
crate1/
    Cargo.toml
crate2/
     Cargo.toml
... (no root Cargo.toml)
```

However this is quite unusual and wouldn't have worked before either. This is only resolvable via manually choosing.

Co-authored-by: nmio <kristo.koert@gmail.com>
crates/ra_project_model/src/lib.rs
crates/rust-analyzer/src/main_loop.rs

index 9df6a0e070e2eef988d42630d80aff381713c4ad..bcf12460d824a30f8663a0bd88b86e970744ab93 100644 (file)
@@ -6,7 +6,7 @@
 
 use std::{
     error::Error,
-    fs::File,
+    fs::{read_dir, File, ReadDir},
     io::BufReader,
     path::{Path, PathBuf},
     process::Command,
 };
 
 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
-pub struct CargoTomlNotFoundError(pub PathBuf);
+pub struct CargoTomlNotFoundError {
+    pub searched_at: PathBuf,
+    pub reason: String,
+}
 
 impl std::fmt::Display for CargoTomlNotFoundError {
     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(fmt, "can't find Cargo.toml at {}", self.0.display())
+        write!(
+            fmt,
+            "can't find Cargo.toml at {}, due to {}",
+            self.searched_at.display(),
+            self.reason
+        )
     }
 }
 
@@ -406,19 +414,68 @@ fn find_rust_project_json(path: &Path) -> Option<PathBuf> {
     None
 }
 
-fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
-    if path.ends_with("Cargo.toml") {
-        return Ok(path.to_path_buf());
-    }
+fn find_cargo_toml_in_parent_dir(path: &Path) -> Option<PathBuf> {
     let mut curr = Some(path);
     while let Some(path) = curr {
         let candidate = path.join("Cargo.toml");
         if candidate.exists() {
-            return Ok(candidate);
+            return Some(candidate);
         }
         curr = path.parent();
     }
-    Err(CargoTomlNotFoundError(path.to_path_buf()).into())
+
+    None
+}
+
+fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> {
+    // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
+    let mut valid_canditates = vec![];
+    for entity in entities.filter_map(Result::ok) {
+        let candidate = entity.path().join("Cargo.toml");
+        if candidate.exists() {
+            valid_canditates.push(candidate)
+        }
+    }
+    valid_canditates
+}
+
+fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
+    if path.ends_with("Cargo.toml") {
+        return Ok(path.to_path_buf());
+    }
+
+    if let Some(p) = find_cargo_toml_in_parent_dir(path) {
+        return Ok(p);
+    }
+
+    let entities = match read_dir(path) {
+        Ok(entities) => entities,
+        Err(e) => {
+            return Err(CargoTomlNotFoundError {
+                searched_at: path.to_path_buf(),
+                reason: format!("file system error: {}", e),
+            }
+            .into());
+        }
+    };
+
+    let mut valid_canditates = find_cargo_toml_in_child_dir(entities);
+    match valid_canditates.len() {
+        1 => Ok(valid_canditates.remove(0)),
+        0 => Err(CargoTomlNotFoundError {
+            searched_at: path.to_path_buf(),
+            reason: "no Cargo.toml file found".to_string(),
+        }
+        .into()),
+        _ => Err(CargoTomlNotFoundError {
+            searched_at: path.to_path_buf(),
+            reason: format!(
+                "multiple equally valid Cargo.toml files found: {:?}",
+                valid_canditates
+            ),
+        }
+        .into()),
+    }
 }
 
 pub fn get_rustc_cfg_options() -> CfgOptions {
index 2b25f54436caca00c56f69667c3a2a2938a3d7d3..fe804aadaa9b0205deb0f881271a980feed175b2 100644 (file)
@@ -115,12 +115,15 @@ pub fn main_loop(
                     Ok(workspace) => loaded_workspaces.push(workspace),
                     Err(e) => {
                         log::error!("loading workspace failed: {:?}", e);
-                        if let Some(ra_project_model::CargoTomlNotFoundError(_)) = e.downcast_ref()
+
+                        if let Some(ra_project_model::CargoTomlNotFoundError { .. }) =
+                            e.downcast_ref()
                         {
                             if !feature_flags.get("notifications.cargo-toml-not-found") {
                                 continue;
                             }
                         }
+
                         show_message(
                             req::MessageType::Error,
                             format!("rust-analyzer failed to load workspace: {:?}", e),