Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Python attach feature #97

Draft
wants to merge 30 commits into
base: debugger
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7919bc3
WIP add toolchain for python
RemcoSmitsDev Jan 16, 2025
7c55f4c
Integrate toolchain store with dap_store & use it for debugpy
Anthony-Eid Jan 16, 2025
4ded4d1
Merge branch 'debugger' into use-toolchain-for-python
RemcoSmitsDev Jan 21, 2025
03626e8
Add python attach implementation
RemcoSmitsDev Jan 21, 2025
cc545ff
Merge branch 'debugger' into use-toolchain-for-python
Anthony-Eid Jan 21, 2025
fa6aec3
Wip require worktree to start debug session
RemcoSmitsDev Jan 21, 2025
3e1131a
Move start debug session to project for determing the worktree
RemcoSmitsDev Jan 22, 2025
f16a458
Make all tests pass again
RemcoSmitsDev Jan 22, 2025
402a1b3
Make collab tests pass
RemcoSmitsDev Jan 22, 2025
a0b9bf5
Use notify instead of manual notification
RemcoSmitsDev Jan 22, 2025
61469bb
Use reference instead of clone
RemcoSmitsDev Jan 22, 2025
8ba15ae
Revert "Use reference instead of clone"
RemcoSmitsDev Jan 22, 2025
898e613
Revert "Use notify instead of manual notification"
RemcoSmitsDev Jan 22, 2025
56c883d
Revert debugger branch merge
RemcoSmitsDev Jan 22, 2025
59a480d
Revert "Revert debugger branch merge"
RemcoSmitsDev Jan 22, 2025
c4688fe
Clean up
RemcoSmitsDev Jan 22, 2025
af696bb
Make node runtime required
RemcoSmitsDev Jan 22, 2025
2014307
Pass worktree id into get_environment
RemcoSmitsDev Jan 22, 2025
abbb4ba
Fix use the resolved debug adapter config
RemcoSmitsDev Jan 22, 2025
fe7f08a
Merge branch 'use-toolchain-for-python' into python-attach
RemcoSmitsDev Jan 22, 2025
0d86235
Fix clippy
RemcoSmitsDev Jan 22, 2025
4f7b27e
Merge branch 'debugger' into use-toolchain-for-python
RemcoSmitsDev Jan 29, 2025
9192ed7
Add fallback if toolchain could not be found to common binary names
RemcoSmitsDev Jan 29, 2025
0c20d0d
Merge branch 'use-toolchain-for-python' into python-attach
RemcoSmitsDev Jan 29, 2025
24c484e
Merge branch 'debugger' into python-attach
RemcoSmitsDev Feb 1, 2025
357019b
Only consider process ids that are bigger then o
RemcoSmitsDev Feb 1, 2025
8c04f95
Filter out all NULL values as python seem to only check if the key is…
RemcoSmitsDev Feb 1, 2025
5badf70
Merge branch 'debugger' into python-attach
RemcoSmitsDev Feb 3, 2025
b5305c6
Update debugger.md
RemcoSmitsDev Feb 3, 2025
94dd31f
Merge branch 'debugger' into python-attach
RemcoSmitsDev Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions crates/dap_adapters/src/python.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use crate::*;
use dap::transport::{TcpTransport, Transport};
use dap::{
transport::{TcpTransport, Transport},
DebugRequestType,
};
use gpui::AsyncApp;
use std::{ffi::OsStr, net::Ipv4Addr, path::PathBuf, sync::Arc};
use regex::Regex;
use std::{collections::HashMap, ffi::OsStr, net::Ipv4Addr, path::PathBuf, sync::Arc};
use sysinfo::{Pid, Process};

pub(crate) struct PythonDebugAdapter {
port: u16,
Expand Down Expand Up @@ -131,10 +136,45 @@ impl DebugAdapter for PythonDebugAdapter {
}

fn request_args(&self, config: &DebugAdapterConfig) -> Value {
json!({
let pid = if let DebugRequestType::Attach(attach_config) = &config.request {
attach_config.process_id.filter(|pid| *pid > 0)
} else {
None
};

let mut config = json!({
"request": match config.request {
DebugRequestType::Launch => "launch",
DebugRequestType::Attach(_) => "attach",
},
"processId": pid,
"program": config.program,
"subProcess": true,
"cwd": config.cwd,
})
});

if let Value::Object(config) = &mut config {
config.retain(|_, v| !v.is_null());
}

config
}

fn supports_attach(&self) -> bool {
true
}

fn attach_processes<'a>(
&self,
processes: &'a HashMap<Pid, Process>,
) -> Option<Vec<(&'a Pid, &'a Process)>> {
let regex = Regex::new(r"(?i)^(?:python3|python|py)(?:$|\b)").unwrap();

Some(
processes
.iter()
.filter(|(_, process)| regex.is_match(&process.name().to_string_lossy()))
.collect::<Vec<_>>(),
)
}
}
2 changes: 1 addition & 1 deletion docs/src/debugger.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ To create a custom debug configuration you have to create a `.zed/debug.json` fi

### Using Attach [WIP]

Only javascript and lldb supports starting a debug session using attach.
Only JavaScript, Python and lldb supports starting a debug session using attach.

When using the attach request with a process ID the syntax is as follows:

Expand Down