Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 939ebb4

Browse files
committed
Forbid canonicalization of paths and normalize all rust-project.json paths
1 parent 60f4b3e commit 939ebb4

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

crates/paths/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,8 @@ impl AbsPath {
166166
AbsPathBuf::try_from(self.0.to_path_buf()).unwrap()
167167
}
168168

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())
169+
pub fn canonicalize(&self) -> ! {
170+
panic!("We explicitly do not provide canonicalization API, as that is almost always a wrong solution, see #14430")
172171
}
173172

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

crates/project-model/src/manifest_path.rs

Lines changed: 2 additions & 3 deletions
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

Lines changed: 13 additions & 18 deletions
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,24 @@ 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 =
102+
|p| AbsPathBuf::try_from(p).unwrap_or_else(|path| base.join(&path)).normalize();
102103
ProjectJson {
103-
sysroot: data.sysroot.map(|it| base.join(it)),
104-
sysroot_src: data.sysroot_src.map(|it| base.join(it)),
104+
sysroot: data.sysroot.map(absolutize),
105+
sysroot_src: data.sysroot_src.map(absolutize),
105106
project_root: base.to_path_buf(),
106107
crates: data
107108
.crates
108109
.into_iter()
109110
.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();
111+
let root_module = absolutize(crate_data.root_module);
112+
let is_workspace_member = crate_data
113+
.is_workspace_member
114+
.unwrap_or_else(|| root_module.starts_with(base));
116115
let (include, exclude) = match crate_data.source {
117116
Some(src) => {
118117
let absolutize = |dirs: Vec<PathBuf>| {
119-
dirs.into_iter()
120-
.map(|it| base.join(it).normalize())
121-
.collect::<Vec<_>>()
118+
dirs.into_iter().map(absolutize).collect::<Vec<_>>()
122119
};
123120
(absolutize(src.include_dirs), absolutize(src.exclude_dirs))
124121
}
@@ -145,17 +142,15 @@ impl ProjectJson {
145142
cfg: crate_data.cfg,
146143
target: crate_data.target,
147144
env: crate_data.env,
148-
proc_macro_dylib_path: crate_data
149-
.proc_macro_dylib_path
150-
.map(|it| base.join(it)),
145+
proc_macro_dylib_path: crate_data.proc_macro_dylib_path.map(absolutize),
151146
is_workspace_member,
152147
include,
153148
exclude,
154149
is_proc_macro: crate_data.is_proc_macro,
155150
repository: crate_data.repository,
156151
}
157152
})
158-
.collect::<Vec<_>>(),
153+
.collect(),
159154
}
160155
}
161156

@@ -243,7 +238,7 @@ struct CrateSource {
243238
exclude_dirs: Vec<PathBuf>,
244239
}
245240

246-
fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error>
241+
fn deserialize_crate_name<'de, D>(de: D) -> std::result::Result<CrateName, D::Error>
247242
where
248243
D: de::Deserializer<'de>,
249244
{

crates/project-model/src/workspace.rs

Lines changed: 0 additions & 1 deletion
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)