on master help sign in
Set the working directory before loading the config (#8498)
a857480Bjorn Ove Hay Andersen
helix-loader/src/lib.rs+3−3
@@ -33,9 +33,9 @@ pub fn current_working_dir() -> PathBuf {
33 path
33 path
34}
34}
35
35
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<()> {
37 let path = dunce::canonicalize(path)?;
37 let path = dunce::canonicalize(path)?;
38− std::env::set_current_dir(path.clone())?;
38+ std::env::set_current_dir(&path)?;
39 let mut cwd = CWD.write().unwrap();
39 let mut cwd = CWD.write().unwrap();
40 *cwd = Some(path);
40 *cwd = Some(path);
41 Ok(())
41 Ok(())
@@ -280,7 +280,7 @@ mod merge_toml_tests {
280 let cwd = current_working_dir();
280 let cwd = current_working_dir();
281 assert_ne!(cwd, new_path);
281 assert_ne!(cwd, new_path);
282
282
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");
284
284
285 let cwd = current_working_dir();
285 let cwd = current_working_dir();
286 assert_eq!(cwd, new_path);
286 assert_eq!(cwd, new_path);
helix-term/src/application.rs+1−4
@@ -156,9 +156,6 @@ impl Application {
156 let editor_view = Box::new(ui::EditorView::new(Keymaps::new(keys)));
156 let editor_view = Box::new(ui::EditorView::new(Keymaps::new(keys)));
157 compositor.push(editor_view);
157 compositor.push(editor_view);
158
158
159− if let Some(path) = args.working_directory {
160− helix_loader::set_current_working_dir(path)?
161− }
162 if args.load_tutor {
159 if args.load_tutor {
163 let path = helix_loader::runtime_file(Path::new("tutor"));
160 let path = helix_loader::runtime_file(Path::new("tutor"));
164 editor.open(&path, Action::VerticalSplit)?;
161 editor.open(&path, Action::VerticalSplit)?;
@@ -167,7 +164,7 @@ impl Application {
167 } else if !args.files.is_empty() {
164 } else if !args.files.is_empty() {
168 let first = &args.files[0].0; // we know it's not empty
165 let first = &args.files[0].0; // we know it's not empty
169 if first.is_dir() {
166 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()
171 editor.new_file(Action::VerticalSplit);
168 editor.new_file(Action::VerticalSplit);
172 let picker = ui::file_picker(".".into(), &config.load().editor);
169 let picker = ui::file_picker(".".into(), &config.load().editor);
173 compositor.push(Box::new(overlaid(picker)));
170 compositor.push(Box::new(overlaid(picker)));
helix-term/src/main.rs+8
@@ -115,6 +115,14 @@ FLAGS:
115
115
116 setup_logging(args.verbosity).context("failed to initialize logging")?;
116 setup_logging(args.verbosity).context("failed to initialize logging")?;
117
117
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+
118 let config = match Config::load_default() {
126 let config = match Config::load_default() {
119 Ok(config) => config,
127 Ok(config) => config,
120 Err(ConfigLoadError::Error(err)) if err.kind() == std::io::ErrorKind::NotFound => {
128 Err(ConfigLoadError::Error(err)) if err.kind() == std::io::ErrorKind::NotFound => {