From 9d2c3905989c15c89370f8e0d47b050f92a4ecad Mon Sep 17 00:00:00 2001 From: Rajkumar Natarajan Date: Sun, 10 May 2026 19:14:01 -0700 Subject: [PATCH 1/3] issue-12: added support for haskell-debugger Update src/debugger.rs Co-authored-by: Finn Evers incorporate the review comments fix github actions incorporated the review comments Add debugger module in haskell.rs rebased with upstream removed unnessary files --- Cargo.lock | 101 +++++---- debug_adapter_schemas/haskell-debugger.json | 58 +++++ extension.toml | 2 + src/debugger.rs | 231 ++++++++++++++++++++ src/haskell.rs | 34 ++- 5 files changed, 383 insertions(+), 43 deletions(-) create mode 100644 debug_adapter_schemas/haskell-debugger.json create mode 100644 src/debugger.rs diff --git a/Cargo.lock b/Cargo.lock index d727ed3..8ad9039 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,9 +10,9 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" [[package]] name = "anyhow" -version = "1.0.97" +version = "1.0.102" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" [[package]] name = "auditable-serde" @@ -28,9 +28,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.9.0" +version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" [[package]] name = "cfg-if" @@ -179,13 +179,19 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.2" +version = "0.15.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" dependencies = [ "foldhash", ] +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + [[package]] name = "heck" version = "0.5.0" @@ -276,9 +282,9 @@ dependencies = [ [[package]] name = "id-arena" -version = "2.2.1" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" [[package]] name = "idna" @@ -303,20 +309,21 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.7.1" +version = "2.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.17.1", "serde", + "serde_core", ] [[package]] name = "itoa" -version = "1.0.15" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" [[package]] name = "leb128fmt" @@ -332,15 +339,15 @@ checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0" [[package]] name = "log" -version = "0.4.26" +version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" [[package]] name = "memchr" -version = "2.7.4" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" [[package]] name = "miniz_oxide" @@ -391,9 +398,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.94" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" dependencies = [ "unicode-ident", ] @@ -407,35 +414,40 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "ryu" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" - [[package]] name = "semver" -version = "1.0.26" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" dependencies = [ "serde", + "serde_core", ] [[package]] name = "serde" -version = "1.0.218" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.218" +version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", @@ -444,14 +456,15 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.140" +version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" dependencies = [ "itoa", "memchr", - "ryu", "serde", + "serde_core", + "zmij", ] [[package]] @@ -468,15 +481,15 @@ checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] name = "smallvec" -version = "1.14.0" +version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" [[package]] name = "spdx" -version = "0.10.8" +version = "0.10.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58b69356da67e2fc1f542c71ea7e654a361a79c938e4424392ecf4fa065d2193" +checksum = "c3e17e880bafaeb362a7b751ec46bdc5b61445a188f80e0606e68167cd540fa3" dependencies = [ "smallvec", ] @@ -527,9 +540,9 @@ checksum = "ea68304e134ecd095ac6c3574494fc62b909f416c4fca77e440530221e549d3d" [[package]] name = "unicode-ident" -version = "1.0.18" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" [[package]] name = "unicode-xid" @@ -591,7 +604,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f51cad774fb3c9461ab9bccc9c62dfb7388397b5deda31bf40e8108ccd678b2" dependencies = [ "bitflags", - "hashbrown", + "hashbrown 0.15.5", "indexmap", "semver", ] @@ -796,3 +809,9 @@ dependencies = [ "quote", "syn", ] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/debug_adapter_schemas/haskell-debugger.json b/debug_adapter_schemas/haskell-debugger.json new file mode 100644 index 0000000..c3bfa80 --- /dev/null +++ b/debug_adapter_schemas/haskell-debugger.json @@ -0,0 +1,58 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Haskell Debugger (hdb)", + "description": "Launch configuration for the Well-Typed Haskell debugger (hdb). Requires GHC 9.14+ and the `hdb` executable on PATH. See https://well-typed.github.io/haskell-debugger/", + "type": "object", + "required": ["projectRoot", "entryFile"], + "properties": { + "type": { + "type": "string", + "const": "haskell-debugger", + "description": "Debug adapter id (matches VS Code `launch.json` type)." + }, + "request": { + "type": "string", + "enum": ["launch"], + "default": "launch", + "description": "hdb currently supports launch sessions only." + }, + "name": { + "type": "string", + "description": "Label for this debug session in the UI." + }, + "projectRoot": { + "type": "string", + "description": "Absolute path to the project root (Cabal/Stack/hie-bios cradle root)." + }, + "entryFile": { + "type": "string", + "description": "Path to the Haskell source file containing the entry point, relative to `projectRoot`." + }, + "entryPoint": { + "type": "string", + "description": "Name of the function to start (`main` or another top-level binding).", + "default": "main" + }, + "entryArgs": { + "type": "array", + "items": { "type": "string" }, + "description": "Arguments for the entry point. For `main`, these are passed as program arguments (`getArgs`).", + "default": [] + }, + "extraGhcArgs": { + "type": "array", + "items": { "type": "string" }, + "description": "Extra flags passed to the GHC invocation inferred by hie-bios.", + "default": [] + }, + "internalInterpreter": { + "type": "boolean", + "description": "Whether to use the internal GHCi interpreter for debugging.", + "default": false + }, + "debugServer": { + "type": "integer", + "description": "If set, Zed connects to an already-running `hdb server` on this TCP port instead of spawning `hdb`. Same as VS Code `launch.json` `debugServer`. See https://well-typed.github.io/haskell-debugger/" + } + } +} diff --git a/extension.toml b/extension.toml index 1d42f6c..bd17562 100644 --- a/extension.toml +++ b/extension.toml @@ -18,6 +18,8 @@ languages = [ # "Alex", # https://github.com/haskell/haskell-language-server/issues/4820 ] +[debug_adapters.haskell-debugger] + [grammars.haskell] repository = "https://github.com/tree-sitter/tree-sitter-haskell" commit = "c30d812bc90827f1a54106a25bc9a6307f5cdcec" # 0.23.1 diff --git a/src/debugger.rs b/src/debugger.rs new file mode 100644 index 0000000..c5666fc --- /dev/null +++ b/src/debugger.rs @@ -0,0 +1,231 @@ +use std::path::{Component, Path}; + +use zed::serde_json::{self, json}; +use zed_extension_api::{ + self as zed, resolve_tcp_template, DebugAdapterBinary, DebugConfig, DebugRequest, + DebugScenario, DebugTaskDefinition, Result, StartDebuggingRequestArguments, + StartDebuggingRequestArgumentsRequest, TcpArguments, TcpArgumentsTemplate, +}; + +pub(crate) const HASKELL_DEBUG_ADAPTER: &str = "haskell-debugger"; + +fn merge_haskell_debug_configuration( + task: &DebugTaskDefinition, +) -> Result { + let mut map: serde_json::Map = serde_json::from_str(&task.config) + .map_err(|err| { + format!("Invalid Haskell debug configuration (expected JSON object): {err}") + })?; + map.entry("type".to_string()) + .or_insert(json!(HASKELL_DEBUG_ADAPTER)); + map.entry("request".to_string()) + .or_insert(json!("launch")); + map.entry("name".to_string()) + .or_insert(json!(task.label.clone())); + Ok(serde_json::Value::Object(map)) +} + +fn project_root_from_config(config: &serde_json::Value) -> Option { + config + .get("projectRoot") + .and_then(|v| v.as_str()) + .map(str::to_string) +} + +fn resolve_hdb_binary( + user_path: Option, + worktree: &zed::Worktree, +) -> Result { + if let Some(path) = user_path.filter(|p| !p.is_empty()) { + return Ok(path); + } + worktree.which("hdb").ok_or_else(|| { + "Could not find `hdb` on PATH. Install the Haskell debugger from https://well-typed.github.io/haskell-debugger/" + .to_string() + }) +} + +fn entry_path_relative_to_root(project_root: &str, program: &str) -> Result { + if program.is_empty() { + return Err( + "Program path is empty. Choose the Haskell entry file (e.g. app/Main.hs).".into(), + ); + } + let root = Path::new(project_root); + let program_path = Path::new(program); + let absolute_program = if program_path.is_absolute() { + program_path.to_path_buf() + } else { + root.join(program_path) + }; + let relative = absolute_program.strip_prefix(root).map_err(|_| { + format!( + "Program path `{}` must be inside project root `{}`", + absolute_program.display(), + project_root + ) + })?; + path_components_to_string(relative) +} + +fn path_components_to_string(path: &Path) -> Result { + path.components() + .filter_map(|c| match c { + Component::Normal(part) => Some(Ok(part.to_string_lossy().into_owned())), + Component::CurDir => None, + Component::ParentDir => Some(Err("Program path must not contain `..`.".into())), + _ => Some(Err("Invalid program path.".into())), + }) + .collect::, _>>() + .and_then(|parts| { + if parts.is_empty() { + Err("Resolved entry file path is empty.".into()) + } else { + Ok(parts.join(std::path::MAIN_SEPARATOR_STR)) + } + }) +} + +pub(crate) fn get_dap_binary( + adapter_name: String, + task: DebugTaskDefinition, + user_provided_debug_adapter_path: Option, + worktree: &zed::Worktree, +) -> Result { + if adapter_name != HASKELL_DEBUG_ADAPTER { + return Err(format!( + "Unknown Haskell debug adapter: {adapter_name} (expected {HASKELL_DEBUG_ADAPTER})" + )); + } + + let merged = merge_haskell_debug_configuration(&task)?; + let request = dap_request_kind(adapter_name.as_str(), &merged)?; + let configuration = serde_json::to_string(&merged) + .map_err(|err| format!("Failed to serialize debug configuration: {err}"))?; + + let cwd = project_root_from_config(&merged).or_else(|| Some(worktree.root_path())); + let envs = worktree.shell_env(); + + if let Some(debug_server) = merged.get("debugServer").and_then(|v| v.as_u64()) { + let port = u16::try_from(debug_server) + .map_err(|_| format!("`debugServer` port must fit in 16 bits, got {debug_server}"))?; + let tcp_connection = task.tcp_connection.unwrap_or(TcpArgumentsTemplate { + host: None, + port: Some(port), + timeout: None, + }); + let tcp = resolve_tcp_template(tcp_connection)?; + return Ok(DebugAdapterBinary { + command: None, + arguments: vec![], + envs, + cwd, + connection: Some(tcp), + request_args: StartDebuggingRequestArguments { + configuration, + request, + }, + }); + } + + let tcp_connection = task.tcp_connection.unwrap_or(TcpArgumentsTemplate { + host: None, + port: None, + timeout: None, + }); + let TcpArguments { + host, + port, + timeout, + } = resolve_tcp_template(tcp_connection)?; + + let command = resolve_hdb_binary(user_provided_debug_adapter_path, worktree)?; + let mut arguments = vec!["server".to_string(), "--port".to_string(), port.to_string()]; + if merged + .get("internalInterpreter") + .and_then(|v| v.as_bool()) + .unwrap_or(false) + { + arguments.push("--internal-interpreter".to_string()); + } + + Ok(DebugAdapterBinary { + command: Some(command), + arguments, + envs, + cwd, + connection: Some(TcpArguments { + host, + port, + timeout, + }), + request_args: StartDebuggingRequestArguments { + configuration, + request, + }, + }) +} + +pub(crate) fn dap_request_kind( + adapter_name: &str, + config: &serde_json::Value, +) -> Result { + if adapter_name != HASKELL_DEBUG_ADAPTER { + return Err(format!( + "Unknown Haskell debug adapter: {adapter_name} (expected {HASKELL_DEBUG_ADAPTER})" + )); + } + + match config.get("request").and_then(|v| v.as_str()) { + Some("attach") => Err( + "The Haskell debugger (hdb) does not support attach; use request \"launch\".".into(), + ), + Some("launch") | None => Ok(StartDebuggingRequestArgumentsRequest::Launch), + Some(other) => Err(format!( + "Unsupported `request` for Haskell debugger: {other}" + )), + } +} + +pub(crate) fn dap_config_to_scenario(config: DebugConfig) -> Result { + if config.adapter != HASKELL_DEBUG_ADAPTER { + return Err(format!( + "This extension only defines the `{HASKELL_DEBUG_ADAPTER}` debug adapter (got {}).", + config.adapter + )); + } + + match &config.request { + DebugRequest::Attach(_) => Err( + "The Haskell debugger (hdb) does not support attach in Zed; use a launch configuration." + .into(), + ), + DebugRequest::Launch(launch) => { + let project_root = launch.cwd.clone().ok_or_else(|| { + "Set working directory to your Cabal/Stack project root; it becomes `projectRoot` for hdb." + .to_string() + })?; + let entry_file = entry_path_relative_to_root(&project_root, &launch.program)?; + + let body = json!({ + "projectRoot": project_root, + "entryFile": entry_file, + "entryPoint": "main", + "entryArgs": launch.args.clone(), + "extraGhcArgs": [], + }); + + Ok(DebugScenario { + label: config.label, + adapter: config.adapter, + build: None, + config: body.to_string(), + tcp_connection: Some(TcpArgumentsTemplate { + host: None, + port: None, + timeout: None, + }), + }) + } + } +} diff --git a/src/haskell.rs b/src/haskell.rs index 0c887e2..a5442f1 100644 --- a/src/haskell.rs +++ b/src/haskell.rs @@ -1,9 +1,11 @@ +mod debugger; + use zed_extension_api::{ self as zed, lsp::{Symbol, SymbolKind}, - serde_json, settings::LspSettings, - CodeLabel, CodeLabelSpan, Result, + CodeLabel, CodeLabelSpan, DebugAdapterBinary, DebugConfig, DebugScenario, DebugTaskDefinition, + Result, StartDebuggingRequestArgumentsRequest, }; struct HaskellExtension; @@ -91,6 +93,34 @@ impl zed::Extension for HaskellExtension { code, }) } + + fn get_dap_binary( + &mut self, + adapter_name: String, + task: DebugTaskDefinition, + user_provided_debug_adapter_path: Option, + worktree: &zed::Worktree, + ) -> Result { + crate::debugger::get_dap_binary( + adapter_name, + task, + user_provided_debug_adapter_path, + worktree, + ) + } + + fn dap_request_kind( + &mut self, + adapter_name: String, + config: zed::serde_json::Value, + ) -> Result { + crate::debugger::dap_request_kind(&adapter_name, &config) + } + + fn dap_config_to_scenario(&mut self, config: DebugConfig) -> Result { + crate::debugger::dap_config_to_scenario(config) + } + } zed::register_extension!(HaskellExtension); From 95754fba299c9ddd2176c274f28f94e8950b37f6 Mon Sep 17 00:00:00 2001 From: MrSubidubi Date: Mon, 20 Jul 2026 19:05:10 +0200 Subject: [PATCH 2/3] CI --- src/haskell.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/haskell.rs b/src/haskell.rs index a5442f1..ed081cc 100644 --- a/src/haskell.rs +++ b/src/haskell.rs @@ -50,7 +50,7 @@ impl zed::Extension for HaskellExtension { &mut self, server_id: &zed::LanguageServerId, worktree: &zed::Worktree, - ) -> Result> { + ) -> Result> { Ok(LspSettings::for_worktree(server_id.as_ref(), worktree) .ok() .and_then(|s| s.settings)) @@ -120,7 +120,6 @@ impl zed::Extension for HaskellExtension { fn dap_config_to_scenario(&mut self, config: DebugConfig) -> Result { crate::debugger::dap_config_to_scenario(config) } - } zed::register_extension!(HaskellExtension); From ee0c1236efaf8f229303a38ab0b75bc94be674fa Mon Sep 17 00:00:00 2001 From: MrSubidubi Date: Mon, 20 Jul 2026 19:06:26 +0200 Subject: [PATCH 3/3] Format that one too --- src/debugger.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/debugger.rs b/src/debugger.rs index c5666fc..a14086e 100644 --- a/src/debugger.rs +++ b/src/debugger.rs @@ -18,8 +18,7 @@ fn merge_haskell_debug_configuration( })?; map.entry("type".to_string()) .or_insert(json!(HASKELL_DEBUG_ADAPTER)); - map.entry("request".to_string()) - .or_insert(json!("launch")); + map.entry("request".to_string()).or_insert(json!("launch")); map.entry("name".to_string()) .or_insert(json!(task.label.clone())); Ok(serde_json::Value::Object(map))