Forbid canonicalization of paths and normalize all rust-project.json paths

This commit is contained in:
Lukas Wirth 2023-05-04 14:20:52 +02:00
parent 60f4b3e26e
commit 939ebb4454
4 changed files with 17 additions and 25 deletions

View File

@ -166,9 +166,8 @@ pub fn to_path_buf(&self) -> AbsPathBuf {
AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
}
/// Equivalent of [`Path::canonicalize`] for `AbsPath`.
pub fn canonicalize(&self) -> Result<AbsPathBuf, std::io::Error> {
Ok(self.as_ref().canonicalize()?.try_into().unwrap())
pub fn canonicalize(&self) -> ! {
panic!("We explicitly do not provide canonicalization API, as that is almost always a wrong solution, see #14430")
}
/// Equivalent of [`Path::strip_prefix`] for `AbsPath`.

View File

@ -35,9 +35,8 @@ pub fn parent(&self) -> &AbsPath {
self.file.parent().unwrap()
}
/// Equivalent of [`Path::canonicalize`] for `ManifestPath`.
pub fn canonicalize(&self) -> Result<ManifestPath, std::io::Error> {
Ok((&**self).canonicalize()?.try_into().unwrap())
pub fn canonicalize(&self) -> ! {
(&**self).canonicalize()
}
}

View File

@ -49,13 +49,12 @@
//! user explores them belongs to that extension (it's totally valid to change
//! rust-project.json over time via configuration request!)
use std::path::PathBuf;
use base_db::{CrateDisplayName, CrateId, CrateName, Dependency, Edition};
use la_arena::RawIdx;
use paths::{AbsPath, AbsPathBuf};
use rustc_hash::FxHashMap;
use serde::{de, Deserialize};
use std::path::PathBuf;
use crate::cfg_flag::CfgFlag;
@ -99,26 +98,24 @@ impl ProjectJson {
/// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via
/// configuration.
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
let absolutize =
|p| AbsPathBuf::try_from(p).unwrap_or_else(|path| base.join(&path)).normalize();
ProjectJson {
sysroot: data.sysroot.map(|it| base.join(it)),
sysroot_src: data.sysroot_src.map(|it| base.join(it)),
sysroot: data.sysroot.map(absolutize),
sysroot_src: data.sysroot_src.map(absolutize),
project_root: base.to_path_buf(),
crates: data
.crates
.into_iter()
.map(|crate_data| {
let is_workspace_member = crate_data.is_workspace_member.unwrap_or_else(|| {
crate_data.root_module.is_relative()
&& !crate_data.root_module.starts_with("..")
|| crate_data.root_module.starts_with(base)
});
let root_module = base.join(crate_data.root_module).normalize();
let root_module = absolutize(crate_data.root_module);
let is_workspace_member = crate_data
.is_workspace_member
.unwrap_or_else(|| root_module.starts_with(base));
let (include, exclude) = match crate_data.source {
Some(src) => {
let absolutize = |dirs: Vec<PathBuf>| {
dirs.into_iter()
.map(|it| base.join(it).normalize())
.collect::<Vec<_>>()
dirs.into_iter().map(absolutize).collect::<Vec<_>>()
};
(absolutize(src.include_dirs), absolutize(src.exclude_dirs))
}
@ -145,9 +142,7 @@ pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
cfg: crate_data.cfg,
target: crate_data.target,
env: crate_data.env,
proc_macro_dylib_path: crate_data
.proc_macro_dylib_path
.map(|it| base.join(it)),
proc_macro_dylib_path: crate_data.proc_macro_dylib_path.map(absolutize),
is_workspace_member,
include,
exclude,
@ -155,7 +150,7 @@ pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
repository: crate_data.repository,
}
})
.collect::<Vec<_>>(),
.collect(),
}
}
@ -243,7 +238,7 @@ struct CrateSource {
exclude_dirs: Vec<PathBuf>,
}
fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error>
fn deserialize_crate_name<'de, D>(de: D) -> std::result::Result<CrateName, D::Error>
where
D: de::Deserializer<'de>,
{

View File

@ -179,7 +179,6 @@ pub fn load(
};
let res = match manifest {
ProjectManifest::ProjectJson(project_json) => {
let project_json = project_json.canonicalize()?;
let file = fs::read_to_string(&project_json).with_context(|| {
format!("Failed to read json file {}", project_json.display())
})?;