Debugger implementation (#13433)

###  DISCLAIMER

> As of 6th March 2025, debugger is still in development. We plan to
merge it behind a staff-only feature flag for staff use only, followed
by non-public release and then finally a public one (akin to how Git
panel release was handled). This is done to ensure the best experience
when it gets released.

### END OF DISCLAIMER 

**The current state of the debugger implementation:**


https://github.com/user-attachments/assets/c4deff07-80dd-4dc6-ad2e-0c252a478fe9


https://github.com/user-attachments/assets/e1ed2345-b750-4bb6-9c97-50961b76904f

----

All the todo's are in the following channel, so it's easier to work on
this together:
https://zed.dev/channel/zed-debugger-11370

If you are on Linux, you can use the following command to join the
channel:
```cli
zed https://zed.dev/channel/zed-debugger-11370 
```

## Current Features

- Collab
  - Breakpoints
    - Sync when you (re)join a project
    - Sync when you add/remove a breakpoint
  - Sync active debug line
  - Stack frames
    - Click on stack frame
      - View variables that belong to the stack frame
      - Visit the source file
    - Restart stack frame (if adapter supports this)
  - Variables
  - Loaded sources
  - Modules
  - Controls
    - Continue
    - Step back
      - Stepping granularity (configurable)
    - Step into
      - Stepping granularity (configurable)
    - Step over
      - Stepping granularity (configurable)
    - Step out
      - Stepping granularity (configurable)
  - Debug console
- Breakpoints
  - Log breakpoints
  - line breakpoints
  - Persistent between zed sessions (configurable)
  - Multi buffer support
  - Toggle disable/enable all breakpoints
- Stack frames
  - Click on stack frame
    - View variables that belong to the stack frame
    - Visit the source file
    - Show collapsed stack frames
  - Restart stack frame (if adapter supports this)
- Loaded sources
  - View all used loaded sources if supported by adapter.
- Modules
  - View all used modules (if adapter supports this)
- Variables
  - Copy value
  - Copy name
  - Copy memory reference
  - Set value (if adapter supports this)
  - keyboard navigation
- Debug Console
  - See logs
  - View output that was sent from debug adapter
    - Output grouping
  - Evaluate code
    - Updates the variable list
    - Auto completion
- If not supported by adapter, we will show auto-completion for existing
variables
- Debug Terminal
- Run custom commands and change env values right inside your Zed
terminal
- Attach to process (if adapter supports this)
  - Process picker
- Controls
  - Continue
  - Step back
    - Stepping granularity (configurable)
  - Step into
    - Stepping granularity (configurable)
  - Step over
    - Stepping granularity (configurable)
  - Step out
    - Stepping granularity (configurable)
  - Disconnect
  - Restart
  - Stop
- Warning when a debug session exited without hitting any breakpoint
- Debug view to see Adapter/RPC log messages
- Testing
  - Fake debug adapter
    - Fake requests & events

---

Release Notes:

- N/A

---------

Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
Co-authored-by: Anthony Eid <hello@anthonyeid.me>
Co-authored-by: Anthony <anthony@zed.dev>
Co-authored-by: Piotr Osiewicz <peterosiewicz@gmail.com>
Co-authored-by: Piotr <piotr@zed.dev>
This commit is contained in:
Remco Smits 2025-03-18 17:55:25 +01:00 committed by GitHub
parent ed4e654fdf
commit 41a60ffecf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
156 changed files with 25840 additions and 451 deletions

View file

@ -5,18 +5,26 @@ edition.workspace = true
publish.workspace = true
license = "GPL-3.0-or-later"
[features]
test-support = [
"gpui/test-support",
"util/test-support"
]
[lints]
workspace = true
[dependencies]
anyhow.workspace = true
collections.workspace = true
dap-types.workspace = true
futures.workspace = true
gpui.workspace = true
hex.workspace = true
parking_lot.workspace = true
schemars.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_json_lenient.workspace = true
sha2.workspace = true
shellexpand.workspace = true

View file

@ -0,0 +1,227 @@
use schemars::{gen::SchemaSettings, JsonSchema};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::Ipv4Addr;
use std::path::PathBuf;
use util::ResultExt;
use crate::{TaskTemplate, TaskTemplates, TaskType};
impl Default for DebugConnectionType {
fn default() -> Self {
DebugConnectionType::TCP(TCPHost::default())
}
}
/// Represents the host information of the debug adapter
#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
pub struct TCPHost {
/// The port that the debug adapter is listening on
///
/// Default: We will try to find an open port
pub port: Option<u16>,
/// The host that the debug adapter is listening too
///
/// Default: 127.0.0.1
pub host: Option<Ipv4Addr>,
/// The max amount of time in milliseconds to connect to a tcp DAP before returning an error
///
/// Default: 2000ms
pub timeout: Option<u64>,
}
impl TCPHost {
/// Get the host or fallback to the default host
pub fn host(&self) -> Ipv4Addr {
self.host.unwrap_or_else(|| Ipv4Addr::new(127, 0, 0, 1))
}
}
/// Represents the attach request information of the debug adapter
#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
pub struct AttachConfig {
/// The processId to attach to, if left empty we will show a process picker
#[serde(default)]
pub process_id: Option<u32>,
}
/// Represents the type that will determine which request to call on the debug adapter
#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum DebugRequestType {
/// Call the `launch` request on the debug adapter
#[default]
Launch,
/// Call the `attach` request on the debug adapter
Attach(AttachConfig),
}
/// The Debug adapter to use
#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
#[serde(rename_all = "lowercase", tag = "adapter")]
pub enum DebugAdapterKind {
/// Manually setup starting a debug adapter
/// The argument within is used to start the DAP
Custom(CustomArgs),
/// Use debugpy
Python(TCPHost),
/// Use vscode-php-debug
Php(TCPHost),
/// Use vscode-js-debug
Javascript(TCPHost),
/// Use delve
Go(TCPHost),
/// Use lldb
Lldb,
/// Use GDB's built-in DAP support
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Gdb,
/// Used for integration tests
#[cfg(any(test, feature = "test-support"))]
#[serde(skip)]
Fake((bool, dap_types::Capabilities)),
}
impl DebugAdapterKind {
/// Returns the display name for the adapter kind
pub fn display_name(&self) -> &str {
match self {
Self::Custom(_) => "Custom",
Self::Python(_) => "Python",
Self::Php(_) => "PHP",
Self::Javascript(_) => "JavaScript",
Self::Lldb => "LLDB",
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Self::Gdb => "GDB",
Self::Go(_) => "Go",
#[cfg(any(test, feature = "test-support"))]
Self::Fake(_) => "Fake",
}
}
}
/// Custom arguments used to setup a custom debugger
#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
pub struct CustomArgs {
/// The connection that a custom debugger should use
#[serde(flatten)]
pub connection: DebugConnectionType,
/// The cli command used to start the debug adapter e.g. `python3`, `node` or the adapter binary
pub command: String,
/// The cli arguments used to start the debug adapter
pub args: Option<Vec<String>>,
/// The cli envs used to start the debug adapter
pub envs: Option<HashMap<String, String>>,
}
/// Represents the configuration for the debug adapter
#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct DebugAdapterConfig {
/// Name of the debug task
pub label: String,
/// The type of adapter you want to use
#[serde(flatten)]
pub kind: DebugAdapterKind,
/// The type of request that should be called on the debug adapter
#[serde(default)]
pub request: DebugRequestType,
/// The program that you trying to debug
pub program: Option<String>,
/// The current working directory of your project
pub cwd: Option<PathBuf>,
/// Additional initialization arguments to be sent on DAP initialization
pub initialize_args: Option<serde_json::Value>,
/// Whether the debug adapter supports attaching to a running process.
pub supports_attach: bool,
}
/// Represents the type of the debugger adapter connection
#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
#[serde(rename_all = "lowercase", tag = "connection")]
pub enum DebugConnectionType {
/// Connect to the debug adapter via TCP
TCP(TCPHost),
/// Connect to the debug adapter via STDIO
STDIO,
}
/// This struct represent a user created debug task
#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub struct DebugTaskDefinition {
/// The adapter to run
#[serde(flatten)]
kind: DebugAdapterKind,
/// The type of request that should be called on the debug adapter
#[serde(default)]
request: DebugRequestType,
/// Name of the debug task
label: String,
/// Program to run the debugger on
program: Option<String>,
/// The current working directory of your project
cwd: Option<String>,
/// Additional initialization arguments to be sent on DAP initialization
initialize_args: Option<serde_json::Value>,
}
impl DebugTaskDefinition {
/// Translate from debug definition to a task template
pub fn to_zed_format(self) -> anyhow::Result<TaskTemplate> {
let command = "".to_string();
let cwd = self.cwd.clone().map(PathBuf::from).take_if(|p| p.exists());
let task_type = TaskType::Debug(DebugAdapterConfig {
label: self.label.clone(),
kind: self.kind,
request: self.request,
program: self.program,
cwd: cwd.clone(),
initialize_args: self.initialize_args,
supports_attach: true,
});
let args: Vec<String> = Vec::new();
Ok(TaskTemplate {
label: self.label,
command,
args,
task_type,
cwd: self.cwd,
..Default::default()
})
}
}
/// A group of Debug Tasks defined in a JSON file.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(transparent)]
pub struct DebugTaskFile(pub Vec<DebugTaskDefinition>);
impl DebugTaskFile {
/// Generates JSON schema of Tasks JSON template format.
pub fn generate_json_schema() -> serde_json_lenient::Value {
let schema = SchemaSettings::draft07()
.with(|settings| settings.option_add_null_type = false)
.into_generator()
.into_root_schema_for::<Self>();
serde_json_lenient::to_value(schema).unwrap()
}
}
impl TryFrom<DebugTaskFile> for TaskTemplates {
type Error = anyhow::Error;
fn try_from(value: DebugTaskFile) -> Result<Self, Self::Error> {
let templates = value
.0
.into_iter()
.filter_map(|debug_definition| debug_definition.to_zed_format().log_err())
.collect();
Ok(Self(templates))
}
}

View file

@ -1,6 +1,7 @@
//! Baseline interface of Tasks in Zed: all tasks in Zed are intended to use those for implementing their own logic.
#![deny(missing_docs)]
mod debug_format;
pub mod static_source;
mod task_template;
mod vscode_format;
@ -13,7 +14,13 @@ use std::borrow::Cow;
use std::path::PathBuf;
use std::str::FromStr;
pub use task_template::{HideStrategy, RevealStrategy, TaskTemplate, TaskTemplates};
pub use debug_format::{
AttachConfig, CustomArgs, DebugAdapterConfig, DebugAdapterKind, DebugConnectionType,
DebugRequestType, DebugTaskDefinition, DebugTaskFile, TCPHost,
};
pub use task_template::{
HideStrategy, RevealStrategy, TaskModal, TaskTemplate, TaskTemplates, TaskType,
};
pub use vscode_format::VsCodeTaskFile;
pub use zed_actions::RevealTarget;
@ -54,6 +61,8 @@ pub struct SpawnInTerminal {
pub hide: HideStrategy,
/// Which shell to use when spawning the task.
pub shell: Shell,
/// Tells debug tasks which program to debug
pub program: Option<String>,
/// Whether to show the task summary line in the task output (sucess/failure).
pub show_summary: bool,
/// Whether to show the command line in the task output.
@ -88,6 +97,28 @@ impl ResolvedTask {
&self.original_task
}
/// Get the task type that determines what this task is used for
/// And where is it shown in the UI
pub fn task_type(&self) -> TaskType {
self.original_task.task_type.clone()
}
/// Get the configuration for the debug adapter that should be used for this task.
pub fn resolved_debug_adapter_config(&self) -> Option<DebugAdapterConfig> {
match self.original_task.task_type.clone() {
TaskType::Script => None,
TaskType::Debug(mut adapter_config) => {
if let Some(resolved) = &self.resolved {
adapter_config.label = resolved.label.clone();
adapter_config.program = resolved.program.clone().or(adapter_config.program);
adapter_config.cwd = resolved.cwd.clone().or(adapter_config.cwd);
}
Some(adapter_config)
}
}
}
/// Variables that were substituted during the task template resolution.
pub fn substituted_variables(&self) -> &HashSet<VariableName> {
&self.substituted_variables

View file

@ -9,8 +9,8 @@ use sha2::{Digest, Sha256};
use util::{truncate_and_remove_front, ResultExt};
use crate::{
ResolvedTask, RevealTarget, Shell, SpawnInTerminal, TaskContext, TaskId, VariableName,
ZED_VARIABLE_NAME_PREFIX,
debug_format::DebugAdapterConfig, ResolvedTask, RevealTarget, Shell, SpawnInTerminal,
TaskContext, TaskId, VariableName, ZED_VARIABLE_NAME_PREFIX,
};
/// A template definition of a Zed task to run.
@ -58,6 +58,9 @@ pub struct TaskTemplate {
/// * `on_success` — hide the terminal tab on task success only, otherwise behaves similar to `always`.
#[serde(default)]
pub hide: HideStrategy,
/// If this task should start a debugger or not
#[serde(default, skip)]
pub task_type: TaskType,
/// Represents the tags which this template attaches to. Adding this removes this task from other UI.
#[serde(default)]
pub tags: Vec<String>,
@ -72,6 +75,72 @@ pub struct TaskTemplate {
pub show_command: bool,
}
/// Represents the type of task that is being ran
#[derive(Default, Deserialize, Serialize, Eq, PartialEq, JsonSchema, Clone, Debug)]
#[serde(rename_all = "snake_case", tag = "type")]
#[expect(clippy::large_enum_variant)]
pub enum TaskType {
/// Act like a typically task that runs commands
#[default]
Script,
/// This task starts the debugger for a language
Debug(DebugAdapterConfig),
}
#[cfg(test)]
mod deserialization_tests {
use crate::{DebugAdapterKind, TCPHost};
use super::*;
use serde_json::json;
#[test]
fn deserialize_task_type_script() {
let json = json!({"type": "script"});
let task_type: TaskType =
serde_json::from_value(json).expect("Failed to deserialize TaskType::Script");
assert_eq!(task_type, TaskType::Script);
}
#[test]
fn deserialize_task_type_debug() {
let adapter_config = DebugAdapterConfig {
label: "test config".into(),
kind: DebugAdapterKind::Python(TCPHost::default()),
request: crate::DebugRequestType::Launch,
program: Some("main".to_string()),
supports_attach: false,
cwd: None,
initialize_args: None,
};
let json = json!({
"label": "test config",
"type": "debug",
"adapter": "python",
"program": "main",
"supports_attach": false,
});
let task_type: TaskType =
serde_json::from_value(json).expect("Failed to deserialize TaskType::Debug");
if let TaskType::Debug(config) = task_type {
assert_eq!(config, adapter_config);
} else {
panic!("Expected TaskType::Debug");
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
/// The type of task modal to spawn
pub enum TaskModal {
/// Show regular tasks
ScriptModal,
/// Show debug tasks
DebugModal,
}
/// What to do with the terminal pane and tab, after the command was started.
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
@ -122,7 +191,9 @@ impl TaskTemplate {
/// Every [`ResolvedTask`] gets a [`TaskId`], based on the `id_base` (to avoid collision with various task sources),
/// and hashes of its template and [`TaskContext`], see [`ResolvedTask`] fields' documentation for more details.
pub fn resolve_task(&self, id_base: &str, cx: &TaskContext) -> Option<ResolvedTask> {
if self.label.trim().is_empty() || self.command.trim().is_empty() {
if self.label.trim().is_empty()
|| (self.command.trim().is_empty() && matches!(self.task_type, TaskType::Script))
{
return None;
}
@ -198,6 +269,22 @@ impl TaskTemplate {
&mut substituted_variables,
)?;
let program = match &self.task_type {
TaskType::Script => None,
TaskType::Debug(adapter_config) => {
if let Some(program) = &adapter_config.program {
Some(substitute_all_template_variables_in_str(
program,
&task_variables,
&variable_names,
&mut substituted_variables,
)?)
} else {
None
}
}
};
let task_hash = to_hex_hash(self)
.context("hashing task template")
.log_err()?;
@ -253,6 +340,7 @@ impl TaskTemplate {
reveal_target: self.reveal_target,
hide: self.hide,
shell: self.shell.clone(),
program,
show_summary: self.show_summary,
show_command: self.show_command,
show_rerun: true,