mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
4ed25b63a6
# Release-Notes Short Description * Nushell now always loads its internal `default_env.nu` before the user `env.nu` is loaded, then loads the internal `default_config.nu` before the user's `config.nu` is loaded. This allows for a simpler user-configuration experience. The Configuration Chapter of the Book will be updated soon with the new behavior. # Description Implements the main ideas in #13671 and a few more: * Users can now specify only the environment and config options they want to override in *their* `env.nu` and `config.nu`and yet still have access to all of the defaults: * `default_env.nu` (internally defined) will be loaded whenever (and before) the user's `env.nu` is loaded. * `default_config.nu` (internally defined) will be loaded whenever (and before) the user's `config.nu` is loaded. * No more 900+ line config out-of-the-box. * Faster startup (again): ~40-45% improvement in launch time with a default configuration. * New keys that are added to the defaults in the future will automatically be available to all users after updating Nushell. No need to regenerate config to get the new defaults. * It is now possible to have different internal defaults (which will be used with `-c` and scripts) vs. REPL defaults. This would have solved many of the user complaints about the [`display_errors` implementation](https://www.nushell.sh/blog/2024-09-17-nushell_0_98_0.html#non-zero-exit-codes-are-now-errors-toc). * A basic "scaffold" `config.nu` and `env.nu` are created on first launch (if the config directory isn't present). * Improved "out-of-the-box" experience (OOBE) - No longer asks to create the files; the minimal scaffolding will be automatically created. If deleted, they will not be regenerated. This provides a better "out-of-the-box" experience for the user as they no longer have to make this decision (without much info on the pros or cons) when first launching. * <s>(New: 2024-11-07) Runs the env_conversions process after the `default_env.nu` is loaded so that users can treat `Path`/`PATH` as lists in their own config.</s> * (New: 2024-11-08) Given the changes in #13802, `default_config.nu` will be a minimal file to minimize load-times. This shaves another (on my system) ~3ms off the base launch time. * Related: Keybindings, menus, and hooks that are already internal defaults are no longer duplicated in `$env.config`. The documentation will be updated to cover these scenarios. * (New: 2024-11-08) Move existing "full" `default_config.nu` to `sample_config.nu` for short-term "documentation" purposes. * (New: 2024-11-18) Move the `dark-theme` and `light-theme` to Standard Library and demonstrate their use - Also improves startup times, but we're reaching the limit of optimization. * (New: 2024-11-18) Extensively documented/commented `sample_env.nu` and `sample_config.nu`. These can be displayed in-shell using (for example) `config nu --sample | nu-highlight | less -R`. Note: Much of this will eventually be moved to or (some) duplicated in the Doc. But for now, this some nice in-shell doc that replaces the older "commented/documented default". * (New: 2024-11-20) Runs the `ENV_CONVERSIONS` process (1) after the `default_env.nu` (allows `PATH` to be used as a list in user's `env.nu`) and (2) before `default_config.nu` is loaded (allows user's `ENV_CONVERSIONS` from their `env.nu` to be used in their `config.nu`). * <s>(New: 2024-11-20) The default `ENV_CONVERSIONS` is now an empty record. The internal Rust code handles `PATH` (and variants) conversions regardless of the `ENV_CONVERSIONS` variable. This shaves a *very* small amount of time off the startup.</s> Reset - Looks like there might be a bug in `nu-enginer::env::ensure_path()` on Windows that would need to be fixed in order for this to work. # User-Facing Changes By default, you shouldn't see much, if any, change when running this with your existing configuration. To see the greatest benefit from these changes, you'll probably want to start with a "fresh" config. This can be easily tested using something like: ```nushell let temp_home = (mktemp -d) $env.XDG_CONFIG_HOME = $temp_home $env.XDG_DATA_HOME = $temp_home ./target/release/nu ``` You should see a message where the (mostly empty) `env.nu` and `config.nu` are created on first start. Defaults should be the same (or similar to) those before the PR. Please let me know if you notice any differences. --- Users should now specify configuration in terms of overrides of each setting. For instance, rather than modifying `history` settings in the monolithic `config.nu`, the following is recommended in an updated `config.nu`: ```nu $env.config.history = { file_format: sqlite, sync_on_enter: true isolation: true max_size: 1_000_000 } ``` or even just: ```nu $env.config.history.file_format = sqlite $env.config.history.isolation: true $env.config.history.max_size = 1_000_000 ``` Note: It seems many users are already appending a `source my_config.nu` (or similar pattern) to the end of the existing `config.nu` to make updates easier. In this case, they will likely want to remove all of the previous defaults and just move their `my_config.nu` to `config.nu`. Note: It should be unlikely that there are any breaking changes here, but there's a slim chance that some code, somewhere, *expects* an absence of certain config values. Otherwise, all config values are available before and after this change. # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting Configuration Chapter (and related) of the doc is currently WIP and will be finished in time for 0.101 release.
300 lines
10 KiB
Rust
300 lines
10 KiB
Rust
use nu_path::{AbsolutePath, AbsolutePathBuf, Path};
|
|
use nu_test_support::nu;
|
|
use nu_test_support::playground::{Executable, Playground};
|
|
use pretty_assertions::assert_eq;
|
|
use std::fs::{self, File};
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
fn adjust_canonicalization<P: AsRef<Path>>(p: P) -> String {
|
|
p.as_ref().display().to_string()
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
fn adjust_canonicalization<P: AsRef<Path>>(p: P) -> String {
|
|
const VERBATIM_PREFIX: &str = r"\\?\";
|
|
let p = p.as_ref().display().to_string();
|
|
if let Some(stripped) = p.strip_prefix(VERBATIM_PREFIX) {
|
|
stripped.to_string()
|
|
} else {
|
|
p
|
|
}
|
|
}
|
|
|
|
/// Make the config directory a symlink that points to a temporary folder, and also makes
|
|
/// the nushell directory inside a symlink.
|
|
/// Returns the path to the `nushell` config folder inside, via the symlink.
|
|
fn setup_fake_config(playground: &mut Playground) -> AbsolutePathBuf {
|
|
let config_real = "config_real";
|
|
let config_link = "config_link";
|
|
let nushell_real = "nushell_real";
|
|
let nushell_link = Path::new(config_real)
|
|
.join("nushell")
|
|
.into_os_string()
|
|
.into_string()
|
|
.unwrap();
|
|
|
|
let config_home = playground.cwd().join(config_link);
|
|
|
|
playground.mkdir(nushell_real);
|
|
playground.mkdir(config_real);
|
|
playground.symlink(nushell_real, &nushell_link);
|
|
playground.symlink(config_real, config_link);
|
|
playground.with_env("XDG_CONFIG_HOME", config_home.to_str().unwrap());
|
|
|
|
let path = config_home.join("nushell");
|
|
path.canonicalize().map(Into::into).unwrap_or(path)
|
|
}
|
|
|
|
fn run(playground: &mut Playground, command: &str) -> String {
|
|
let result = playground.pipeline(command).execute().map_err(|e| {
|
|
let outcome = e.output.map(|outcome| {
|
|
format!(
|
|
"out: '{}', err: '{}'",
|
|
String::from_utf8_lossy(&outcome.out),
|
|
String::from_utf8_lossy(&outcome.err)
|
|
)
|
|
});
|
|
format!(
|
|
"desc: {}, exit: {:?}, outcome: {}",
|
|
e.desc,
|
|
e.exit,
|
|
outcome.unwrap_or("empty".to_owned())
|
|
)
|
|
});
|
|
String::from_utf8_lossy(&result.unwrap().out)
|
|
.trim()
|
|
.to_string()
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
fn run_interactive_stderr(xdg_config_home: impl AsRef<Path>) -> String {
|
|
let child_output = std::process::Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!(
|
|
"{:?} -i -c 'echo $nu.is-interactive'",
|
|
nu_test_support::fs::executable_path()
|
|
))
|
|
.env("XDG_CONFIG_HOME", adjust_canonicalization(xdg_config_home))
|
|
.output()
|
|
.expect("Should have outputted");
|
|
|
|
return String::from_utf8_lossy(&child_output.stderr)
|
|
.trim()
|
|
.to_string();
|
|
}
|
|
|
|
fn test_config_path_helper(
|
|
playground: &mut Playground,
|
|
config_dir_nushell: impl AsRef<AbsolutePath>,
|
|
) {
|
|
let config_dir_nushell = config_dir_nushell.as_ref();
|
|
|
|
// Create the config dir folder structure if it does not already exist
|
|
if !config_dir_nushell.exists() {
|
|
let _ = fs::create_dir_all(config_dir_nushell);
|
|
}
|
|
|
|
let config_dir_nushell = config_dir_nushell
|
|
.canonicalize()
|
|
.expect("canonicalize config dir failed");
|
|
let actual = run(playground, "$nu.default-config-dir");
|
|
assert_eq!(actual, adjust_canonicalization(&config_dir_nushell));
|
|
|
|
let config_path = config_dir_nushell.join("config.nu");
|
|
// We use canonicalize here in case the config or env is symlinked since $nu.config-path is returning the canonicalized path in #8653
|
|
let canon_config_path =
|
|
adjust_canonicalization(std::fs::canonicalize(&config_path).unwrap_or(config_path.into()));
|
|
let actual = run(playground, "$nu.config-path");
|
|
assert_eq!(actual, canon_config_path);
|
|
|
|
let env_path = config_dir_nushell.join("env.nu");
|
|
let canon_env_path =
|
|
adjust_canonicalization(std::fs::canonicalize(&env_path).unwrap_or(env_path.into()));
|
|
let actual = run(playground, "$nu.env-path");
|
|
assert_eq!(actual, canon_env_path);
|
|
|
|
let history_path = config_dir_nushell.join("history.txt");
|
|
let canon_history_path = adjust_canonicalization(
|
|
std::fs::canonicalize(&history_path).unwrap_or(history_path.into()),
|
|
);
|
|
let actual = run(playground, "$nu.history-path");
|
|
assert_eq!(actual, canon_history_path);
|
|
|
|
let login_path = config_dir_nushell.join("login.nu");
|
|
let canon_login_path =
|
|
adjust_canonicalization(std::fs::canonicalize(&login_path).unwrap_or(login_path.into()));
|
|
let actual = run(playground, "$nu.loginshell-path");
|
|
assert_eq!(actual, canon_login_path);
|
|
|
|
#[cfg(feature = "plugin")]
|
|
{
|
|
let plugin_path = config_dir_nushell.join("plugin.msgpackz");
|
|
let canon_plugin_path = adjust_canonicalization(
|
|
std::fs::canonicalize(&plugin_path).unwrap_or(plugin_path.into()),
|
|
);
|
|
let actual = run(playground, "$nu.plugin-path");
|
|
assert_eq!(actual, canon_plugin_path);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_default_config_path() {
|
|
Playground::setup("default_config_path", |_, playground| {
|
|
let config_dir = nu_path::nu_config_dir().expect("Could not get config directory");
|
|
test_config_path_helper(playground, config_dir);
|
|
});
|
|
}
|
|
|
|
/// Make the config folder a symlink to a temporary folder without any config files
|
|
/// and see if the config files' paths are properly canonicalized
|
|
#[test]
|
|
fn test_default_symlinked_config_path_empty() {
|
|
Playground::setup("symlinked_empty_config_dir", |_, playground| {
|
|
let config_dir_nushell = setup_fake_config(playground);
|
|
test_config_path_helper(playground, config_dir_nushell);
|
|
});
|
|
}
|
|
|
|
/// Like [`test_default_symlinked_config_path_empty`], but fill the temporary folder
|
|
/// with broken symlinks and see if they're properly canonicalized
|
|
#[test]
|
|
fn test_default_symlink_config_path_broken_symlink_config_files() {
|
|
Playground::setup(
|
|
"symlinked_cfg_dir_with_symlinked_cfg_files_broken",
|
|
|_, playground| {
|
|
let fake_config_dir_nushell = setup_fake_config(playground);
|
|
|
|
let fake_dir = "fake";
|
|
playground.mkdir(fake_dir);
|
|
let fake_dir = Path::new(fake_dir);
|
|
|
|
for config_file in [
|
|
"config.nu",
|
|
"env.nu",
|
|
"history.txt",
|
|
"history.sqlite3",
|
|
"login.nu",
|
|
"plugin.msgpackz",
|
|
] {
|
|
let fake_file = fake_dir.join(config_file);
|
|
File::create(playground.cwd().join(&fake_file)).unwrap();
|
|
|
|
playground.symlink(&fake_file, fake_config_dir_nushell.join(config_file));
|
|
}
|
|
|
|
// Windows doesn't allow creating a symlink without the file existing,
|
|
// so we first create original files for the symlinks, then delete them
|
|
// to break the symlinks
|
|
std::fs::remove_dir_all(playground.cwd().join(fake_dir)).unwrap();
|
|
|
|
test_config_path_helper(playground, fake_config_dir_nushell);
|
|
},
|
|
);
|
|
}
|
|
|
|
/// Like [`test_default_symlinked_config_path_empty`], but fill the temporary folder
|
|
/// with working symlinks to empty files and see if they're properly canonicalized
|
|
#[test]
|
|
fn test_default_config_path_symlinked_config_files() {
|
|
Playground::setup(
|
|
"symlinked_cfg_dir_with_symlinked_cfg_files",
|
|
|_, playground| {
|
|
let fake_config_dir_nushell = setup_fake_config(playground);
|
|
|
|
for config_file in [
|
|
"config.nu",
|
|
"env.nu",
|
|
"history.txt",
|
|
"history.sqlite3",
|
|
"login.nu",
|
|
"plugin.msgpackz",
|
|
] {
|
|
let empty_file = playground.cwd().join(format!("empty-{config_file}"));
|
|
File::create(&empty_file).unwrap();
|
|
playground.symlink(empty_file, fake_config_dir_nushell.join(config_file));
|
|
}
|
|
|
|
test_config_path_helper(playground, fake_config_dir_nushell);
|
|
},
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_alternate_config_path() {
|
|
let config_file = "crates/nu-utils/src/default_files/scaffold_config.nu";
|
|
let env_file = "crates/nu-utils/src/default_files/scaffold_env.nu";
|
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory");
|
|
|
|
let config_path =
|
|
nu_path::canonicalize_with(config_file, &cwd).expect("Could not get config path");
|
|
let actual = nu!(
|
|
cwd: &cwd,
|
|
format!("nu --config {config_path:?} -c '$nu.config-path'")
|
|
);
|
|
assert_eq!(actual.out, config_path.to_string_lossy().to_string());
|
|
|
|
let env_path = nu_path::canonicalize_with(env_file, &cwd).expect("Could not get env path");
|
|
let actual = nu!(
|
|
cwd: &cwd,
|
|
format!("nu --env-config {env_path:?} -c '$nu.env-path'")
|
|
);
|
|
assert_eq!(actual.out, env_path.to_string_lossy().to_string());
|
|
}
|
|
|
|
#[test]
|
|
fn test_xdg_config_empty() {
|
|
Playground::setup("xdg_config_empty", |_, playground| {
|
|
playground.with_env("XDG_CONFIG_HOME", "");
|
|
|
|
let actual = run(playground, "$nu.default-config-dir");
|
|
let expected = dirs::config_dir().unwrap().join("nushell");
|
|
assert_eq!(
|
|
actual,
|
|
adjust_canonicalization(expected.canonicalize().unwrap_or(expected))
|
|
);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn test_xdg_config_bad() {
|
|
Playground::setup("xdg_config_bad", |_, playground| {
|
|
let xdg_config_home = r#"mn2''6t\/k*((*&^//k//: "#;
|
|
playground.with_env("XDG_CONFIG_HOME", xdg_config_home);
|
|
|
|
let actual = run(playground, "$nu.default-config-dir");
|
|
let expected = dirs::config_dir().unwrap().join("nushell");
|
|
assert_eq!(
|
|
actual,
|
|
adjust_canonicalization(expected.canonicalize().unwrap_or(expected))
|
|
);
|
|
|
|
#[cfg(not(windows))]
|
|
{
|
|
let stderr = run_interactive_stderr(xdg_config_home);
|
|
assert!(
|
|
stderr.contains("xdg_config_home_invalid"),
|
|
"stderr was {}",
|
|
stderr
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Shouldn't complain if XDG_CONFIG_HOME is a symlink
|
|
#[test]
|
|
#[cfg(not(windows))]
|
|
fn test_xdg_config_symlink() {
|
|
Playground::setup("xdg_config_symlink", |_, playground| {
|
|
let config_link = "config_link";
|
|
|
|
playground.symlink("real", config_link);
|
|
|
|
let stderr = run_interactive_stderr(playground.cwd().join(config_link));
|
|
assert!(
|
|
!stderr.contains("xdg_config_home_invalid"),
|
|
"stderr was {}",
|
|
stderr
|
|
);
|
|
});
|
|
}
|