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

@ -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 @@ impl 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 @@ impl 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>,
{