This commit is contained in:
Piotr Kufel 2024-08-06 04:16:30 +00:00 committed by GitHub
commit 17ce40be49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 248 additions and 167 deletions

View File

@ -5,6 +5,8 @@ use reedline::{
SqliteBackedHistory, SqliteBackedHistory,
}; };
use crate::config_files::get_history_path;
#[derive(Clone)] #[derive(Clone)]
pub struct History; pub struct History;
@ -44,22 +46,13 @@ impl Command for History {
}; };
// todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history` // todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history`
if let Some(config_path) = nu_path::config_dir() { let Some(history_path) = get_history_path(history.file_format) else {
return Err(ShellError::ConfigDirNotFound { span: Some(head) });
};
let clear = call.has_flag(engine_state, stack, "clear")?; let clear = call.has_flag(engine_state, stack, "clear")?;
let long = call.has_flag(engine_state, stack, "long")?; let long = call.has_flag(engine_state, stack, "long")?;
let signals = engine_state.signals().clone(); let signals = engine_state.signals().clone();
let mut history_path = config_path;
history_path.push("nushell");
match history.file_format {
HistoryFileFormat::Sqlite => {
history_path.push("history.sqlite3");
}
HistoryFileFormat::PlainText => {
history_path.push("history.txt");
}
}
if clear { if clear {
let _ = std::fs::remove_file(history_path); let _ = std::fs::remove_file(history_path);
// TODO: FIXME also clear the auxiliary files when using sqlite // TODO: FIXME also clear the auxiliary files when using sqlite
@ -67,7 +60,7 @@ impl Command for History {
} else { } else {
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format { let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
HistoryFileFormat::Sqlite => { HistoryFileFormat::Sqlite => {
SqliteBackedHistory::with_file(history_path.clone().into(), None, None) SqliteBackedHistory::with_file(history_path.clone(), None, None)
.map(|inner| { .map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner); let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
boxed boxed
@ -75,17 +68,15 @@ impl Command for History {
.ok() .ok()
} }
HistoryFileFormat::PlainText => FileBackedHistory::with_file( HistoryFileFormat::PlainText => {
history.max_size as usize, FileBackedHistory::with_file(history.max_size as usize, history_path.clone())
history_path.clone().into(),
)
.map(|inner| { .map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner); let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
boxed boxed
}) })
.ok(), .ok()
}
}; };
match history.file_format { match history.file_format {
HistoryFileFormat::PlainText => Ok(history_reader HistoryFileFormat::PlainText => Ok(history_reader
.and_then(|h| { .and_then(|h| {
@ -114,9 +105,10 @@ impl Command for History {
.ok() .ok()
}) })
.map(move |entries| { .map(move |entries| {
entries.into_iter().enumerate().map(move |(idx, entry)| { entries
create_history_record(idx, entry, long, head) .into_iter()
}) .enumerate()
.map(move |(idx, entry)| create_history_record(idx, entry, long, head))
}) })
.ok_or(ShellError::FileNotFound { .ok_or(ShellError::FileNotFound {
file: history_path.display().to_string(), file: history_path.display().to_string(),
@ -125,9 +117,6 @@ impl Command for History {
.into_pipeline_data(head, signals)), .into_pipeline_data(head, signals)),
} }
} }
} else {
Err(ShellError::ConfigDirNotFound { span: Some(head) })
}
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {

View File

@ -16,15 +16,8 @@ const PLUGIN_FILE: &str = "plugin.msgpackz";
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
const OLD_PLUGIN_FILE: &str = "plugin.nu"; const OLD_PLUGIN_FILE: &str = "plugin.nu";
const HISTORY_FILE_TXT: &str = "history.txt";
const HISTORY_FILE_SQLITE: &str = "history.sqlite3";
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
pub fn read_plugin_file( pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Spanned<String>>) {
engine_state: &mut EngineState,
plugin_file: Option<Spanned<String>>,
storage_path: &str,
) {
use nu_protocol::ShellError; use nu_protocol::ShellError;
use std::path::Path; use std::path::Path;
@ -52,7 +45,7 @@ pub fn read_plugin_file(
let mut start_time = std::time::Instant::now(); let mut start_time = std::time::Instant::now();
// Reading signatures from plugin registry file // Reading signatures from plugin registry file
// The plugin.msgpackz file stores the parsed signature collected from each registered plugin // The plugin.msgpackz file stores the parsed signature collected from each registered plugin
add_plugin_file(engine_state, plugin_file.clone(), storage_path); add_plugin_file(engine_state, plugin_file.clone());
perf!( perf!(
"add plugin file to engine_state", "add plugin file to engine_state",
start_time, start_time,
@ -70,8 +63,7 @@ pub fn read_plugin_file(
log::warn!("Plugin file not found: {}", plugin_path.display()); log::warn!("Plugin file not found: {}", plugin_path.display());
// Try migration of an old plugin file if this wasn't a custom plugin file // Try migration of an old plugin file if this wasn't a custom plugin file
if plugin_file.is_none() && migrate_old_plugin_file(engine_state, storage_path) if plugin_file.is_none() && migrate_old_plugin_file(engine_state) {
{
let Ok(file) = std::fs::File::open(&plugin_path) else { let Ok(file) = std::fs::File::open(&plugin_path) else {
log::warn!("Failed to load newly migrated plugin file"); log::warn!("Failed to load newly migrated plugin file");
return; return;
@ -159,11 +151,7 @@ pub fn read_plugin_file(
} }
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
pub fn add_plugin_file( pub fn add_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Spanned<String>>) {
engine_state: &mut EngineState,
plugin_file: Option<Spanned<String>>,
storage_path: &str,
) {
use std::path::Path; use std::path::Path;
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
@ -189,9 +177,8 @@ pub fn add_plugin_file(
), ),
); );
} }
} else if let Some(mut plugin_path) = nu_path::config_dir() { } else if let Some(plugin_path) = nu_path::nu_config_dir() {
// Path to store plugins signatures // Path to store plugins signatures
plugin_path.push(storage_path);
let mut plugin_path = let mut plugin_path =
canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path.into()); canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path.into());
plugin_path.push(PLUGIN_FILE); plugin_path.push(PLUGIN_FILE);
@ -241,19 +228,15 @@ pub fn eval_config_contents(
} }
} }
pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> Option<PathBuf> { pub(crate) fn get_history_path(mode: HistoryFileFormat) -> Option<PathBuf> {
nu_path::config_dir().map(|mut history_path| { nu_path::nu_config_dir().map(|mut history_path| {
history_path.push(storage_path); history_path.push(mode.default_file_name());
history_path.push(match mode {
HistoryFileFormat::PlainText => HISTORY_FILE_TXT,
HistoryFileFormat::Sqlite => HISTORY_FILE_SQLITE,
});
history_path.into() history_path.into()
}) })
} }
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -> bool { pub fn migrate_old_plugin_file(engine_state: &EngineState) -> bool {
use nu_protocol::{ use nu_protocol::{
PluginExample, PluginIdentity, PluginRegistryItem, PluginRegistryItemData, PluginSignature, PluginExample, PluginIdentity, PluginRegistryItem, PluginRegistryItemData, PluginSignature,
ShellError, ShellError,
@ -266,10 +249,9 @@ pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -
return false; return false;
}; };
let Some(config_dir) = nu_path::config_dir().and_then(|mut dir| { let Some(config_dir) =
dir.push(storage_path); nu_path::nu_config_dir().and_then(|dir| nu_path::canonicalize_with(dir, &cwd).ok())
nu_path::canonicalize_with(dir, &cwd).ok() else {
}) else {
return false; return false;
}; };

View File

@ -52,7 +52,6 @@ use sysinfo::System;
pub fn evaluate_repl( pub fn evaluate_repl(
engine_state: &mut EngineState, engine_state: &mut EngineState,
stack: Stack, stack: Stack,
nushell_path: &str,
prerun_command: Option<Spanned<String>>, prerun_command: Option<Spanned<String>>,
load_std_lib: Option<Spanned<String>>, load_std_lib: Option<Spanned<String>>,
entire_start_time: Instant, entire_start_time: Instant,
@ -99,7 +98,7 @@ pub fn evaluate_repl(
unique_stack.add_env_var("LAST_EXIT_CODE".into(), Value::int(0, Span::unknown())); unique_stack.add_env_var("LAST_EXIT_CODE".into(), Value::int(0, Span::unknown()));
let mut line_editor = get_line_editor(engine_state, nushell_path, use_color)?; let mut line_editor = get_line_editor(engine_state, use_color)?;
let temp_file = temp_dir().join(format!("{}.nu", uuid::Uuid::new_v4())); let temp_file = temp_dir().join(format!("{}.nu", uuid::Uuid::new_v4()));
if let Some(s) = prerun_command { if let Some(s) = prerun_command {
@ -200,7 +199,7 @@ pub fn evaluate_repl(
} }
Err(_) => { Err(_) => {
// line_editor is lost in the error case so reconstruct a new one // line_editor is lost in the error case so reconstruct a new one
line_editor = get_line_editor(engine_state, nushell_path, use_color)?; line_editor = get_line_editor(engine_state, use_color)?;
} }
} }
} }
@ -208,11 +207,7 @@ pub fn evaluate_repl(
Ok(()) Ok(())
} }
fn get_line_editor( fn get_line_editor(engine_state: &mut EngineState, use_color: bool) -> Result<Reedline> {
engine_state: &mut EngineState,
nushell_path: &str,
use_color: bool,
) -> Result<Reedline> {
let mut start_time = std::time::Instant::now(); let mut start_time = std::time::Instant::now();
let mut line_editor = Reedline::create(); let mut line_editor = Reedline::create();
@ -223,7 +218,7 @@ fn get_line_editor(
if let Some(history) = engine_state.history_config() { if let Some(history) = engine_state.history_config() {
start_time = std::time::Instant::now(); start_time = std::time::Instant::now();
line_editor = setup_history(nushell_path, engine_state, line_editor, history)?; line_editor = setup_history(engine_state, line_editor, history)?;
perf!("setup history", start_time, use_color); perf!("setup history", start_time, use_color);
} }
@ -1037,7 +1032,6 @@ fn flush_engine_state_repl_buffer(engine_state: &mut EngineState, line_editor: &
/// Setup history management for Reedline /// Setup history management for Reedline
/// ///
fn setup_history( fn setup_history(
nushell_path: &str,
engine_state: &mut EngineState, engine_state: &mut EngineState,
line_editor: Reedline, line_editor: Reedline,
history: HistoryConfig, history: HistoryConfig,
@ -1049,7 +1043,7 @@ fn setup_history(
None None
}; };
if let Some(path) = crate::config_files::get_history_path(nushell_path, history.file_format) { if let Some(path) = crate::config_files::get_history_path(history.file_format) {
return update_line_editor_history( return update_line_editor_history(
engine_state, engine_state,
path, path,
@ -1317,8 +1311,7 @@ fn trailing_slash_looks_like_path() {
fn are_session_ids_in_sync() { fn are_session_ids_in_sync() {
let engine_state = &mut EngineState::new(); let engine_state = &mut EngineState::new();
let history = engine_state.history_config().unwrap(); let history = engine_state.history_config().unwrap();
let history_path = let history_path = crate::config_files::get_history_path(history.file_format).unwrap();
crate::config_files::get_history_path("nushell", history.file_format).unwrap();
let line_editor = reedline::Reedline::create(); let line_editor = reedline::Reedline::create();
let history_session_id = reedline::Reedline::create_history_session_id(); let history_session_id = reedline::Reedline::create_history_session_id();
let line_editor = update_line_editor_history( let line_editor = update_line_editor_history(

View File

@ -30,3 +30,11 @@ pub fn config_dir() -> Option<AbsolutePathBuf> {
.or_else(|| dirs::config_dir().and_then(|path| AbsolutePathBuf::try_from(path).ok())) .or_else(|| dirs::config_dir().and_then(|path| AbsolutePathBuf::try_from(path).ok()))
.map(|path| path.canonicalize().map(Into::into).unwrap_or(path)) .map(|path| path.canonicalize().map(Into::into).unwrap_or(path))
} }
/// Return the nushell config directory.
pub fn nu_config_dir() -> Option<AbsolutePathBuf> {
config_dir().map(|mut p| {
p.push("nushell");
p
})
}

View File

@ -11,7 +11,7 @@ mod trailing_slash;
pub use components::components; pub use components::components;
pub use expansions::{canonicalize_with, expand_path_with, expand_to_real_path, locate_in_dirs}; pub use expansions::{canonicalize_with, expand_path_with, expand_to_real_path, locate_in_dirs};
pub use helpers::{cache_dir, config_dir, data_dir, home_dir}; pub use helpers::{cache_dir, config_dir, data_dir, home_dir, nu_config_dir};
pub use path::*; pub use path::*;
pub use tilde::expand_tilde; pub use tilde::expand_tilde;
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash}; pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};

View File

@ -1,5 +1,5 @@
use crate::{Record, ShellError, Span, Value}; use crate::{Record, ShellError, Span, Value};
use std::{collections::HashMap, fmt::Display, str::FromStr}; use std::{collections::HashMap, fmt::Display, str::FromStr, sync::Arc};
pub(super) trait ReconstructVal { pub(super) trait ReconstructVal {
fn reconstruct_value(&self, span: Span) -> Value; fn reconstruct_value(&self, span: Span) -> Value;
@ -88,6 +88,34 @@ pub(super) fn process_int_config(
} }
} }
pub(super) fn process_opt_str_config(
value: &mut Value,
errors: &mut Vec<ShellError>,
config_point: &mut Option<Arc<str>>,
) {
if value.is_nothing() {
*config_point = None;
return;
}
match value.coerce_str() {
Ok(s) => *config_point = Some(s.into()),
Err(e) => {
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: e.to_string(),
span: Some(value.span()),
help: Some("This value will be ignored.".into()),
inner: vec![],
});
// Reconstruct
*value = match config_point {
Some(s) => Value::string(s.as_ref(), value.span()),
None => Value::nothing(value.span()),
}
}
}
}
pub(super) fn report_invalid_key(keys: &[&str], span: Span, errors: &mut Vec<ShellError>) { pub(super) fn report_invalid_key(keys: &[&str], span: Span, errors: &mut Vec<ShellError>) {
// Because Value::Record discards all of the spans of its // Because Value::Record discards all of the spans of its
// column names (by storing them as Strings), the key name cannot be provided // column names (by storing them as Strings), the key name cannot be provided

View File

@ -8,8 +8,12 @@ use self::table::*;
use crate::engine::Closure; use crate::engine::Closure;
use crate::{record, ShellError, Span, Value}; use crate::{record, ShellError, Span, Value};
use serde::Deserializer;
use serde::Serializer;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::path::MAIN_SEPARATOR;
use std::sync::Arc;
pub use self::completer::{CompletionAlgorithm, CompletionSort}; pub use self::completer::{CompletionAlgorithm, CompletionSort};
pub use self::helper::extract_value; pub use self::helper::extract_value;
@ -29,12 +33,63 @@ mod plugin_gc;
mod reedline; mod reedline;
mod table; mod table;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)] fn deserialize_config_path<'de, D>(deserializer: D) -> Result<Option<Arc<str>>, D::Error>
where
D: Deserializer<'de>,
{
let s: Option<String> = Deserialize::deserialize(deserializer)?;
Ok(s.map(|s| s.into()))
}
fn serialize_config_path<S>(v: &Option<Arc<str>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match v {
None => serializer.serialize_none(),
Some(s) => serializer.serialize_str(s),
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryConfig { pub struct HistoryConfig {
pub max_size: i64, pub max_size: i64,
pub sync_on_enter: bool, pub sync_on_enter: bool,
pub file_format: HistoryFileFormat, pub file_format: HistoryFileFormat,
pub isolation: bool, pub isolation: bool,
// History path. Can be either a directory (ending with a path separator), in which case a
// default file name will be appended - or a full file path.
//
// PathBuf would be more correct, but it causes conversion issues down the line. String is more
// strict in terms of characters, but less strict in terms of semantics. We use Arc<str> for
// cheap clone.
#[serde(
serialize_with = "serialize_config_path",
deserialize_with = "deserialize_config_path"
)]
path: Option<Arc<str>>,
}
impl HistoryConfig {
#[allow(clippy::question_mark)]
pub fn file_path(&self) -> Option<String> {
let Some(path) = &self.path else {
return None;
};
if path.ends_with(MAIN_SEPARATOR) {
let mut path = path.to_string();
path.push_str(match self.file_format {
HistoryFileFormat::PlainText => "history.txt",
HistoryFileFormat::Sqlite => "history.sqlite3",
});
Some(path)
} else if !path.is_empty() {
Some(path.to_string())
} else {
None
}
}
} }
impl Default for HistoryConfig { impl Default for HistoryConfig {
@ -44,6 +99,17 @@ impl Default for HistoryConfig {
sync_on_enter: true, sync_on_enter: true,
file_format: HistoryFileFormat::PlainText, file_format: HistoryFileFormat::PlainText,
isolation: false, isolation: false,
// TODO: This reading an env var is not great. Ideally we'd plumb config location
// explicitly, but there's already lots of uses of Config::default and converting them
// all is not trivial.
path: nu_path::nu_config_dir().and_then(|dir| {
let s = dir.into_os_string().into_string();
s.map(|mut s| {
s.push(MAIN_SEPARATOR);
Arc::from(s)
})
.ok()
}),
} }
} }
} }
@ -301,6 +367,9 @@ impl Value {
value, value,
&mut errors); &mut errors);
} }
"path" => {
process_opt_str_config(value, &mut errors, &mut history.path);
}
_ => { _ => {
report_invalid_key(&[key, key2], span, &mut errors); report_invalid_key(&[key, key2], span, &mut errors);
return false; return false;

View File

@ -79,6 +79,16 @@ pub enum HistoryFileFormat {
PlainText, PlainText,
} }
impl HistoryFileFormat {
pub fn default_file_name(self) -> std::path::PathBuf {
match self {
HistoryFileFormat::PlainText => "history.txt",
HistoryFileFormat::Sqlite => "history.sqlite3",
}
.into()
}
}
impl FromStr for HistoryFileFormat { impl FromStr for HistoryFileFormat {
type Err = &'static str; type Err = &'static str;

View File

@ -762,7 +762,11 @@ impl EngineState {
/// Returns the configuration settings for command history or `None` if history is disabled /// Returns the configuration settings for command history or `None` if history is disabled
pub fn history_config(&self) -> Option<HistoryConfig> { pub fn history_config(&self) -> Option<HistoryConfig> {
self.history_enabled.then(|| self.config.history) self.history_enabled.then(|| self.config.history.clone())
}
pub fn history_file_path(&self) -> Option<String> {
self.config.history.file_path()
} }
pub fn get_var(&self, var_id: VarId) -> &Variable { pub fn get_var(&self, var_id: VarId) -> &Variable {

View File

@ -7,37 +7,50 @@ use crate::{
debugger::{DebugContext, WithoutDebug}, debugger::{DebugContext, WithoutDebug},
engine::{EngineState, StateWorkingSet}, engine::{EngineState, StateWorkingSet},
eval_base::Eval, eval_base::Eval,
record, Config, HistoryFileFormat, PipelineData, Record, ShellError, Span, Value, VarId, record, Config, PipelineData, Record, ShellError, Span, Value, VarId,
}; };
use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name}; use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name};
use std::{ use std::{
io,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc,
}; };
/// Create a Value for `$nu`. /// Create a Value for `$nu`.
pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Value { pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Value {
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf { fn canonicalize_path_fallible(engine_state: &EngineState, path: &Path) -> io::Result<PathBuf> {
#[allow(deprecated)] #[allow(deprecated)]
let cwd = engine_state.current_work_dir(); let cwd = engine_state.current_work_dir();
if path.exists() { match nu_path::canonicalize_with(path, &cwd) {
match nu_path::canonicalize_with(path, cwd) { Err(e) if e.kind() == io::ErrorKind::NotFound => {
Ok(canon_path) => canon_path, // If the path does not exist, try to canonicalize the parent (directory) alone.
Err(_) => path.to_owned(), // Some tests (and perhaps, users) rely on path to non-existent files being
// canonicalized to some extent. This works for the most part due to how those
// paths happen to be constructed, but in some cases there is no intermediate
// canonicalization and we have to do it here.
match (path.parent(), path.file_name()) {
(Some(dir), Some(file)) => {
let mut path = nu_path::canonicalize_with(dir, cwd)?;
path.push(file);
Ok(path)
} }
} else { _ => Err(e),
path.to_owned()
} }
} }
Err(e) => Err(e),
Ok(p) => Ok(p),
}
}
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
canonicalize_path_fallible(engine_state, path).unwrap_or_else(|_| path.to_owned())
}
let mut record = Record::new(); let mut record = Record::new();
let config_path = match nu_path::config_dir() { let config_path = match nu_path::nu_config_dir() {
Some(mut path) => { Some(path) => Ok(canonicalize_path(engine_state, path.as_ref())),
path.push("nushell");
Ok(canonicalize_path(engine_state, path.as_ref()))
}
None => Err(Value::error( None => Err(Value::error(
ShellError::ConfigDirNotFound { span: Some(span) }, ShellError::ConfigDirNotFound { span: Some(span) },
span, span,
@ -88,18 +101,10 @@ pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Valu
record.push( record.push(
"history-path", "history-path",
config_path.clone().map_or_else( engine_state.history_file_path().map_or_else(
|e| e, || Value::error(ShellError::ConfigDirNotFound { span: Some(span) }, span),
|mut path| { |path| {
match engine_state.config.history.file_format { let canon_hist_path = canonicalize_path(engine_state, Path::new(&path));
HistoryFileFormat::Sqlite => {
path.push("history.sqlite3");
}
HistoryFileFormat::PlainText => {
path.push("history.txt");
}
}
let canon_hist_path = canonicalize_path(engine_state, &path);
Value::string(canon_hist_path.to_string_lossy(), span) Value::string(canon_hist_path.to_string_lossy(), span)
}, },
), ),

View File

@ -17,7 +17,6 @@ use std::{
sync::Arc, sync::Arc,
}; };
pub(crate) const NUSHELL_FOLDER: &str = "nushell";
const CONFIG_FILE: &str = "config.nu"; const CONFIG_FILE: &str = "config.nu";
const ENV_FILE: &str = "env.nu"; const ENV_FILE: &str = "env.nu";
const LOGINSHELL_FILE: &str = "login.nu"; const LOGINSHELL_FILE: &str = "login.nu";
@ -49,9 +48,7 @@ pub(crate) fn read_config_file(
report_error(&working_set, &e); report_error(&working_set, &e);
} }
} }
} else if let Some(mut config_path) = nu_path::config_dir() { } else if let Some(mut config_path) = nu_path::nu_config_dir() {
config_path.push(NUSHELL_FOLDER);
// Create config directory if it does not exist // Create config directory if it does not exist
if !config_path.exists() { if !config_path.exists() {
if let Err(err) = std::fs::create_dir_all(&config_path) { if let Err(err) = std::fs::create_dir_all(&config_path) {
@ -134,8 +131,7 @@ pub(crate) fn read_loginshell_file(engine_state: &mut EngineState, stack: &mut S
); );
// read and execute loginshell file if exists // read and execute loginshell file if exists
if let Some(mut config_path) = nu_path::config_dir() { if let Some(mut config_path) = nu_path::nu_config_dir() {
config_path.push(NUSHELL_FOLDER);
config_path.push(LOGINSHELL_FILE); config_path.push(LOGINSHELL_FILE);
warn!("loginshell_file: {}", config_path.display()); warn!("loginshell_file: {}", config_path.display());
@ -268,7 +264,7 @@ pub(crate) fn setup_config(
); );
let result = catch_unwind(AssertUnwindSafe(|| { let result = catch_unwind(AssertUnwindSafe(|| {
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
read_plugin_file(engine_state, plugin_file, NUSHELL_FOLDER); read_plugin_file(engine_state, plugin_file);
read_config_file(engine_state, stack, env_file, true); read_config_file(engine_state, stack, env_file, true);
read_config_file(engine_state, stack, config_file, false); read_config_file(engine_state, stack, config_file, false);
@ -300,8 +296,7 @@ pub(crate) fn set_config_path(
); );
let config_path = match config_file { let config_path = match config_file {
Some(s) => canonicalize_with(&s.item, cwd).ok(), Some(s) => canonicalize_with(&s.item, cwd).ok(),
None => nu_path::config_dir().map(|mut p| { None => nu_path::nu_config_dir().map(|p| {
p.push(NUSHELL_FOLDER);
let mut p = canonicalize_with(&p, cwd).unwrap_or(p.into()); let mut p = canonicalize_with(&p, cwd).unwrap_or(p.into());
p.push(default_config_name); p.push(default_config_name);
canonicalize_with(&p, cwd).unwrap_or(p) canonicalize_with(&p, cwd).unwrap_or(p)

View File

@ -1,5 +1,3 @@
#[cfg(feature = "plugin")]
use crate::config_files::NUSHELL_FOLDER;
use crate::{ use crate::{
command, command,
config_files::{self, setup_config}, config_files::{self, setup_config},
@ -37,7 +35,7 @@ pub(crate) fn run_commands(
// if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files // if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files
if parsed_nu_cli_args.no_config_file.is_none() { if parsed_nu_cli_args.no_config_file.is_none() {
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER); read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
perf!("read plugins", start_time, use_color); perf!("read plugins", start_time, use_color);
@ -125,7 +123,7 @@ pub(crate) fn run_file(
if parsed_nu_cli_args.no_config_file.is_none() { if parsed_nu_cli_args.no_config_file.is_none() {
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER); read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
perf!("read plugins", start_time, use_color); perf!("read plugins", start_time, use_color);
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
@ -216,7 +214,6 @@ pub(crate) fn run_repl(
let ret_val = evaluate_repl( let ret_val = evaluate_repl(
engine_state, engine_state,
stack, stack,
config_files::NUSHELL_FOLDER,
parsed_nu_cli_args.execute, parsed_nu_cli_args.execute,
parsed_nu_cli_args.no_std_lib, parsed_nu_cli_args.no_std_lib,
entire_start_time, entire_start_time,

View File

@ -151,6 +151,7 @@ fn test_default_config_path() {
fn test_default_symlinked_config_path_empty() { fn test_default_symlinked_config_path_empty() {
Playground::setup("symlinked_empty_config_dir", |_, playground| { Playground::setup("symlinked_empty_config_dir", |_, playground| {
let config_dir_nushell = setup_fake_config(playground); let config_dir_nushell = setup_fake_config(playground);
println!("config_dir_nushell = {}", config_dir_nushell.display());
test_config_path_helper(playground, config_dir_nushell); test_config_path_helper(playground, config_dir_nushell);
}); });
} }