@@ -3,9 +3,12 @@ pub mod grammar;
4use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
4use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
5use std::path::{Path, PathBuf};
5use std::path::{Path, PathBuf};
7pub const VERSION_AND_GIT_HASH: &str = env!("VERSION_AND_GIT_HASH");
8pub const VERSION_AND_GIT_HASH: &str = env!("VERSION_AND_GIT_HASH");
10+static CWD: RwLock<Option<PathBuf>> = RwLock::new(None);
9static RUNTIME_DIRS: once_cell::sync::Lazy<Vec<PathBuf>> =
12static RUNTIME_DIRS: once_cell::sync::Lazy<Vec<PathBuf>> =
10 once_cell::sync::Lazy::new(prioritize_runtime_dirs);
13 once_cell::sync::Lazy::new(prioritize_runtime_dirs);
@@ -13,6 +16,31 @@ static CONFIG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCe
14static LOG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
17static LOG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
19+// Get the current working directory.
20+// This information is managed internally as the call to std::env::current_dir
21+// might fail if the cwd has been deleted.
22+pub fn current_working_dir() -> PathBuf {
23+ if let Some(path) = &*CWD.read().unwrap() {
27+ let path = std::env::current_dir()
28+ .and_then(dunce::canonicalize)
29+ .expect("Couldn't determine current working directory");
30+ let mut cwd = CWD.write().unwrap();
31+ *cwd = Some(path.clone());
36+pub fn set_current_working_dir(path: PathBuf) -> std::io::Result<()> {
37+ let path = dunce::canonicalize(path)?;
38+ std::env::set_current_dir(path.clone())?;
39+ let mut cwd = CWD.write().unwrap();
16pub fn initialize_config_file(specified_file: Option<PathBuf>) {
44pub fn initialize_config_file(specified_file: Option<PathBuf>) {
17 let config_file = specified_file.unwrap_or_else(default_config_file);
45 let config_file = specified_file.unwrap_or_else(default_config_file);
18 ensure_parent_dir(&config_file);
46 ensure_parent_dir(&config_file);
@@ -217,7 +245,7 @@ pub fn merge_toml_values(left: toml::Value, right: toml::Value, merge_depth: usi
217/// If no workspace was found returns (CWD, true).
245/// If no workspace was found returns (CWD, true).
218/// Otherwise (workspace, false) is returned
246/// Otherwise (workspace, false) is returned
219pub fn find_workspace() -> (PathBuf, bool) {
247pub fn find_workspace() -> (PathBuf, bool) {
220− let current_dir = std::env::current_dir().expect("unable to determine current directory");
248+ let current_dir = current_working_dir();
221 for ancestor in current_dir.ancestors() {
249 for ancestor in current_dir.ancestors() {
222 if ancestor.join(".git").exists() || ancestor.join(".helix").exists() {
250 if ancestor.join(".git").exists() || ancestor.join(".helix").exists() {
223 return (ancestor.to_owned(), false);
251 return (ancestor.to_owned(), false);
@@ -243,9 +271,21 @@ fn ensure_parent_dir(path: &Path) {
243mod merge_toml_tests {
271mod merge_toml_tests {
244 use std::str;
272 use std::str;
246− use super::merge_toml_values;
274+ use super::{current_working_dir, merge_toml_values, set_current_working_dir};
247 use toml::Value;
275 use toml::Value;
278+ fn current_dir_is_set() {
279+ let new_path = dunce::canonicalize(std::env::temp_dir()).unwrap();
280+ let cwd = current_working_dir();
281+ assert_ne!(cwd, new_path);
283+ set_current_working_dir(new_path.clone()).expect("Couldn't set new path");
285+ let cwd = current_working_dir();
286+ assert_eq!(cwd, new_path);
250 fn language_toml_map_merges() {
290 fn language_toml_map_merges() {
251 const USER: &str = r#"
291 const USER: &str = r#"