Skip to content

Commit e5ff81b

Browse files
author
Seb E
committed
Fix use of .. in dep-info-basedir config
1 parent aee9262 commit e5ff81b

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
lines changed

src/cargo/core/compiler/output_depinfo.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//! dependencies are not included under the assumption that changes to them can
2323
//! be detected via changes to `Cargo.lock`.
2424
25+
use cargo_util::paths::normalize_path;
2526
use std::collections::{BTreeSet, HashSet};
2627
use std::io::{BufWriter, Write};
2728
use std::path::{Path, PathBuf};
@@ -33,16 +34,21 @@ use log::debug;
3334

3435
fn render_filename<P: AsRef<Path>>(path: P, basedir: Option<&str>) -> CargoResult<String> {
3536
let path = path.as_ref();
36-
let relpath = match basedir {
37-
None => path,
38-
Some(base) => match path.strip_prefix(base) {
39-
Ok(relpath) => relpath,
40-
_ => path,
41-
},
42-
};
43-
relpath
44-
.to_str()
45-
.ok_or_else(|| internal(format!("path `{:?}` not utf-8", relpath)))
37+
if let Some(basedir) = basedir {
38+
let norm_path = normalize_path(path);
39+
let norm_basedir = normalize_path(basedir.as_ref());
40+
match norm_path.strip_prefix(norm_basedir) {
41+
Ok(relpath) => wrap_path(relpath),
42+
_ => wrap_path(path),
43+
}
44+
} else {
45+
wrap_path(path)
46+
}
47+
}
48+
49+
fn wrap_path(path: &Path) -> CargoResult<String> {
50+
path.to_str()
51+
.ok_or_else(|| internal(format!("path `{:?}` not utf-8", path)))
4652
.map(|f| f.replace(" ", "\\ "))
4753
}
4854

tests/testsuite/build_script.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cargo_test_support::compare::assert_match_exact;
44
use cargo_test_support::paths::CargoPathExt;
55
use cargo_test_support::registry::Package;
66
use cargo_test_support::tools;
7-
use cargo_test_support::{basic_manifest, cross_compile, is_coarse_mtime, project};
7+
use cargo_test_support::{basic_manifest, cross_compile, is_coarse_mtime, project, project_in};
88
use cargo_test_support::{rustc_host, sleep_ms, slow_cpu_multiplier, symlink_supported};
99
use cargo_util::paths::remove_dir_all;
1010
use std::env;
@@ -3248,6 +3248,69 @@ fn generate_good_d_files() {
32483248
.any(|v| v == "barkbarkbark" || v == "build.rs"));
32493249
}
32503250

3251+
#[cargo_test]
3252+
fn generate_good_d_files_for_external_tools() {
3253+
// This tests having a relative paths going out of the
3254+
// project root in config's dep-info-basedir
3255+
let p = project_in("rust_things")
3256+
.file(
3257+
"awoo/Cargo.toml",
3258+
r#"
3259+
[project]
3260+
name = "awoo"
3261+
version = "0.5.0"
3262+
build = "build.rs"
3263+
"#,
3264+
)
3265+
.file("awoo/src/lib.rs", "")
3266+
.file(
3267+
"awoo/build.rs",
3268+
r#"
3269+
fn main() {
3270+
println!("cargo:rerun-if-changed=build.rs");
3271+
println!("cargo:rerun-if-changed=barkbarkbark");
3272+
}
3273+
"#,
3274+
)
3275+
.file(
3276+
"Cargo.toml",
3277+
r#"
3278+
[project]
3279+
name = "meow"
3280+
version = "0.5.0"
3281+
[dependencies]
3282+
awoo = { path = "awoo" }
3283+
"#,
3284+
)
3285+
.file("src/main.rs", "fn main() {}")
3286+
.file(
3287+
".cargo/config.toml",
3288+
r#"
3289+
[build]
3290+
dep-info-basedir="../.."
3291+
"#,
3292+
)
3293+
.build();
3294+
3295+
p.cargo("build -v").run();
3296+
3297+
let dot_d_path = p.bin("meow").with_extension("d");
3298+
let dot_d = fs::read_to_string(&dot_d_path).unwrap();
3299+
3300+
println!("*.d file content with dep-info-basedir*: {}", &dot_d);
3301+
3302+
assert_match_exact(
3303+
concat!(
3304+
"rust_things/foo/target/debug/meow[EXE]:",
3305+
" rust_things/foo/awoo/barkbarkbark",
3306+
" rust_things/foo/awoo/build.rs",
3307+
" rust_things/foo/awoo/src/lib.rs",
3308+
" rust_things/foo/src/main.rs",
3309+
),
3310+
&dot_d,
3311+
);
3312+
}
3313+
32513314
#[cargo_test]
32523315
fn rebuild_only_on_explicit_paths() {
32533316
let p = project()

0 commit comments

Comments
 (0)