Skip to content

Commit 387dae9

Browse files
committed
Move canonicalization into current_dll_path
And consistently use try_canonicalize rather than canonicalize.
1 parent 425e142 commit 387dae9

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

compiler/rustc_session/src/filesearch.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::path::{Path, PathBuf};
44
use std::{env, fs};
55

6-
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
6+
use rustc_fs_util::try_canonicalize;
77
use rustc_target::spec::Target;
88
use smallvec::{SmallVec, smallvec};
99

@@ -87,7 +87,7 @@ fn current_dll_path() -> Result<PathBuf, String> {
8787
};
8888
let bytes = CStr::from_ptr(fname_ptr).to_bytes();
8989
let os = OsStr::from_bytes(bytes);
90-
Ok(PathBuf::from(os))
90+
try_canonicalize(Path::new(os)).map_err(|e| e.to_string())
9191
}
9292

9393
#[cfg(target_os = "aix")]
@@ -122,7 +122,7 @@ fn current_dll_path() -> Result<PathBuf, String> {
122122
if (data_base..data_end).contains(&addr) {
123123
let bytes = CStr::from_ptr(&(*current).ldinfo_filename[0]).to_bytes();
124124
let os = OsStr::from_bytes(bytes);
125-
return Ok(PathBuf::from(os));
125+
return try_canonicalize(Path::new(os)).map_err(|e| e.to_string());
126126
}
127127
if (*current).ldinfo_next == 0 {
128128
break;
@@ -169,7 +169,12 @@ fn current_dll_path() -> Result<PathBuf, String> {
169169

170170
filename.truncate(n);
171171

172-
Ok(OsString::from_wide(&filename).into())
172+
let path = try_canonicalize(OsString::from_wide(&filename)).map_err(|e| e.to_string())?;
173+
174+
// See comments on this target function, but the gist is that
175+
// gcc chokes on verbatim paths which fs::canonicalize generates
176+
// so we try to avoid those kinds of paths.
177+
Ok(rustc_fs_util::fix_windows_verbatim_for_gcc(&path))
173178
}
174179

175180
#[cfg(target_os = "wasi")]
@@ -178,10 +183,8 @@ fn current_dll_path() -> Result<PathBuf, String> {
178183
}
179184

180185
pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> {
181-
let target = crate::config::host_tuple();
182186
let mut sysroot_candidates: SmallVec<[PathBuf; 2]> = smallvec![get_or_default_sysroot()];
183-
let path = current_dll_path().and_then(|s| try_canonicalize(s).map_err(|e| e.to_string()));
184-
if let Ok(dll) = path {
187+
if let Ok(dll) = current_dll_path() {
185188
// use `parent` twice to chop off the file name and then also the
186189
// directory containing the dll which should be either `lib` or `bin`.
187190
if let Some(path) = dll.parent().and_then(|p| p.parent()) {
@@ -196,7 +199,7 @@ pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> {
196199
// assume that we may be in the main libdir.
197200
sysroot_candidates.push(path.to_owned());
198201

199-
if path.ends_with(target) {
202+
if path.ends_with(crate::config::host_tuple()) {
200203
sysroot_candidates.extend(
201204
path.parent() // chop off `$target`
202205
.and_then(|p| p.parent()) // chop off `rustlib`
@@ -219,17 +222,8 @@ pub fn materialize_sysroot(maybe_sysroot: Option<PathBuf>) -> PathBuf {
219222
/// This function checks if sysroot is found using env::args().next(), and if it
220223
/// is not found, finds sysroot from current rustc_driver dll.
221224
pub fn get_or_default_sysroot() -> PathBuf {
222-
// Follow symlinks. If the resolved path is relative, make it absolute.
223-
fn canonicalize(path: PathBuf) -> PathBuf {
224-
let path = try_canonicalize(&path).unwrap_or(path);
225-
// See comments on this target function, but the gist is that
226-
// gcc chokes on verbatim paths which fs::canonicalize generates
227-
// so we try to avoid those kinds of paths.
228-
fix_windows_verbatim_for_gcc(&path)
229-
}
230-
231225
fn default_from_rustc_driver_dll() -> Result<PathBuf, String> {
232-
let dll = current_dll_path().map(|s| canonicalize(s))?;
226+
let dll = current_dll_path()?;
233227

234228
// `dll` will be in one of the following two:
235229
// - compiler's libdir: $sysroot/lib/*.dll

0 commit comments

Comments
 (0)