1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! Preparing a debugger off the UI thread (#2803): the Python debug venv
//! and the JavaScript debugger (node and the js-debug server) are made ready
//! on a job, and the launches that need them wait, parked, and start when
//! the job answers. A launch whose tooling is ready starts at once, as
//! before.
use super::*;
use crate::dap::install::{Ready, Tooling};
/// A debug launch parked until its debugger is ready, with what it needs to
/// start again exactly as asked.
pub(in crate::app) enum ParkedLaunch {
/// F5 on a Python file.
PythonFile(PathBuf),
/// F5 on a JavaScript or TypeScript file.
JsFile(PathBuf),
/// A launch.json configuration, and where it goes.
Resolved(Box<crate::dap::configs::ResolvedConfig>, LaunchSlot),
/// Debug Test, for the test `name` in `file`.
Test { name: String, file: Option<PathBuf> },
}
impl ParkedLaunch {
/// Whether it joins the running set, as a compound member does, rather
/// than replacing what runs and what waits.
fn joins(&self) -> bool {
matches!(self, ParkedLaunch::Resolved(_, LaunchSlot::Add))
}
}
/// A debugger being prepared on a job, and the launches waiting for it.
pub(in crate::app) struct DebugSetup {
tooling: Tooling,
job: crate::jobs::Job<Result<(), String>>,
waiting: Vec<ParkedLaunch>,
/// A member of the set its launches join ended while this was being
/// prepared: the set has not ended while they wait, so its
/// `postDebugTask`s wait for them too, and run here if they never start.
set_end_deferred: bool,
}
impl App {
/// The tooling `tooling` names, when it is ready; else it is prepared on
/// a job, `then` waits for it, and `None` says the launch is parked.
///
/// While a job prepares it, it is not ready, whatever it looks like: a
/// venv's `python` is there before debugpy is installed in it, and a
/// launch let through then fails where it should wait.
pub(in crate::app) fn debugger_or_park(
&mut self,
tooling: Tooling,
then: impl FnOnce() -> ParkedLaunch,
) -> Option<Ready> {
let preparing = self.debug.setups.iter().any(|s| s.tooling == tooling);
if !preparing && let Some(ready) = self.debug.tooling.ready(tooling) {
return Some(ready);
}
let then = then();
if !then.joins() {
self.forget_parked_setup_launches();
}
if let Some(setup) = self.debug.setups.iter_mut().find(|s| s.tooling == tooling) {
setup.waiting.push(then);
} else {
let tools = std::sync::Arc::clone(&self.debug.tooling);
match crate::jobs::Job::spawn("debug-setup", crate::jobs::Start::Now, move || {
tools.prepare(tooling)
}) {
Ok(job) => self.debug.setups.push(DebugSetup {
tooling,
job,
waiting: vec![then],
set_end_deferred: false,
}),
Err(e) => {
self.debug_error(format!("Debugger setup failed: {e}"));
return None;
}
}
}
self.status = String::from(tooling.preparing());
self.run_debug.feedback = Some(String::from(tooling.preparing()));
self.run_debug.feedback_is_error = false;
None
}
/// The Python debug venv's interpreter, or `None` with the launch
/// parked until it is built.
pub(in crate::app) fn debug_python_or_park(
&mut self,
then: impl FnOnce() -> ParkedLaunch,
) -> Option<PathBuf> {
match self.debugger_or_park(Tooling::Python, then)? {
Ready::Python(py) => Some(py),
Ready::Js { .. } => None,
}
}
/// `node` and the js-debug server, or `None` with the launch parked
/// until both are ready.
pub(in crate::app) fn debug_js_or_park(
&mut self,
then: impl FnOnce() -> ParkedLaunch,
) -> Option<(String, PathBuf)> {
match self.debugger_or_park(Tooling::Js, then)? {
Ready::Js { node, server } => Some((node, server)),
Ready::Python(_) => None,
}
}
/// A workspace switch's part for Run and Debug. A launch waiting for
/// its debugger was asked for in the old workspace: a Debug Test there
/// would run in this one (#2803). The configuration or compound F5
/// starts was picked from the old workspace's launch.json: Run and Debug
/// shows the new one's own entries, and F5 debugs the active file until
/// one of them is picked (#2902). Opening Run and Debug counts its
/// entries, so a switch made while it is shown counts the new
/// workspace's now, or the row keeps the old count until the view is
/// opened again.
pub(in crate::app) fn forget_workspace_launches(&mut self) {
self.forget_parked_setup_launches();
self.selected_debug_config = None;
self.selected_debug_compound = None;
self.run_debug.selected_config = None;
if self.sidebar_view == SidebarView::RunDebug {
self.refresh_run_debug();
}
}
/// Drop every launch still waiting for a debugger: a newer launch
/// supersedes them, as it does one parked behind its task (#567). The
/// jobs go on; what they prepare is there for the next launch.
pub(in crate::app) fn forget_parked_setup_launches(&mut self) {
for setup in &mut self.debug.setups {
setup.waiting.clear();
setup.set_end_deferred = false;
}
}
/// Drop every launch still waiting to start, behind its
/// `preLaunchTask` (#567) or for its debugger (#2803). Every launch the
/// user starts does this first: left in place, an older one would start
/// when its wait ends and replace the newer one.
pub(in crate::app) fn supersede_waiting_launches(&mut self) {
self.pending_debug_launch = None;
self.forget_parked_setup_launches();
}
/// How many launches wait for a debugger.
pub(in crate::app) fn parked_setup_launches(&self) -> usize {
self.debug.setups.iter().map(|s| s.waiting.len()).sum()
}
/// How many compound members wait for a debugger to join the running
/// set: while any do, a member ending does not end the set.
pub(in crate::app) fn members_waiting_for_setup(&self) -> usize {
self.debug
.setups
.iter()
.flat_map(|s| &s.waiting)
.filter(|launch| launch.joins())
.count()
}
/// A member of the set ended while others wait for their debugger: the
/// set ends, and its `postDebugTask`s run, once they have run too, or
/// here if their debugger cannot be set up.
pub(in crate::app) fn defer_set_end_to_setup(&mut self) {
for setup in &mut self.debug.setups {
if setup.waiting.iter().any(ParkedLaunch::joins) {
setup.set_end_deferred = true;
}
}
}
/// Settle the debugger jobs that have answered: start what waited on
/// one that made its tooling ready, and say why for one that could not.
/// Whether anything changed.
pub(in crate::app) fn poll_debug_setups(&mut self) -> bool {
let mut settled = Vec::new();
let mut i = 0;
while i < self.debug.setups.len() {
let answer = match self.debug.setups[i].job.poll() {
crate::jobs::Poll::Pending => {
i += 1;
continue;
}
crate::jobs::Poll::Done(answer) => answer,
crate::jobs::Poll::Died => Err(String::from(
"Debugger setup failed: it stopped before it finished",
)),
};
let setup = self.debug.setups.remove(i);
settled.push((setup.waiting, setup.set_end_deferred, answer));
}
let changed = !settled.is_empty();
// What could start starts first, so a set whose other member could
// not is seen still running.
settled.sort_by_key(|(_, _, answer)| answer.is_err());
for (waiting, deferred, answer) in settled {
match answer {
Ok(()) => {
for launch in waiting {
self.resume_parked_launch(launch);
}
}
Err(why) if !waiting.is_empty() => {
self.debug_error(why);
// The set's end waited for these; nothing else runs or
// waits, so it has ended now.
if deferred
&& self.debug.sessions.is_empty()
&& self.members_waiting_for_setup() == 0
{
self.run_post_debug_tasks();
}
}
Err(_) => {}
}
}
changed
}
/// Start `launch` again, now its debugger is ready.
fn resume_parked_launch(&mut self, launch: ParkedLaunch) {
match launch {
ParkedLaunch::PythonFile(path) => self.start_python_debug_session(&path),
ParkedLaunch::JsFile(path) => self.start_js_debug_session(&path),
ParkedLaunch::Resolved(rc, slot) => self.launch_resolved_in(*rc, slot),
ParkedLaunch::Test { name, file } => self.debug_test_in(name, file),
}
}
}