Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(turbo): validate graph extension #6995

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub const INVOCATION_DIR_ENV_VAR: &str = "TURBO_INVOCATION_DIR";

// Default value for the --cache-workers argument
const DEFAULT_NUM_WORKERS: u32 = 10;
const SUPPORTED_GRAPH_FILE_EXTENSIONS: [&str; 8] =
["svg", "png", "jpg", "pdf", "json", "html", "mermaid", "dot"];

#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize, ValueEnum)]
pub enum OutputLogsMode {
Expand Down Expand Up @@ -580,6 +582,23 @@ pub enum GenerateCommand {
Run(GeneratorCustomArgs),
}

fn validate_graph_extension(s: &str) -> Result<String, String> {
match s.is_empty() {
true => Ok(s.to_string()),
_ => match Utf8Path::new(s).extension() {
Some(ext) if SUPPORTED_GRAPH_FILE_EXTENSIONS.contains(&ext) => Ok(s.to_string()),
Some(ext) => Err(format!(
"Invalid file extension: '{}'. Allowed extensions are: {:?}",
ext, SUPPORTED_GRAPH_FILE_EXTENSIONS
)),
None => Err(format!(
"The provided filename is missing a file extension. Allowed extensions are: {:?}",
SUPPORTED_GRAPH_FILE_EXTENSIONS
)),
},
}
}

#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
#[command(groups = [
ArgGroup::new("daemon-group").multiple(false).required(false),
Expand Down Expand Up @@ -621,8 +640,9 @@ pub struct RunArgs {
pub global_deps: Vec<String>,
/// Generate a graph of the task execution and output to a file when a
/// filename is specified (.svg, .png, .jpg, .pdf, .json,
/// .html). Outputs dot graph to stdout when if no filename is provided
#[clap(long, num_args = 0..=1, default_missing_value = "")]
/// .html, .mermaid, .dot). Outputs dot graph to stdout when if no filename
/// is provided
#[clap(long, num_args = 0..=1, default_missing_value = "", value_parser = validate_graph_extension)]
pub graph: Option<String>,
/// Environment variable mode.
/// Use "loose" to pass the entire existing environment.
Expand Down Expand Up @@ -823,7 +843,9 @@ impl RunArgs {
}

if let Some(graph) = &self.graph {
telemetry.track_arg_value("graph", graph, EventType::NonSensitive);
// track the extension used only
let extension = Utf8Path::new(graph).extension().unwrap_or("stdout");
telemetry.track_arg_value("graph", extension, EventType::NonSensitive);
}

if self.env_mode != EnvMode::default() {
Expand Down
8 changes: 8 additions & 0 deletions turborepo-tests/integration/tests/run/graph.t
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ Graph to stdout
graph TD
\\t[A-Z]{4}\("my-app#build"\) --> [A-Z]{4}\("util#build"\).* (re)
\\t[A-Z]{4}\("util#build"\) --> [A-Z]{4}\("___ROOT___"\).* (re)

$ ${TURBO} build -F my-app --graph=graph.mdx
ERROR invalid value 'graph.mdx' for '--graph [<GRAPH>]': Invalid file extension: 'mdx'. Allowed extensions are: ["svg", "png", "jpg", "pdf", "json", "html", "mermaid", "dot"]

For more information, try '--help'.

[1]