From 448739132ae4bd223a95dae5f82acb5c4b3cba7f Mon Sep 17 00:00:00 2001 From: ef3d0c3e Date: Thu, 31 Oct 2024 01:19:03 +0100 Subject: [PATCH] Fix current_dir issue with lsp --- src/compiler/process.rs | 8 ------- src/parser/langparser.rs | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/compiler/process.rs b/src/compiler/process.rs index 1342ff9..b75e8d1 100644 --- a/src/compiler/process.rs +++ b/src/compiler/process.rs @@ -75,9 +75,6 @@ pub fn process( ) -> Result, Option)>, String> { let mut compiled = vec![]; - let current_dir = std::env::current_dir() - .map_err(|err| format!("Unable to get the current working directory: {err}"))?; - let con = db_path .as_ref() .map_or(Connection::open_in_memory(), Connection::open) @@ -98,8 +95,6 @@ pub fn process( let file_parent_path = file .parent() .ok_or(format!("Failed to get parent path for `{file:#?}`"))?; - std::env::set_current_dir(file_parent_path) - .map_err(|err| format!("Failed to move to path `{file_parent_path:#?}`: {err}"))?; let parse_and_compile = || -> Result<(CompiledDocument, Option), String> { // Parse @@ -155,9 +150,6 @@ pub fn process( })?; } - std::env::set_current_dir(current_dir) - .map_err(|err| format!("Failed to set current directory: {err}"))?; - Ok(compiled) } diff --git a/src/parser/langparser.rs b/src/parser/langparser.rs index 807a4f0..cd89a10 100644 --- a/src/parser/langparser.rs +++ b/src/parser/langparser.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::path::PathBuf; use std::rc::Rc; use crate::document::document::Document; @@ -18,6 +19,7 @@ use super::rule::Rule; use super::source::Cursor; use super::source::Source; use super::source::SourceFile; +use super::source::SourcePosition; use super::source::Token; use super::util; @@ -89,6 +91,42 @@ impl<'b> Parser for LangParser<'b> { ) -> (Box + 'doc>, ParserState<'p, 'a>) { let doc = LangDocument::new(source.clone(), parent); + let current_dir = match std::env::current_dir() { + Ok(dir) => dir, + Err(err) => { + eprintln!("Unable to get the current working directory: {err}"); + return (Box::new(doc), state); + } + }; + + let path = source + .original_position(0) + .0 + .downcast_rc::() + .ok() + .map(|source| { + let start = if source.path().starts_with("file:///") { + 7 + } else { + 0 + }; + let mut path = PathBuf::from(&source.path()[start..]); + match path.canonicalize() { + Ok(cano) => path = cano, + Err(err) => eprintln!("Failed to canonicalize path `{}`: {err}", source.path()), + } + path.pop(); + path + }); + if let Some(path) = path { + if let Err(err) = std::env::set_current_dir(&path) { + eprintln!( + "Failed to set working directory to `{}`: {err}", + path.to_str().unwrap_or("") + ); + } + } + // Insert lsp data into state if let (Some(_), Some(lsp)) = ( source.clone().downcast_rc::().ok(), @@ -171,6 +209,13 @@ impl<'b> Parser for LangParser<'b> { ); } + if let Err(err) = std::env::set_current_dir(¤t_dir) { + println!( + "Failed to set working directory to `{}`: {err} {source:#?}", + current_dir.to_str().unwrap_or("") + ); + } + (Box::new(doc), state) }