Skip to content

Commit 730e8ca

Browse files
committed
make call to paths::join_paths and warn if path contains invalid chars
1 parent 23c1a51 commit 730e8ca

File tree

6 files changed

+24
-22
lines changed

6 files changed

+24
-22
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ use tempfile::Builder as TempFileBuilder;
1919
/// error message.
2020
pub fn join_paths<T: AsRef<OsStr>>(paths: &[T], env: &str) -> Result<OsString> {
2121
env::join_paths(paths.iter()).with_context(|| {
22-
let paths = paths.iter().map(Path::new).collect::<Vec<_>>();
22+
let paths = paths.iter()
23+
.map(|s| s.as_ref().to_string_lossy())
24+
.map(|s| format!(" \"{s}\""))
25+
.collect::<Vec<String>>()
26+
.join(",\n");
27+
2328
format!(
24-
"failed to join paths from `${}` together\
25-
\n \
26-
If you set `${}` manually, check if it contains an unterminated quote character \
27-
or path separators (usually `:` or `:`). Please avoid using them.\
28-
\n\n \
29-
Otherwise, check if any of paths listed contain one of those characters: {:?}",
30-
env, env, paths
29+
"failed to join paths from `${env}` together\n\
30+
If you set `${env}` manually, check if it contains an unterminated quote character \
31+
or path separators (usually `:` or `;`). Please avoid using them. \
32+
Otherwise, check if any of paths listed below contain one of those characters:\n{paths}"
3133
)
3234
})
3335
}

src/cargo/ops/cargo_new.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use cargo_util::paths;
77
use serde::de;
88
use serde::Deserialize;
99
use std::collections::BTreeMap;
10-
use std::fmt;
10+
use std::ffi::OsStr;
1111
use std::io::{BufRead, BufReader, ErrorKind};
1212
use std::path::{Path, PathBuf};
1313
use std::str::FromStr;
14+
use std::{fmt, slice};
1415
use toml_edit::easy as toml;
1516

1617
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -263,11 +264,12 @@ fn check_name(
263264

264265
/// Checks if the path contains any invalid PATH env characters.
265266
fn check_path(path: &Path, shell: &mut Shell) -> CargoResult<()> {
266-
if path.to_string_lossy().contains(&[';', ':', '"'][..]) {
267+
// warn if the path contains characters that will break `env::join_paths`
268+
if let Err(_) = paths::join_paths(slice::from_ref(&OsStr::new(path)), "") {
269+
let path = path.to_string_lossy();
267270
shell.warn(format!(
268-
"the path `{}` contains invalid PATH characters (usually `:`, `;`, or `\"`)\n\
269-
It is recommended to use a different name to avoid problems.",
270-
path.to_string_lossy()
271+
"the path `{path}` contains invalid PATH characters (usually `:`, `;`, or `\"`)\n\
272+
It is recommended to use a different name to avoid problems."
271273
))?;
272274
}
273275
Ok(())

tests/testsuite/init/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod mercurial_autodetect;
2828
mod multibin_project_name_clash;
2929
#[cfg(not(windows))]
3030
mod no_filename;
31+
#[cfg(unix)]
3132
mod path_contains_separator;
3233
mod pijul_autodetect;
3334
mod reserved_name;

tests/testsuite/init/path_contains_separator/in/test:ing/mod.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/testsuite/init/path_contains_separator/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cargo_test_support::compare::assert_ui;
22
use cargo_test_support::prelude::*;
3-
use cargo_test_support::Project;
3+
use cargo_test_support::{t, Project};
44

55
use cargo_test_support::curr_dir;
66

@@ -9,6 +9,10 @@ fn path_contains_separator() {
99
let project = Project::from_template(curr_dir!().join("in"));
1010
let project_root = &project.root().join("test:ing");
1111

12+
if !project_root.exists() {
13+
t!(std::fs::create_dir(&project_root));
14+
}
15+
1216
snapbox::cmd::Command::cargo_ui()
1317
.arg_line("init --bin --vcs none --edition 2015 --name testing")
1418
.current_dir(project_root)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
warning: the path `[ROOT]/case/test:ing/.` contains PATH separators (usually `:` or `:`)
1+
warning: the path `[ROOT]/case/test:ing/.` contains invalid PATH characters (usually `:`, `;`, or `"`)
22
It is recommended to use a different name to avoid problems.
33
Created binary (application) package

0 commit comments

Comments
 (0)