From e7b300f3d5b9272a09f088bb87b9ec270e87873a Mon Sep 17 00:00:00 2001 From: Vitaly Slobodin Date: Sat, 28 Jun 2025 07:53:30 +0200 Subject: [PATCH] feat(rdbg): support RUBY_DEBUG env vars for host and port 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" } } ``` --- src/ruby.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/ruby.rs b/src/ruby.rs index ab917fb..616df31 100644 --- a/src/ruby.rs +++ b/src/ruby.rs @@ -163,17 +163,35 @@ impl zed::Extension for RubyExtension { host: None, timeout: None, }); - let connection = resolve_tcp_template(tcp_connection)?; + let mut connection = resolve_tcp_template(tcp_connection)?; let mut configuration: serde_json::Value = serde_json::from_str(&config.config) .map_err(|e| format!("`config` is not a valid JSON: {e}"))?; let ruby_config: RubyDebugConfig = serde_json::from_value(configuration.clone()) .map_err(|e| format!("`config` is not a valid rdbg config: {e}"))?; - let mut arguments = vec![ - "--open".to_string(), - format!("--port={}", connection.port), - format!("--host={}", connection.host), - ]; + let mut arguments = vec![]; + + if !ruby_config.env.contains_key("RUBY_DEBUG_OPEN") { + arguments.push("--open".to_string()); + } + + if let Some(host) = ruby_config.env.get("RUBY_DEBUG_HOST") { + connection.host = host + .parse::() + .map(|ip_addr| ip_addr.into()) + .map_err(|_| format!("Invalid host '{host}' specified via RUBY_DEBUG_HOST"))?; + } else { + arguments.push(format!("--host={}", connection.host)); + } + + if let Some(port) = ruby_config.env.get("RUBY_DEBUG_PORT") { + connection.port = port.parse::().map_err(|_| { + format!("Invalid port number '{port}' specified via RUBY_DEBUG_PORT") + })?; + } else { + arguments.push(format!("--port={}", connection.port)); + } + if let Some(script) = &ruby_config.script { arguments.push(script.clone()); } else if let Some(command) = &ruby_config.command { @@ -187,6 +205,7 @@ impl zed::Extension for RubyExtension { } else { return Err("Ruby debug config must have 'script' or 'command' args".into()); } + if let Some(configuration) = configuration.as_object_mut() { configuration .entry("cwd")