From 6b35bbeab9f33ad3ea4e2b277f8a80fedc67442d Mon Sep 17 00:00:00 2001 From: Chris Olszewski Date: Tue, 5 Mar 2024 17:12:32 -0800 Subject: [PATCH] fix: reject empty cache dir path (#7630) ### Description As mentioned in #6294 we don't want to allow for an empty cache dir to be set. This PR now errors if the user passes an empty string as an argument for `--cache-dir` ### Testing Instructions Added unit test. Quick manual test: ``` [0 olszewski@chriss-mbp] /Users/olszewski/code/vercel/turborepo $ turbo_dev build --cache-dir= ERROR invalid value '' for '--cache-dir ': path must not be empty For more information, try '--help'. ``` Closes TURBO-2534 --- crates/turborepo-lib/src/cli/mod.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/turborepo-lib/src/cli/mod.rs b/crates/turborepo-lib/src/cli/mod.rs index 5b654ae13561d..40a773d352738 100644 --- a/crates/turborepo-lib/src/cli/mod.rs +++ b/crates/turborepo-lib/src/cli/mod.rs @@ -605,6 +605,14 @@ fn validate_graph_extension(s: &str) -> Result { } } +fn path_non_empty(s: &str) -> Result { + if s.is_empty() { + Err("path must not be empty".to_string()) + } else { + Ok(Utf8Path::new(s).to_path_buf()) + } +} + #[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)] #[command(groups = [ ArgGroup::new("daemon-group").multiple(false).required(false), @@ -612,7 +620,7 @@ fn validate_graph_extension(s: &str) -> Result { ])] pub struct RunArgs { /// Override the filesystem cache directory. - #[clap(long)] + #[clap(long, value_parser = path_non_empty)] pub cache_dir: Option, /// Set the number of concurrent cache operations (default 10) #[clap(long, default_value_t = DEFAULT_NUM_WORKERS)] @@ -2344,4 +2352,11 @@ mod test { ]) .is_err()); } + + #[test] + fn test_empty_cache_dir() { + assert!(Args::try_parse_from(["turbo", "build", "--cache-dir"]).is_err()); + assert!(Args::try_parse_from(["turbo", "build", "--cache-dir="]).is_err()); + assert!(Args::try_parse_from(["turbo", "build", "--cache-dir", ""]).is_err()); + } }