Skip to content

Commit a7944a9

Browse files
committed
Auto merge of rust-lang#14735 - Veykril:forbid-relative-json, r=Veykril
internal: Forbid canonicalization of paths and normalize all rust-project.json paths Closes rust-lang/rust-analyzer#14728 cc rust-lang/rust-analyzer#14430 - Removes canonicalization (and forbids it from being used in a sense, clippy could help here again with its lint in the future) - Normalizes all paths in rust-project.json which we weren't doing in some cases
2 parents e9f9bc2 + f47caa6 commit a7944a9

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

crates/paths/src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ impl AbsPath {
140140
self.0.parent().map(AbsPath::assert)
141141
}
142142

143+
/// Equivalent of [`Path::join`] for `AbsPath` with an additional normalize step afterwards.
144+
pub fn absolutize(&self, path: impl AsRef<Path>) -> AbsPathBuf {
145+
self.join(path).normalize()
146+
}
147+
143148
/// Equivalent of [`Path::join`] for `AbsPath`.
144149
pub fn join(&self, path: impl AsRef<Path>) -> AbsPathBuf {
145150
self.as_ref().join(path).try_into().unwrap()
@@ -166,9 +171,8 @@ impl AbsPath {
166171
AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
167172
}
168173

169-
/// Equivalent of [`Path::canonicalize`] for `AbsPath`.
170-
pub fn canonicalize(&self) -> Result<AbsPathBuf, std::io::Error> {
171-
Ok(self.as_ref().canonicalize()?.try_into().unwrap())
174+
pub fn canonicalize(&self) -> ! {
175+
panic!("We explicitly do not provide canonicalization API, as that is almost always a wrong solution, see #14430")
172176
}
173177

174178
/// Equivalent of [`Path::strip_prefix`] for `AbsPath`.

crates/project-model/src/manifest_path.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ impl ManifestPath {
3535
self.file.parent().unwrap()
3636
}
3737

38-
/// Equivalent of [`Path::canonicalize`] for `ManifestPath`.
39-
pub fn canonicalize(&self) -> Result<ManifestPath, std::io::Error> {
40-
Ok((&**self).canonicalize()?.try_into().unwrap())
38+
pub fn canonicalize(&self) -> ! {
39+
(&**self).canonicalize()
4140
}
4241
}
4342

crates/project-model/src/project_json.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@
4949
//! user explores them belongs to that extension (it's totally valid to change
5050
//! rust-project.json over time via configuration request!)
5151
52-
use std::path::PathBuf;
53-
5452
use base_db::{CrateDisplayName, CrateId, CrateName, Dependency, Edition};
5553
use la_arena::RawIdx;
5654
use paths::{AbsPath, AbsPathBuf};
5755
use rustc_hash::FxHashMap;
5856
use serde::{de, Deserialize};
57+
use std::path::PathBuf;
5958

6059
use crate::cfg_flag::CfgFlag;
6160

@@ -99,26 +98,23 @@ impl ProjectJson {
9998
/// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via
10099
/// configuration.
101100
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
101+
let absolutize_on_base = |p| base.absolutize(p);
102102
ProjectJson {
103-
sysroot: data.sysroot.map(|it| base.join(it)),
104-
sysroot_src: data.sysroot_src.map(|it| base.join(it)),
103+
sysroot: data.sysroot.map(absolutize_on_base),
104+
sysroot_src: data.sysroot_src.map(absolutize_on_base),
105105
project_root: base.to_path_buf(),
106106
crates: data
107107
.crates
108108
.into_iter()
109109
.map(|crate_data| {
110-
let is_workspace_member = crate_data.is_workspace_member.unwrap_or_else(|| {
111-
crate_data.root_module.is_relative()
112-
&& !crate_data.root_module.starts_with("..")
113-
|| crate_data.root_module.starts_with(base)
114-
});
115-
let root_module = base.join(crate_data.root_module).normalize();
110+
let root_module = absolutize_on_base(crate_data.root_module);
111+
let is_workspace_member = crate_data
112+
.is_workspace_member
113+
.unwrap_or_else(|| root_module.starts_with(base));
116114
let (include, exclude) = match crate_data.source {
117115
Some(src) => {
118116
let absolutize = |dirs: Vec<PathBuf>| {
119-
dirs.into_iter()
120-
.map(|it| base.join(it).normalize())
121-
.collect::<Vec<_>>()
117+
dirs.into_iter().map(absolutize_on_base).collect::<Vec<_>>()
122118
};
123119
(absolutize(src.include_dirs), absolutize(src.exclude_dirs))
124120
}
@@ -147,15 +143,15 @@ impl ProjectJson {
147143
env: crate_data.env,
148144
proc_macro_dylib_path: crate_data
149145
.proc_macro_dylib_path
150-
.map(|it| base.join(it)),
146+
.map(absolutize_on_base),
151147
is_workspace_member,
152148
include,
153149
exclude,
154150
is_proc_macro: crate_data.is_proc_macro,
155151
repository: crate_data.repository,
156152
}
157153
})
158-
.collect::<Vec<_>>(),
154+
.collect(),
159155
}
160156
}
161157

@@ -243,7 +239,7 @@ struct CrateSource {
243239
exclude_dirs: Vec<PathBuf>,
244240
}
245241

246-
fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error>
242+
fn deserialize_crate_name<'de, D>(de: D) -> std::result::Result<CrateName, D::Error>
247243
where
248244
D: de::Deserializer<'de>,
249245
{

crates/project-model/src/workspace.rs

-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ impl ProjectWorkspace {
179179
};
180180
let res = match manifest {
181181
ProjectManifest::ProjectJson(project_json) => {
182-
let project_json = project_json.canonicalize()?;
183182
let file = fs::read_to_string(&project_json).with_context(|| {
184183
format!("Failed to read json file {}", project_json.display())
185184
})?;

0 commit comments

Comments
 (0)