fix: avoid pass encoded empty string to goose run --recipe (#3361)

This commit is contained in:
Lifei Zhou
2025-07-15 13:14:36 +10:00
committed by GitHub
parent 026988d975
commit 515b20e361
4 changed files with 24 additions and 4 deletions

View File

@@ -47,7 +47,7 @@ pub fn extract_recipe_info_from_cli(
}
Ok((
InputConfig {
contents: recipe.prompt,
contents: recipe.prompt.filter(|s| !s.trim().is_empty()),
extensions_override: recipe.extensions,
additional_system_prompt: recipe.instructions,
},

View File

@@ -37,7 +37,6 @@ pub fn load_recipe_content_as_template(
missing_parameters_command_line(missing_params)
));
}
render_recipe_content_with_params(&recipe_file_content, &params_for_template)
}

View File

@@ -117,7 +117,6 @@ fn read_recipe_file<P: AsRef<Path>>(recipe_path: P) -> Result<RecipeFile> {
let content = fs::read_to_string(&path)
.map_err(|e| anyhow!("Failed to read recipe file {}: {}", path.display(), e))?;
let canonical = path.canonicalize().map_err(|e| {
anyhow!(
"Failed to resolve absolute path for {}: {}",

View File

@@ -6,6 +6,7 @@ use std::{
use anyhow::Result;
use goose::recipe::Recipe;
use minijinja::{Environment, UndefinedBehavior};
use regex::Regex;
use crate::recipes::recipe::BUILT_IN_RECIPE_DIR_PARAM;
@@ -15,8 +16,13 @@ pub fn render_recipe_content_with_params(
content: &str,
params: &HashMap<String, String>,
) -> Result<String> {
// Pre-process content to replace empty double quotes with single quotes
// This prevents MiniJinja from escaping "" to "\"\"" which would break YAML parsing
let re = Regex::new(r#":\s*"""#).unwrap();
let processed_content = re.replace_all(content, ": ''");
let env = add_template_in_env(
content,
&processed_content,
params.get(BUILT_IN_RECIPE_DIR_PARAM).unwrap().clone(),
UndefinedBehavior::Strict,
)?;
@@ -168,5 +174,21 @@ mod tests {
let err = render_recipe_content_with_params(content, &params).unwrap_err();
assert!(err.to_string().contains("unexpected end of input"));
}
#[test]
fn test_empty_prompt() {
let content = r#"
prompt: ""
name: "Simple Recipe"
description: "A test recipe"
"#;
let params = HashMap::from([("recipe_dir".to_string(), "test_dir".to_string())]);
let result = render_recipe_content_with_params(content, &params).unwrap();
assert!(result.contains("prompt: ''"));
assert!(!result.contains(r#"prompt: "\"\"""#)); // Should not contain escaped quotes
assert!(result.contains(r#"name: "Simple Recipe""#));
}
}
}