on master help sign in
Set the working directory before loading the config (#8498)
commita8574805617d86304232b47d84fd7c7f4f773520 authorBjorn Ove Hay Andersen <bjrnove@gmail.com> dateMon 9 Oct 2023, 18:38 (3 years ago) parent5cb76e7
3 files changed+12−7
helix-loader/src/lib.rs+3−3 helix-term/src/application.rs+1−4 helix-term/src/main.rs+8
helix-loader/src/lib.rs+3−3
@@ -33,9 +33,9 @@ pub fn current_working_dir() -> PathBuf {
3333 path
3434}
3535
36−pub fn set_current_working_dir(path: PathBuf) -> std::io::Result<()> {
36+pub fn set_current_working_dir(path: impl AsRef<Path>) -> std::io::Result<()> {
3737 let path = dunce::canonicalize(path)?;
38− std::env::set_current_dir(path.clone())?;
38+ std::env::set_current_dir(&path)?;
3939 let mut cwd = CWD.write().unwrap();
4040 *cwd = Some(path);
4141 Ok(())
@@ -280,7 +280,7 @@ mod merge_toml_tests {
280280 let cwd = current_working_dir();
281281 assert_ne!(cwd, new_path);
282282
283− set_current_working_dir(new_path.clone()).expect("Couldn't set new path");
283+ set_current_working_dir(&new_path).expect("Couldn't set new path");
284284
285285 let cwd = current_working_dir();
286286 assert_eq!(cwd, new_path);
helix-term/src/application.rs+1−4
@@ -156,9 +156,6 @@ impl Application {
156156 let editor_view = Box::new(ui::EditorView::new(Keymaps::new(keys)));
157157 compositor.push(editor_view);
158158
159− if let Some(path) = args.working_directory {
160− helix_loader::set_current_working_dir(path)?
161− }
162159 if args.load_tutor {
163160 let path = helix_loader::runtime_file(Path::new("tutor"));
164161 editor.open(&path, Action::VerticalSplit)?;
@@ -167,7 +164,7 @@ impl Application {
167164 } else if !args.files.is_empty() {
168165 let first = &args.files[0].0; // we know it's not empty
169166 if first.is_dir() {
170− helix_loader::set_current_working_dir(first.clone())?;
167+ // NOTE: The working directory is already set to args.files[0] in main()
171168 editor.new_file(Action::VerticalSplit);
172169 let picker = ui::file_picker(".".into(), &config.load().editor);
173170 compositor.push(Box::new(overlaid(picker)));
helix-term/src/main.rs+8
@@ -115,6 +115,14 @@ FLAGS:
115115
116116 setup_logging(args.verbosity).context("failed to initialize logging")?;
117117
118+ // NOTE: Set the working directory early so the correct configuration is loaded. Be aware that
119+ // Application::new() depends on this logic so it must be updated if this changes.
120+ if let Some((path, true)) = args.files.first().map(|(path, _)| (path, path.is_dir())) {
121+ helix_loader::set_current_working_dir(path)?;
122+ } else if let Some(path) = &args.working_directory {
123+ helix_loader::set_current_working_dir(path)?;
124+ }
125+
118126 let config = match Config::load_default() {
119127 Ok(config) => config,
120128 Err(ConfigLoadError::Error(err)) if err.kind() == std::io::ErrorKind::NotFound => {