use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use serde_json::{Map, Value, json};
use crate::dap::session::AdapterKind;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequestKind {
Launch,
Attach,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DebugConfig {
pub name: String,
pub type_name: String,
pub request: RequestKind,
pub source: &'static str,
pub presentation: Option<Presentation>,
pub inputs: Vec<LaunchInput>,
pub answers: BTreeMap<String, String>,
pub root: Option<PathBuf>,
raw: Map<String, Value>,
}
impl DebugConfig {
pub fn pre_launch_task(&self) -> Option<&str> {
self.raw.get("preLaunchTask").and_then(Value::as_str)
}
pub fn input_refs(&self) -> Vec<String> {
let mut out = Vec::new();
value_input_refs(&Value::Object(self.raw.clone()), &mut out);
out
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LaunchInput {
pub id: String,
pub description: String,
pub default: Option<String>,
pub kind: InputKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InputKind {
Prompt { password: bool },
Pick { options: Vec<(String, String)> },
Command { command: String },
}
pub fn parse_inputs(v: &Value) -> Vec<LaunchInput> {
let Some(list) = v.get("inputs").and_then(Value::as_array) else {
return Vec::new();
};
list.iter()
.filter_map(|i| {
let id = i.get("id")?.as_str()?.to_string();
let text = |key: &str| i.get(key).and_then(Value::as_str).map(str::to_string);
let kind = match i.get("type")?.as_str()? {
"promptString" => InputKind::Prompt {
password: i.get("password").and_then(Value::as_bool) == Some(true),
},
"pickString" => {
let options: Vec<(String, String)> = i
.get("options")?
.as_array()?
.iter()
.filter_map(|o| match o {
Value::String(s) => Some((s.clone(), s.clone())),
Value::Object(o) => {
let value = o.get("value")?.as_str()?.to_string();
let label = o
.get("label")
.and_then(Value::as_str)
.map_or_else(|| value.clone(), str::to_string);
Some((label, value))
}
_ => None,
})
.collect();
if options.is_empty() {
return None;
}
InputKind::Pick { options }
}
"command" => InputKind::Command {
command: text("command")?,
},
_ => return None,
};
Some(LaunchInput {
description: text("description").unwrap_or_else(|| id.clone()),
default: text("default"),
id,
kind,
})
})
.collect()
}
pub fn inputs_to_ask(
refs: &[String],
answers: &BTreeMap<String, String>,
defs: &[LaunchInput],
) -> Result<Vec<LaunchInput>, String> {
refs.iter()
.filter(|id| !answers.contains_key(*id))
.map(|id| {
defs.iter()
.find(|d| d.id == *id)
.cloned()
.ok_or_else(|| format!("${{input:{id}}}: no \"inputs\" entry has the id \"{id}\""))
})
.collect()
}
pub fn text_input_refs(text: &str, out: &mut Vec<String>) {
let mut rest = text;
while let Some(start) = rest.find("${input:") {
let after = &rest[start + "${input:".len()..];
let Some(end) = after.find('}') else {
return;
};
let id = after[..end].trim();
if !id.is_empty() && !out.iter().any(|o| o == id) {
out.push(id.to_string());
}
rest = &after[end + 1..];
}
}
fn value_input_refs(v: &Value, out: &mut Vec<String>) {
match v {
Value::String(s) => text_input_refs(s, out),
Value::Array(a) => a.iter().for_each(|x| value_input_refs(x, out)),
Value::Object(o) => o.values().for_each(|x| value_input_refs(x, out)),
_ => {}
}
}
pub fn discover_compounds(root: &Path) -> Vec<Compound> {
let mut out: Vec<Compound> = Vec::new();
for rel in [".croft/launch.json", ".vscode/launch.json"] {
let Ok(text) = std::fs::read_to_string(root.join(rel)) else {
continue;
};
for c in parse_compounds(&text, rel) {
if !out.iter().any(|existing| existing.name == c.name) {
out.push(c);
}
}
}
out
}
pub fn discover_configs_all(root: &Path) -> Vec<DebugConfig> {
let mut out: Vec<DebugConfig> = Vec::new();
for (rel, source) in [
(".croft/launch.json", ".croft/launch.json"),
(".vscode/launch.json", ".vscode/launch.json"),
] {
let Ok(text) = std::fs::read_to_string(root.join(rel)) else {
continue;
};
out.extend(parse_launch_json(&text, source));
}
out
}
pub fn discover_configs(root: &Path) -> Vec<DebugConfig> {
let mut out: Vec<DebugConfig> = Vec::new();
for (rel, source) in [
(".croft/launch.json", ".croft/launch.json"),
(".vscode/launch.json", ".vscode/launch.json"),
] {
let Ok(text) = std::fs::read_to_string(root.join(rel)) else {
continue;
};
for cfg in parse_launch_json(&text, source) {
if !out.iter().any(|c| c.name == cfg.name) {
out.push(cfg);
}
}
}
out
}
pub fn parse_launch_json(text: &str, source: &'static str) -> Vec<DebugConfig> {
let Ok(v) = serde_json::from_str::<Value>(&crate::jsonc::strip_jsonc(text)) else {
return Vec::new();
};
let list = match (&v, v.get("configurations")) {
(_, Some(Value::Array(a))) => a.clone(),
(Value::Array(a), _) => a.clone(),
_ => return Vec::new(),
};
let inputs = parse_inputs(&v);
list.into_iter()
.filter_map(|c| {
let obj = c.as_object()?;
let name = obj.get("name")?.as_str()?.to_string();
let type_name = obj.get("type")?.as_str()?.to_string();
let request = match obj.get("request").and_then(Value::as_str) {
Some("attach") => RequestKind::Attach,
_ => RequestKind::Launch,
};
Some(DebugConfig {
name,
type_name,
request,
source,
presentation: parse_presentation(obj),
inputs: inputs.clone(),
answers: BTreeMap::new(),
root: None,
raw: obj.clone(),
})
})
.collect()
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Presentation {
pub hidden: bool,
pub group: Option<String>,
pub order: Option<f64>,
}
pub fn parse_presentation(obj: &Map<String, Value>) -> Option<Presentation> {
let p = obj.get("presentation")?.as_object()?;
let hidden = p.get("hidden").and_then(Value::as_bool).unwrap_or(false);
let group = p
.get("group")
.and_then(Value::as_str)
.filter(|g| !g.is_empty())
.map(str::to_string);
let order = p.get("order").and_then(Value::as_f64);
Some(Presentation {
hidden,
group,
order,
})
}
pub fn malformed_presentation_keys(obj: &Map<String, Value>) -> Vec<&'static str> {
let Some(value) = obj.get("presentation") else {
return Vec::new();
};
if value.is_null() || value == &Value::Bool(false) || value.as_str().is_some_and(str::is_empty)
{
return Vec::new();
}
let Some(p) = value.as_object() else {
return vec!["presentation"];
};
let mut out = Vec::new();
for (key, path, well_formed) in [
(
"hidden",
"presentation.hidden",
Value::is_boolean as fn(&Value) -> bool,
),
(
"group",
"presentation.group",
Value::is_string as fn(&Value) -> bool,
),
(
"order",
"presentation.order",
Value::is_number as fn(&Value) -> bool,
),
] {
if p.get(key).is_some_and(|v| !v.is_null() && !well_formed(v)) {
out.push(path);
}
}
out
}
pub fn visible_and_sorted<T>(
items: Vec<T>,
presentation: impl Fn(&T) -> Option<&Presentation>,
) -> Vec<T> {
let mut kept: Vec<T> = items
.into_iter()
.filter(|i| !presentation(i).is_some_and(|p| p.hidden))
.collect();
kept.sort_by(|a, b| {
use std::cmp::Ordering;
let (Some(first), Some(second)) = (presentation(a), presentation(b)) else {
return match (presentation(a), presentation(b)) {
(None, None) => Ordering::Equal,
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
_ => unreachable!("both-Some is handled by the outer let-else"),
};
};
match (&first.group, &second.group) {
(None, None) => compare_orders(first.order, second.order),
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(Some(g1), Some(g2)) if g1 != g2 => g1.cmp(g2),
_ => compare_orders(first.order, second.order),
}
});
kept
}
fn compare_orders(first: Option<f64>, second: Option<f64>) -> std::cmp::Ordering {
match (first, second) {
(None, None) => std::cmp::Ordering::Equal,
(None, Some(_)) => std::cmp::Ordering::Greater,
(Some(_), None) => std::cmp::Ordering::Less,
(Some(a), Some(b)) => a.total_cmp(&b),
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Compound {
pub name: String,
pub configurations: Vec<String>,
pub folders: Vec<Option<String>>,
pub pre_launch_task: Option<String>,
pub presentation: Option<Presentation>,
pub unsupported_keys: Vec<&'static str>,
pub source: &'static str,
pub stop_all: bool,
pub answers: BTreeMap<String, String>,
}
impl Compound {
pub fn hidden(&self) -> bool {
self.presentation.as_ref().is_some_and(|p| p.hidden)
}
}
impl DebugConfig {
pub fn hidden(&self) -> bool {
self.presentation.as_ref().is_some_and(|p| p.hidden)
}
}
pub fn parse_compounds(text: &str, source: &'static str) -> Vec<Compound> {
let Ok(v) = serde_json::from_str::<Value>(&crate::jsonc::strip_jsonc(text)) else {
return Vec::new();
};
let Some(Value::Array(list)) = v.get("compounds") else {
return Vec::new();
};
list.iter()
.filter_map(|c| {
let obj = c.as_object()?;
let name = obj.get("name")?.as_str()?.to_string();
let members: Vec<(String, Option<String>)> = obj
.get("configurations")?
.as_array()?
.iter()
.map(|m| match m {
Value::String(s) => Some((s.clone(), None)),
Value::Object(o) => {
let name = o.get("name")?.as_str()?.to_string();
let folder = match o.get("folder") {
None | Some(Value::Null) => None,
Some(f) => Some(f.as_str()?.to_string()),
};
Some((name, folder))
}
_ => None,
})
.collect::<Option<Vec<_>>>()?;
let (configurations, folders): (Vec<String>, Vec<Option<String>>) =
members.into_iter().unzip();
if configurations.is_empty() {
return None;
}
let asks_for_something = |k: &str| match obj.get(k) {
None | Some(Value::Null) => false,
Some(Value::String(s)) => !s.is_empty(),
Some(Value::Bool(b)) => *b,
Some(Value::Object(o)) => !o.is_empty(),
Some(_) => true,
};
let presentation = parse_presentation(obj);
let mut unsupported_keys: Vec<&'static str> = malformed_presentation_keys(obj);
let stop_all = matches!(obj.get("stopAll"), Some(Value::Bool(true)));
if configurations.len() > 1
&& !matches!(
obj.get("stopAll"),
None | Some(Value::Null) | Some(Value::Bool(_))
)
{
unsupported_keys.push("stopAll");
}
let pre_launch_task = asks_for_something("preLaunchTask")
.then(|| obj.get("preLaunchTask").and_then(|v| v.as_str()))
.flatten()
.map(str::to_string);
if pre_launch_task.is_none() && asks_for_something("preLaunchTask") {
unsupported_keys.push("preLaunchTask");
}
Some(Compound {
name,
configurations,
folders,
source,
pre_launch_task,
presentation,
unsupported_keys,
stop_all,
answers: BTreeMap::new(),
})
})
.collect()
}
pub fn resolve_compound(
compound: &Compound,
configs: &[DebugConfig],
folders: &[(String, PathBuf)],
) -> Result<Vec<DebugConfig>, String> {
compound
.configurations
.iter()
.enumerate()
.map(|(i, want)| {
if let Some(folder) = compound.folders.get(i).cloned().flatten() {
let root = folder_root(folders, &folder).ok_or_else(|| {
format!(
"compound \"{}\" names folder \"{folder}\", which is not a folder of this workspace",
compound.name
)
})?;
let mut cfg = discover_configs_all(&root)
.into_iter()
.find(|c| &c.name == want)
.ok_or_else(|| {
format!(
"compound \"{}\" names configuration \"{want}\" in folder \"{folder}\", which no launch.json there declares",
compound.name
)
})?;
cfg.root = Some(root);
return Ok(cfg);
}
configs
.iter()
.find(|c| &c.name == want && c.source == compound.source)
.or_else(|| configs.iter().find(|c| &c.name == want))
.cloned()
.ok_or_else(|| {
format!(
"compound \"{}\" names configuration \"{}\", which no launch.json declares",
compound.name, want
)
})
})
.collect()
}
fn folder_root(folders: &[(String, PathBuf)], name: &str) -> Option<PathBuf> {
folders
.iter()
.find(|(n, _)| n == name)
.map(|(_, root)| root.clone())
}
pub fn workspace_folders<'a>(roots: impl Iterator<Item = &'a Path>) -> Vec<(String, PathBuf)> {
roots
.map(|r| {
let name = r
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
(name, r.to_path_buf())
})
.collect()
}
pub fn adapter_for_type(type_name: &str) -> Option<AdapterKind> {
match type_name {
"python" | "debugpy" => Some(AdapterKind::Debugpy),
"lldb" | "lldb-dap" | "cppdbg" | "codelldb" => Some(AdapterKind::LldbDap),
"go" => Some(AdapterKind::Delve),
"node" | "pwa-node" | "node-terminal" | "javascript" => Some(AdapterKind::JsDebug),
t if browser_type(t).is_some() => Some(AdapterKind::JsDebug),
_ => None,
}
}
pub fn browser_type(type_name: &str) -> Option<&'static str> {
match type_name {
"chrome" | "pwa-chrome" => Some("pwa-chrome"),
"msedge" | "pwa-msedge" => Some("pwa-msedge"),
_ => None,
}
}
pub const BROWSER_DEBUG_PORT: u16 = 9222;
pub fn js_config_error(rc: &ResolvedConfig) -> Option<&'static str> {
if browser_type(&rc.type_name).is_some() {
return None;
}
match rc.request {
RequestKind::Launch if !js_launch_has_target(rc) => {
Some("a node launch needs a \"program\", a \"runtimeExecutable\" or a \"command\"")
}
RequestKind::Attach if rc.port.is_none() => Some("a node attach needs a \"port\""),
_ => None,
}
}
pub fn with_headless_browser(mut request: Value) -> Value {
if let Some(args) = request["arguments"].as_object_mut() {
let runtime = args
.entry("runtimeArgs")
.or_insert_with(|| Value::Array(Vec::new()));
if runtime.is_null() {
*runtime = Value::Array(Vec::new());
}
if let Some(list) = runtime.as_array_mut() {
let headless = list
.iter()
.any(|a| a.as_str().is_some_and(|a| a.starts_with("--headless")));
if !headless {
list.push(json!("--headless=new"));
}
}
}
request
}
#[derive(Debug, Clone)]
pub struct SubstCtx {
pub workspace_folder: PathBuf,
pub file: Option<PathBuf>,
pub inputs: BTreeMap<String, String>,
pub folders: Vec<(String, PathBuf)>,
}
pub fn substitute(input: &str, ctx: &SubstCtx) -> Result<String, String> {
let mut out = String::with_capacity(input.len());
let mut rest = input;
while let Some(start) = rest.find("${") {
out.push_str(&rest[..start]);
let after = &rest[start + 2..];
let Some(end) = after.find('}') else {
out.push_str(&rest[start..]);
return Ok(out);
};
let var = &after[..end];
out.push_str(&expand_variable(var, ctx)?);
rest = &after[end + 1..];
}
out.push_str(rest);
Ok(out)
}
fn expand_variable(var: &str, ctx: &SubstCtx) -> Result<String, String> {
let file = || {
ctx.file
.as_deref()
.ok_or_else(|| format!("${{{var}}}: no active file"))
};
match var {
"workspaceFolder" | "workspaceRoot" => {
Ok(ctx.workspace_folder.to_string_lossy().into_owned())
}
"workspaceFolderBasename" => Ok(ctx
.workspace_folder
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default()),
"file" => Ok(file()?.to_string_lossy().into_owned()),
"fileBasename" => Ok(file()?
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default()),
"fileBasenameNoExtension" => Ok(file()?
.file_stem()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default()),
"fileDirname" => Ok(file()?
.parent()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default()),
"fileExtname" => Ok(file()?
.extension()
.map(|e| format!(".{}", e.to_string_lossy()))
.unwrap_or_default()),
"cwd" => Ok(ctx.workspace_folder.to_string_lossy().into_owned()),
"pathSeparator" => Ok(String::from(std::path::MAIN_SEPARATOR_STR)),
_ => {
if let Some(id) = var.strip_prefix("input:") {
return ctx
.inputs
.get(id.trim())
.cloned()
.ok_or_else(|| format!("${{{var}}}: no answer for this input"));
}
if let Some(name) = var.strip_prefix("env:") {
return std::env::var(name)
.map_err(|_| format!("${{env:{name}}}: environment variable not set"));
}
if let Some(name) = var.strip_prefix("workspaceFolder:") {
return folder_root(&ctx.folders, name)
.map(|r| r.to_string_lossy().into_owned())
.ok_or_else(|| format!("${{{var}}}: no workspace folder is named \"{name}\""));
}
Err(format!("${{{var}}}: unsupported substitution variable"))
}
}
}
fn substitute_value(v: &Value, ctx: &SubstCtx) -> Result<Value, String> {
Ok(match v {
Value::String(s) => Value::String(substitute(s, ctx)?),
Value::Array(a) => Value::Array(
a.iter()
.map(|x| substitute_value(x, ctx))
.collect::<Result<_, _>>()?,
),
Value::Object(o) => Value::Object(
o.iter()
.map(|(k, x)| Ok((k.clone(), substitute_value(x, ctx)?)))
.collect::<Result<_, String>>()?,
),
other => other.clone(),
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedConfig {
pub name: String,
pub kind: AdapterKind,
pub type_name: String,
pub request: RequestKind,
pub program: Option<String>,
pub module: Option<String>,
pub args: Vec<String>,
pub env: BTreeMap<String, String>,
pub cwd: Option<PathBuf>,
pub pre_launch_task: Option<String>,
pub post_debug_task: Option<String>,
pub stop_on_entry: bool,
pub port: Option<u16>,
pub process_id: Option<i64>,
pub pick_process: bool,
pub extra: Map<String, Value>,
pub ignored_keys: Vec<&'static str>,
}
const MAPPED_KEYS: &[&str] = &[
"name",
"type",
"request",
"program",
"module",
"args",
"env",
"envFile",
"cwd",
"preLaunchTask",
"postDebugTask",
"stopOnEntry",
"port",
"processId",
"console",
];
pub fn is_debug_test_config(cfg: &DebugConfig) -> bool {
let debug_test = match cfg.raw.get("purpose") {
Some(Value::String(s)) => s == "debug-test",
Some(Value::Array(list)) => list.iter().any(|v| v.as_str() == Some("debug-test")),
_ => false,
};
debug_test && adapter_for_type(&cfg.type_name) == Some(AdapterKind::Debugpy)
}
pub fn debug_test_config(configs: &[DebugConfig]) -> Option<&DebugConfig> {
configs.iter().find(|c| is_debug_test_config(c))
}
const DEBUG_TEST_META_KEYS: &[&str] = &["purpose", "presentation"];
pub fn with_debug_test_config(mut request: Value, rc: &ResolvedConfig) -> Value {
let Some(args) = request["arguments"].as_object_mut() else {
return request;
};
for (key, value) in &rc.extra {
if !DEBUG_TEST_META_KEYS.contains(&key.as_str()) {
args.insert(key.clone(), value.clone());
}
}
if !rc.env.is_empty() {
args.insert("env".into(), json!(rc.env));
}
if let Some(cwd) = &rc.cwd {
args.insert("cwd".into(), json!(cwd.to_string_lossy()));
}
if let Some(list) = args.get_mut("args").and_then(Value::as_array_mut) {
list.extend(rc.args.iter().map(|a| json!(a)));
}
if rc.stop_on_entry {
args.insert("stopOnEntry".into(), json!(true));
}
request
}
const CPPDBG_MAPPED_KEYS: &[&str] = &["environment", "stopAtEntry"];
const CPPDBG_IGNORED_KEYS: &[&str] = &[
"MIMode",
"miDebuggerPath",
"miDebuggerArgs",
"miDebuggerServerAddress",
"setupCommands",
"externalConsole",
"targetArchitecture",
"symbolSearchPath",
"additionalSOLibSearchPath",
"sourceFileMap",
"visualizerFile",
"logging",
];
pub fn ignored_cppdbg_keys(cfg: &DebugConfig) -> Vec<&'static str> {
if cfg.type_name != "cppdbg" {
return Vec::new();
}
CPPDBG_IGNORED_KEYS
.iter()
.copied()
.filter(|k| cfg.raw.contains_key(*k))
.collect()
}
pub fn resolve(cfg: &DebugConfig, ctx: &SubstCtx) -> Result<ResolvedConfig, String> {
let kind = adapter_for_type(&cfg.type_name).ok_or_else(|| {
format!(
"config \"{}\": unsupported type \"{}\" (python, lldb, node, go, chrome and msedge families are supported)",
cfg.name, cfg.type_name
)
})?;
let mut raw_obj = cfg.raw.clone();
let pick_process = raw_obj
.get("processId")
.and_then(Value::as_str)
.map(str::trim)
== Some("${command:pickProcess}");
if pick_process {
raw_obj.remove("processId");
}
let raw = Value::Object(raw_obj);
let raw = substitute_value(&raw, ctx).map_err(|e| format!("config \"{}\": {e}", cfg.name))?;
let obj = raw.as_object().expect("substitution preserves the object");
let get_str = |key: &str| obj.get(key).and_then(Value::as_str).map(str::to_string);
let mut env: BTreeMap<String, String> = BTreeMap::new();
if let Some(env_file) = get_str("envFile") {
let path = absolute_in(&env_file, &ctx.workspace_folder);
let text = std::fs::read_to_string(&path)
.map_err(|e| format!("config \"{}\": envFile {}: {e}", cfg.name, path.display()))?;
env.extend(parse_env_file(&text));
}
let cppdbg = cfg.type_name == "cppdbg";
if cppdbg && let Some(list) = obj.get("environment").and_then(Value::as_array) {
for item in list {
if let Some(name) = item.get("name").and_then(Value::as_str) {
let value = match item.get("value") {
Some(Value::String(s)) => s.clone(),
Some(Value::Null) | None => String::new(),
Some(other) => other.to_string(),
};
env.insert(name.to_string(), value);
}
}
}
if let Some(map) = obj.get("env").and_then(Value::as_object) {
for (k, v) in map {
let val = match v {
Value::String(s) => s.clone(),
other => other.to_string(),
};
env.insert(k.clone(), val);
}
}
let args = obj
.get("args")
.and_then(Value::as_array)
.map(|a| {
a.iter()
.map(|x| match x {
Value::String(s) => s.clone(),
other => other.to_string(),
})
.collect()
})
.unwrap_or_default();
let port = obj
.get("port")
.and_then(Value::as_u64)
.and_then(|p| u16::try_from(p).ok());
let process_id = match obj.get("processId") {
Some(Value::Number(n)) => n.as_i64(),
Some(Value::String(s)) => s.trim().parse::<i64>().ok(),
_ => None,
};
let mut extra: Map<String, Value> = obj
.iter()
.filter(|(k, _)| !MAPPED_KEYS.contains(&k.as_str()))
.filter(|(k, _)| !(cppdbg && CPPDBG_MAPPED_KEYS.contains(&k.as_str())))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
if browser_type(&cfg.type_name).is_some() && !extra.contains_key("webRoot") {
extra.insert(
"webRoot".into(),
json!(ctx.workspace_folder.to_string_lossy()),
);
}
if cfg.type_name == "node-terminal" && cfg.request == RequestKind::Launch {
node_terminal_command(&mut extra);
}
let mut program = get_str("program");
if kind == AdapterKind::Delve && cfg.request == RequestKind::Launch {
resolve_go_auto_mode(&mut extra, &mut program, ctx.file.as_deref());
}
Ok(ResolvedConfig {
name: cfg.name.clone(),
kind,
type_name: cfg.type_name.clone(),
request: cfg.request,
program,
module: get_str("module"),
args,
env,
cwd: get_str("cwd")
.map(|c| absolute_in(&c, &ctx.workspace_folder))
.or_else(|| cfg.root.clone()),
pre_launch_task: get_str("preLaunchTask"),
post_debug_task: get_str("postDebugTask"),
stop_on_entry: obj
.get("stopOnEntry")
.or_else(|| obj.get("stopAtEntry").filter(|_| cppdbg))
.and_then(Value::as_bool)
.unwrap_or(false),
port,
process_id,
pick_process,
extra,
ignored_keys: ignored_cppdbg_keys(cfg),
})
}
fn node_terminal_command(extra: &mut Map<String, Value>) {
let Some(Value::String(command)) = extra.remove("command") else {
return;
};
if names_runtime_executable(extra) || command.trim().is_empty() {
return;
}
let shell_syntax = command.contains(['&', '|', ';', '<', '>', '$', '`', '(', ')', '*', '?']);
let words = match shlex::split(&command) {
Some(words) if !shell_syntax && !words.is_empty() && !is_assignment(&words[0]) => words,
_ => vec![String::from("sh"), String::from("-c"), command],
};
let mut words = words.into_iter().map(Value::String);
extra.insert("runtimeExecutable".into(), words.next().expect("not empty"));
let mut runtime_args: Vec<Value> = words.collect();
if let Some(Value::Array(more)) = extra.get("runtimeArgs") {
runtime_args.extend(more.iter().cloned());
}
extra.insert("runtimeArgs".into(), Value::Array(runtime_args));
}
fn resolve_go_auto_mode(
extra: &mut Map<String, Value>,
program: &mut Option<String>,
active: Option<&Path>,
) {
if extra.get("mode").and_then(Value::as_str) != Some("auto") {
return;
}
let decides = match program.as_deref() {
Some(p) if p.ends_with(".go") => Some(p.to_string()),
_ => active.map(|f| f.to_string_lossy().into_owned()),
};
let test = decides.is_some_and(|f| f.ends_with("_test.go"));
extra.insert("mode".into(), json!(if test { "test" } else { "debug" }));
if let Some(file) = program.as_deref().filter(|p| p.ends_with("_test.go")) {
let dir = Path::new(file)
.parent()
.map(|d| d.to_string_lossy().into_owned())
.filter(|d| !d.is_empty())
.unwrap_or_else(|| String::from("."));
*program = Some(dir);
}
}
fn is_assignment(word: &str) -> bool {
word.split_once('=').is_some_and(|(name, _)| {
name.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
})
}
fn names_runtime_executable(extra: &Map<String, Value>) -> bool {
matches!(extra.get("runtimeExecutable"), Some(Value::String(s)) if !s.trim().is_empty())
}
pub fn js_launch_has_target(rc: &ResolvedConfig) -> bool {
rc.program.is_some()
|| names_runtime_executable(&rc.extra)
|| matches!(rc.extra.get("runtimeArgs"), Some(Value::Array(a)) if !a.is_empty())
}
pub fn parse_ps_processes(output: &str, skip: i64) -> Vec<(i64, String)> {
output
.lines()
.filter_map(|line| {
let line = line.trim();
let (pid, comm) = line.split_once(char::is_whitespace)?;
let pid: i64 = pid.parse().ok()?;
let comm = comm.trim();
(pid != skip && !comm.is_empty()).then(|| (pid, comm.to_string()))
})
.collect()
}
pub fn absolute_in(path: &str, workspace_folder: &Path) -> PathBuf {
let p = PathBuf::from(path);
if p.is_absolute() {
p
} else {
workspace_folder.join(p)
}
}
pub fn parse_env_file(text: &str) -> BTreeMap<String, String> {
parse_env_file_in(text, |name| std::env::var(name).ok())
}
fn parse_env_file_in(
text: &str,
process_env: impl Fn(&str) -> Option<String>,
) -> BTreeMap<String, String> {
let mut out = BTreeMap::new();
let mut pos = 0;
while pos < text.len() {
let line_end = text[pos..].find('\n').map_or(text.len(), |i| pos + i);
let line = &text[pos..line_end];
pos = line_end + 1;
let trimmed = line.trim_start();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
let entry = trimmed.strip_prefix("export ").unwrap_or(trimmed);
let Some((key, raw)) = entry.split_once('=') else {
continue;
};
let key = key.trim();
if key.is_empty() || key.contains(char::is_whitespace) {
continue;
}
let value = raw.trim_start();
let start = line_end - line.len() + (line.len() - value.len());
let quoted = match value.chars().next() {
Some(q @ ('"' | '\'')) => closing_quote(text, start + 1, q).map(|end| (q, end)),
_ => None,
};
let value = match quoted {
Some(('\'', end)) => {
pos = pos.max(next_line(text, end));
text[start + 1..end].to_string()
}
Some((_, end)) => {
pos = pos.max(next_line(text, end));
let unescaped = unescape_double_quoted(&text[start + 1..end]);
expand_env_refs(&unescaped, &out, &process_env)
}
None => {
let comment = raw
.char_indices()
.find(|&(i, c)| c == '#' && raw[..i].ends_with(char::is_whitespace))
.map_or(raw.len(), |(i, _)| i);
expand_env_refs(raw[..comment].trim(), &out, &process_env)
}
};
out.insert(key.to_string(), value);
}
out
}
fn closing_quote(text: &str, from: usize, quote: char) -> Option<usize> {
let mut chars = text[from..].char_indices();
while let Some((i, c)) = chars.next() {
match c {
'\\' if quote == '"' => {
chars.next();
}
c if c == quote => return Some(from + i),
_ => {}
}
}
None
}
fn next_line(text: &str, at: usize) -> usize {
text[at..].find('\n').map_or(text.len(), |i| at + i + 1)
}
fn unescape_double_quoted(body: &str) -> String {
let mut out = String::with_capacity(body.len());
let mut chars = body.chars();
while let Some(c) = chars.next() {
if c != '\\' {
out.push(c);
continue;
}
match chars.next() {
Some('n') => out.push('\n'),
Some('t') => out.push('\t'),
Some('r') => out.push('\r'),
Some(c @ ('"' | '\\')) => out.push(c),
Some(other) => {
out.push('\\');
out.push(other);
}
None => out.push('\\'),
}
}
out
}
fn expand_env_refs(
value: &str,
file: &BTreeMap<String, String>,
process_env: &impl Fn(&str) -> Option<String>,
) -> String {
let mut out = String::with_capacity(value.len());
let mut rest = value;
while let Some(start) = rest.find("${") {
out.push_str(&rest[..start]);
let after = &rest[start + 2..];
let Some(end) = after.find('}') else {
out.push_str(&rest[start..]);
return out;
};
let (name, default) = match after[..end].split_once(":-") {
Some((name, default)) => (name, Some(default)),
None => (&after[..end], None),
};
match file.get(name).cloned().or_else(|| process_env(name)) {
Some(found) => out.push_str(&found),
None => out.push_str(default.unwrap_or("")),
}
rest = &after[end + 1..];
}
out.push_str(rest);
out
}
fn base_arguments(rc: &ResolvedConfig) -> Map<String, Value> {
let mut args = rc.extra.clone();
if let Some(p) = &rc.program {
args.insert("program".into(), json!(p));
}
if !rc.args.is_empty() {
args.insert("args".into(), json!(rc.args));
}
if let Some(c) = &rc.cwd {
args.insert("cwd".into(), json!(c.to_string_lossy()));
}
args.insert("stopOnEntry".into(), json!(rc.stop_on_entry));
args
}
fn request_value(command: &str, arguments: Map<String, Value>) -> Value {
json!({
"type": "request",
"command": command,
"arguments": Value::Object(arguments),
})
}
pub fn debugpy_request(rc: &ResolvedConfig, project_python: &Path) -> Value {
let mut args = base_arguments(rc);
match rc.request {
RequestKind::Launch => {
args.insert("request".into(), json!("launch"));
if let Some(m) = &rc.module {
args.insert("module".into(), json!(m));
args.remove("program");
}
if !rc.env.is_empty() {
args.insert("env".into(), json!(rc.env));
}
if !args.contains_key("python") && !args.contains_key("pythonPath") {
args.insert("python".into(), json!([project_python.to_string_lossy()]));
}
args.insert("console".into(), json!("internalConsole"));
if !args.contains_key("justMyCode") {
args.insert("justMyCode".into(), json!(true));
}
request_value("launch", args)
}
RequestKind::Attach => {
args.insert("request".into(), json!("attach"));
if let Some(port) = rc.port
&& !args.contains_key("connect")
{
let host = args.remove("host").unwrap_or_else(|| json!("127.0.0.1"));
args.insert("connect".into(), json!({ "host": host, "port": port }));
}
request_value("attach", args)
}
}
}
pub fn debugpy_connect_address(request: &Value) -> Option<(String, u16)> {
if request.get("command").and_then(Value::as_str) != Some("attach") {
return None;
}
let args = request.get("arguments")?;
if args.get("listen").is_some() {
return None;
}
let connect = args.get("connect")?;
let port = match connect.get("port")? {
Value::Number(n) => n.as_u64(),
Value::String(s) => s.trim().parse().ok(),
_ => None,
}
.and_then(|p| u16::try_from(p).ok())?;
let host = connect
.get("host")
.and_then(Value::as_str)
.filter(|h| !h.trim().is_empty())
.unwrap_or("127.0.0.1");
Some((host.to_string(), port))
}
pub fn lldb_request(rc: &ResolvedConfig) -> Value {
let mut args = base_arguments(rc);
args.remove("cargo");
if !rc.env.is_empty() {
let pairs: Vec<String> = rc.env.iter().map(|(k, v)| format!("{k}={v}")).collect();
args.insert("env".into(), json!(pairs));
}
match rc.request {
RequestKind::Launch => request_value("launch", args),
RequestKind::Attach => {
if let Some(pid) = rc.process_id {
args.insert("pid".into(), json!(pid));
}
args.remove("stopOnEntry");
request_value("attach", args)
}
}
}
pub fn lldb_attach_has_target(rc: &ResolvedConfig) -> bool {
rc.process_id.is_some()
|| rc.program.as_deref().is_some_and(|p| !p.trim().is_empty())
|| rc
.extra
.get("attachCommands")
.and_then(Value::as_array)
.is_some_and(|c| !c.is_empty())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CargoSpec {
pub args: Vec<String>,
pub name: Option<String>,
pub kind: Option<String>,
pub env: BTreeMap<String, String>,
pub cwd: Option<PathBuf>,
}
pub fn cargo_spec(rc: &ResolvedConfig) -> Option<Result<CargoSpec, String>> {
let cargo = rc.extra.get("cargo")?;
let strings = |v: Option<&Value>| -> Option<Vec<String>> {
v?.as_array()?
.iter()
.map(|a| a.as_str().map(str::to_string))
.collect()
};
let Some(args) = strings(cargo.get("args")).filter(|a| !a.is_empty()) else {
return Some(Err(String::from(
"\"cargo.args\" must list cargo's arguments (\"build\", …)",
)));
};
let filter = cargo.get("filter");
let text = |key: &str| {
filter
.and_then(|f| f.get(key))
.and_then(Value::as_str)
.map(str::to_string)
};
let env = cargo
.get("env")
.and_then(Value::as_object)
.map(|e| {
e.iter()
.filter_map(|(k, v)| Some((k.clone(), v.as_str()?.to_string())))
.collect()
})
.unwrap_or_default();
Some(Ok(CargoSpec {
args,
name: text("name"),
kind: text("kind"),
env,
cwd: cargo.get("cwd").and_then(Value::as_str).map(PathBuf::from),
}))
}
pub fn cargo_build_args(spec: &CargoSpec) -> Vec<String> {
let mut args = spec.args.clone();
if args.first().map(String::as_str) == Some("test") && !args.iter().any(|a| a == "--no-run") {
args.insert(1, String::from("--no-run"));
}
if !args.iter().any(|a| a.starts_with("--message-format")) {
args.push(String::from("--message-format=json"));
}
args
}
pub fn cargo_artifact(output: &str, spec: &CargoSpec) -> Result<PathBuf, String> {
let mut found: Vec<PathBuf> = Vec::new();
for line in output.lines() {
let Ok(v) = serde_json::from_str::<Value>(line) else {
continue;
};
if v.get("reason").and_then(Value::as_str) != Some("compiler-artifact") {
continue;
}
let Some(exe) = v.get("executable").and_then(Value::as_str) else {
continue;
};
let target = v.get("target");
let name_ok = spec.name.as_deref().is_none_or(|want| {
target.and_then(|t| t.get("name")).and_then(Value::as_str) == Some(want)
});
let kind_ok = spec.kind.as_deref().is_none_or(|want| {
target
.and_then(|t| t.get("kind"))
.and_then(Value::as_array)
.is_some_and(|k| k.iter().any(|k| k.as_str() == Some(want)))
});
let exe = PathBuf::from(exe);
if name_ok && kind_ok && !found.contains(&exe) {
found.push(exe);
}
}
let filter = match (&spec.name, &spec.kind) {
(Some(n), Some(k)) => format!("name \"{n}\", kind \"{k}\""),
(Some(n), None) => format!("name \"{n}\""),
(None, Some(k)) => format!("kind \"{k}\""),
(None, None) => String::from("no filter"),
};
match found.len() {
1 => Ok(found.remove(0)),
0 => Err(format!("cargo built no executable matching {filter}")),
n => Err(format!(
"cargo built {n} executables matching {filter} ({}): set \"cargo.filter\" to pick one",
found
.iter()
.filter_map(|p| p.file_name())
.map(|f| f.to_string_lossy().into_owned())
.collect::<Vec<_>>()
.join(", ")
)),
}
}
pub fn build_cargo_program(spec: &CargoSpec, cwd: &Path) -> Result<PathBuf, String> {
let dir = spec.cwd.as_deref().map_or_else(
|| cwd.to_path_buf(),
|c| absolute_in(&c.to_string_lossy(), cwd),
);
let out = std::process::Command::new("cargo")
.args(cargo_build_args(spec))
.envs(&spec.env)
.current_dir(&dir)
.stdin(std::process::Stdio::null())
.output()
.map_err(|e| format!("cargo: {e}"))?;
let stdout = String::from_utf8_lossy(&out.stdout);
if !out.status.success() {
let rendered = stdout.lines().find_map(|l| {
let v: Value = serde_json::from_str(l).ok()?;
let m = v.get("message")?;
(m.get("level")?.as_str()? == "error")
.then(|| m.get("message")?.as_str().map(str::to_string))?
});
let stderr = String::from_utf8_lossy(&out.stderr);
let why = rendered
.or_else(|| {
stderr
.lines()
.find(|l| l.starts_with("error"))
.map(str::to_string)
})
.unwrap_or_else(|| stderr.lines().last().unwrap_or("").to_string());
return Err(format!(
"cargo {} failed: {}",
spec.args.join(" "),
why.trim()
));
}
cargo_artifact(&stdout, spec)
}
pub fn delve_remote_address(rc: &ResolvedConfig) -> Option<(String, u16)> {
if rc.request != RequestKind::Attach
|| rc.extra.get("mode").and_then(Value::as_str) != Some("remote")
{
return None;
}
let host = rc
.extra
.get("host")
.and_then(Value::as_str)
.map(str::trim)
.filter(|h| !h.is_empty())
.unwrap_or("127.0.0.1");
Some((host.to_string(), rc.port?))
}
pub fn delve_request(rc: &ResolvedConfig) -> Value {
let mut args = base_arguments(rc);
if !rc.env.is_empty() {
args.insert("env".into(), json!(rc.env));
}
match rc.request {
RequestKind::Launch => {
args.entry("mode").or_insert_with(|| json!("debug"));
request_value("launch", args)
}
RequestKind::Attach => {
args.entry("mode").or_insert_with(|| json!("local"));
if let Some(pid) = rc.process_id {
args.insert("processId".into(), json!(pid));
}
args.remove("stopOnEntry");
request_value("attach", args)
}
}
}
pub fn with_workspace_folder(mut request: Value, root: &Path) -> Value {
if let Some(args) = request.get_mut("arguments").and_then(Value::as_object_mut) {
args.entry("__workspaceFolder")
.or_insert_with(|| json!(root.to_string_lossy()));
}
request
}
pub fn js_request(rc: &ResolvedConfig) -> Value {
let browser = browser_type(&rc.type_name);
let mut args = base_arguments(rc);
args.insert("type".into(), json!(browser.unwrap_or("pwa-node")));
if !rc.env.is_empty() {
args.insert("env".into(), json!(rc.env));
}
match rc.request {
RequestKind::Launch => {
args.insert("request".into(), json!("launch"));
if browser.is_none() {
args.insert("console".into(), json!("internalConsole"));
}
if !args.contains_key("sourceMaps") {
args.insert("sourceMaps".into(), json!(true));
}
request_value("launch", args)
}
RequestKind::Attach => {
args.insert("request".into(), json!("attach"));
let port = rc.port.or(browser.map(|_| BROWSER_DEBUG_PORT));
if let Some(port) = port {
args.insert("port".into(), json!(port));
}
if !args.contains_key("address") {
args.insert("address".into(), json!("127.0.0.1"));
}
args.remove("stopOnEntry");
request_value("attach", args)
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DraftField {
Name,
Program,
Args,
Cwd,
Env,
PreLaunchTask,
Port,
ProcessId,
}
pub const DRAFT_TYPES: [(&str, &str); 4] = [
("python", "Python — debugpy"),
("lldb", "Rust / C / C++ — lldb-dap"),
("go", "Go — delve"),
("node", "JavaScript / TypeScript — Node"),
];
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConfigDraft {
pub type_name: String,
pub request: RequestKind,
pub name: String,
pub program: String,
pub args: Vec<String>,
pub cwd: String,
pub env: Vec<(String, String)>,
pub pre_launch_task: String,
pub port: Option<u16>,
pub process_id: Option<u32>,
}
impl ConfigDraft {
pub fn new(type_name: &str, request: RequestKind) -> Self {
Self {
type_name: type_name.to_string(),
request,
name: String::new(),
program: String::new(),
args: Vec::new(),
cwd: String::new(),
env: Vec::new(),
pre_launch_task: String::new(),
port: None,
process_id: None,
}
}
fn attaches_by_port(&self) -> bool {
matches!(
adapter_for_type(&self.type_name),
Some(AdapterKind::Debugpy | AdapterKind::JsDebug)
)
}
pub fn next_field(&self, after: Option<DraftField>) -> Option<DraftField> {
use DraftField::*;
let order: &[DraftField] = match self.request {
RequestKind::Launch => &[Name, Program, Args, Cwd, Env, PreLaunchTask],
RequestKind::Attach if self.attaches_by_port() => &[Name, Port],
RequestKind::Attach => &[Name, ProcessId],
};
match after {
None => order.first().copied(),
Some(f) => order
.iter()
.position(|&o| o == f)
.and_then(|i| order.get(i + 1).copied()),
}
}
pub fn optional(field: DraftField) -> bool {
matches!(
field,
DraftField::Args | DraftField::Cwd | DraftField::Env | DraftField::PreLaunchTask
)
}
pub fn prompt(&self, field: DraftField) -> (String, &'static str, String) {
let seed = match field {
DraftField::Name => {
let label = DRAFT_TYPES
.iter()
.find(|(t, _)| *t == self.type_name)
.map_or(self.type_name.as_str(), |(_, l)| {
l.split(" — ").next().unwrap_or(l)
});
match self.request {
RequestKind::Launch => format!("Launch {label}"),
RequestKind::Attach => format!("Attach to {label}"),
}
}
DraftField::Program => match adapter_for_type(&self.type_name) {
Some(AdapterKind::LldbDap) => String::from("${workspaceFolder}/target/debug/"),
Some(AdapterKind::Delve) => String::from("${workspaceFolder}"),
_ => String::from("${file}"),
},
DraftField::Cwd => String::from("${workspaceFolder}"),
_ => String::new(),
};
let (title, hint) = match field {
DraftField::Name => ("Name", "What the configuration is called"),
DraftField::Program => ("Program", "The program to debug"),
DraftField::Args => (
"Arguments",
"Space-separated, \"quoted\" for spaces; Enter on blank for none",
),
DraftField::Cwd => ("Working directory", "Enter on blank for the default"),
DraftField::Env => ("Environment", "NAME=value pairs; Enter on blank for none"),
DraftField::PreLaunchTask => (
"Pre-launch task",
"A task label to run first; Enter on blank for none",
),
DraftField::Port => ("Port", "The port the debuggee listens on"),
DraftField::ProcessId => ("Process id", "The process to attach to"),
};
(format!("Add Configuration: {title}"), hint, seed)
}
pub fn set(&mut self, field: DraftField, value: &str) -> Result<(), String> {
let value = value.trim();
match field {
DraftField::Name => self.name = value.to_string(),
DraftField::Program => self.program = value.to_string(),
DraftField::Args => self.args = split_args(value)?,
DraftField::Cwd => self.cwd = value.to_string(),
DraftField::Env => self.env = parse_env_pairs(value)?,
DraftField::PreLaunchTask => self.pre_launch_task = value.to_string(),
DraftField::Port => {
self.port = Some(
value
.parse::<u16>()
.ok()
.filter(|p| *p > 0)
.ok_or_else(|| format!("Not a port: {value}"))?,
);
}
DraftField::ProcessId => {
self.process_id = Some(
value
.parse::<u32>()
.ok()
.filter(|p| *p > 0)
.ok_or_else(|| format!("Not a process id: {value}"))?,
);
}
}
Ok(())
}
pub fn to_json(&self) -> Value {
let mut o = Map::new();
o.insert("name".into(), json!(self.name));
o.insert("type".into(), json!(self.type_name));
let request = match self.request {
RequestKind::Launch => "launch",
RequestKind::Attach => "attach",
};
o.insert("request".into(), json!(request));
let adapter = adapter_for_type(&self.type_name);
match self.request {
RequestKind::Launch => {
o.insert("program".into(), json!(self.program));
if !self.args.is_empty() {
o.insert("args".into(), json!(self.args));
}
if !self.cwd.is_empty() {
o.insert("cwd".into(), json!(self.cwd));
}
if !self.env.is_empty() {
let env: Map<String, Value> = self
.env
.iter()
.map(|(k, v)| (k.clone(), json!(v)))
.collect();
o.insert("env".into(), Value::Object(env));
}
if !self.pre_launch_task.is_empty() {
o.insert("preLaunchTask".into(), json!(self.pre_launch_task));
}
}
RequestKind::Attach => {
if self.attaches_by_port() {
o.insert("port".into(), json!(self.port));
} else {
if adapter == Some(AdapterKind::Delve) {
o.insert("mode".into(), json!("local"));
}
o.insert("processId".into(), json!(self.process_id));
}
}
}
Value::Object(o)
}
}
fn split_args(line: &str) -> Result<Vec<String>, String> {
let mut out = Vec::new();
let mut cur = String::new();
let mut in_arg = false;
let mut quote: Option<char> = None;
for c in line.chars() {
match quote {
Some(q) if c == q => quote = None,
Some(_) => cur.push(c),
None if c == '"' || c == '\'' => {
quote = Some(c);
in_arg = true;
}
None if c.is_whitespace() => {
if in_arg {
out.push(std::mem::take(&mut cur));
in_arg = false;
}
}
None => {
cur.push(c);
in_arg = true;
}
}
}
if quote.is_some() {
return Err(String::from("Unclosed quote in the arguments"));
}
if in_arg {
out.push(cur);
}
Ok(out)
}
fn parse_env_pairs(line: &str) -> Result<Vec<(String, String)>, String> {
split_args(&line.replace(';', " "))?
.into_iter()
.map(|pair| match pair.split_once('=') {
Some((k, v)) if !k.is_empty() => Ok((k.to_string(), v.to_string())),
_ => Err(format!("Not NAME=value: {pair}")),
})
.collect()
}
pub fn add_to_croft_launch_json(root: &Path, draft: &ConfigDraft) -> Result<PathBuf, String> {
let dir = root.join(".croft");
let path = dir.join("launch.json");
for p in [&dir, &path] {
if std::fs::symlink_metadata(p).is_ok_and(|m| m.file_type().is_symlink()) {
return Err(format!(
"{} is a symlink; not rewriting it",
p.strip_prefix(root).unwrap_or(p).display()
));
}
}
let entry = draft.to_json();
let mut doc = match std::fs::read_to_string(&path) {
Ok(text) => serde_json::from_str::<Value>(&text).map_err(|_| {
format!(
".croft/launch.json has comments or trailing commas, which rewriting it would lose; add this by hand: {entry}"
)
})?,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
json!({ "version": "0.2.0", "configurations": [] })
}
Err(e) => return Err(format!("Could not read .croft/launch.json: {e}")),
};
let list = match &mut doc {
Value::Array(list) => list,
Value::Object(o) => match o
.entry("configurations")
.or_insert_with(|| Value::Array(Vec::new()))
{
Value::Array(list) => list,
_ => {
return Err(String::from(
".croft/launch.json: \"configurations\" is not a list",
));
}
},
_ => {
return Err(String::from(
".croft/launch.json is neither a list nor an object",
));
}
};
if list
.iter()
.any(|c| c.get("name").and_then(Value::as_str) == Some(draft.name.as_str()))
{
return Err(format!(
".croft/launch.json already has a configuration named \"{}\"",
draft.name
));
}
list.push(entry);
std::fs::create_dir_all(&dir).map_err(|e| format!("Could not create .croft: {e}"))?;
let text = serde_json::to_string_pretty(&doc).map_err(|e| e.to_string())?;
let (tmp, mut file) =
create_fresh_temp(&dir).map_err(|e| format!("Could not write .croft/launch.json: {e}"))?;
use std::io::Write as _;
file.write_all((text + "\n").as_bytes())
.and_then(|()| file.sync_all())
.and_then(|()| std::fs::rename(&tmp, &path))
.map_err(|e| {
let _ = std::fs::remove_file(&tmp);
format!("Could not write .croft/launch.json: {e}")
})?;
Ok(path)
}
fn temp_candidate(dir: &Path, n: u32) -> PathBuf {
dir.join(format!("launch.json.{}.{n}.tmp", std::process::id()))
}
fn create_fresh_temp(dir: &Path) -> std::io::Result<(PathBuf, std::fs::File)> {
for n in 0..64 {
let tmp = temp_candidate(dir, n);
match std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&tmp)
{
Ok(file) => return Ok((tmp, file)),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(e) => return Err(e),
}
}
Err(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
"no free temporary file name beside it",
))
}
#[cfg(test)]
mod tests {
use super::*;
fn ctx() -> SubstCtx {
SubstCtx {
workspace_folder: PathBuf::from("/work/proj"),
file: Some(PathBuf::from("/work/proj/src/main.py")),
inputs: Default::default(),
folders: Vec::new(),
}
}
fn config(json_text: &str) -> DebugConfig {
parse_launch_json(json_text, ".vscode/launch.json")
.into_iter()
.next()
.expect("one config")
}
#[test]
fn parses_vscode_shape_bare_arrays_and_jsonc() {
let vscode = r#"{
// launch.json allows comments
"version": "0.2.0",
"configurations": [
{ "name": "Run API", "type": "python", "request": "launch", "program": "api.py", },
],
}"#;
let cfgs = parse_launch_json(vscode, ".vscode/launch.json");
assert_eq!(cfgs.len(), 1);
assert_eq!(cfgs[0].name, "Run API");
assert_eq!(cfgs[0].request, RequestKind::Launch);
let bare = r#"[ { "name": "A", "type": "lldb", "request": "attach", "pid": 1 } ]"#;
let cfgs = parse_launch_json(bare, ".croft/launch.json");
assert_eq!(cfgs.len(), 1);
assert_eq!(cfgs[0].request, RequestKind::Attach);
}
#[test]
fn entries_without_name_or_type_are_skipped_and_request_defaults_to_launch() {
let text = r#"{ "configurations": [
{ "type": "python", "program": "a.py" },
{ "name": "no type" },
{ "name": "ok", "type": "node", "program": "s.js" }
]}"#;
let cfgs = parse_launch_json(text, ".vscode/launch.json");
assert_eq!(cfgs.len(), 1);
assert_eq!(cfgs[0].name, "ok");
assert_eq!(cfgs[0].request, RequestKind::Launch);
}
#[test]
fn croft_file_wins_duplicate_names_and_both_files_are_read() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".croft")).unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".croft/launch.json"),
r#"[ { "name": "Serve", "type": "python", "program": "croft.py" } ]"#,
)
.unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Serve", "type": "python", "program": "vscode.py" },
{ "name": "Worker", "type": "node", "program": "w.js" }
]}"#,
)
.unwrap();
let cfgs = discover_configs(tmp.path());
assert_eq!(cfgs.len(), 2);
assert_eq!(cfgs[0].name, "Serve");
assert_eq!(cfgs[0].source, ".croft/launch.json");
assert_eq!(cfgs[0].raw["program"], "croft.py");
assert_eq!(cfgs[1].name, "Worker");
}
#[test]
fn delve_request_defaults_mode_and_passes_an_explicit_one_through() {
let launch = resolve(
&config(
r#"[{ "name": "G", "type": "go", "request": "launch", "program": "./cmd/app" }]"#,
),
&ctx(),
)
.unwrap();
assert_eq!(launch.kind, AdapterKind::Delve);
let req = delve_request(&launch);
assert_eq!(req["command"], "launch");
assert_eq!(req["arguments"]["mode"], "debug");
assert_eq!(req["arguments"]["program"], "./cmd/app");
let test = resolve(
&config(r#"[{ "name": "T", "type": "go", "request": "launch", "mode": "test", "program": "." }]"#),
&ctx(),
)
.unwrap();
assert_eq!(delve_request(&test)["arguments"]["mode"], "test");
let attach = resolve(
&config(r#"[{ "name": "A", "type": "go", "request": "attach", "processId": 77 }]"#),
&ctx(),
)
.unwrap();
let req = delve_request(&attach);
assert_eq!(req["command"], "attach");
assert_eq!(req["arguments"]["mode"], "local");
assert_eq!(req["arguments"]["processId"], 77);
assert!(req["arguments"].get("stopOnEntry").is_none());
}
#[test]
fn a_go_auto_mode_resolves_to_test_or_debug_as_the_go_extension_does() {
let request = |cfg: &str, active: &str| {
let ctx = SubstCtx {
file: Some(PathBuf::from(active)),
..ctx()
};
delve_request(&resolve(&config(cfg), &ctx).unwrap())
};
let launch = |mode: &str, program: &str| {
format!(
r#"[{{ "name": "L", "type": "go", "request": "launch",
"mode": "{mode}", "program": "{program}" }}]"#
)
};
let req = request(&launch("auto", "/w/cmd/app"), "/w/cmd/app/main.go");
assert_eq!(req["arguments"]["mode"], "debug");
assert_eq!(req["arguments"]["program"], "/w/cmd/app");
let req = request(&launch("auto", "/w/pkg/foo_test.go"), "/w/cmd/app/main.go");
assert_eq!(req["arguments"]["mode"], "test");
assert_eq!(
req["arguments"]["program"], "/w/pkg",
"delve tests the package"
);
let req = request(&launch("auto", "${fileDirname}"), "/w/pkg/foo_test.go");
assert_eq!(req["arguments"]["mode"], "test");
assert_eq!(req["arguments"]["program"], "/w/pkg");
let req = request(&launch("auto", "${fileDirname}"), "/w/pkg/foo.go");
assert_eq!(req["arguments"]["mode"], "debug");
assert_eq!(req["arguments"]["program"], "/w/pkg");
let req = request(&launch("exec", "/w/bin/app"), "/w/pkg/foo_test.go");
assert_eq!(req["arguments"]["mode"], "exec");
assert_eq!(req["arguments"]["program"], "/w/bin/app");
}
#[test]
fn a_go_remote_attach_targets_the_headless_delve() {
let rc = resolve(
&config(
r#"[{ "name": "Connect to server", "type": "go", "request": "attach",
"mode": "remote", "port": 2345, "host": "10.0.0.5",
"substitutePath": [{ "from": "${workspaceFolder}", "to": "/app" }] }]"#,
),
&ctx(),
)
.unwrap();
assert_eq!(
delve_remote_address(&rc),
Some((String::from("10.0.0.5"), 2345))
);
let req = delve_request(&rc);
assert_eq!(req["command"], "attach");
assert_eq!(req["arguments"]["mode"], "remote");
assert_eq!(
req["arguments"]["substitutePath"][0]["from"], "/work/proj",
"path mapping passes through"
);
assert!(req["arguments"].get("processId").is_none());
let local_host = resolve(
&config(r#"[{ "name": "C", "type": "go", "request": "attach", "mode": "remote", "port": 2345 }]"#),
&ctx(),
)
.unwrap();
assert_eq!(
delve_remote_address(&local_host),
Some((String::from("127.0.0.1"), 2345))
);
}
#[test]
fn only_a_remote_go_attach_with_a_port_connects_to_a_server() {
for text in [
r#"[{ "name": "A", "type": "go", "request": "attach", "mode": "remote" }]"#,
r#"[{ "name": "A", "type": "go", "request": "attach", "processId": 77, "port": 2345 }]"#,
r#"[{ "name": "A", "type": "go", "request": "launch", "mode": "remote", "port": 2345, "program": "." }]"#,
] {
let rc = resolve(&config(text), &ctx()).unwrap();
assert_eq!(delve_remote_address(&rc), None, "{text}");
}
}
#[test]
fn maps_the_adapter_families_and_rejects_unknown_types() {
use AdapterKind::*;
for (t, k) in [
("python", Debugpy),
("debugpy", Debugpy),
("lldb", LldbDap),
("cppdbg", LldbDap),
("node", JsDebug),
("pwa-node", JsDebug),
("go", Delve),
] {
assert_eq!(adapter_for_type(t), Some(k), "{t}");
}
let cfg = config(r#"[{ "name": "J", "type": "java", "program": "Main.java" }]"#);
let err = resolve(&cfg, &ctx()).unwrap_err();
assert!(err.contains("unsupported type \"java\""), "{err}");
assert!(err.contains("go"), "the supported list names go: {err}");
}
#[test]
fn pick_process_resolves_to_a_picker_request_not_an_error() {
let cfg = config(
r#"[{ "name": "Attach", "type": "lldb", "request": "attach",
"processId": "${command:pickProcess}" }]"#,
);
let rc = resolve(&cfg, &ctx()).expect("pickProcess resolves");
assert!(rc.pick_process);
assert_eq!(rc.process_id, None);
assert!(!rc.extra.contains_key("processId"));
let cfg = config(
r#"[{ "name": "Attach", "type": "lldb", "request": "attach", "processId": 4242 }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
assert!(!rc.pick_process);
assert_eq!(rc.process_id, Some(4242));
}
#[test]
fn ps_output_parses_to_pids_and_names_without_our_own() {
let out = " 101 /usr/bin/zsh\n 202 server\n\n 303 croft\n bad line\n 404 \n";
assert_eq!(
parse_ps_processes(out, 303),
vec![
(101, String::from("/usr/bin/zsh")),
(202, String::from("server"))
]
);
}
#[test]
fn substitutes_workspace_and_file_variables() {
let c = ctx();
assert_eq!(
substitute("${workspaceFolder}/bin", &c).unwrap(),
"/work/proj/bin"
);
assert_eq!(
substitute("${workspaceFolderBasename}", &c).unwrap(),
"proj"
);
assert_eq!(substitute("${file}", &c).unwrap(), "/work/proj/src/main.py");
assert_eq!(substitute("${fileBasename}", &c).unwrap(), "main.py");
assert_eq!(
substitute("${fileBasenameNoExtension}", &c).unwrap(),
"main"
);
assert_eq!(substitute("${fileDirname}", &c).unwrap(), "/work/proj/src");
assert_eq!(substitute("${fileExtname}", &c).unwrap(), ".py");
assert_eq!(
substitute("a ${fileBasename} b ${fileBasename}", &c).unwrap(),
"a main.py b main.py"
);
assert_eq!(substitute("plain", &c).unwrap(), "plain");
assert_eq!(substitute("odd ${file", &c).unwrap(), "odd ${file");
}
#[test]
fn env_variable_expands_and_unknowns_error_loudly() {
let c = ctx();
unsafe { std::env::set_var("CROFT_TEST_SUBST_VAR", "xyz") };
assert_eq!(
substitute("${env:CROFT_TEST_SUBST_VAR}", &c).unwrap(),
"xyz"
);
let err = substitute("${env:CROFT_TEST_UNSET_VAR}", &c).unwrap_err();
assert!(err.contains("CROFT_TEST_UNSET_VAR"), "{err}");
let err = substitute("${command:pickProcess}", &c).unwrap_err();
assert!(err.contains("command:pickProcess"), "{err}");
let no_file = SubstCtx {
workspace_folder: PathBuf::from("/w"),
file: None,
inputs: Default::default(),
folders: Vec::new(),
};
assert!(substitute("${fileBasename}", &no_file).is_err());
}
#[test]
fn resolve_splits_mapped_fields_and_passes_the_rest_through() {
let cfg = config(
r#"[{
"name": "API",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/api.py",
"args": ["--port", 8080],
"env": { "MODE": "dev", "RETRIES": 3 },
"cwd": "${workspaceFolder}",
"preLaunchTask": "build",
"postDebugTask": "cleanup",
"stopOnEntry": true,
"justMyCode": true,
"subProcess": "${workspaceFolderBasename}"
}]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
assert_eq!(rc.program.as_deref(), Some("/work/proj/api.py"));
assert_eq!(rc.args, vec!["--port", "8080"]);
assert_eq!(rc.env["MODE"], "dev");
assert_eq!(rc.env["RETRIES"], "3");
assert_eq!(rc.cwd.as_deref(), Some(Path::new("/work/proj")));
assert_eq!(rc.pre_launch_task.as_deref(), Some("build"));
assert_eq!(rc.post_debug_task.as_deref(), Some("cleanup"));
assert!(!rc.extra.contains_key("postDebugTask"));
assert!(rc.stop_on_entry);
assert_eq!(rc.extra["justMyCode"], json!(true));
assert_eq!(rc.extra["subProcess"], json!("proj"));
assert!(!rc.extra.contains_key("program"));
}
#[test]
fn a_relative_cwd_resolves_against_the_workspace_folder() {
let cfg =
config(r#"[{ "name": "R", "type": "python", "program": "a.py", "cwd": "backend" }]"#);
let rc = resolve(&cfg, &ctx()).unwrap();
assert_eq!(rc.cwd.as_deref(), Some(Path::new("/work/proj/backend")));
let cfg =
config(r#"[{ "name": "R", "type": "python", "program": "a.py", "cwd": "/srv/app" }]"#);
let rc = resolve(&cfg, &ctx()).unwrap();
assert_eq!(rc.cwd.as_deref(), Some(Path::new("/srv/app")));
}
#[test]
fn env_file_merges_under_explicit_env() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join(".env"),
"# comment\nexport FROM_FILE=1\nSHARED=file\nQUOTED=\"a b\"\n",
)
.unwrap();
let cfg = config(
r#"[{
"name": "E", "type": "python", "program": "a.py",
"envFile": "${workspaceFolder}/.env",
"env": { "SHARED": "config" }
}]"#,
);
let c = SubstCtx {
workspace_folder: tmp.path().to_path_buf(),
file: None,
inputs: Default::default(),
folders: Vec::new(),
};
let rc = resolve(&cfg, &c).unwrap();
assert_eq!(rc.env["FROM_FILE"], "1");
assert_eq!(rc.env["QUOTED"], "a b");
assert_eq!(rc.env["SHARED"], "config", "explicit env wins per key");
let cfg = config(
r#"[{ "name": "E", "type": "python", "program": "a.py", "envFile": "/nope/.env" }]"#,
);
let err = resolve(&cfg, &c).unwrap_err();
assert!(err.contains("/nope/.env"), "{err}");
}
#[test]
fn a_debugpy_config_without_just_my_code_steps_through_user_code_only() {
let cfg = config(r#"[{ "name": "API", "type": "python", "program": "/p/api.py" }]"#);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = debugpy_request(&rc, Path::new("/venv/bin/python"));
assert_eq!(req["arguments"]["justMyCode"], true);
}
#[test]
fn a_debugpy_config_can_still_step_into_libraries() {
let cfg = config(
r#"[{ "name": "API", "type": "python", "program": "/p/api.py", "justMyCode": false }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = debugpy_request(&rc, Path::new("/venv/bin/python"));
assert_eq!(req["arguments"]["justMyCode"], false);
}
#[test]
fn debugpy_launch_request_carries_env_args_and_forces_internal_console() {
let cfg = config(
r#"[{
"name": "API", "type": "python", "program": "/p/api.py",
"args": ["--serve"], "env": { "MODE": "dev" },
"console": "integratedTerminal"
}]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = debugpy_request(&rc, Path::new("/venv/bin/python"));
assert_eq!(req["command"], "launch");
assert_eq!(req["arguments"]["program"], "/p/api.py");
assert_eq!(req["arguments"]["args"][0], "--serve");
assert_eq!(req["arguments"]["env"]["MODE"], "dev");
assert_eq!(req["arguments"]["python"][0], "/venv/bin/python");
assert_eq!(req["arguments"]["console"], "internalConsole");
}
#[test]
fn debugpy_module_launch_drops_program_and_attach_builds_connect() {
let cfg = config(
r#"[{ "name": "T", "type": "python", "module": "pytest", "args": ["-k", "x"] }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = debugpy_request(&rc, Path::new("/venv/bin/python"));
assert_eq!(req["arguments"]["module"], "pytest");
assert!(req["arguments"].get("program").is_none());
let cfg =
config(r#"[{ "name": "A", "type": "python", "request": "attach", "port": 5678 }]"#);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = debugpy_request(&rc, Path::new("/venv/bin/python"));
assert_eq!(req["command"], "attach");
assert_eq!(req["arguments"]["connect"]["port"], 5678);
assert_eq!(req["arguments"]["connect"]["host"], "127.0.0.1");
}
#[test]
fn a_configs_own_python_wins_over_the_project_interpreter() {
let cfg = config(
r#"[{ "name": "P", "type": "python", "program": "a.py", "python": "/opt/py/bin/python" }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = debugpy_request(&rc, Path::new("/proj/.venv/bin/python"));
assert_eq!(req["arguments"]["python"], "/opt/py/bin/python");
let cfg = config(
r#"[{ "name": "L", "type": "python", "program": "a.py", "pythonPath": "/opt/py/bin/python" }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = debugpy_request(&rc, Path::new("/proj/.venv/bin/python"));
assert!(req["arguments"].get("python").is_none());
assert_eq!(req["arguments"]["pythonPath"], "/opt/py/bin/python");
}
#[test]
fn lldb_requests_use_kv_env_and_attach_by_pid() {
let cfg = config(
r#"[{
"name": "Bin", "type": "lldb", "program": "/t/debug/app",
"args": ["--flag"], "env": { "RUST_LOG": "debug" }
}]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = lldb_request(&rc);
assert_eq!(req["command"], "launch");
assert_eq!(req["arguments"]["program"], "/t/debug/app");
assert_eq!(req["arguments"]["env"][0], "RUST_LOG=debug");
let cfg = config(
r#"[{ "name": "At", "type": "lldb", "request": "attach", "processId": "4242" }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
assert_eq!(rc.process_id, Some(4242), "numeric string processId parses");
let req = lldb_request(&rc);
assert_eq!(req["command"], "attach");
assert_eq!(req["arguments"]["pid"], 4242);
assert!(req["arguments"].get("stopOnEntry").is_none());
}
#[test]
fn an_lldb_attach_by_program_or_attach_commands_has_a_target() {
let cfg = config(
r#"[{ "name": "By name", "type": "lldb", "request": "attach",
"program": "${workspaceFolder}/app", "waitFor": true }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
assert!(lldb_attach_has_target(&rc));
let req = lldb_request(&rc);
assert_eq!(req["command"], "attach");
assert_eq!(req["arguments"]["program"], "/work/proj/app");
assert_eq!(req["arguments"]["waitFor"], true);
assert!(req["arguments"].get("pid").is_none());
assert!(req["arguments"].get("stopOnEntry").is_none());
let cfg = config(
r#"[{ "name": "gdbserver", "type": "lldb", "request": "attach",
"program": "${workspaceFolder}/app",
"attachCommands": ["target create ${workspaceFolder}/app", "gdb-remote localhost:1234"] }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
assert!(lldb_attach_has_target(&rc));
let req = lldb_request(&rc);
assert_eq!(
req["arguments"]["attachCommands"],
json!(["target create /work/proj/app", "gdb-remote localhost:1234"])
);
assert!(req["arguments"].get("pid").is_none());
let cfg = config(
r#"[{ "name": "Remote", "type": "lldb", "request": "attach",
"attachCommands": ["gdb-remote 10.0.0.2:3333"] }]"#,
);
assert!(lldb_attach_has_target(&resolve(&cfg, &ctx()).unwrap()));
}
#[test]
fn an_lldb_attach_with_nothing_to_attach_to_has_no_target() {
for text in [
r#"[{ "name": "A", "type": "lldb", "request": "attach" }]"#,
r#"[{ "name": "A", "type": "lldb", "request": "attach", "attachCommands": [] }]"#,
r#"[{ "name": "A", "type": "lldb", "request": "attach", "program": " " }]"#,
] {
let rc = resolve(&config(text), &ctx()).unwrap();
assert!(!lldb_attach_has_target(&rc), "{text}");
}
}
#[test]
fn js_requests_are_pwa_node_and_attach_by_port() {
let cfg = config(
r#"[{ "name": "S", "type": "node", "program": "/p/server.js", "sourceMaps": false }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = js_request(&rc);
assert_eq!(req["command"], "launch");
assert_eq!(req["arguments"]["type"], "pwa-node");
assert_eq!(
req["arguments"]["sourceMaps"], false,
"user value respected"
);
assert_eq!(req["arguments"]["console"], "internalConsole");
let cfg = config(r#"[{ "name": "A", "type": "node", "request": "attach", "port": 9229 }]"#);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = js_request(&rc);
assert_eq!(req["command"], "attach");
assert_eq!(req["arguments"]["port"], 9229);
assert_eq!(req["arguments"]["address"], "127.0.0.1");
}
#[test]
fn js_requests_carry_the_workspace_folder_unless_the_config_sets_one() {
let root = Path::new("/work/proj");
for text in [
r#"[{ "name": "L", "type": "node", "program": "${workspaceFolder}/dist/main.js", "outFiles": ["${workspaceFolder}/dist/**/*.js"] }]"#,
r#"[{ "name": "A", "type": "node", "request": "attach", "port": 9229 }]"#,
] {
let rc = resolve(&config(text), &ctx()).unwrap();
let req = with_workspace_folder(js_request(&rc), root);
assert_eq!(
req["arguments"]["__workspaceFolder"], "/work/proj",
"{text}"
);
}
let rc = resolve(
&config(r#"[{ "name": "W", "type": "node", "program": "a.js", "__workspaceFolder": "/elsewhere" }]"#),
&ctx(),
)
.unwrap();
let req = with_workspace_folder(js_request(&rc), root);
assert_eq!(
req["arguments"]["__workspaceFolder"], "/elsewhere",
"the config's own value wins"
);
let bare = json!({ "type": "request", "command": "launch" });
assert_eq!(with_workspace_folder(bare.clone(), root), bare);
}
#[test]
fn chrome_and_edge_types_resolve_to_js_debug() {
for (t, js) in [
("chrome", "pwa-chrome"),
("pwa-chrome", "pwa-chrome"),
("msedge", "pwa-msedge"),
("pwa-msedge", "pwa-msedge"),
] {
assert_eq!(adapter_for_type(t), Some(AdapterKind::JsDebug), "{t}");
assert_eq!(browser_type(t), Some(js), "{t}");
}
}
#[test]
fn node_is_not_a_browser_and_firefox_is_still_refused() {
for t in ["node", "pwa-node", "node-terminal", "python"] {
assert_eq!(browser_type(t), None, "{t}");
}
assert_eq!(adapter_for_type("firefox"), None);
let cfg = config(r#"[{ "name": "F", "type": "firefox", "url": "http://localhost" }]"#);
let err = resolve(&cfg, &ctx()).unwrap_err();
assert!(err.contains("unsupported type \"firefox\""), "{err}");
}
#[test]
fn a_browser_config_needs_no_program_or_port() {
for json in [
r#"[{ "name": "C", "type": "chrome", "url": "http://localhost:3000" }]"#,
r#"[{ "name": "C", "type": "msedge", "request": "attach" }]"#,
] {
let rc = resolve(&config(json), &ctx()).unwrap();
assert_eq!(js_config_error(&rc), None, "{json}");
}
let rc = resolve(&config(r#"[{ "name": "N", "type": "node" }]"#), &ctx()).unwrap();
assert_eq!(
js_config_error(&rc),
Some("a node launch needs a \"program\", a \"runtimeExecutable\" or a \"command\"")
);
let rc = resolve(
&config(r#"[{ "name": "N", "type": "node", "request": "attach" }]"#),
&ctx(),
)
.unwrap();
assert_eq!(js_config_error(&rc), Some("a node attach needs a \"port\""));
}
#[test]
fn a_chrome_launch_is_a_pwa_chrome_request() {
let cfg = config(
r#"[{ "name": "C", "type": "chrome", "request": "launch",
"url": "http://localhost:5173", "webRoot": "${workspaceFolder}/web",
"runtimeExecutable": "/usr/bin/chromium", "userDataDir": false }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
let req = js_request(&rc);
let a = &req["arguments"];
assert_eq!(req["command"], "launch");
assert_eq!(a["type"], "pwa-chrome");
assert_eq!(a["url"], "http://localhost:5173");
assert_eq!(a["webRoot"], "/work/proj/web");
assert_eq!(a["runtimeExecutable"], "/usr/bin/chromium");
assert_eq!(a["userDataDir"], false);
assert!(a.get("console").is_none(), "{a}");
assert!(a.get("program").is_none(), "{a}");
}
#[test]
fn a_browser_config_without_a_web_root_uses_the_workspace_folder() {
let cfg = config(
r#"[{ "name": "E", "type": "msedge", "file": "${workspaceFolder}/index.html" }]"#,
);
let req = js_request(&resolve(&cfg, &ctx()).unwrap());
assert_eq!(req["arguments"]["type"], "pwa-msedge");
assert_eq!(req["arguments"]["webRoot"], "/work/proj");
assert_eq!(req["arguments"]["file"], "/work/proj/index.html");
let cfg = config(r#"[{ "name": "N", "type": "node", "program": "/p/a.js" }]"#);
let req = js_request(&resolve(&cfg, &ctx()).unwrap());
assert!(req["arguments"].get("webRoot").is_none());
}
#[test]
fn a_browser_attach_defaults_to_port_9222() {
let cfg = config(r#"[{ "name": "A", "type": "chrome", "request": "attach" }]"#);
let req = js_request(&resolve(&cfg, &ctx()).unwrap());
assert_eq!(req["command"], "attach");
assert_eq!(req["arguments"]["type"], "pwa-chrome");
assert_eq!(req["arguments"]["port"], 9222);
let cfg =
config(r#"[{ "name": "A", "type": "msedge", "request": "attach", "port": 9333 }]"#);
let req = js_request(&resolve(&cfg, &ctx()).unwrap());
assert_eq!(req["arguments"]["port"], 9333);
}
#[test]
fn a_headless_browser_adds_its_flag_once() {
let cfg = config(
r#"[{ "name": "C", "type": "chrome", "url": "http://x", "runtimeArgs": ["--incognito"] }]"#,
);
let req = with_headless_browser(js_request(&resolve(&cfg, &ctx()).unwrap()));
assert_eq!(
req["arguments"]["runtimeArgs"],
json!(["--incognito", "--headless=new"])
);
let cfg = config(r#"[{ "name": "C", "type": "chrome", "runtimeArgs": ["--headless"] }]"#);
let req = with_headless_browser(js_request(&resolve(&cfg, &ctx()).unwrap()));
assert_eq!(req["arguments"]["runtimeArgs"], json!(["--headless"]));
let cfg = config(r#"[{ "name": "C", "type": "chrome", "runtimeArgs": null }]"#);
let req = with_headless_browser(js_request(&resolve(&cfg, &ctx()).unwrap()));
assert_eq!(req["arguments"]["runtimeArgs"], json!(["--headless=new"]));
}
const DEBUG_TESTS: &str = r#"[
{ "name": "Run", "type": "debugpy", "request": "launch", "program": "${file}" },
{ "name": "Debug Tests", "type": "debugpy", "request": "launch",
"purpose": ["debug-test"], "console": "integratedTerminal",
"env": { "APP_MODE": "test" }, "justMyCode": false,
"args": ["--maxfail=1"], "cwd": "${workspaceFolder}/tests",
"subProcess": true }
]"#;
#[test]
fn the_debug_test_config_is_found_by_its_purpose() {
let cfgs = parse_launch_json(DEBUG_TESTS, ".vscode/launch.json");
assert_eq!(
debug_test_config(&cfgs).map(|c| c.name.as_str()),
Some("Debug Tests")
);
assert!(!is_debug_test_config(&cfgs[0]));
let single = config(r#"[{ "name": "T", "type": "python", "purpose": "debug-test" }]"#);
assert!(is_debug_test_config(&single), "a single string counts too");
}
#[test]
fn other_purposes_and_other_debuggers_are_not_debug_test_configs() {
let cfgs = parse_launch_json(
r#"[
{ "name": "A", "type": "debugpy", "purpose": ["debug-in-terminal"] },
{ "name": "B", "type": "node", "purpose": ["debug-test"], "program": "x.js" },
{ "name": "C", "type": "debugpy" }
]"#,
".vscode/launch.json",
);
assert!(debug_test_config(&cfgs).is_none());
}
#[test]
fn a_debug_test_config_overlays_the_pytest_request() {
let cfgs = parse_launch_json(DEBUG_TESTS, ".vscode/launch.json");
let rc = resolve(debug_test_config(&cfgs).unwrap(), &ctx()).unwrap();
let base = crate::dap::session::pytest_debug_launch_request(
Path::new("/venv/bin/python"),
Path::new("/work/proj"),
"test_mode",
);
let req = with_debug_test_config(base, &rc);
let a = &req["arguments"];
assert_eq!(a["env"]["APP_MODE"], "test");
assert_eq!(a["cwd"], "/work/proj/tests");
assert_eq!(a["justMyCode"], false);
assert_eq!(a["subProcess"], true);
assert_eq!(a["args"], json!(["-v", "-k", "test_mode", "--maxfail=1"]));
assert_eq!(a["module"], "pytest");
assert_eq!(a["console"], "internalConsole");
assert_eq!(a["python"], json!(["/venv/bin/python"]));
assert!(a.get("purpose").is_none(), "{a}");
}
#[test]
fn an_empty_debug_test_config_changes_nothing() {
let cfg = config(r#"[{ "name": "T", "type": "debugpy", "purpose": ["debug-test"] }]"#);
let rc = resolve(&cfg, &ctx()).unwrap();
let base = crate::dap::session::pytest_debug_launch_request(
Path::new("/venv/bin/python"),
Path::new("/work/proj"),
"test_mode",
);
assert_eq!(with_debug_test_config(base.clone(), &rc), base);
}
const CPPDBG: &str = r#"[{
"name": "(gdb) Launch", "type": "cppdbg", "request": "launch",
"program": "${workspaceFolder}/app", "args": ["one"],
"stopAtEntry": true, "cwd": "${workspaceFolder}",
"environment": [{ "name": "GREETING", "value": "hi" }, { "name": "N", "value": 3 }],
"externalConsole": false, "MIMode": "gdb",
"setupCommands": [{ "text": "-enable-pretty-printing" }]
}]"#;
#[test]
fn a_cppdbg_environment_and_stop_at_entry_reach_lldb_dap() {
let rc = resolve(&config(CPPDBG), &ctx()).unwrap();
let req = lldb_request(&rc);
let a = &req["arguments"];
assert_eq!(a["env"], json!(["GREETING=hi", "N=3"]));
assert_eq!(a["stopOnEntry"], true);
assert_eq!(a["args"], json!(["one"]));
assert!(a.get("environment").is_none(), "{a}");
assert!(a.get("stopAtEntry").is_none(), "{a}");
}
#[test]
fn a_cppdbg_env_wins_over_its_environment_list() {
let cfg = config(
r#"[{ "name": "C", "type": "cppdbg", "program": "/a",
"environment": [{ "name": "A", "value": "1" }, { "name": "B", "value": "x" }],
"env": { "A": "2" } }]"#,
);
let req = lldb_request(&resolve(&cfg, &ctx()).unwrap());
assert_eq!(req["arguments"]["env"], json!(["A=2", "B=x"]));
}
#[test]
fn a_cppdbg_config_names_the_keys_lldb_dap_ignores() {
assert_eq!(
ignored_cppdbg_keys(&config(CPPDBG)),
["MIMode", "setupCommands", "externalConsole"]
);
assert_eq!(
resolve(&config(CPPDBG), &ctx()).unwrap().ignored_keys,
["MIMode", "setupCommands", "externalConsole"]
);
}
#[test]
fn only_cppdbg_configs_get_cppdbg_spellings() {
let cfg = config(
r#"[{ "name": "L", "type": "lldb", "program": "/a", "MIMode": "gdb",
"environment": [{ "name": "A", "value": "1" }], "stopAtEntry": true }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
assert!(rc.env.is_empty());
assert!(!rc.stop_on_entry);
assert!(ignored_cppdbg_keys(&cfg).is_empty());
let rc = resolve(
&config(r#"[{ "name": "C", "type": "cppdbg", "program": "/a" }]"#),
&ctx(),
)
.unwrap();
assert!(rc.env.is_empty());
assert!(!rc.stop_on_entry);
}
#[test]
fn parse_env_file_handles_comments_export_and_quotes() {
let env = parse_env_file(
"# a comment\n\nexport A=1\nB = spaced \nC='sq'\nnot a pair\nBAD KEY=x\n",
);
assert_eq!(env["A"], "1");
assert_eq!(env["B"], "spaced");
assert_eq!(env["C"], "sq");
assert!(!env.contains_key("not a pair"));
assert!(!env.contains_key("BAD KEY"));
}
#[test]
fn an_env_file_expands_variable_references() {
let process = |name: &str| (name == "HOME").then(|| String::from("/home/u"));
let env = parse_env_file_in(
"A=/x\nB=${A}/src\nP=${HOME}/data\nC=${MISSING}x\nD=${MISSING:-fallback}\n\
E=\"${A}:\\t\"\nF='${A}'\nG=${A\n",
process,
);
assert_eq!(env["B"], "/x/src");
assert_eq!(env["P"], "/home/u/data");
assert_eq!(env["C"], "x");
assert_eq!(env["D"], "fallback");
assert_eq!(env["E"], "/x:\t", "double quotes expand and unescape");
assert_eq!(env["F"], "${A}", "single quotes are literal");
assert_eq!(env["G"], "${A", "an unterminated reference stays");
}
#[test]
fn an_env_file_reads_inline_comments_escapes_and_multiline_values() {
let env = parse_env_file_in(
"PORT=8000 # dev port\nHASH=a#b\nBLANK= # nothing\n\
GREETING=\"hello\\nworld\" # greeting\n\
QUOTE=\"say \\\"hi\\\"\"\n\
KEY=\"-----BEGIN-----\nabc\n-----END-----\"\nAFTER=1\n\
LITERAL='a # b'\nRAW='a\\nb'\nOPEN=\"never closed\n",
|_| None,
);
assert_eq!(env["PORT"], "8000");
assert_eq!(env["HASH"], "a#b", "a # inside a word is no comment");
assert_eq!(env["BLANK"], "");
assert_eq!(env["GREETING"], "hello\nworld");
assert_eq!(env["QUOTE"], "say \"hi\"");
assert_eq!(env["KEY"], "-----BEGIN-----\nabc\n-----END-----");
assert_eq!(env["AFTER"], "1", "the line after a multi-line value");
assert_eq!(env["LITERAL"], "a # b");
assert_eq!(env["RAW"], "a\\nb");
assert_eq!(env["OPEN"], "\"never closed", "an unclosed quote is kept");
}
#[test]
fn compound_members_may_be_folder_name_objects_as_well_as_bare_strings() {
let text = r#"{"configurations":[
{"name":"Server","type":"lldb","request":"launch"},
{"name":"Client","type":"lldb","request":"launch"}],
"compounds":[{"name":"Mixed","configurations":[
"Server",
{"folder":"web","name":"Client"}]}]}"#;
let compounds = parse_compounds(text, ".vscode/launch.json");
assert_eq!(compounds.len(), 1, "precondition: the compound parsed");
assert_eq!(
compounds[0].configurations,
vec!["Server", "Client"],
"an object member contributes its name, exactly as a bare string does"
);
}
#[test]
fn a_compound_binds_to_its_own_files_configuration_not_a_same_named_one() {
let croft = r#"{"configurations":[
{"name":"Server","type":"python","request":"launch","program":"croft.py"}]}"#;
let vscode = r#"{"configurations":[
{"name":"Server","type":"node","request":"launch","program":"vscode.js"}],
"compounds":[{"name":"VsCompound","configurations":["Server"]}]}"#;
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".croft")).unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(tmp.path().join(".croft/launch.json"), croft).unwrap();
std::fs::write(tmp.path().join(".vscode/launch.json"), vscode).unwrap();
let configs = discover_configs_all(tmp.path());
let compounds = discover_compounds(tmp.path());
assert_eq!(compounds.len(), 1, "precondition: the compound parsed");
assert_eq!(
compounds[0].source, ".vscode/launch.json",
"precondition: the compound remembers which file declared it"
);
assert_eq!(
configs.iter().filter(|c| c.name == "Server").count(),
2,
"precondition: the list resolve_compound receives still holds BOTH \
Servers — `discover_configs` would have dropped one, leaving the \
source preference nothing to prefer"
);
assert_eq!(
discover_configs(tmp.path())
.iter()
.filter(|c| c.name == "Server")
.count(),
1,
"and the picker's list drops it, which is why the two lists differ"
);
let members = resolve_compound(&compounds[0], &configs, &[]).expect("resolves");
assert_eq!(
members[0].source, ".vscode/launch.json",
"the .vscode compound binds .vscode's Server, not .croft's same-named one"
);
assert_eq!(
members[0].type_name, "node",
"which is a different adapter — binding .croft's python config here \
would debug a different program than the compound declares"
);
}
#[test]
fn a_compound_with_one_unusable_member_is_dropped_rather_than_silently_shrunk() {
let text = r#"{"configurations":[
{"name":"A","type":"lldb","request":"launch"},
{"name":"B","type":"lldb","request":"launch"}],
"compounds":[
{"name":"Partial","configurations":["A", 7, "B"]},
{"name":"Whole","configurations":["A","B"]}]}"#;
let names: Vec<String> = parse_compounds(text, ".vscode/launch.json")
.into_iter()
.map(|c| c.name)
.collect();
assert_eq!(
names,
vec!["Whole"],
"the compound with a malformed member is dropped entirely — shrinking \
it to [\"A\", \"B\"] would launch two of the three the user wrote"
);
}
#[test]
fn a_compound_with_no_usable_members_is_skipped_rather_than_listed_empty() {
let text = r#"{"configurations":[{"name":"A","type":"lldb","request":"launch"}],
"compounds":[
{"name":"Empty","configurations":[]},
{"name":"Unusable","configurations":[7, true]},
{"name":"Real","configurations":["A"]}]}"#;
let names: Vec<String> = parse_compounds(text, ".vscode/launch.json")
.into_iter()
.map(|c| c.name)
.collect();
assert_eq!(
names,
vec!["Real"],
"an empty compound and one whose members are all unusable are both dropped"
);
}
#[test]
fn discover_compounds_prefers_croft_over_vscode_for_a_duplicate_name() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::create_dir_all(root.join(".croft")).unwrap();
std::fs::create_dir_all(root.join(".vscode")).unwrap();
std::fs::write(
root.join(".croft/launch.json"),
r#"{"configurations":[{"name":"A","type":"lldb","request":"launch"}],
"compounds":[{"name":"Both","configurations":["A"]}]}"#,
)
.unwrap();
std::fs::write(
root.join(".vscode/launch.json"),
r#"{"configurations":[{"name":"B","type":"lldb","request":"launch"}],
"compounds":[{"name":"Both","configurations":["B"]},
{"name":"VsOnly","configurations":["B"]}]}"#,
)
.unwrap();
let found = discover_compounds(root);
let names: Vec<&str> = found.iter().map(|c| c.name.as_str()).collect();
assert_eq!(
names,
vec!["Both", "VsOnly"],
"both files contribute, .croft first"
);
assert_eq!(
found[0].configurations,
vec!["A"],
"the .croft definition of a duplicate name wins, not .vscode's"
);
}
#[test]
fn compounds_are_parsed_alongside_the_configurations_they_name() {
let text = r#"{
"configurations": [
{ "name": "Server", "type": "node", "program": "s.js" },
{ "name": "Client", "type": "node", "program": "c.js" }
],
"compounds": [
{ "name": "Full Stack", "configurations": ["Server", "Client"] }
]
}"#;
let cfgs = parse_launch_json(text, ".vscode/launch.json");
assert_eq!(cfgs.len(), 2, "precondition: both configurations parsed");
let compounds = parse_compounds(text, ".vscode/launch.json");
assert_eq!(compounds.len(), 1, "the compound is found");
assert_eq!(compounds[0].name, "Full Stack");
assert_eq!(compounds[0].configurations, vec!["Server", "Client"]);
}
#[test]
fn a_compound_naming_a_missing_configuration_errors_rather_than_launching_a_subset() {
let text = r#"{
"configurations": [ { "name": "Server", "type": "node", "program": "s.js" } ],
"compounds": [ { "name": "Both", "configurations": ["Server", "Ghost"] } ]
}"#;
let cfgs = parse_launch_json(text, ".vscode/launch.json");
let compounds = parse_compounds(text, ".vscode/launch.json");
assert_eq!(compounds.len(), 1, "precondition: the compound parsed");
let err = resolve_compound(&compounds[0], &cfgs, &[])
.expect_err("a missing member is an error, not a shorter list");
assert!(
err.contains("Ghost"),
"the error names the missing configuration, got: {err}"
);
}
#[test]
fn a_compound_key_that_requests_nothing_is_not_recorded_as_unsupported() {
let text = r#"{
"configurations": [
{ "name": "A", "type": "node", "program": "a.js" },
{ "name": "B", "type": "node", "program": "b.js" }
],
"compounds": [
{ "name": "DefaultStopAll", "configurations": ["A"], "stopAll": false },
{ "name": "TrueStopAll", "configurations": ["A"], "stopAll": true },
{ "name": "NulledTask", "configurations": ["A"], "preLaunchTask": null },
{ "name": "EmptyTask", "configurations": ["A"], "preLaunchTask": "" },
{ "name": "BoolTask", "configurations": ["A"], "preLaunchTask": true },
{ "name": "EmptyPresentation", "configurations": ["A"], "presentation": {} },
{ "name": "RealPresentation", "configurations": ["A"], "presentation": { "order": 1 } },
{ "name": "Malformed", "configurations": ["A"], "presentation": 7 }
]
}"#;
let cs = parse_compounds(text, ".vscode/launch.json");
assert_eq!(cs.len(), 8, "precondition: every compound parsed");
let by = |n: &str| {
cs.iter()
.find(|c| c.name == n)
.unwrap_or_else(|| panic!("{n} parsed"))
};
assert!(
by("DefaultStopAll").unsupported_keys.is_empty(),
"stopAll:false is VS Code's default and cannot change a one-member \
outcome: {:?}",
by("DefaultStopAll").unsupported_keys
);
assert_eq!(
by("BoolTask").unsupported_keys,
vec!["preLaunchTask"],
"a non-string task cannot be named or run, so it is refused \
rather than skipped in silence"
);
assert!(
by("NulledTask").unsupported_keys.is_empty(),
"while a null one asks for nothing: {:?}",
by("NulledTask").unsupported_keys
);
assert!(
by("TrueStopAll").unsupported_keys.is_empty(),
"nor can stopAll:true — with one session there are no others to \
stop: {:?}",
by("TrueStopAll").unsupported_keys
);
assert!(
by("NulledTask").unsupported_keys.is_empty(),
"a null preLaunchTask asks for nothing: {:?}",
by("NulledTask").unsupported_keys
);
assert!(
by("EmptyTask").unsupported_keys.is_empty(),
"nor does an empty one: {:?}",
by("EmptyTask").unsupported_keys
);
assert!(
by("EmptyPresentation").unsupported_keys.is_empty(),
"nor an empty presentation object: {:?}",
by("EmptyPresentation").unsupported_keys
);
assert_eq!(
by("Malformed").unsupported_keys,
vec!["presentation"],
"and a shape croft cannot act on is still recorded"
);
assert_eq!(
by("RealPresentation").unsupported_keys,
Vec::<&str>::new(),
"while a well-formed order is honoured rather than refused"
);
}
#[test]
fn stop_all_true_is_honoured_and_only_an_unreadable_value_refused() {
let text = r#"{
"configurations": [
{ "name": "A", "type": "node", "program": "a.js" },
{ "name": "B", "type": "node", "program": "b.js" }
],
"compounds": [
{ "name": "OneTrue", "configurations": ["A"], "stopAll": true },
{ "name": "TwoTrue", "configurations": ["A", "B"], "stopAll": true },
{ "name": "TwoQuoted", "configurations": ["A", "B"], "stopAll": "true" },
{ "name": "TwoFalse", "configurations": ["A", "B"], "stopAll": false },
{ "name": "TwoAbsent", "configurations": ["A", "B"] }
]
}"#;
let cs = parse_compounds(text, ".vscode/launch.json");
assert_eq!(cs.len(), 5, "precondition: every compound parsed");
let by = |n: &str| cs.iter().find(|c| c.name == n).expect("parsed");
assert!(
by("TwoTrue").unsupported_keys.is_empty() && by("TwoTrue").stop_all,
"`true` is honoured: the set is torn down when a member ends"
);
assert!(!by("TwoFalse").stop_all && !by("TwoAbsent").stop_all);
assert_eq!(
by("TwoQuoted").unsupported_keys,
vec!["stopAll"],
"and `\"true\"` is the same typo the presentation keys already \
refuse - matching only the BOOL let it launch as `false`"
);
assert!(
by("OneTrue").unsupported_keys.is_empty(),
"but one member has no siblings to stop, so the same value is \
honoured by doing nothing: {:?}",
by("OneTrue").unsupported_keys
);
assert!(
by("TwoFalse").unsupported_keys.is_empty(),
"and `false` is VS Code's default, which croft already does: {:?}",
by("TwoFalse").unsupported_keys
);
assert!(
by("TwoAbsent").unsupported_keys.is_empty(),
"as is omitting it: {:?}",
by("TwoAbsent").unsupported_keys
);
}
#[test]
fn a_compounds_pre_launch_task_is_captured_and_no_longer_unsupported() {
let text = r#"{
"configurations": [ { "name": "A", "type": "node", "program": "a.js" } ],
"compounds": [
{ "name": "Builds", "configurations": ["A"], "preLaunchTask": "build" },
{ "name": "Nulled", "configurations": ["A"], "preLaunchTask": null },
{ "name": "Empty", "configurations": ["A"], "preLaunchTask": "" },
{ "name": "Presents", "configurations": ["A"], "presentation": { "order": 1 } }
]
}"#;
let cs = parse_compounds(text, ".vscode/launch.json");
let by = |n: &str| {
cs.iter()
.find(|c| c.name == n)
.unwrap_or_else(|| panic!("{n} parsed"))
};
assert_eq!(
by("Builds").pre_launch_task.as_deref(),
Some("build"),
"the task's NAME is what running it needs; the key alone cannot"
);
assert!(
by("Builds").unsupported_keys.is_empty(),
"preLaunchTask is honoured now, so it must stop being a refusal reason: {:?}",
by("Builds").unsupported_keys
);
assert_eq!(by("Nulled").pre_launch_task, None);
assert_eq!(by("Empty").pre_launch_task, None);
assert!(
by("Presents").unsupported_keys.is_empty(),
"order is honoured now and must stop refusing: {:?}",
by("Presents").unsupported_keys
);
assert_eq!(
by("Presents").presentation.as_ref().and_then(|p| p.order),
Some(1.0),
"the order's VALUE is what sorting needs; tolerating the key cannot"
);
assert_eq!(by("Presents").pre_launch_task, None);
}
#[test]
fn presentation_keys_are_honoured_and_only_unreadable_values_refuse() {
let text = r#"{
"compounds": [
{ "name": "Hid", "configurations": ["A"], "presentation": { "hidden": true } },
{ "name": "Shown", "configurations": ["A"], "presentation": { "hidden": false } },
{ "name": "Grouped", "configurations": ["A"], "presentation": { "group": "g" } },
{ "name": "Ordered", "configurations": ["A"], "presentation": { "order": 2 } },
{ "name": "Both", "configurations": ["A"],
"presentation": { "hidden": true, "order": 1 } },
{ "name": "Empty", "configurations": ["A"], "presentation": {} }
]
}"#;
let cs = parse_compounds(text, "test");
let by = |n: &str| cs.iter().find(|c| c.name == n).expect("compound present");
assert!(by("Hid").hidden(), "hidden: true asks not to be listed");
assert!(
!by("Shown").hidden(),
"hidden: false is the default, spelled out"
);
assert!(
!by("Empty").hidden(),
"an empty presentation asks for nothing"
);
assert!(
by("Hid").unsupported_keys.is_empty(),
"hidden alone is honoured, so nothing is unsupported: {:?}",
by("Hid").unsupported_keys
);
assert!(
by("Empty").unsupported_keys.is_empty(),
"an empty presentation refuses nothing"
);
assert!(
by("Grouped").unsupported_keys.is_empty(),
"group no longer refuses: {:?}",
by("Grouped").unsupported_keys
);
assert_eq!(
by("Grouped")
.presentation
.as_ref()
.and_then(|p| p.group.clone()),
Some(String::from("g")),
"and its VALUE is what places the row"
);
assert!(by("Ordered").unsupported_keys.is_empty());
assert_eq!(
by("Ordered").presentation.as_ref().and_then(|p| p.order),
Some(2.0)
);
let bad = parse_compounds(
r#"{
"compounds": [
{ "name": "QuotedBool", "configurations": ["A"],
"presentation": { "hidden": "true" } },
{ "name": "NotAnObject", "configurations": ["A"],
"presentation": "grouped" }
]
}"#,
"test",
);
let bad_by = |n: &str| bad.iter().find(|c| c.name == n).expect("present");
assert!(
!bad_by("QuotedBool").hidden(),
"a quoted bool is not a bool"
);
assert_eq!(
bad_by("QuotedBool").unsupported_keys,
vec!["presentation.hidden"],
"and it refuses rather than launching unhidden in silence"
);
assert_eq!(
bad_by("NotAnObject").unsupported_keys,
vec!["presentation"],
"a non-object presentation refuses under its own name"
);
assert!(by("Both").hidden(), "Both asks to be hidden");
assert!(
by("Both").unsupported_keys.is_empty(),
"and both halves are honoured now, so nothing refuses: {:?}",
by("Both").unsupported_keys
);
}
#[test]
fn a_malformed_presentation_value_refuses_whichever_key_it_is_on() {
let cs = parse_compounds(
r#"{
"compounds": [
{ "name": "QuotedOrder", "configurations": ["A"],
"presentation": { "order": "1" } },
{ "name": "NumberGroup", "configurations": ["A"],
"presentation": { "group": 7 } },
{ "name": "FractionalOrder", "configurations": ["A"],
"presentation": { "order": 1.5 } },
{ "name": "NulledKeys", "configurations": ["A"],
"presentation": { "group": null, "order": null } }
]
}"#,
".vscode/launch.json",
);
assert_eq!(cs.len(), 4, "precondition: every compound parsed");
let by = |n: &str| {
cs.iter()
.find(|c| c.name == n)
.unwrap_or_else(|| panic!("{n} parsed"))
};
assert_eq!(
by("QuotedOrder").unsupported_keys,
vec!["presentation.order"]
);
assert_eq!(
by("NumberGroup").unsupported_keys,
vec!["presentation.group"]
);
assert!(
by("FractionalOrder").unsupported_keys.is_empty(),
"a fractional order is a position, not a mistake: {:?}",
by("FractionalOrder").unsupported_keys
);
assert_eq!(
by("FractionalOrder")
.presentation
.as_ref()
.and_then(|p| p.order),
Some(1.5)
);
assert!(
by("NulledKeys").unsupported_keys.is_empty(),
"null asks for nothing: {:?}",
by("NulledKeys").unsupported_keys
);
}
#[test]
fn presentation_sorts_the_picker_the_way_vs_code_does() {
fn p(group: Option<&str>, order: Option<f64>) -> Option<Presentation> {
Some(Presentation {
hidden: false,
group: group.map(str::to_string),
order,
})
}
let items: Vec<(&str, Option<Presentation>)> = vec![
("plain-first", None),
("b-group", p(Some("b"), None)),
("no-group-ord-2", p(None, Some(2.0))),
(
"hidden",
Some(Presentation {
hidden: true,
group: None,
order: None,
}),
),
("a-group-ord-9", p(Some("a"), Some(9.0))),
("plain-second", None),
("a-group-ord-1", p(Some("a"), Some(1.0))),
("a-group-no-ord", p(Some("a"), None)),
("no-group-ord-1", p(None, Some(1.0))),
];
let sorted: Vec<&str> = visible_and_sorted(items, |(_, p)| p.as_ref())
.into_iter()
.map(|(n, _)| n)
.collect();
assert_eq!(
sorted,
vec![
"a-group-ord-1",
"a-group-ord-9",
"a-group-no-ord",
"b-group",
"no-group-ord-1",
"no-group-ord-2",
"plain-first",
"plain-second",
],
"hidden is dropped, and every other rule is VS Code's"
);
}
#[test]
fn an_empty_presentation_block_still_outranks_no_block() {
let cfgs = parse_launch_json(
r#"{ "configurations": [
{ "name": "Bare", "type": "node", "program": "a.js" },
{ "name": "EmptyBlock", "type": "node", "program": "b.js",
"presentation": {} },
{ "name": "OnlyShown", "type": "node", "program": "c.js",
"presentation": { "hidden": false } }
] }"#,
".vscode/launch.json",
);
let by = |n: &str| {
cfgs.iter()
.find(|c| c.name == n)
.unwrap_or_else(|| panic!("{n} parsed"))
.clone()
};
assert_eq!(by("Bare").presentation, None, "no block is no block");
assert_eq!(
by("EmptyBlock").presentation,
Some(Presentation::default()),
"an empty block asks for nothing but is still declared"
);
assert_eq!(by("OnlyShown").presentation, Some(Presentation::default()));
let sorted: Vec<String> = visible_and_sorted(cfgs, |c| c.presentation.as_ref())
.into_iter()
.map(|c| c.name)
.collect();
assert_eq!(
sorted,
vec!["EmptyBlock", "OnlyShown", "Bare"],
"both declared blocks sort ahead of the bare entry, in file order"
);
}
#[test]
fn a_presentation_that_requests_nothing_is_not_a_refusal() {
let cs = parse_compounds(
r#"{
"compounds": [
{ "name": "FalseBlock", "configurations": ["A"], "presentation": false },
{ "name": "EmptyString", "configurations": ["A"], "presentation": "" },
{ "name": "NulledBlock", "configurations": ["A"], "presentation": null },
{ "name": "Truthy", "configurations": ["A"], "presentation": true }
]
}"#,
".vscode/launch.json",
);
assert_eq!(cs.len(), 4, "precondition: every compound parsed");
let by = |n: &str| {
cs.iter()
.find(|c| c.name == n)
.unwrap_or_else(|| panic!("{n} parsed"))
};
for name in ["FalseBlock", "EmptyString", "NulledBlock"] {
assert!(
by(name).unsupported_keys.is_empty(),
"{name} asks for nothing and must launch: {:?}",
by(name).unsupported_keys
);
}
assert_eq!(
by("Truthy").unsupported_keys,
vec!["presentation"],
"a non-object that asks for something is unreadable"
);
}
#[test]
fn a_workspace_with_no_presentation_keeps_its_file_order() {
let items: Vec<(&str, Option<Presentation>)> = vec![("z", None), ("a", None), ("m", None)];
let sorted: Vec<&str> = visible_and_sorted(items, |(_, p)| p.as_ref())
.into_iter()
.map(|(n, _)| n)
.collect();
assert_eq!(
sorted,
vec!["z", "a", "m"],
"sorting must be stable, or introducing the feature would silently \
reorder every existing workspace's picker"
);
}
#[test]
fn a_configuration_reads_presentation_like_a_compound_does() {
let cfgs = parse_launch_json(
r#"{ "configurations": [
{ "name": "Plain", "type": "node", "program": "a.js" },
{ "name": "Grouped", "type": "node", "program": "b.js",
"presentation": { "group": "servers", "order": 3 } },
{ "name": "Hid", "type": "node", "program": "c.js",
"presentation": { "hidden": true } }
] }"#,
".vscode/launch.json",
);
let by = |n: &str| {
cfgs.iter()
.find(|c| c.name == n)
.unwrap_or_else(|| panic!("{n} parsed"))
.clone()
};
assert_eq!(by("Plain").presentation, None, "no block asks for nothing");
assert_eq!(
by("Grouped").presentation,
Some(Presentation {
hidden: false,
group: Some(String::from("servers")),
order: Some(3.0),
})
);
assert!(by("Hid").presentation.is_some_and(|p| p.hidden));
}
fn multi_root() -> (tempfile::TempDir, Vec<(String, PathBuf)>) {
let tmp = tempfile::tempdir().unwrap();
for (folder, text) in [
(
"api",
r#"{ "configurations": [
{ "name": "Run api", "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/main.py" }
],
"compounds": [
{ "name": "API + web", "configurations": ["Run api", { "folder": "web", "name": "Run web" }] },
{ "name": "Nowhere", "configurations": [{ "folder": "docs", "name": "Run web" }] },
{ "name": "Missing", "configurations": [{ "folder": "web", "name": "Run api" }] }
] }"#,
),
(
"web",
r#"{ "configurations": [
{ "name": "Run web", "type": "debugpy", "request": "launch", "program": "${workspaceFolder}/main.py" }
] }"#,
),
] {
std::fs::create_dir_all(tmp.path().join(folder).join(".vscode")).unwrap();
std::fs::write(tmp.path().join(folder).join(".vscode/launch.json"), text).unwrap();
}
let roots = [tmp.path().join("api"), tmp.path().join("web")];
let folders = workspace_folders(roots.iter().map(PathBuf::as_path));
(tmp, folders)
}
#[test]
fn a_folder_member_resolves_in_that_workspace_folder() {
let (tmp, folders) = multi_root();
let api = tmp.path().join("api");
let compounds = discover_compounds(&api);
assert_eq!(compounds[0].folders, vec![None, Some(String::from("web"))]);
let members =
resolve_compound(&compounds[0], &discover_configs_all(&api), &folders).unwrap();
assert_eq!(
members.iter().map(|m| m.name.as_str()).collect::<Vec<_>>(),
vec!["Run api", "Run web"]
);
assert_eq!(members[0].root, None);
let web = tmp.path().join("web");
assert_eq!(members[1].root.as_deref(), Some(web.as_path()));
let ctx = SubstCtx {
workspace_folder: members[1].root.clone().unwrap(),
file: None,
inputs: Default::default(),
folders: folders.clone(),
};
let rc = resolve(&members[1], &ctx).unwrap();
assert_eq!(
rc.program.as_deref(),
Some(web.join("main.py").to_string_lossy().as_ref())
);
assert_eq!(
rc.cwd.as_deref(),
Some(web.as_path()),
"a foreign member with no cwd runs in its own folder"
);
}
#[test]
fn a_folder_member_with_no_such_folder_or_config_is_refused() {
let (tmp, folders) = multi_root();
let api = tmp.path().join("api");
let compounds = discover_compounds(&api);
let all = discover_configs_all(&api);
let err = resolve_compound(&compounds[1], &all, &folders).unwrap_err();
assert_eq!(
err,
"compound \"Nowhere\" names folder \"docs\", which is not a folder of this workspace"
);
let err = resolve_compound(&compounds[2], &all, &folders).unwrap_err();
assert_eq!(
err,
"compound \"Missing\" names configuration \"Run api\" in folder \"web\", which no launch.json there declares"
);
let bad = parse_compounds(
r#"{ "compounds": [{ "name": "Bad", "configurations": [{ "folder": 3, "name": "x" }] }] }"#,
".vscode/launch.json",
);
assert!(bad.is_empty());
}
#[test]
fn workspace_folder_by_name_expands_or_names_the_missing_folder() {
let (tmp, folders) = multi_root();
let ctx = SubstCtx {
workspace_folder: tmp.path().join("api"),
file: None,
inputs: Default::default(),
folders,
};
assert_eq!(
substitute("${workspaceFolder:web}/main.py", &ctx).unwrap(),
tmp.path().join("web/main.py").to_string_lossy()
);
assert_eq!(
substitute("${workspaceFolder}", &ctx).unwrap(),
tmp.path().join("api").to_string_lossy()
);
assert_eq!(
substitute("${workspaceFolder:docs}/x", &ctx).unwrap_err(),
"${workspaceFolder:docs}: no workspace folder is named \"docs\""
);
}
#[test]
fn a_resolved_compound_yields_its_members_in_declaration_order() {
let text = r#"{
"configurations": [
{ "name": "Client", "type": "node", "program": "c.js" },
{ "name": "Server", "type": "node", "program": "s.js" }
],
"compounds": [ { "name": "Both", "configurations": ["Server", "Client"] } ]
}"#;
let cfgs = parse_launch_json(text, ".vscode/launch.json");
let compounds = parse_compounds(text, ".vscode/launch.json");
let members = resolve_compound(&compounds[0], &cfgs, &[]).unwrap();
assert_eq!(
members.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(),
vec!["Server", "Client"],
"compound order wins over the order in `configurations`"
);
}
#[test]
fn a_draft_asks_for_what_its_request_needs() {
use DraftField::*;
let fields = |d: &ConfigDraft| {
let mut out = Vec::new();
let mut at = d.next_field(None);
while let Some(f) = at {
out.push(f);
at = d.next_field(Some(f));
}
out
};
let launch = ConfigDraft::new("python", RequestKind::Launch);
assert_eq!(
fields(&launch),
[Name, Program, Args, Cwd, Env, PreLaunchTask]
);
for t in ["python", "node"] {
assert_eq!(
fields(&ConfigDraft::new(t, RequestKind::Attach)),
[Name, Port]
);
}
for t in ["lldb", "go"] {
assert_eq!(
fields(&ConfigDraft::new(t, RequestKind::Attach)),
[Name, ProcessId]
);
}
for (t, _) in DRAFT_TYPES {
assert!(adapter_for_type(t).is_some(), "{t}");
}
}
#[test]
fn a_launch_draft_writes_an_entry_that_reads_back() {
let mut d = ConfigDraft::new("python", RequestKind::Launch);
assert_eq!(d.prompt(DraftField::Name).2, "Launch Python");
assert_eq!(d.prompt(DraftField::Program).2, "${file}");
d.set(DraftField::Name, "Serve").unwrap();
d.set(DraftField::Program, "${workspaceFolder}/api.py")
.unwrap();
d.set(DraftField::Args, r#"--port 8000 "a b" 'c d'"#)
.unwrap();
assert_eq!(d.args, ["--port", "8000", "a b", "c d"]);
assert!(d.set(DraftField::Args, r#"--x "open"#).is_err());
d.set(DraftField::Cwd, "").unwrap();
d.set(DraftField::Env, "DEBUG=1; URL=http://x?a=b").unwrap();
assert!(d.set(DraftField::Env, "NOEQUALS").is_err());
d.set(DraftField::PreLaunchTask, "build").unwrap();
let text = serde_json::to_string(&json!({ "configurations": [d.to_json()] })).unwrap();
let cfgs = parse_launch_json(&text, ".croft/launch.json");
assert_eq!(cfgs.len(), 1);
assert_eq!(cfgs[0].name, "Serve");
assert_eq!(cfgs[0].request, RequestKind::Launch);
let raw = d.to_json();
assert_eq!(raw["args"], json!(["--port", "8000", "a b", "c d"]));
assert_eq!(raw["env"], json!({ "DEBUG": "1", "URL": "http://x?a=b" }));
assert_eq!(raw["preLaunchTask"], json!("build"));
assert!(raw.get("cwd").is_none(), "a blank field is left out");
}
#[test]
fn an_attach_draft_uses_each_adapters_own_fields() {
let attach = |t: &str, field: DraftField, value: &str| {
let mut d = ConfigDraft::new(t, RequestKind::Attach);
d.set(DraftField::Name, "A").unwrap();
d.set(field, value).unwrap();
d.to_json()
};
assert_eq!(
attach("python", DraftField::Port, "5678")["port"],
json!(5678)
);
assert_eq!(
attach("node", DraftField::Port, "9229")["port"],
json!(9229)
);
assert_eq!(
attach("lldb", DraftField::ProcessId, "42")["processId"],
json!(42)
);
let go = attach("go", DraftField::ProcessId, "7");
assert_eq!(
(go["processId"].clone(), go["mode"].clone()),
(json!(7), json!("local"))
);
let resolved = |t: &str, field: DraftField, value: &str| {
let text = json!({ "configurations": [attach(t, field, value)] }).to_string();
resolve(&config(&text), &ctx()).unwrap()
};
let py = debugpy_request(
&resolved("python", DraftField::Port, "5678"),
Path::new("/usr/bin/python3"),
);
assert_eq!(py["arguments"]["connect"]["port"], 5678);
let lldb = lldb_request(&resolved("lldb", DraftField::ProcessId, "42"));
assert_eq!(lldb["arguments"]["pid"], 42);
let dlv = delve_request(&resolved("go", DraftField::ProcessId, "7"));
assert_eq!(dlv["arguments"]["processId"], 7);
let mut d = ConfigDraft::new("node", RequestKind::Attach);
assert!(d.set(DraftField::Port, "0").is_err());
assert!(d.set(DraftField::Port, "70000").is_err());
assert!(d.set(DraftField::ProcessId, "pid").is_err());
}
#[test]
fn a_draft_is_added_to_croft_launch_json() {
let tmp = tempfile::tempdir().unwrap();
let mut d = ConfigDraft::new("node", RequestKind::Launch);
d.set(DraftField::Name, "Web").unwrap();
d.set(DraftField::Program, "${workspaceFolder}/index.js")
.unwrap();
let path = add_to_croft_launch_json(tmp.path(), &d).unwrap();
assert_eq!(path, tmp.path().join(".croft/launch.json"));
let mut d2 = d.clone();
d2.name = String::from("Web 2");
add_to_croft_launch_json(tmp.path(), &d2).unwrap();
let names: Vec<String> = discover_configs(tmp.path())
.into_iter()
.map(|c| c.name)
.collect();
assert_eq!(names, ["Web", "Web 2"]);
let err = add_to_croft_launch_json(tmp.path(), &d).unwrap_err();
assert!(err.contains("already has"), "{err}");
let bare = tempfile::tempdir().unwrap();
std::fs::create_dir_all(bare.path().join(".croft")).unwrap();
std::fs::write(bare.path().join(".croft/launch.json"), "[]").unwrap();
add_to_croft_launch_json(bare.path(), &d).unwrap();
assert_eq!(discover_configs(bare.path()).len(), 1);
let jsonc = tempfile::tempdir().unwrap();
std::fs::create_dir_all(jsonc.path().join(".croft")).unwrap();
let text = "{ // mine\n \"configurations\": [] }";
std::fs::write(jsonc.path().join(".croft/launch.json"), text).unwrap();
let err = add_to_croft_launch_json(jsonc.path(), &d).unwrap_err();
assert!(err.contains("by hand") && err.contains("\"Web\""), "{err}");
let leftovers: Vec<_> = std::fs::read_dir(tmp.path().join(".croft"))
.unwrap()
.map(|e| e.unwrap().file_name())
.collect();
assert_eq!(leftovers, [std::ffi::OsString::from("launch.json")]);
assert_eq!(
std::fs::read_to_string(jsonc.path().join(".croft/launch.json")).unwrap(),
text
);
}
#[cfg(unix)]
#[test]
fn adding_a_configuration_refuses_a_symlinked_croft_launch_json() {
let mut d = ConfigDraft::new("node", RequestKind::Launch);
d.set(DraftField::Name, "Web").unwrap();
let elsewhere = tempfile::tempdir().unwrap();
let target = elsewhere.path().join("victim.json");
std::fs::write(&target, "[]").unwrap();
let file_link = tempfile::tempdir().unwrap();
std::fs::create_dir_all(file_link.path().join(".croft")).unwrap();
std::os::unix::fs::symlink(&target, file_link.path().join(".croft/launch.json")).unwrap();
let err = add_to_croft_launch_json(file_link.path(), &d).unwrap_err();
assert!(err.contains("symlink"), "{err}");
let dir_link = tempfile::tempdir().unwrap();
std::os::unix::fs::symlink(elsewhere.path(), dir_link.path().join(".croft")).unwrap();
let err = add_to_croft_launch_json(dir_link.path(), &d).unwrap_err();
assert!(err.contains("symlink"), "{err}");
assert_eq!(std::fs::read_to_string(&target).unwrap(), "[]");
assert!(!elsewhere.path().join("launch.json").exists());
let planted = tempfile::tempdir().unwrap();
let dir = planted.path().join(".croft");
std::fs::create_dir_all(&dir).unwrap();
let mut first = ConfigDraft::new("node", RequestKind::Launch);
first.set(DraftField::Name, "First").unwrap();
add_to_croft_launch_json(planted.path(), &first).unwrap();
let before = std::fs::read_to_string(dir.join("launch.json")).unwrap();
let bait = elsewhere.path().join("bait.json");
std::fs::write(&bait, "bait").unwrap();
std::os::unix::fs::symlink(&bait, temp_candidate(&dir, 0)).unwrap();
add_to_croft_launch_json(planted.path(), &d).unwrap();
assert_eq!(
std::fs::read_to_string(&bait).unwrap(),
"bait",
"the link is not followed"
);
let names: Vec<String> = discover_configs(planted.path())
.into_iter()
.map(|c| c.name)
.collect();
assert_eq!(names, ["First", "Web"], "and the file is whole: {before}");
}
#[test]
fn input_variables_take_the_answers() {
let text = r#"{ "configurations": [
{ "name": "Run", "type": "python", "program": "app.py",
"args": ["--env", "${input:envName}", "--user", "${input:userName}"],
"env": { "ENV": "${input:envName}" } }
],
"inputs": [
{ "id": "envName", "type": "pickString", "description": "Environment",
"options": ["dev", { "label": "Staging", "value": "stg" }], "default": "dev" },
{ "id": "userName", "type": "promptString", "default": "alice", "password": true },
{ "id": "branch", "type": "command", "command": "git.branch" }
]}"#;
let cfg = config(text);
assert_eq!(cfg.input_refs(), ["envName", "userName"]);
let ask = inputs_to_ask(&cfg.input_refs(), &BTreeMap::new(), &cfg.inputs).unwrap();
assert_eq!(ask.len(), 2);
assert_eq!(ask[0].description, "Environment");
assert_eq!(ask[0].default.as_deref(), Some("dev"));
assert_eq!(
ask[0].kind,
InputKind::Pick {
options: vec![
(String::from("dev"), String::from("dev")),
(String::from("Staging"), String::from("stg")),
]
}
);
assert_eq!(ask[1].description, "userName", "no description: the id");
assert_eq!(ask[1].kind, InputKind::Prompt { password: true });
assert_eq!(
cfg.inputs[2].kind,
InputKind::Command {
command: String::from("git.branch")
}
);
let mut c = ctx();
c.inputs
.insert(String::from("envName"), String::from("stg"));
c.inputs
.insert(String::from("userName"), String::from("bob"));
let rc = resolve(&cfg, &c).unwrap();
assert_eq!(rc.args, ["--env", "stg", "--user", "bob"]);
assert_eq!(rc.env.get("ENV").map(String::as_str), Some("stg"));
let answered = BTreeMap::from([(String::from("envName"), String::from("x"))]);
let ask = inputs_to_ask(&cfg.input_refs(), &answered, &cfg.inputs).unwrap();
assert_eq!(ask.len(), 1, "an answered input is not asked again");
assert_eq!(ask[0].id, "userName");
}
#[test]
fn an_unknown_or_unanswered_input_is_refused() {
let text = r#"{ "configurations": [
{ "name": "Run", "type": "python", "program": "app.py",
"args": ["${input:nope}", "$input:plain", "${inputs:x}"] }
],
"inputs": [
{ "type": "promptString" },
{ "id": "odd", "type": "colorPicker" },
{ "id": "empty", "type": "pickString", "options": [] },
{ "id": "nope2", "type": "promptString" }
]}"#;
let cfg = config(text);
assert!(
cfg.inputs.len() == 1 && cfg.inputs[0].id == "nope2",
"{:?}",
cfg.inputs
);
assert_eq!(cfg.input_refs(), ["nope"]);
let err = inputs_to_ask(&cfg.input_refs(), &BTreeMap::new(), &cfg.inputs).unwrap_err();
assert!(
err.contains("${input:nope}") && err.contains("\"nope\""),
"{err}"
);
let err = resolve(&cfg, &ctx()).unwrap_err();
assert!(err.contains("${input:nope}: no answer"), "{err}");
let none = config(r#"[{ "name": "Plain", "type": "python", "program": "a.py" }]"#);
assert!(none.input_refs().is_empty() && none.inputs.is_empty());
}
fn attach_address(cfg: &str) -> Option<(String, u16)> {
let rc = resolve(&config(cfg), &ctx()).unwrap();
debugpy_connect_address(&debugpy_request(&rc, Path::new("python3")))
}
#[test]
fn a_debugpy_connect_attach_goes_to_the_listening_port() {
assert_eq!(
attach_address(
r#"[{ "name": "R", "type": "debugpy", "request": "attach",
"connect": { "host": "127.0.0.1", "port": 5679 } }]"#
),
Some((String::from("127.0.0.1"), 5679))
);
assert_eq!(
attach_address(
r#"[{ "name": "R", "type": "debugpy", "request": "attach",
"connect": { "host": "box", "port": "5680" } }]"#
),
Some((String::from("box"), 5680))
);
assert_eq!(
attach_address(
r#"[{ "name": "R", "type": "python", "request": "attach",
"host": "10.0.0.5", "port": 5678 }]"#
),
Some((String::from("10.0.0.5"), 5678))
);
assert_eq!(
attach_address(
r#"[{ "name": "R", "type": "debugpy", "request": "attach", "connect": { "port": 5678 } }]"#
),
Some((String::from("127.0.0.1"), 5678)),
"no host is this machine"
);
}
#[test]
fn a_debugpy_listen_attach_or_launch_keeps_the_local_adapter() {
for cfg in [
r#"[{ "name": "L", "type": "debugpy", "request": "attach", "listen": { "host": "0.0.0.0", "port": 5678 } }]"#,
r#"[{ "name": "P", "type": "debugpy", "request": "launch", "program": "a.py", "connect": { "port": 5678 } }]"#,
r#"[{ "name": "I", "type": "debugpy", "request": "attach", "processId": 4242 }]"#,
r#"[{ "name": "B", "type": "debugpy", "request": "attach", "connect": { "port": 70000 } }]"#,
] {
assert_eq!(attach_address(cfg), None, "{cfg}");
}
}
#[test]
fn a_node_launch_can_start_an_npm_script() {
let cfg = config(
r#"[{ "name": "npm start", "type": "node", "request": "launch",
"runtimeExecutable": "npm", "runtimeArgs": ["run", "start"],
"cwd": "${workspaceFolder}" }]"#,
);
let rc = resolve(&cfg, &ctx()).unwrap();
assert!(js_launch_has_target(&rc));
let req = js_request(&rc);
assert_eq!(req["arguments"]["type"], "pwa-node");
assert_eq!(req["arguments"]["runtimeExecutable"], "npm");
assert_eq!(req["arguments"]["runtimeArgs"], json!(["run", "start"]));
assert!(req["arguments"].get("program").is_none());
}
#[test]
fn a_node_terminal_command_becomes_the_runtime() {
let rc = |command: &str| {
let text = format!(
r#"[{{ "name": "dev", "type": "node-terminal", "request": "launch", "command": {} }}]"#,
json!(command)
);
resolve(&config(&text), &ctx()).unwrap()
};
let dev = rc("npm run dev -- --port 3001");
assert!(js_launch_has_target(&dev));
let req = js_request(&dev);
assert_eq!(req["arguments"]["type"], "pwa-node");
assert_eq!(req["arguments"]["runtimeExecutable"], "npm");
assert_eq!(
req["arguments"]["runtimeArgs"],
json!(["run", "dev", "--", "--port", "3001"])
);
assert!(req["arguments"].get("command").is_none());
let quoted = js_request(&rc("node 'my app.js'"));
assert_eq!(quoted["arguments"]["runtimeArgs"], json!(["my app.js"]));
let chained = js_request(&rc("npm run build && npm start"));
assert_eq!(chained["arguments"]["runtimeExecutable"], "sh");
assert_eq!(
chained["arguments"]["runtimeArgs"],
json!(["-c", "npm run build && npm start"])
);
let env = js_request(&rc("NODE_ENV=production npm start"));
assert_eq!(env["arguments"]["runtimeExecutable"], "sh");
assert_eq!(
env["arguments"]["runtimeArgs"],
json!(["-c", "NODE_ENV=production npm start"])
);
let flag = js_request(&rc("node --max-old-space-size=4096 app.js"));
assert_eq!(flag["arguments"]["runtimeExecutable"], "node");
}
#[test]
fn a_node_launch_with_nothing_to_run_has_no_target() {
let one = |text: &str| resolve(&config(text), &ctx()).unwrap();
let bare = one(r#"[{ "name": "n", "type": "node", "request": "launch" }]"#);
assert!(!js_launch_has_target(&bare));
let node = one(r#"[{ "name": "n", "type": "node", "command": "npm start" }]"#);
assert!(
!js_launch_has_target(&node),
"only node-terminal reads command"
);
assert_eq!(node.extra["command"], "npm start");
let explicit = one(
r#"[{ "name": "t", "type": "node-terminal", "command": "npm start",
"runtimeExecutable": "yarn", "runtimeArgs": ["dev"] }]"#,
);
assert_eq!(explicit.extra["runtimeExecutable"], "yarn");
assert_eq!(explicit.extra["runtimeArgs"], json!(["dev"]));
assert!(!explicit.extra.contains_key("command"));
let empty = one(r#"[{ "name": "t", "type": "node-terminal", "command": " " }]"#);
assert!(!js_launch_has_target(&empty));
let hollow = one(r#"[{ "name": "n", "type": "node", "request": "launch",
"runtimeExecutable": null, "runtimeArgs": [] }]"#);
assert!(!js_launch_has_target(&hollow));
let blank = one(r#"[{ "name": "n", "type": "node", "runtimeExecutable": "" }]"#);
assert!(!js_launch_has_target(&blank));
}
const CARGO_JSON_1392: &str = concat!(
r#"{"reason":"compiler-artifact","target":{"kind":["lib"],"name":"rc","src_path":"/w/src/lib.rs"},"profile":{"test":false},"executable":null}"#,
"\n",
r#"{"reason":"compiler-artifact","target":{"kind":["bin"],"name":"rc","src_path":"/w/src/main.rs"},"profile":{"test":false},"executable":"/w/target/debug/rc"}"#,
"\n",
r#"{"reason":"compiler-artifact","target":{"kind":["lib"],"name":"rc","src_path":"/w/src/lib.rs"},"profile":{"test":true},"executable":"/w/target/debug/deps/rc-1a2b"}"#,
"\n",
r#"{"reason":"build-finished","success":true}"#,
"\n"
);
fn cargo_cfg(cargo: &str) -> ResolvedConfig {
let text = format!(
r#"[{{ "name": "Debug executable 'rc'", "type": "lldb", "request": "launch", "cargo": {cargo}, "args": [] }}]"#
);
resolve(&config(&text), &ctx()).unwrap()
}
#[test]
fn a_cargo_launch_debugs_the_executable_its_filter_picks() {
let rc = cargo_cfg(
r#"{ "args": ["build", "--bin=rc", "--package=rc"], "filter": { "name": "rc", "kind": "bin" },
"env": { "RUSTFLAGS": "-g" }, "cwd": "sub" }"#,
);
assert!(rc.program.is_none());
let spec = cargo_spec(&rc).expect("a cargo block").unwrap();
assert_eq!(
cargo_build_args(&spec),
["build", "--bin=rc", "--package=rc", "--message-format=json"]
);
assert_eq!(spec.env.get("RUSTFLAGS").map(String::as_str), Some("-g"));
assert_eq!(spec.cwd, Some(PathBuf::from("sub")));
assert_eq!(
cargo_artifact(CARGO_JSON_1392, &spec).unwrap(),
PathBuf::from("/w/target/debug/rc")
);
let lib_tests = cargo_spec(&cargo_cfg(
r#"{ "args": ["test", "--lib"], "filter": { "kind": "lib" } }"#,
))
.unwrap()
.unwrap();
assert_eq!(
cargo_build_args(&lib_tests),
["test", "--no-run", "--lib", "--message-format=json"]
);
assert_eq!(
cargo_artifact(CARGO_JSON_1392, &lib_tests).unwrap(),
PathBuf::from("/w/target/debug/deps/rc-1a2b")
);
let mut with_program = rc.clone();
with_program.program = Some(String::from("/w/target/debug/rc"));
let req = lldb_request(&with_program);
assert!(req["arguments"].get("cargo").is_none(), "{req}");
assert_eq!(req["arguments"]["program"], "/w/target/debug/rc");
}
#[test]
fn a_cargo_launch_refuses_no_match_or_several() {
let spec = |cargo: &str| cargo_spec(&cargo_cfg(cargo)).unwrap();
let none = spec(r#"{ "args": ["build"], "filter": { "name": "nope" } }"#).unwrap();
let err = cargo_artifact(CARGO_JSON_1392, &none).unwrap_err();
assert!(
err.contains("no executable matching name \"nope\""),
"{err}"
);
let both = spec(r#"{ "args": ["build"] }"#).unwrap();
let err = cargo_artifact(CARGO_JSON_1392, &both).unwrap_err();
assert!(
err.contains("2 executables matching no filter") && err.contains("rc, rc-1a2b"),
"{err}"
);
assert!(spec(r#"{ "filter": { "name": "rc" } }"#).is_err());
assert!(spec(r#"{ "args": [] }"#).is_err());
let plain = config(r#"[{ "name": "P", "type": "lldb", "program": "/bin/true" }]"#);
assert!(cargo_spec(&resolve(&plain, &ctx()).unwrap()).is_none());
}
#[test]
fn cargo_builds_the_program_or_says_why_not() {
let tmp = tempfile::tempdir().unwrap();
let w = tmp.path();
std::fs::create_dir_all(w.join("src")).unwrap();
std::fs::write(
w.join("Cargo.toml"),
"[package]\nname = \"rc\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[workspace]\n",
)
.unwrap();
std::fs::write(w.join("src/main.rs"), "fn main() { println!(\"hi\"); }\n").unwrap();
let spec = CargoSpec {
args: vec![
String::from("build"),
String::from("--bin=rc"),
String::from("--offline"),
],
name: Some(String::from("rc")),
kind: Some(String::from("bin")),
env: BTreeMap::from([(
String::from("CARGO_TARGET_DIR"),
w.join("target").to_string_lossy().into_owned(),
)]),
cwd: None,
};
let exe = build_cargo_program(&spec, w).unwrap();
assert!(exe.is_file(), "{}", exe.display());
assert_eq!(exe.file_name().unwrap(), "rc");
std::fs::write(w.join("src/main.rs"), "fn main() { let x: u8 = \"no\"; }\n").unwrap();
let err = build_cargo_program(&spec, w).unwrap_err();
assert!(
err.starts_with("cargo build --bin=rc --offline failed: ")
&& err.contains("mismatched types"),
"{err}"
);
}
}