Skip to content

Commit 82a92a2

Browse files
committed
Warn if project path contains an invalid PATH env character
1 parent f5cdfa4 commit 82a92a2

File tree

9 files changed

+74
-0
lines changed

9 files changed

+74
-0
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,18 @@ fn check_name(
261261
Ok(())
262262
}
263263

264+
/// Checks if the path contains any invalid PATH env characters.
265+
fn check_path(path: &Path, shell: &mut Shell) -> CargoResult<()> {
266+
if path.to_string_lossy().contains(&[';', ':', '"'][..]) {
267+
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+
))?;
272+
}
273+
Ok(())
274+
}
275+
264276
fn detect_source_paths_and_types(
265277
package_path: &Path,
266278
package_name: &str,
@@ -421,6 +433,8 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
421433
)
422434
}
423435

436+
check_path(path, &mut config.shell())?;
437+
424438
let is_bin = opts.kind.is_bin();
425439

426440
let name = get_name(path, opts)?;
@@ -458,6 +472,8 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<NewProjectKind> {
458472
anyhow::bail!("`cargo init` cannot be run on existing Cargo packages")
459473
}
460474

475+
check_path(path, &mut config.shell())?;
476+
461477
let name = get_name(path, opts)?;
462478

463479
let mut src_paths_types = vec![];

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+
mod path_contains_separator;
3132
mod pijul_autodetect;
3233
mod reserved_name;
3334
mod simple_bin;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::prelude::*;
3+
use cargo_test_support::{command_is_available, paths, Project};
4+
use std::fs;
5+
use std::process::Command;
6+
7+
use crate::test_root;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::prelude::*;
3+
use cargo_test_support::Project;
4+
5+
use cargo_test_support::curr_dir;
6+
7+
#[cargo_test]
8+
fn path_contains_separator() {
9+
let project = Project::from_template(curr_dir!().join("in"));
10+
let project_root = &project.root().join("test:ing");
11+
12+
snapbox::cmd::Command::cargo_ui()
13+
.arg_line("init --bin --vcs none --edition 2015 --name testing")
14+
.current_dir(project_root)
15+
.assert()
16+
.success()
17+
.stdout_matches_path(curr_dir!().join("stdout.log"))
18+
.stderr_matches_path(curr_dir!().join("stderr.log"));
19+
20+
assert_ui().subset_matches(curr_dir!().join("out"), project_root);
21+
assert!(!project_root.join(".gitignore").is_file());
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "testing"
3+
version = "0.1.0"
4+
edition = "2015"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
warning: the path `[ROOT]/case/test:ing/.` contains PATH separators (usually `:` or `:`)
2+
It is recommended to use a different name to avoid problems.
3+
Created binary (application) package

tests/testsuite/init/path_contains_separator/stdout.log

Whitespace-only changes.

tests/testsuite/new.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,17 @@ Caused by:
529529
)
530530
.run();
531531
}
532+
533+
#[cfg(unix)]
534+
#[cargo_test]
535+
fn path_with_invalid_character() {
536+
cargo_process("new --name testing test:ing")
537+
.with_stderr(
538+
"\
539+
[WARNING] the path `[CWD]/test:ing` contains invalid PATH characters (usually `:`, `;`, or `\"`)
540+
It is recommended to use a different name to avoid problems.
541+
[CREATED] binary (application) `testing` package
542+
",
543+
)
544+
.run();
545+
}

0 commit comments

Comments
 (0)