From 4b747c070d251fa0453548c2b4bfaa49d84651b4 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Sat, 3 Aug 2019 15:10:31 +0100 Subject: [PATCH 1/3] Fix rustfmt during builds by reading edition from manifest --- rls/src/actions/mod.rs | 53 ++++++++++++++++++++++++++++++++++++++++-- rls/src/build/plan.rs | 12 ++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/rls/src/actions/mod.rs b/rls/src/actions/mod.rs index d0535b1ec97..09987faf6ad 100644 --- a/rls/src/actions/mod.rs +++ b/rls/src/actions/mod.rs @@ -28,6 +28,7 @@ use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use std::thread; +use std::convert::TryFrom; // TODO: Support non-`file` URI schemes in VFS. We're currently ignoring them because // we don't want to crash the RLS in case a client opens a file under different URI scheme @@ -281,12 +282,21 @@ impl InitActionContext { fn file_edition(&self, file: PathBuf) -> Option { 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 edition + let manifest_path = + cargo::util::important_paths::find_root_manifest_for_wd(&file).ok()?; + edition_from_manifest(manifest_path) + } } } @@ -425,6 +435,24 @@ impl InitActionContext { } } +/// Read edition from the manifest +fn edition_from_manifest>(manifest_path: P) -> Option { + #[derive(Debug, serde::Deserialize)] + struct Manifest { + package: Package, + } + #[derive(Debug, serde::Deserialize)] + struct Package { + edition: Option, + } + + 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. @@ -662,4 +690,25 @@ 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(()) + } } diff --git a/rls/src/build/plan.rs b/rls/src/build/plan.rs index 6ad96d1d13f..c4df0a3ac0e 100644 --- a/rls/src/build/plan.rs +++ b/rls/src/build/plan.rs @@ -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 { + Ok(match val { + "2015" => Edition::Edition2015, + "2018" => Edition::Edition2018, + _ => return Err("unknown"), + }) + } +} From ea8551acc58e78b46ad3c8ceda427e539177cddb Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Sun, 4 Aug 2019 23:55:35 +0100 Subject: [PATCH 2/3] Clarify comments Co-Authored-By: Igor Matuszewski --- rls/src/actions/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rls/src/actions/mod.rs b/rls/src/actions/mod.rs index 09987faf6ad..0c3f7d04049 100644 --- a/rls/src/actions/mod.rs +++ b/rls/src/actions/mod.rs @@ -292,7 +292,7 @@ impl InitActionContext { (ret @ Some(_), None) => ret, (Some(_), Some(_)) => None, _ => { - // fall back on checking the root manifest for edition + // 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) @@ -435,7 +435,7 @@ impl InitActionContext { } } -/// Read edition from the manifest +/// Read package edition from the Cargo manifest fn edition_from_manifest>(manifest_path: P) -> Option { #[derive(Debug, serde::Deserialize)] struct Manifest { From f20f14c08a9564f1a461e00bf07e0c6db0c28752 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Mon, 5 Aug 2019 10:06:07 +0100 Subject: [PATCH 3/3] rustfmt --- rls/src/actions/mod.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/rls/src/actions/mod.rs b/rls/src/actions/mod.rs index 0c3f7d04049..9297c3708fb 100644 --- a/rls/src/actions/mod.rs +++ b/rls/src/actions/mod.rs @@ -23,12 +23,12 @@ 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}; use std::sync::{Arc, Mutex}; use std::thread; -use std::convert::TryFrom; // TODO: Support non-`file` URI schemes in VFS. We're currently ignoring them because // we don't want to crash the RLS in case a client opens a file under different URI scheme @@ -700,10 +700,13 @@ mod test { 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\"")?; + writeln!( + m, + "[package]\n\ + name = \"foo\"\n\ + version = \"1.0.0\"\n\ + edition = \"2018\"" + )?; path };