mirror of
https://github.com/nushell/nushell.git
synced 2025-08-12 20:27:52 +02:00
relocate debug commands (#8071)
# Description Now that we've landed the debug commands we were working on, let's relocate them to an easier place to find all of them. That's what this PR does. The only actual code change was changing the `timeit` command to a `Category::Debug` command. The rest is just moving things around and hooking them up. # User-Facing Changes # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `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 # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
@ -1,13 +1,3 @@
|
||||
mod is_admin;
|
||||
mod profile;
|
||||
mod view;
|
||||
mod view_files;
|
||||
mod view_source;
|
||||
mod view_span;
|
||||
|
||||
pub use is_admin::IsAdmin;
|
||||
pub use profile::Profile;
|
||||
pub use view::View;
|
||||
pub use view_files::ViewFiles;
|
||||
pub use view_source::ViewSource;
|
||||
pub use view_span::ViewSpan;
|
||||
|
@ -1,113 +0,0 @@
|
||||
use nu_engine::{eval_block, CallExt};
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Closure, Command, EngineState, ProfilingConfig, Stack};
|
||||
use nu_protocol::{
|
||||
Category, DataSource, Example, IntoPipelineData, PipelineData, PipelineMetadata, Signature,
|
||||
Spanned, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Profile;
|
||||
|
||||
impl Command for Profile {
|
||||
fn name(&self) -> &str {
|
||||
"profile"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Profile each pipeline element in a closure."
|
||||
}
|
||||
|
||||
fn extra_usage(&self) -> &str {
|
||||
r#"The command collects run time of every pipeline element, recursively stepping into child closures
|
||||
until a maximum depth. Optionally, it also collects the source code and intermediate values.
|
||||
|
||||
Current known limitations are:
|
||||
* profiling data from subexpressions is not tracked
|
||||
* it does not step into loop iterations"#
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("profile")
|
||||
.required(
|
||||
"closure",
|
||||
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
|
||||
"the closure to run",
|
||||
)
|
||||
.switch("source", "Collect source code in the report", None)
|
||||
.switch("values", "Collect values in the report", None)
|
||||
.named(
|
||||
"max-depth",
|
||||
SyntaxShape::Int,
|
||||
"How many levels of blocks to step into (default: 1)",
|
||||
Some('d'),
|
||||
)
|
||||
.input_output_types(vec![(Type::Any, Type::Table(vec![]))])
|
||||
.allow_variants_without_examples(true)
|
||||
.category(Category::Debug)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let capture_block: Spanned<Closure> = call.req(engine_state, stack, 0)?;
|
||||
let block = engine_state.get_block(capture_block.item.block_id);
|
||||
|
||||
let redirect_stdout = call.redirect_stdout;
|
||||
let redirect_stderr = call.redirect_stderr;
|
||||
|
||||
let mut stack = stack.captures_to_stack(&capture_block.item.captures);
|
||||
|
||||
let input_val = input.into_value(call.head);
|
||||
|
||||
if let Some(var) = block.signature.get_positional(0) {
|
||||
if let Some(var_id) = &var.var_id {
|
||||
stack.add_var(*var_id, input_val.clone());
|
||||
}
|
||||
}
|
||||
|
||||
stack.profiling_config = ProfilingConfig::new(
|
||||
call.get_flag::<i64>(engine_state, &mut stack, "max-depth")?
|
||||
.unwrap_or(1),
|
||||
call.has_flag("source"),
|
||||
call.has_flag("values"),
|
||||
);
|
||||
|
||||
let profiling_metadata = Box::new(PipelineMetadata {
|
||||
data_source: DataSource::Profiling(vec![]),
|
||||
});
|
||||
|
||||
let result = if let Some(PipelineMetadata {
|
||||
data_source: DataSource::Profiling(values),
|
||||
}) = eval_block(
|
||||
engine_state,
|
||||
&mut stack,
|
||||
block,
|
||||
input_val.into_pipeline_data_with_metadata(profiling_metadata),
|
||||
redirect_stdout,
|
||||
redirect_stderr,
|
||||
)?
|
||||
.metadata()
|
||||
.map(|m| *m)
|
||||
{
|
||||
Value::list(values, call.head)
|
||||
} else {
|
||||
Value::nothing(call.head)
|
||||
};
|
||||
|
||||
Ok(result.into_pipeline_data())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description:
|
||||
"Profile some code, stepping into the `spam` command and collecting source.",
|
||||
example: r#"def spam [] { "spam" }; profile { spam | str length } -d 2 --source"#,
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
use nu_engine::get_full_help;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct View;
|
||||
|
||||
impl Command for View {
|
||||
fn name(&self) -> &str {
|
||||
"view"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("view")
|
||||
.category(Category::Debug)
|
||||
.input_output_types(vec![(Type::Nothing, Type::String)])
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Various commands for viewing debug information"
|
||||
}
|
||||
|
||||
fn extra_usage(&self) -> &str {
|
||||
"You must use one of the following subcommands. Using this command as-is will only produce this help message."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
Ok(Value::String {
|
||||
val: get_full_help(
|
||||
&View.signature(),
|
||||
&View.examples(),
|
||||
engine_state,
|
||||
stack,
|
||||
self.is_parser_keyword(),
|
||||
),
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ViewFiles;
|
||||
|
||||
impl Command for ViewFiles {
|
||||
fn name(&self) -> &str {
|
||||
"view files"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"View the files registered in nushell's EngineState memory"
|
||||
}
|
||||
|
||||
fn extra_usage(&self) -> &str {
|
||||
"These are files parsed and loaded at runtime."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("view files")
|
||||
.input_output_types(vec![(Type::Nothing, Type::String)])
|
||||
.category(Category::Debug)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let mut records = vec![];
|
||||
|
||||
for (file, start, end) in engine_state.files() {
|
||||
records.push(Value::Record {
|
||||
cols: vec![
|
||||
"filename".to_string(),
|
||||
"start".to_string(),
|
||||
"end".to_string(),
|
||||
"size".to_string(),
|
||||
],
|
||||
vals: vec![
|
||||
Value::string(file, call.head),
|
||||
Value::int(*start as i64, call.head),
|
||||
Value::int(*end as i64, call.head),
|
||||
Value::int(*end as i64 - *start as i64, call.head),
|
||||
],
|
||||
span: call.head,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Value::List {
|
||||
vals: records,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "View the files registered in nushell's EngineState memory",
|
||||
example: r#"view files"#,
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
}
|
@ -1,194 +0,0 @@
|
||||
use itertools::Itertools;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
|
||||
Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ViewSource;
|
||||
|
||||
impl Command for ViewSource {
|
||||
fn name(&self) -> &str {
|
||||
"view source"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"View a block, module, or a definition"
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("view source")
|
||||
.input_output_types(vec![(Type::Nothing, Type::String)])
|
||||
.required("item", SyntaxShape::Any, "name or block to view")
|
||||
.category(Category::Debug)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let arg: Value = call.req(engine_state, stack, 0)?;
|
||||
let arg_span = arg.span()?;
|
||||
|
||||
match arg {
|
||||
Value::Block { val: block_id, .. } | Value::Closure { val: block_id, .. } => {
|
||||
let block = engine_state.get_block(block_id);
|
||||
|
||||
if let Some(span) = block.span {
|
||||
let contents = engine_state.get_span_contents(&span);
|
||||
Ok(Value::string(String::from_utf8_lossy(contents), call.head)
|
||||
.into_pipeline_data())
|
||||
} else {
|
||||
Ok(Value::string("<internal command>", call.head).into_pipeline_data())
|
||||
}
|
||||
}
|
||||
Value::String { val, .. } => {
|
||||
if let Some(decl_id) = engine_state.find_decl(val.as_bytes(), &[]) {
|
||||
// arg is a command
|
||||
let decl = engine_state.get_decl(decl_id);
|
||||
let sig = decl.signature();
|
||||
let vec_of_required = &sig.required_positional;
|
||||
let vec_of_optional = &sig.optional_positional;
|
||||
let vec_of_flags = &sig.named;
|
||||
// gets vector of positionals.
|
||||
if let Some(block_id) = decl.get_block_id() {
|
||||
let block = engine_state.get_block(block_id);
|
||||
if let Some(block_span) = block.span {
|
||||
let contents = engine_state.get_span_contents(&block_span);
|
||||
let mut final_contents = String::from("def ");
|
||||
final_contents.push_str(&val);
|
||||
// The name of the function...
|
||||
final_contents.push_str(" [ ");
|
||||
for n in vec_of_required {
|
||||
final_contents.push_str(&n.name);
|
||||
// name of positional arg
|
||||
final_contents.push(':');
|
||||
final_contents.push_str(&n.shape.to_string());
|
||||
final_contents.push(' ');
|
||||
}
|
||||
for n in vec_of_optional {
|
||||
final_contents.push_str(&n.name);
|
||||
// name of positional arg
|
||||
final_contents.push_str("?:");
|
||||
final_contents.push_str(&n.shape.to_string());
|
||||
final_contents.push(' ');
|
||||
}
|
||||
for n in vec_of_flags {
|
||||
final_contents.push_str("--");
|
||||
final_contents.push_str(&n.long);
|
||||
final_contents.push(' ');
|
||||
if n.short.is_some() {
|
||||
final_contents.push_str("(-");
|
||||
final_contents.push(n.short.expect("this cannot trigger."));
|
||||
final_contents.push(')');
|
||||
}
|
||||
if n.arg.is_some() {
|
||||
final_contents.push_str(": ");
|
||||
final_contents.push_str(
|
||||
&n.arg.as_ref().expect("this cannot trigger.").to_string(),
|
||||
);
|
||||
}
|
||||
final_contents.push(' ');
|
||||
}
|
||||
final_contents.push_str("] ");
|
||||
final_contents.push_str(&String::from_utf8_lossy(contents));
|
||||
Ok(Value::string(final_contents, call.head).into_pipeline_data())
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Cannot view value".to_string(),
|
||||
"the command does not have a viewable block".to_string(),
|
||||
Some(arg_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Cannot view value".to_string(),
|
||||
"the command does not have a viewable block".to_string(),
|
||||
Some(arg_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
} else if let Some(module_id) = engine_state.find_module(val.as_bytes(), &[]) {
|
||||
// arg is a module
|
||||
let module = engine_state.get_module(module_id);
|
||||
if let Some(module_span) = module.span {
|
||||
let contents = engine_state.get_span_contents(&module_span);
|
||||
Ok(Value::string(String::from_utf8_lossy(contents), call.head)
|
||||
.into_pipeline_data())
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Cannot view value".to_string(),
|
||||
"the module does not have a viewable block".to_string(),
|
||||
Some(arg_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
} else if let Some(alias_id) = engine_state.find_alias(val.as_bytes(), &[]) {
|
||||
let contents = &mut engine_state.get_alias(alias_id).iter().map(|span| {
|
||||
String::from_utf8_lossy(engine_state.get_span_contents(span)).to_string()
|
||||
});
|
||||
Ok(Value::String {
|
||||
val: contents.join(" "),
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Cannot view value".to_string(),
|
||||
"this name does not correspond to a viewable value".to_string(),
|
||||
Some(arg_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
}
|
||||
_ => Err(ShellError::GenericError(
|
||||
"Cannot view value".to_string(),
|
||||
"this value cannot be viewed".to_string(),
|
||||
Some(arg_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "View the source of a code block",
|
||||
example: r#"let abc = { echo 'hi' }; view source $abc"#,
|
||||
result: Some(Value::test_string("{ echo 'hi' }")),
|
||||
},
|
||||
Example {
|
||||
description: "View the source of a custom command",
|
||||
example: r#"def hi [] { echo 'Hi!' }; view source hi"#,
|
||||
result: Some(Value::test_string("{ echo 'Hi!' }")),
|
||||
},
|
||||
Example {
|
||||
description: "View the source of a custom command, which participates in the caller environment",
|
||||
example: r#"def-env foo [] { let-env BAR = 'BAZ' }; view source foo"#,
|
||||
result: Some(Value::test_string("{ let-env BAR = 'BAZ' }")),
|
||||
},
|
||||
Example {
|
||||
description: "View the source of a module",
|
||||
example: r#"module mod-foo { export-env { let-env FOO_ENV = 'BAZ' } }; view source mod-foo"#,
|
||||
result: Some(Value::test_string(" export-env { let-env FOO_ENV = 'BAZ' }")),
|
||||
},
|
||||
Example {
|
||||
description: "View the source of an alias",
|
||||
example: r#"alias hello = echo hi; view source hello"#,
|
||||
result: Some(Value::test_string("echo hi")),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned,
|
||||
SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ViewSpan;
|
||||
|
||||
impl Command for ViewSpan {
|
||||
fn name(&self) -> &str {
|
||||
"view span"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"View the contents of a span"
|
||||
}
|
||||
|
||||
fn extra_usage(&self) -> &str {
|
||||
"This command is meant for debugging purposes.\nIt allows you to view the contents of nushell spans.\nOne way to get spans is to pipe something into 'debug --raw'.\nThen you can use the Span { start, end } values as the start and end values for this command."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("view span")
|
||||
.input_output_types(vec![(Type::Nothing, Type::String)])
|
||||
.required("start", SyntaxShape::Int, "start of the span")
|
||||
.required("end", SyntaxShape::Int, "end of the span")
|
||||
.category(Category::Debug)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let start_span: Spanned<usize> = call.req(engine_state, stack, 0)?;
|
||||
let end_span: Spanned<usize> = call.req(engine_state, stack, 1)?;
|
||||
|
||||
if start_span.item < end_span.item {
|
||||
let bin_contents =
|
||||
engine_state.get_span_contents(&Span::new(start_span.item, end_span.item));
|
||||
Ok(
|
||||
Value::string(String::from_utf8_lossy(bin_contents), call.head)
|
||||
.into_pipeline_data(),
|
||||
)
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Cannot view span".to_string(),
|
||||
"this start and end does not correspond to a viewable value".to_string(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "View the source of a span. 1 and 2 are just example values. Use the return of debug -r to get the actual values",
|
||||
example: r#"some | pipeline | or | variable | debug -r; view span 1 2"#,
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user