Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
104 changes: 98 additions & 6 deletions src/gemset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
path::{Path, PathBuf},
sync::{LazyLock, OnceLock},
};
use zed_extension_api::Os;

pub fn versioned_gem_home(
base_dir: &Path,
Expand Down Expand Up @@ -34,19 +35,30 @@ pub fn versioned_gem_home(
/// A simple wrapper around the `gem` command.
pub struct Gemset {
gem_home: PathBuf,
path_separator: char,
envs: Vec<(String, String)>,
cached_env: OnceLock<Vec<(String, String)>>,
command_executor: Box<dyn CommandExecutor>,
}

fn path_list_separator(os: Os) -> char {
if matches!(os, Os::Windows) {
';'
} else {
':'
}
}

impl Gemset {
pub fn new(
gem_home: PathBuf,
os: Os,
envs: Option<&[(&str, &str)]>,
command_executor: Box<dyn CommandExecutor>,
) -> Self {
Self {
gem_home,
path_separator: path_list_separator(os),
envs: envs.map_or(Vec::new(), |envs| {
envs.iter()
.map(|&(k, v)| (k.to_string(), v.to_string()))
Expand Down Expand Up @@ -75,18 +87,21 @@ impl Gemset {
.collect();

let gem_path = self.gem_home.display().to_string();
let separator = self.path_separator;

// If the GEM_PATH env variable is already set,
// prepend our gem home directory to it to ensure
// that our gems are prioritized over system/user gems.
env_map
.entry("GEM_PATH".to_string())
.and_modify(|existing_gem_path| {
let paths: Vec<_> = std::env::split_paths(existing_gem_path).collect();
let gem_home_path = std::path::Path::new(&gem_path);
let gem_home_path = Path::new(&gem_path);
let already_listed = existing_gem_path
.split(separator)
.any(|entry| Path::new(entry) == gem_home_path);

if !paths.iter().any(|p| p == gem_home_path) {
*existing_gem_path = format!("{gem_path}:{existing_gem_path}");
if !already_listed {
*existing_gem_path = format!("{gem_path}{separator}{existing_gem_path}");
}
})
.or_insert(gem_path);
Expand All @@ -95,7 +110,7 @@ impl Gemset {
env_map
.entry("PATH".to_string())
.and_modify(|path| {
*path = format!("{}:{}", path, self.gem_home.join("bin").display())
*path = format!("{path}{separator}{}", self.gem_home.join("bin").display())
})
.or_insert(self.gem_home.join("bin").display().to_string());

Expand Down Expand Up @@ -209,7 +224,12 @@ mod tests {
const TEST_GEM_PATH: &str = "/test/gem_path";

fn create_gemset(envs: Option<&[(&str, &str)]>, mock_executor: MockCommandExecutor) -> Gemset {
Gemset::new(TEST_GEM_HOME.into(), envs, Box::new(mock_executor))
Gemset::new(
TEST_GEM_HOME.into(),
Os::Linux,
envs,
Box::new(mock_executor),
)
}

#[test]
Expand Down Expand Up @@ -349,6 +369,7 @@ mod tests {
fn test_gem_bin_path() {
let gemset = Gemset::new(
TEST_GEM_HOME.into(),
Os::Linux,
None,
Box::new(MockCommandExecutor::new()),
);
Expand All @@ -365,6 +386,7 @@ mod tests {
fn test_gem_env() {
let gemset = Gemset::new(
TEST_GEM_HOME.into(),
Os::Linux,
Some(&[("GEM_PATH", TEST_GEM_PATH), ("PATH", "/usr/bin")]),
Box::new(MockCommandExecutor::new()),
);
Expand All @@ -381,6 +403,75 @@ mod tests {
assert_eq!(env.get("PATH").unwrap(), &format!("/usr/bin:{gem_bin}"));
}

#[test]
fn test_gem_env_does_not_duplicate_gem_home_in_gem_path() {
let existing = format!("{TEST_GEM_PATH}:{TEST_GEM_HOME}");
let gemset = Gemset::new(
TEST_GEM_HOME.into(),
Os::Linux,
Some(&[("GEM_PATH", &existing)]),
Box::new(MockCommandExecutor::new()),
);
let env: std::collections::HashMap<String, String> = gemset.env().iter().cloned().collect();

assert_eq!(env.get("GEM_PATH").unwrap(), &existing);
}

#[test]
fn test_gem_env_without_gem_path() {
let gemset = Gemset::new(
TEST_GEM_HOME.into(),
Os::Linux,
Some(&[("PATH", "/usr/bin")]),
Box::new(MockCommandExecutor::new()),
);
let env: std::collections::HashMap<String, String> = gemset.env().iter().cloned().collect();

assert_eq!(env.get("GEM_PATH").unwrap(), TEST_GEM_HOME);
}

#[test]
fn test_gem_env_on_windows() {
let gem_home = "C:/zed/gems/abc";
let gemset = Gemset::new(
gem_home.into(),
Os::Windows,
Some(&[
("GEM_PATH", "C:/Ruby34/lib/ruby/gems/3.4.0;C:/Users/x/.gem"),
("PATH", "C:/Windows/System32"),
]),
Box::new(MockCommandExecutor::new()),
);
let env: std::collections::HashMap<String, String> = gemset.env().iter().cloned().collect();

assert_eq!(
env.get("GEM_PATH").unwrap(),
"C:/zed/gems/abc;C:/Ruby34/lib/ruby/gems/3.4.0;C:/Users/x/.gem"
);
assert_eq!(
env.get("PATH").unwrap(),
&format!(
"C:/Windows/System32;{}",
Path::new(gem_home).join("bin").display()
)
);
}

#[test]
fn test_gem_env_on_windows_does_not_duplicate_gem_home_in_gem_path() {
let gem_home = "C:/zed/gems/abc";
let existing = format!("C:/Ruby34/lib/ruby/gems/3.4.0;{gem_home}");
let gemset = Gemset::new(
gem_home.into(),
Os::Windows,
Some(&[("GEM_PATH", &existing)]),
Box::new(MockCommandExecutor::new()),
);
let env: std::collections::HashMap<String, String> = gemset.env().iter().cloned().collect();

assert_eq!(env.get("GEM_PATH").unwrap(), &existing);
}

#[test]
fn test_install_gem_success() {
let mock_executor = MockCommandExecutor::new();
Expand Down Expand Up @@ -429,6 +520,7 @@ mod tests {
);
let gemset = Gemset::new(
TEST_GEM_HOME.into(),
Os::Linux,
Some(&[("CUSTOM_VAR", "custom_value")]),
Box::new(mock_executor),
);
Expand Down
1 change: 1 addition & 0 deletions src/language_servers/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ pub trait LanguageServer {

let gemset = Gemset::new(
gem_home,
zed::current_platform().0,
Some(&worktree_shell_env_vars),
Box::new(RealCommandExecutor),
);
Expand Down
7 changes: 6 additions & 1 deletion src/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ impl zed::Extension for RubyExtension {
.map_err(|e| format!("Failed to get extension directory: {e:#}"))?;
let gem_home = versioned_gem_home(&base_dir, &env_vars, &RealCommandExecutor)
.map_err(|e| format!("{:#}", e))?;
let gemset = Gemset::new(gem_home, Some(&env_vars), Box::new(RealCommandExecutor));
let gemset = Gemset::new(
gem_home,
zed::current_platform().0,
Some(&env_vars),
Box::new(RealCommandExecutor),
);
gemset
.install_gem("debug")
.map_err(|e| format!("Failed to install debug gem: {e:#}"))?;
Expand Down