Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions rls/src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::project_model::{ProjectModel, RacerFallbackModel, RacerProjectModel};
use crate::server::Output;

use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
Expand Down Expand Up @@ -281,12 +282,21 @@ impl InitActionContext {
fn file_edition(&self, file: PathBuf) -> Option<Edition> {
let files_to_crates = self.file_to_crates.lock().unwrap();

let editions: HashSet<_> = files_to_crates.get(&file)?.iter().map(|c| c.edition).collect();
let editions: HashSet<_> = files_to_crates
.get(&file)
.map(|crates| crates.iter().map(|c| c.edition).collect())
.unwrap_or_default();

let mut iter = editions.into_iter();
match (iter.next(), iter.next()) {
(ret @ Some(_), None) => ret,
_ => None,
(Some(_), Some(_)) => None,
_ => {
// fall back on checking the root manifest for package edition
let manifest_path =
cargo::util::important_paths::find_root_manifest_for_wd(&file).ok()?;
edition_from_manifest(manifest_path)
}
}
}

Expand Down Expand Up @@ -425,6 +435,24 @@ impl InitActionContext {
}
}

/// Read package edition from the Cargo manifest
fn edition_from_manifest<P: AsRef<Path>>(manifest_path: P) -> Option<Edition> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not thrilled about placement of this function here but maybe it's self-contained enough that it's not a problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a private function that's only used here so it seems ok to me. I'd imagine it won't be hard to move or replace later. But if you can think of a better place to put it, along with the test, then sure.

#[derive(Debug, serde::Deserialize)]
struct Manifest {
package: Package,
}
#[derive(Debug, serde::Deserialize)]
struct Package {
edition: Option<String>,
}

let manifest: Manifest = toml::from_str(&std::fs::read_to_string(manifest_path).ok()?).ok()?;
match manifest.package.edition {
Some(edition) => Edition::try_from(edition.as_str()).ok(),
None => Some(Edition::default()),
}
}

/// Some notifications come with sequence numbers, we check that these are in
/// order. However, clients might be buggy about sequence numbers so we do cope
/// with them being wrong.
Expand Down Expand Up @@ -662,4 +690,28 @@ mod test {
assert!(!watch.is_relevant_save_doc(&did_save("file:///c:/some/dir/inner/Cargo.lock")));
assert!(!watch.is_relevant_save_doc(&did_save("file:///c:/Cargo.toml")));
}

#[test]
fn explicit_edition_from_manifest() -> Result<(), std::io::Error> {
use std::{fs::File, io::Write};

let dir = tempfile::tempdir()?;

let manifest_path = {
let path = dir.path().join("Cargo.toml");
let mut m = File::create(&path)?;
writeln!(
m,
"[package]\n\
name = \"foo\"\n\
version = \"1.0.0\"\n\
edition = \"2018\""
)?;
path
};

assert_eq!(edition_from_manifest(manifest_path), Some(Edition::Edition2018));

Ok(())
}
}
12 changes: 12 additions & 0 deletions rls/src/build/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,15 @@ impl Default for Edition {
Edition::Edition2015
}
}

impl std::convert::TryFrom<&str> for Edition {
type Error = &'static str;

fn try_from(val: &str) -> Result<Self, Self::Error> {
Ok(match val {
"2015" => Edition::Edition2015,
"2018" => Edition::Edition2018,
_ => return Err("unknown"),
})
}
}