Skip to content

Commit

Permalink
remove nu_cli crate dependency from nu_std (#8807)
Browse files Browse the repository at this point in the history
now nu_std only depends on nu_parser, nu_protocol and miette
and removes the nu_cli dependency

this enables developers moving forward to come along and implement their
own CLI's without having to pull in a redundant nu-cli which will not be
needed for them.

I did this by moving report_error into nu_protocol
which nu_std already has a dependency on anyway....



- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
  • Loading branch information
stormasm committed Apr 8, 2023
1 parent 4156077 commit 60e6ea5
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 40 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/nu-cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use log::info;
use miette::Result;
use nu_command::util::report_error;
use nu_engine::{convert_env_values, eval_block};
use nu_parser::parse;
use nu_protocol::engine::Stack;
use nu_protocol::report_error;
use nu_protocol::{
engine::{EngineState, StateWorkingSet},
PipelineData, Spanned, Value,
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/config_files.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::util::eval_source;
use nu_command::util::report_error;
#[cfg(feature = "plugin")]
use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::report_error;
use nu_protocol::{HistoryFileFormat, PipelineData};
#[cfg(feature = "plugin")]
use nu_protocol::{ParseError, Spanned};
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/eval_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::util::eval_source;
use log::info;
use log::trace;
use miette::{IntoDiagnostic, Result};
use nu_command::util::report_error;
use nu_engine::{convert_env_values, current_dir};
use nu_parser::parse;
use nu_path::canonicalize_with;
use nu_protocol::report_error;
use nu_protocol::{
ast::Call,
engine::{EngineState, Stack, StateWorkingSet},
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use completions::{FileCompletion, NuCompleter};
pub use config_files::eval_config_contents;
pub use eval_file::evaluate_file;
pub use menus::{DescriptionMenu, NuHelpCompleter};
pub use nu_command::util::{get_init_cwd, report_error, report_error_new};
pub use nu_command::util::get_init_cwd;
pub use nu_highlight::NuHighlight;
pub use print::Print;
pub use prompt::NushellPrompt;
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/prompt_update.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::NushellPrompt;
use log::trace;
use nu_command::util::report_error;
use nu_engine::eval_subexpression;
use nu_protocol::report_error;
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
Config, PipelineData, Value,
Expand Down
5 changes: 3 additions & 2 deletions crates/nu-cli/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ use log::{trace, warn};
use miette::{IntoDiagnostic, Result};
use nu_color_config::StyleComputer;
use nu_command::hook::eval_hook;
use nu_command::util::{get_guaranteed_cwd, report_error, report_error_new};
use nu_command::util::get_guaranteed_cwd;
use nu_engine::{convert_env_values, eval_block};
use nu_parser::{lex, parse, trim_quotes_str};
use nu_protocol::{
config::NuCursorShape,
engine::{EngineState, Stack, StateWorkingSet},
format_duration, HistoryFileFormat, PipelineData, ShellError, Span, Spanned, Value,
format_duration, report_error, report_error_new, HistoryFileFormat, PipelineData, ShellError,
Span, Spanned, Value,
};
use nu_utils::utils::perf;
use reedline::{CursorConfig, DefaultHinter, EditCommand, Emacs, SqliteBackedHistory, Vi};
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use nu_command::hook::eval_hook;
use nu_command::util::{report_error, report_error_new};
use nu_engine::{eval_block, eval_block_with_early_return};
use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents};
use nu_protocol::engine::StateWorkingSet;
use nu_protocol::{
engine::{EngineState, Stack},
print_if_stream, PipelineData, ShellError, Span, Value,
};
use nu_protocol::{report_error, report_error_new};
#[cfg(windows)]
use nu_utils::enable_vt_processing;
use nu_utils::utils::perf;
Expand Down
3 changes: 2 additions & 1 deletion crates/nu-command/src/hook.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::util::{get_guaranteed_cwd, report_error, report_error_new};
use crate::util::get_guaranteed_cwd;
use miette::Result;
use nu_engine::{eval_block, eval_block_with_early_return};
use nu_parser::parse;
use nu_protocol::ast::PathMember;
use nu_protocol::cli_error::{report_error, report_error_new};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{BlockId, PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId};

Expand Down
24 changes: 2 additions & 22 deletions crates/nu-command/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
use nu_protocol::report_error;
use nu_protocol::{
ast::RangeInclusion,
engine::{EngineState, Stack, StateWorkingSet},
CliError, Range, ShellError, Span, Value,
Range, ShellError, Span, Value,
};
use std::path::PathBuf;

pub fn report_error(
working_set: &StateWorkingSet,
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
) {
eprintln!("Error: {:?}", CliError(error, working_set));
// reset vt processing, aka ansi because illbehaved externals can break it
#[cfg(windows)]
{
let _ = nu_utils::enable_vt_processing();
}
}

pub fn report_error_new(
engine_state: &EngineState,
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
) {
let working_set = StateWorkingSet::new(engine_state);

report_error(&working_set, error);
}

pub fn get_init_cwd() -> PathBuf {
std::env::current_dir().unwrap_or_else(|_| {
std::env::var("PWD")
Expand Down
23 changes: 22 additions & 1 deletion crates/nu-protocol/src/cli_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::engine::StateWorkingSet;
use crate::engine::{EngineState, StateWorkingSet};
use miette::{LabeledSpan, MietteHandlerOpts, ReportHandler, RgbColors, Severity, SourceCode};
use thiserror::Error;

Expand All @@ -18,6 +18,27 @@ pub fn format_error(
format!("Error: {:?}", CliError(error, working_set))
}

pub fn report_error(
working_set: &StateWorkingSet,
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
) {
eprintln!("Error: {:?}", CliError(error, working_set));
// reset vt processing, aka ansi because illbehaved externals can break it
#[cfg(windows)]
{
let _ = nu_utils::enable_vt_processing();
}
}

pub fn report_error_new(
engine_state: &EngineState,
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
) {
let working_set = StateWorkingSet::new(engine_state);

report_error(&working_set, error);
}

impl std::fmt::Debug for CliError<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let ansi_support = self.1.get_config().use_ansi_coloring;
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod alias;
pub mod ast;
mod cli_error;
pub mod cli_error;
pub mod config;
pub mod engine;
mod example;
Expand Down
1 change: 0 additions & 1 deletion crates/nu-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ version = "0.78.1"

[dependencies]
miette = { version = "5.6.0", features = ["fancy-no-backtrace"] }
nu-cli = { version = "0.78.1", path = "../nu-cli" }
nu-parser = { version = "0.78.1", path = "../nu-parser" }
nu-protocol = { version = "0.78.1", path = "../nu-protocol" }
2 changes: 1 addition & 1 deletion crates/nu-std/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use nu_cli::report_error;
use nu_parser::{parse, parse_module_block};
use nu_protocol::report_error;
use nu_protocol::{engine::StateWorkingSet, Module, ShellError, Span};

fn get_standard_library() -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nu_command::util::report_error;
use nu_engine::{get_full_help, CallExt};
use nu_parser::parse;
use nu_parser::{escape_for_script_arg, escape_quote_string};
use nu_protocol::report_error;
use nu_protocol::{
ast::{Call, Expr, Expression, PipelineElement},
engine::{Command, EngineState, Stack, StateWorkingSet},
Expand Down
2 changes: 1 addition & 1 deletion src/config_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use log::info;
#[cfg(feature = "plugin")]
use nu_cli::read_plugin_file;
use nu_cli::{eval_config_contents, eval_source};
use nu_command::util::report_error;
use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::report_error;
use nu_protocol::{ParseError, PipelineData, Spanned};
use nu_utils::{get_default_config, get_default_env};
use std::fs::File;
Expand Down
3 changes: 2 additions & 1 deletion src/ide.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use miette::IntoDiagnostic;
use nu_cli::{report_error, NuCompleter};
use nu_cli::NuCompleter;
use nu_parser::{flatten_block, parse, FlatShape};
use nu_protocol::report_error;
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
DeclId, ShellError, Span, Value, VarId,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use command::gather_commandline_args;
use log::Level;
use miette::Result;
use nu_cli::gather_parent_env_vars;
use nu_command::util::report_error_new;
use nu_command::{create_default_context, get_init_cwd};
use nu_protocol::report_error_new;
use nu_protocol::{util::BufferedReader, PipelineData, RawStream};
use nu_utils::utils::perf;
use run::{run_commands, run_file, run_repl};
Expand Down

0 comments on commit 60e6ea5

Please sign in to comment.