mirror of
https://github.com/nushell/nushell.git
synced 2025-01-24 07:09:02 +01: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.
121 lines
4.3 KiB
Rust
121 lines
4.3 KiB
Rust
#![doc = include_str!("../README.md")]
|
|
use log::trace;
|
|
use nu_engine::eval_block;
|
|
use nu_parser::parse;
|
|
use nu_protocol::{
|
|
debugger::WithoutDebug,
|
|
engine::{FileStack, Stack, StateWorkingSet, VirtualPath},
|
|
report_parse_error, PipelineData, VirtualPathId,
|
|
};
|
|
use std::path::PathBuf;
|
|
|
|
fn create_virt_file(working_set: &mut StateWorkingSet, name: &str, content: &str) -> VirtualPathId {
|
|
let sanitized_name = PathBuf::from(name).to_string_lossy().to_string();
|
|
let file_id = working_set.add_file(sanitized_name.clone(), content.as_bytes());
|
|
|
|
working_set.add_virtual_path(sanitized_name, VirtualPath::File(file_id))
|
|
}
|
|
|
|
pub fn load_standard_library(
|
|
engine_state: &mut nu_protocol::engine::EngineState,
|
|
) -> Result<(), miette::ErrReport> {
|
|
trace!("load_standard_library");
|
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
|
// Contents of the std virtual directory
|
|
let mut std_virt_paths = vec![];
|
|
|
|
// std/mod.nu
|
|
let std_mod_virt_file_id = create_virt_file(
|
|
&mut working_set,
|
|
"std/mod.nu",
|
|
include_str!("../std/mod.nu"),
|
|
);
|
|
std_virt_paths.push(std_mod_virt_file_id);
|
|
|
|
// Submodules/subdirectories ... std/<module>/mod.nu
|
|
let mut std_submodules = vec![
|
|
// Loaded at startup - Not technically part of std
|
|
("mod.nu", "std/core", include_str!("../std/core/mod.nu")),
|
|
// std submodules
|
|
("mod.nu", "std/assert", include_str!("../std/assert/mod.nu")),
|
|
("mod.nu", "std/bench", include_str!("../std/bench/mod.nu")),
|
|
("mod.nu", "std/dirs", include_str!("../std/dirs/mod.nu")),
|
|
("mod.nu", "std/dt", include_str!("../std/dt/mod.nu")),
|
|
(
|
|
"mod.nu",
|
|
"std/formats",
|
|
include_str!("../std/formats/mod.nu"),
|
|
),
|
|
("mod.nu", "std/help", include_str!("../std/help/mod.nu")),
|
|
("mod.nu", "std/input", include_str!("../std/input/mod.nu")),
|
|
("mod.nu", "std/iter", include_str!("../std/iter/mod.nu")),
|
|
("mod.nu", "std/log", include_str!("../std/log/mod.nu")),
|
|
("mod.nu", "std/math", include_str!("../std/math/mod.nu")),
|
|
("mod.nu", "std/util", include_str!("../std/util/mod.nu")),
|
|
("mod.nu", "std/xml", include_str!("../std/xml/mod.nu")),
|
|
("mod.nu", "std/config", include_str!("../std/config/mod.nu")),
|
|
];
|
|
|
|
for (filename, std_subdir_name, content) in std_submodules.drain(..) {
|
|
let mod_dir = PathBuf::from(std_subdir_name);
|
|
let name = mod_dir.join(filename);
|
|
let virt_file_id = create_virt_file(&mut working_set, &name.to_string_lossy(), content);
|
|
|
|
// Place file in virtual subdir
|
|
let mod_dir_filelist = vec![virt_file_id];
|
|
|
|
let virt_dir_id = working_set.add_virtual_path(
|
|
mod_dir.to_string_lossy().to_string(),
|
|
VirtualPath::Dir(mod_dir_filelist),
|
|
);
|
|
// Add the subdir to the list of paths in std
|
|
std_virt_paths.push(virt_dir_id);
|
|
}
|
|
|
|
// Create std virtual dir with all subdirs and files
|
|
let std_dir = PathBuf::from("std").to_string_lossy().to_string();
|
|
let _ = working_set.add_virtual_path(std_dir, VirtualPath::Dir(std_virt_paths));
|
|
|
|
// Load prelude
|
|
let (block, delta) = {
|
|
let source = r#"
|
|
# Prelude
|
|
use std/core *
|
|
"#;
|
|
|
|
// Add a placeholder file to the stack of files being evaluated.
|
|
// The name of this file doesn't matter; it's only there to set the current working directory to NU_STDLIB_VIRTUAL_DIR.
|
|
let placeholder = PathBuf::from("load std/core");
|
|
working_set.files = FileStack::with_file(placeholder);
|
|
|
|
let block = parse(
|
|
&mut working_set,
|
|
Some("loading stdlib"),
|
|
source.as_bytes(),
|
|
false,
|
|
);
|
|
|
|
// Remove the placeholder file from the stack of files being evaluated.
|
|
working_set.files.pop();
|
|
|
|
if let Some(err) = working_set.parse_errors.first() {
|
|
report_parse_error(&working_set, err);
|
|
}
|
|
|
|
(block, working_set.render())
|
|
};
|
|
|
|
engine_state.merge_delta(delta)?;
|
|
|
|
// We need to evaluate the module in order to run the `export-env` blocks.
|
|
let mut stack = Stack::new();
|
|
let pipeline_data = PipelineData::Empty;
|
|
|
|
eval_block::<WithoutDebug>(engine_state, &mut stack, &block, pipeline_data)?;
|
|
|
|
engine_state.merge_env(&mut stack)?;
|
|
|
|
Ok(())
|
|
}
|