Skip to content

Commit 27366fd

Browse files
authored
Added validation for unmatched brackets in build-dir template (#15414)
### What does this PR try to resolve? This PR adds validation for unmatched brackets (which are used for template variables) in `build.build-dir` paths. See #14125 (comment) in #14125
2 parents f2c4849 + ad3e593 commit 27366fd

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/cargo/util/context/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,16 @@ impl GlobalContext {
687687
} => anyhow!(
688688
"unexpected variable `{variable}` in build.build-dir path `{raw_template}`"
689689
),
690+
path::ResolveTemplateError::UnexpectedBracket { bracket_type, raw_template } => {
691+
let (btype, literal) = match bracket_type {
692+
path::BracketType::Opening => ("opening", "{"),
693+
path::BracketType::Closing => ("closing", "}"),
694+
};
695+
696+
anyhow!(
697+
"unexpected {btype} bracket `{literal}` in build.build-dir path `{raw_template}`"
698+
)
699+
}
690700
})?;
691701

692702
// Check if the target directory is set to an empty string in the config.toml file.

src/cargo/util/context/path.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ impl ConfigRelativePath {
5858
});
5959
};
6060

61+
if value.contains("{") {
62+
return Err(ResolveTemplateError::UnexpectedBracket {
63+
bracket_type: BracketType::Opening,
64+
raw_template: self.0.val.clone(),
65+
});
66+
}
67+
68+
if value.contains("}") {
69+
return Err(ResolveTemplateError::UnexpectedBracket {
70+
bracket_type: BracketType::Closing,
71+
raw_template: self.0.val.clone(),
72+
});
73+
}
74+
6175
Ok(self.0.definition.root(gctx).join(&value))
6276
}
6377

@@ -139,4 +153,14 @@ pub enum ResolveTemplateError {
139153
variable: String,
140154
raw_template: String,
141155
},
156+
UnexpectedBracket {
157+
bracket_type: BracketType,
158+
raw_template: String,
159+
},
160+
}
161+
162+
#[derive(Debug)]
163+
pub enum BracketType {
164+
Opening,
165+
Closing,
142166
}

tests/testsuite/build_dir.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,49 @@ fn template_workspace_path_hash_should_handle_symlink() {
684684
}
685685
}
686686

687+
#[cargo_test]
688+
fn template_should_handle_reject_unmatched_brackets() {
689+
let p = project()
690+
.file("src/lib.rs", "")
691+
.file(
692+
".cargo/config.toml",
693+
r#"
694+
[build]
695+
build-dir = "foo/{bar"
696+
"#,
697+
)
698+
.build();
699+
700+
p.cargo("build -Z build-dir")
701+
.masquerade_as_nightly_cargo(&["build-dir"])
702+
.with_status(101)
703+
.with_stderr_data(str![[r#"
704+
[ERROR] unexpected opening bracket `{` in build.build-dir path `foo/{bar`
705+
706+
"#]])
707+
.run();
708+
709+
let p = project()
710+
.file("src/lib.rs", "")
711+
.file(
712+
".cargo/config.toml",
713+
r#"
714+
[build]
715+
build-dir = "foo/}bar"
716+
"#,
717+
)
718+
.build();
719+
720+
p.cargo("build -Z build-dir")
721+
.masquerade_as_nightly_cargo(&["build-dir"])
722+
.with_status(101)
723+
.with_stderr_data(str![[r#"
724+
[ERROR] unexpected closing bracket `}` in build.build-dir path `foo/}bar`
725+
726+
"#]])
727+
.run();
728+
}
729+
687730
fn parse_workspace_manifest_path_hash(hash_dir: &PathBuf) -> PathBuf {
688731
// Since the hash will change between test runs simply find the first directories and assume
689732
// that is the hash dir. The format is a 2 char directory followed by the remaining hash in the

0 commit comments

Comments
 (0)