Skip to content

Commit d937b31

Browse files
authored
feat(rdbg): support RUBY_DEBUG env vars for host and port (#123)
Consume `RUBY_DEBUG_HOST` and `RUBY_DEBUG_PORT` env variables for launching a debugging session but preserve original connection details if env vars are not specified. This allows the following Rdbg configuration: ```json { "label": "Debug Rails server", "adapter": "rdbg", "request": "launch", "command": "$ZED_WORKTREE_ROOT/bin/rails", "args": ["server"], "cwd": "$ZED_WORKTREE_ROOT", "env": { "RUBY_DEBUG_OPEN": "true", "RUBY_DEBUG_PORT": "38698" } } ``` <!-- GitButler Footer Boundary Top --> --- This is **part 1 of 2 in a stack** made with GitButler: - <kbd>&nbsp;2&nbsp;</kbd> #124 - <kbd>&nbsp;1&nbsp;</kbd> #123 👈 <!-- GitButler Footer Boundary Bottom -->
1 parent a30847c commit d937b31

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/ruby.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,35 @@ impl zed::Extension for RubyExtension {
163163
host: None,
164164
timeout: None,
165165
});
166-
let connection = resolve_tcp_template(tcp_connection)?;
166+
let mut connection = resolve_tcp_template(tcp_connection)?;
167167
let mut configuration: serde_json::Value = serde_json::from_str(&config.config)
168168
.map_err(|e| format!("`config` is not a valid JSON: {e}"))?;
169169

170170
let ruby_config: RubyDebugConfig = serde_json::from_value(configuration.clone())
171171
.map_err(|e| format!("`config` is not a valid rdbg config: {e}"))?;
172-
let mut arguments = vec![
173-
"--open".to_string(),
174-
format!("--port={}", connection.port),
175-
format!("--host={}", connection.host),
176-
];
172+
let mut arguments = vec![];
173+
174+
if !ruby_config.env.contains_key("RUBY_DEBUG_OPEN") {
175+
arguments.push("--open".to_string());
176+
}
177+
178+
if let Some(host) = ruby_config.env.get("RUBY_DEBUG_HOST") {
179+
connection.host = host
180+
.parse::<std::net::Ipv4Addr>()
181+
.map(|ip_addr| ip_addr.into())
182+
.map_err(|_| format!("Invalid host '{host}' specified via RUBY_DEBUG_HOST"))?;
183+
} else {
184+
arguments.push(format!("--host={}", connection.host));
185+
}
186+
187+
if let Some(port) = ruby_config.env.get("RUBY_DEBUG_PORT") {
188+
connection.port = port.parse::<u16>().map_err(|_| {
189+
format!("Invalid port number '{port}' specified via RUBY_DEBUG_PORT")
190+
})?;
191+
} else {
192+
arguments.push(format!("--port={}", connection.port));
193+
}
194+
177195
if let Some(script) = &ruby_config.script {
178196
arguments.push(script.clone());
179197
} else if let Some(command) = &ruby_config.command {
@@ -187,6 +205,7 @@ impl zed::Extension for RubyExtension {
187205
} else {
188206
return Err("Ruby debug config must have 'script' or 'command' args".into());
189207
}
208+
190209
if let Some(configuration) = configuration.as_object_mut() {
191210
configuration
192211
.entry("cwd")

0 commit comments

Comments
 (0)