use std::collections::BTreeSet;
use std::path::Path;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Task {
pub label: String,
pub command: String,
pub source: String,
pub is_build: bool,
pub is_default: bool,
pub problem_matcher: Option<serde_json::Value>,
pub vscode: Option<VscodeCommand>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VscodeCommand {
pub command: String,
pub args: Vec<String>,
pub cwd: Option<String>,
pub env: Vec<(String, String)>,
}
impl Task {
pub fn command_line(&self, ctx: &crate::dap::configs::SubstCtx) -> Result<String, String> {
let Some(v) = &self.vscode else {
return Ok(self.command.clone());
};
let mut line = expand_task_text(&v.command, ctx, false)?;
for raw in &v.args {
let literal_space =
raw.is_empty() || literal_parts(raw).any(|l| l.contains(char::is_whitespace));
line.push(' ');
let arg = expand_task_text(raw, ctx, literal_space)?;
line.push_str(if arg.is_empty() { "''" } else { &arg });
}
let mut setup = Vec::new();
if let Some(cwd) = &v.cwd {
setup.push(format!("cd {}", expand_task_text(cwd, ctx, true)?));
}
for (key, value) in &v.env {
setup.push(format!(
"export {key}={}",
expand_task_text(value, ctx, true)?
));
}
if setup.is_empty() {
return Ok(line);
}
Ok(format!("({} && {line})", setup.join(" && ")))
}
}
fn expand_task_text(
raw: &str,
ctx: &crate::dap::configs::SubstCtx,
quote_literals: bool,
) -> Result<String, String> {
let literal = |out: &mut String, text: &str| {
if quote_literals && !text.is_empty() {
out.push_str("e_word(text));
} else {
out.push_str(text);
}
};
let mut out = String::new();
let mut rest = raw;
while let Some(start) = rest.find("${") {
let Some(len) = rest[start + 2..].find('}') else {
break;
};
literal(&mut out, &rest[..start]);
let var = &rest[start + 2..start + 2 + len];
match crate::dap::configs::substitute(&format!("${{{var}}}"), ctx) {
Ok(value) if needs_quote(&value) || (quote_literals && value.is_empty()) => {
out.push_str("e_word(&value));
}
Ok(value) => out.push_str(&value),
Err(e) if e.contains("no active file") => return Err(e),
Err(_) if var.starts_with("input:") || var.starts_with("command:") => {
return Err(format!("${{{var}}}: croft cannot prompt for task inputs"));
}
Err(_) if var.starts_with("env:") => {}
Err(_) => {
out.push_str("${");
out.push_str(var);
out.push('}');
}
}
rest = &rest[start + 2 + len + 1..];
}
literal(&mut out, rest);
if quote_literals && out.is_empty() {
out.push_str("''");
}
Ok(out)
}
fn literal_parts(raw: &str) -> impl Iterator<Item = &str> {
let mut rest = Some(raw);
std::iter::from_fn(move || {
let r = rest?;
match r
.find("${")
.and_then(|s| r[s + 2..].find('}').map(|l| (s, l)))
{
Some((s, l)) => {
rest = Some(&r[s + 2 + l + 1..]);
Some(&r[..s])
}
None => {
rest = None;
Some(r)
}
}
})
}
fn needs_quote(s: &str) -> bool {
!s.chars()
.all(|c| c.is_ascii_alphanumeric() || "/._-+=:,@%".contains(c))
}
fn quote_word(s: &str) -> String {
format!("'{}'", s.replace('\'', "'\\''"))
}
pub fn discover_tasks(root: &Path) -> Vec<Task> {
let mut out = Vec::new();
out.extend(vscode_tasks(root));
out.extend(makefile_tasks(root));
out.extend(justfile_tasks(root));
out.extend(package_json_tasks(root));
out.extend(cargo_tasks(root));
out.extend(pyproject_tasks(root));
dedup(out)
}
pub fn default_build_task(tasks: &[Task]) -> Option<&Task> {
tasks
.iter()
.find(|t| t.is_build && t.is_default)
.or_else(|| tasks.iter().find(|t| t.is_build))
}
fn read_first(root: &Path, names: &[&str]) -> Option<String> {
names
.iter()
.find_map(|n| std::fs::read_to_string(root.join(n)).ok())
}
fn vscode_tasks(root: &Path) -> Vec<Task> {
let Some(text) = read_first(root, &[".vscode/tasks.json"]) else {
return Vec::new();
};
let Ok(v) = serde_json::from_str::<serde_json::Value>(&strip_jsonc(&text)) else {
return Vec::new();
};
let Some(tasks) = v.get("tasks").and_then(|t| t.as_array()) else {
return Vec::new();
};
tasks
.iter()
.filter_map(|t| {
let command = t.get("command")?.as_str()?;
let args: Vec<String> = t
.get("args")
.and_then(|a| a.as_array())
.map(|a| {
a.iter()
.filter_map(|a| a.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default();
let options = t.get("options");
let vscode = VscodeCommand {
command: command.to_string(),
args: args.clone(),
cwd: options
.and_then(|o| o.get("cwd"))
.and_then(|c| c.as_str())
.map(str::to_string),
env: options
.and_then(|o| o.get("env"))
.and_then(|e| e.as_object())
.map(|e| {
e.iter()
.filter(|(k, _)| {
!k.is_empty()
&& k.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
&& !k.starts_with(|c: char| c.is_ascii_digit())
})
.filter_map(|(k, v)| Some((k.clone(), v.as_str()?.to_string())))
.collect()
})
.unwrap_or_default(),
};
let mut line = command.to_string();
for a in &args {
line.push(' ');
line.push_str(a);
}
let label = t
.get("label")
.and_then(|l| l.as_str())
.unwrap_or(&line)
.to_string();
let is_build = match t.get("group") {
Some(serde_json::Value::String(s)) => s == "build",
Some(g) => g.get("kind").and_then(|k| k.as_str()) == Some("build"),
None => false,
};
let is_default = t
.get("group")
.and_then(|g| g.get("isDefault"))
.and_then(|d| d.as_bool())
.unwrap_or(false);
Some(Task {
label,
command: line,
source: "tasks.json".to_string(),
is_build,
is_default,
problem_matcher: t.get("problemMatcher").cloned(),
vscode: Some(vscode),
})
})
.collect()
}
fn makefile_tasks(root: &Path) -> Vec<Task> {
let Some(text) = read_first(root, &["Makefile", "makefile", "GNUmakefile"]) else {
return Vec::new();
};
let mut out = Vec::new();
let mut seen = BTreeSet::new();
for line in text.lines() {
if line.starts_with(['\t', ' ', '#']) {
continue;
}
let Some((head, rest)) = line.split_once(':') else {
continue;
};
if rest.starts_with('=') {
continue;
}
let name = head.trim();
if name.is_empty()
|| name.starts_with('.')
|| name.contains(['%', '$', '=', ' ', '\t'])
|| !seen.insert(name.to_string())
{
continue;
}
out.push(Task {
label: format!("make {name}"),
command: format!("make {name}"),
source: "Makefile".to_string(),
is_build: name == "build" || name == "all",
is_default: false,
problem_matcher: None,
vscode: None,
});
}
out
}
fn justfile_tasks(root: &Path) -> Vec<Task> {
let Some(text) = read_first(root, &["justfile", "Justfile", ".justfile"]) else {
return Vec::new();
};
let mut out = Vec::new();
for line in text.lines() {
if line.starts_with([' ', '\t', '#', '@']) || line.starts_with("set ") {
continue;
}
let Some((head, rest)) = line.split_once(':') else {
continue;
};
if rest.starts_with('=') {
continue;
}
let name = head.split_whitespace().next().unwrap_or("");
if name.is_empty()
|| !name
.chars()
.all(|c| c.is_alphanumeric() || "_-".contains(c))
{
continue;
}
out.push(Task {
label: format!("just {name}"),
command: format!("just {name}"),
source: "justfile".to_string(),
is_build: name == "build",
is_default: false,
problem_matcher: None,
vscode: None,
});
}
out
}
fn package_json_tasks(root: &Path) -> Vec<Task> {
let Some(text) = read_first(root, &["package.json"]) else {
return Vec::new();
};
let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) else {
return Vec::new();
};
let Some(scripts) = v.get("scripts").and_then(|s| s.as_object()) else {
return Vec::new();
};
let runner = if root.join("bun.lockb").exists() || root.join("bun.lock").exists() {
"bun run"
} else if root.join("pnpm-lock.yaml").exists() {
"pnpm run"
} else if root.join("yarn.lock").exists() {
"yarn"
} else {
"npm run"
};
scripts
.keys()
.map(|name| Task {
label: format!("{runner} {name}"),
command: format!("{runner} {name}"),
source: "package.json".to_string(),
is_build: name == "build",
is_default: false,
problem_matcher: None,
vscode: None,
})
.collect()
}
fn cargo_tasks(root: &Path) -> Vec<Task> {
if !root.join("Cargo.toml").is_file() {
return Vec::new();
}
[
("cargo build", true),
("cargo test", false),
("cargo clippy", false),
("cargo run", false),
]
.into_iter()
.map(|(cmd, is_build)| Task {
label: cmd.to_string(),
command: cmd.to_string(),
source: "Cargo.toml".to_string(),
is_build,
is_default: false,
problem_matcher: None,
vscode: None,
})
.collect()
}
fn pyproject_tasks(root: &Path) -> Vec<Task> {
let Some(text) = read_first(root, &["pyproject.toml"]) else {
return Vec::new();
};
let mut out = Vec::new();
if let Ok(v) = text.parse::<toml::Table>()
&& let Some(scripts) = v
.get("project")
.and_then(|p| p.get("scripts"))
.and_then(|s| s.as_table())
{
for name in scripts.keys() {
out.push(Task {
label: format!("uv run {name}"),
command: format!("uv run {name}"),
source: "pyproject.toml".to_string(),
is_build: false,
is_default: false,
problem_matcher: None,
vscode: None,
});
}
}
if text.contains("pytest") {
out.push(Task {
label: "uv run pytest".to_string(),
command: "uv run pytest".to_string(),
source: "pyproject.toml".to_string(),
is_build: false,
is_default: false,
problem_matcher: None,
vscode: None,
});
}
out
}
pub(crate) fn strip_jsonc(text: &str) -> String {
let mut no_comments = String::with_capacity(text.len());
let mut chars = text.chars().peekable();
let mut in_string = false;
while let Some(c) = chars.next() {
if in_string {
no_comments.push(c);
match c {
'\\' => {
if let Some(next) = chars.next() {
no_comments.push(next);
}
}
'"' => in_string = false,
_ => {}
}
continue;
}
match c {
'"' => {
in_string = true;
no_comments.push(c);
}
'/' if chars.peek() == Some(&'/') => {
for n in chars.by_ref() {
if n == '\n' {
no_comments.push('\n');
break;
}
}
}
'/' if chars.peek() == Some(&'*') => {
chars.next();
let mut prev = ' ';
for n in chars.by_ref() {
if prev == '*' && n == '/' {
break;
}
prev = n;
}
}
_ => no_comments.push(c),
}
}
let mut out = String::with_capacity(no_comments.len());
let mut chars = no_comments.chars().peekable();
let mut in_string = false;
while let Some(c) = chars.next() {
if in_string {
out.push(c);
match c {
'\\' => {
if let Some(next) = chars.next() {
out.push(next);
}
}
'"' => in_string = false,
_ => {}
}
continue;
}
match c {
'"' => {
in_string = true;
out.push(c);
}
',' => {
let closes = chars
.clone()
.find(|n| !n.is_whitespace())
.is_some_and(|n| matches!(n, '}' | ']'));
if !closes {
out.push(',');
}
}
_ => out.push(c),
}
}
out
}
fn dedup(tasks: Vec<Task>) -> Vec<Task> {
let mut seen = BTreeSet::new();
tasks
.into_iter()
.filter(|t| seen.insert((t.label.clone(), t.command.clone())))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
fn commands(root: &Path) -> Vec<String> {
discover_tasks(root)
.into_iter()
.map(|t| t.command)
.collect()
}
fn shell_words(line: &str, program: &str) -> String {
let probe = line.replacen(program, "printf '[%s]'", 1);
let out = std::process::Command::new("sh")
.args(["-c", &probe])
.output()
.unwrap();
String::from_utf8(out.stdout).unwrap()
}
#[test]
fn a_tasks_json_line_expands_variables_quotes_args_and_applies_options() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{"tasks": [
{"label": "build", "command": "${workspaceFolder}/scripts/build.sh",
"args": ["-m", "fix bug", "${file}"],
"options": {"cwd": "${workspaceFolder}/web", "env": {"MODE": "it's"}}},
{"label": "line", "command": "python3 ${file} ${env:CROFT_TEST_UNSET_VAR} ${HOME}"},
{"label": "plain", "command": "npm run lint"},
{"label": "empty", "command": "prog", "args": ["--name", "${env:CROFT_TEST_UNSET_VAR}", "f"]},
{"label": "input", "command": "prog ${input:target}"}
]}"#,
)
.unwrap();
let tasks = discover_tasks(tmp.path());
let task = |label: &str| tasks.iter().find(|t| t.label == label).unwrap();
let dir = tempfile::tempdir().unwrap();
let ws = dir.path().join("it's work");
std::fs::create_dir_all(ws.join("web")).unwrap();
let ctx = crate::dap::configs::SubstCtx {
workspace_folder: ws.clone(),
file: Some(ws.join("a;touch${IFS}pwned.rs")),
};
let build = task("build").command_line(&ctx).unwrap();
assert!(
build.starts_with("(cd ") && build.contains("export MODE="),
"{build}"
);
let program = build.rsplit(" && ").next().unwrap().trim_end_matches(')');
let words = shell_words(program, program.split(" -m").next().unwrap());
assert_eq!(
words,
format!(
"[-m][fix bug][{}]",
ws.join("a;touch${IFS}pwned.rs").display()
)
);
let line = task("line").command_line(&ctx).unwrap();
assert!(line.contains("${HOME}"), "{line}");
let words = shell_words(&line, "python3");
let home = std::env::var("HOME").unwrap_or_default();
assert_eq!(
words,
format!("[{}][{home}]", ws.join("a;touch${IFS}pwned.rs").display())
);
assert!(!std::path::Path::new("pwned.rs").exists());
assert_eq!(task("plain").command_line(&ctx).unwrap(), "npm run lint");
assert_eq!(
task("empty").command_line(&ctx).unwrap(),
"prog --name '' f"
);
assert!(
task("input")
.command_line(&ctx)
.unwrap_err()
.contains("cannot prompt")
);
let no_file = crate::dap::configs::SubstCtx {
workspace_folder: PathBuf::from("/w"),
file: None,
};
assert!(
task("build").command_line(&no_file).is_err(),
"${{file}} with no file"
);
}
#[test]
fn discovers_makefile_targets_and_skips_machinery() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("Makefile"),
"CC := gcc\n\
.PHONY: build test\n\
build: src/main.c\n\
\tgcc -o app src/main.c\n\
test:\n\
\t./run-tests\n\
%.o: %.c\n\
\t$(CC) -c $<\n",
)
.unwrap();
let cmds = commands(tmp.path());
assert!(cmds.contains(&"make build".to_string()), "{cmds:?}");
assert!(cmds.contains(&"make test".to_string()), "{cmds:?}");
assert!(
!cmds
.iter()
.any(|c| c.contains('%') || c.contains(".PHONY") || c.contains("CC")),
"pattern rules, special targets, and variables are not tasks: {cmds:?}"
);
let tasks = discover_tasks(tmp.path());
let build = tasks.iter().find(|t| t.command == "make build").unwrap();
assert!(build.is_build, "a target named build is the build task");
}
#[test]
fn discovers_justfile_recipes() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("justfile"),
"set shell := [\"bash\", \"-c\"]\n\
version := \"1.0\"\n\
# deploy everything\n\
deploy target=\"prod\":\n\
\techo deploying\n\
lint:\n\
\tcargo clippy\n",
)
.unwrap();
let cmds = commands(tmp.path());
assert!(cmds.contains(&"just deploy".to_string()), "{cmds:?}");
assert!(cmds.contains(&"just lint".to_string()), "{cmds:?}");
assert!(
!cmds
.iter()
.any(|c| c.contains("version") || c.contains("set ")),
"assignments and settings are not recipes: {cmds:?}"
);
}
#[test]
fn package_json_scripts_use_the_lockfiles_runner() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("package.json"),
r#"{"name":"x","scripts":{"build":"tsc","dev":"vite"}}"#,
)
.unwrap();
std::fs::write(tmp.path().join("pnpm-lock.yaml"), "").unwrap();
let cmds = commands(tmp.path());
assert!(cmds.contains(&"pnpm run build".to_string()), "{cmds:?}");
assert!(cmds.contains(&"pnpm run dev".to_string()), "{cmds:?}");
let tasks = discover_tasks(tmp.path());
assert!(
tasks
.iter()
.any(|t| t.command == "pnpm run build" && t.is_build),
"the build script is the build task"
);
}
#[test]
fn tasks_json_problem_matcher_rides_along_raw() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "tasks": [
{ "label": "cc", "command": "make", "problemMatcher": "$gcc" },
{ "label": "plain", "command": "true" }
] }"#,
)
.unwrap();
let tasks = discover_tasks(tmp.path());
let cc = tasks.iter().find(|t| t.label == "cc").unwrap();
assert_eq!(
cc.problem_matcher,
Some(serde_json::json!("$gcc")),
"the raw problemMatcher value travels with the task"
);
let plain = tasks.iter().find(|t| t.label == "plain").unwrap();
assert_eq!(plain.problem_matcher, None);
}
#[test]
fn cargo_projects_get_the_standard_verbs() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("Cargo.toml"),
"[package]\nname = \"x\"\nversion = \"0.1.0\"\n",
)
.unwrap();
let tasks = discover_tasks(tmp.path());
let cmds: Vec<&str> = tasks.iter().map(|t| t.command.as_str()).collect();
assert!(cmds.contains(&"cargo build"), "{cmds:?}");
assert!(cmds.contains(&"cargo test"), "{cmds:?}");
assert!(
tasks
.iter()
.any(|t| t.command == "cargo build" && t.is_build),
"cargo build is the build task"
);
}
#[test]
fn a_relabelled_command_keeps_the_convention_label_findable() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("Cargo.toml"),
"[package]\nname = \"x\"\nversion = \"0.1.0\"\n",
)
.unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "tasks": [ { "label": "Build Project", "type": "shell", "command": "cargo build" } ] }"#,
)
.unwrap();
let tasks = discover_tasks(tmp.path());
let labels: Vec<&str> = tasks.iter().map(|t| t.label.as_str()).collect();
assert!(labels.contains(&"Build Project"), "{labels:?}");
assert!(
labels.contains(&"cargo build"),
"the Cargo label must survive a tasks.json entry with the same command: {labels:?}"
);
}
#[test]
fn label_less_entries_sharing_a_program_stay_distinct() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "tasks": [
{ "type": "shell", "command": "npm", "args": ["run", "build"] },
{ "type": "shell", "command": "npm", "args": ["run", "test"] },
{ "type": "shell", "command": "npm", "args": ["run", "lint"] }
] }"#,
)
.unwrap();
let tasks = discover_tasks(tmp.path());
let pairs: Vec<(&str, &str)> = tasks
.iter()
.map(|t| (t.label.as_str(), t.command.as_str()))
.collect();
for cmd in ["npm run build", "npm run test", "npm run lint"] {
assert!(
pairs.contains(&(cmd, cmd)),
"{cmd} must survive as its own task, labelled by its line: {pairs:?}"
);
}
}
#[test]
fn only_a_repeated_label_and_command_pair_collapses() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("Makefile"), "build:\n\ttrue\n").unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "tasks": [
{ "label": "make build", "type": "shell", "command": "make build" },
{ "label": "Build", "type": "shell", "command": "make build" }
] }"#,
)
.unwrap();
let tasks = discover_tasks(tmp.path());
let rows: Vec<(&str, &str, &str)> = tasks
.iter()
.map(|t| (t.label.as_str(), t.command.as_str(), t.source.as_str()))
.collect();
let repeats: Vec<_> = rows
.iter()
.filter(|r| (r.0, r.1) == ("make build", "make build"))
.collect();
assert_eq!(repeats.len(), 1, "the exact repeat folds to one: {rows:?}");
assert_eq!(
repeats[0].2, "tasks.json",
"and the first source wins over the Makefile's"
);
assert!(
rows.iter().any(|r| (r.0, r.1) == ("Build", "make build")),
"a relabelled command keeps its own entry: {rows:?}"
);
}
#[test]
fn a_label_reused_for_a_different_command_keeps_both_and_the_lookup_takes_the_first() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{ "tasks": [
{ "label": "Build", "type": "shell", "command": "make a" },
{ "label": "Build", "type": "shell", "command": "make b" }
] }"#,
)
.unwrap();
let tasks = discover_tasks(tmp.path());
let builds: Vec<&str> = tasks
.iter()
.filter(|t| t.label == "Build")
.map(|t| t.command.as_str())
.collect();
assert_eq!(
builds,
["make a", "make b"],
"both survive, declaration order kept"
);
assert_eq!(
tasks
.iter()
.find(|t| t.label == "Build")
.map(|t| t.command.as_str()),
Some("make a"),
"a lookup by label resolves to the first declared"
);
}
#[test]
fn pyproject_scripts_run_through_uv() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("pyproject.toml"),
"[project]\nname = \"x\"\nversion = \"0\"\ndependencies = [\"pytest\"]\n\n[project.scripts]\nserve = \"x.main:run\"\n",
)
.unwrap();
let cmds = commands(tmp.path());
assert!(cmds.contains(&"uv run serve".to_string()), "{cmds:?}");
assert!(cmds.contains(&"uv run pytest".to_string()), "{cmds:?}");
}
#[test]
fn an_explicit_vscode_default_build_wins_over_an_earlier_build_task() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{"tasks":[
{"label":"build A","type":"shell","command":"make a","group":"build"},
{"label":"build B","type":"shell","command":"make b","group":{"kind":"build","isDefault":true}}
]}"#,
)
.unwrap();
let tasks = discover_tasks(tmp.path());
assert_eq!(
default_build_task(&tasks).unwrap().label,
"build B",
"the explicit default build outranks the first build-group task"
);
}
#[test]
fn vscode_tasks_json_with_comments_is_honoured_and_wins_the_build_slot() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("Makefile"), "build:\n\ttrue\n").unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/tasks.json"),
r#"{
// VS Code allows comments here
"version": "2.0.0",
"tasks": [
{
"label": "full build",
"type": "shell",
"command": "nix",
"args": ["build"],
"group": { "kind": "build", "isDefault": true }, /* default */
},
],
}"#,
)
.unwrap();
let tasks = discover_tasks(tmp.path());
let t = tasks.iter().find(|t| t.label == "full build").unwrap();
assert_eq!(t.command, "nix build", "command joins args");
assert!(t.is_build);
assert_eq!(
default_build_task(&tasks).unwrap().label,
"full build",
"the explicit VS Code default build outranks the Makefile target"
);
}
#[test]
fn strip_jsonc_leaves_strings_intact() {
let cleaned = strip_jsonc(
r#"{"a": "http://x // not a comment", // real
"b": 1, /* gone */ }"#,
);
let v: serde_json::Value = serde_json::from_str(&cleaned).expect("valid strict JSON");
assert_eq!(v["a"], "http://x // not a comment");
assert_eq!(v["b"], 1);
}
#[test]
fn strip_jsonc_preserves_non_ascii() {
let cleaned = strip_jsonc(
"{\n // a comment, so the comment path actually runs\n \"caf\u{e9}\": \"\u{4e2d}\u{6587} \u{444}\u{430}\u{439}\u{43b}\",\n}",
);
let v: serde_json::Value = serde_json::from_str(&cleaned).expect("valid strict JSON");
assert_eq!(
v["caf\u{e9}"],
"\u{4e2d}\u{6587} \u{444}\u{430}\u{439}\u{43b}"
);
}
#[test]
fn empty_workspace_has_no_tasks() {
let tmp = tempfile::tempdir().unwrap();
assert!(discover_tasks(tmp.path()).is_empty());
}
}