Fix incorrect handling of boolean flags for builtin commands (#11492)

# Description
Possible fix of #11456
This PR fixes a bug where builtin commands did not respect the logic of
dynamically passed boolean flags. The reason is
[has_flag](6f59abaf43/crates/nu-protocol/src/ast/call.rs (L204C5-L212C6))
method did not evaluate and take into consideration expression used with
flag.

To address this issue a solution is proposed:
1. `has_flag` method is moved to `CallExt` and new logic to evaluate
expression and check if it is a boolean value is added
2. `has_flag_const` method is added to `CallExt` which is a constant
version of `has_flag`
3. `has_named` method is added to `Call` which is basically the old
logic of `has_flag`
4. All usages of `has_flag` in code are updated, mostly to pass
`engine_state` and `stack` to new `has_flag`. In `run_const` commands it
is replaced with `has_flag_const`. And in a few select places: parser,
`to nuon` and `into string` old logic via `has_named` is used.

# User-Facing Changes
Explicit values of boolean flags are now respected in builtin commands.
Before:

![image](https://github.com/nushell/nushell/assets/17511668/f9fbabb2-3cfd-43f9-ba9e-ece76d80043c)
After:

![image](https://github.com/nushell/nushell/assets/17511668/21867596-2075-437f-9c85-45563ac70083)

Another example:
Before:

![image](https://github.com/nushell/nushell/assets/17511668/efdbc5ca-5227-45a4-ac5b-532cdc2bbf5f)
After:

![image](https://github.com/nushell/nushell/assets/17511668/2907d5c5-aa93-404d-af1c-21cdc3d44646)


# Tests + Formatting
Added test reproducing some variants of original issue.
This commit is contained in:
Artemiy
2024-01-11 18:19:48 +03:00
committed by GitHub
parent 62272975f2
commit 1867bb1a88
149 changed files with 771 additions and 504 deletions

View File

@ -166,9 +166,9 @@ fn from_csv(
.get_flag(engine_state, stack, "escape")?
.map(|v: Value| v.as_char())
.transpose()?;
let no_infer = call.has_flag("no-infer");
let noheaders = call.has_flag("noheaders");
let flexible = call.has_flag("flexible");
let no_infer = call.has_flag(engine_state, stack, "no-infer")?;
let noheaders = call.has_flag(engine_state, stack, "noheaders")?;
let flexible = call.has_flag(engine_state, stack, "flexible")?;
let trim = trim_from_str(call.get_flag(engine_state, stack, "trim")?)?;
let config = DelimitedReaderConfig {

View File

@ -1,3 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -47,7 +48,7 @@ impl Command for FromJson {
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
@ -59,7 +60,7 @@ impl Command for FromJson {
}
// TODO: turn this into a structured underline of the nu_json error
if call.has_flag("objects") {
if call.has_flag(engine_state, stack, "objects")? {
let converted_lines: Vec<Value> = string_input
.lines()
.filter_map(move |x| {

View File

@ -289,8 +289,8 @@ fn from_ssv(
) -> Result<PipelineData, ShellError> {
let name = call.head;
let noheaders = call.has_flag("noheaders");
let aligned_columns = call.has_flag("aligned-columns");
let noheaders = call.has_flag(engine_state, stack, "noheaders")?;
let aligned_columns = call.has_flag(engine_state, stack, "aligned-columns")?;
let minimum_spaces: Option<Spanned<usize>> =
call.get_flag(engine_state, stack, "minimum-spaces")?;

View File

@ -132,9 +132,9 @@ fn from_tsv(
.get_flag(engine_state, stack, "escape")?
.map(|v: Value| v.as_char())
.transpose()?;
let no_infer = call.has_flag("no-infer");
let noheaders = call.has_flag("noheaders");
let flexible = call.has_flag("flexible");
let no_infer = call.has_flag(engine_state, stack, "no-infer")?;
let noheaders = call.has_flag(engine_state, stack, "noheaders")?;
let flexible = call.has_flag(engine_state, stack, "flexible")?;
let trim = trim_from_str(call.get_flag(engine_state, stack, "trim")?)?;
let config = DelimitedReaderConfig {

View File

@ -1,5 +1,6 @@
use crate::formats::nu_xml_format::{COLUMN_ATTRS_NAME, COLUMN_CONTENT_NAME, COLUMN_TAG_NAME};
use indexmap::map::IndexMap;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -46,14 +47,14 @@ string. This way content of every tag is always a table and is easier to parse"#
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let keep_comments = call.has_flag("keep-comments");
let keep_processing_instructions = call.has_flag("keep-pi");
let keep_comments = call.has_flag(engine_state, stack, "keep-comments")?;
let keep_processing_instructions = call.has_flag(engine_state, stack, "keep-pi")?;
let info = ParsingInfo {
span: head,
keep_comments,

View File

@ -67,7 +67,7 @@ impl Command for ToCsv {
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let noheaders = call.has_flag("noheaders");
let noheaders = call.has_flag(engine_state, stack, "noheaders")?;
let separator: Option<Spanned<String>> = call.get_flag(engine_state, stack, "separator")?;
let config = engine_state.get_config();
to_csv(input, noheaders, separator, head, config)

View File

@ -44,8 +44,8 @@ impl Command for ToJson {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let raw = call.has_flag("raw");
let use_tabs = call.has_flag("tabs");
let raw = call.has_flag(engine_state, stack, "raw")?;
let use_tabs = call.has_flag(engine_state, stack, "tabs")?;
let span = call.head;
// allow ranges to expand and turn into array

View File

@ -1,5 +1,6 @@
use indexmap::map::IndexMap;
use nu_cmd_base::formats::to::delimited::merge_descriptors;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -67,13 +68,13 @@ impl Command for ToMd {
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let pretty = call.has_flag("pretty");
let per_element = call.has_flag("per-element");
let pretty = call.has_flag(engine_state, stack, "pretty")?;
let per_element = call.has_flag(engine_state, stack, "per-element")?;
let config = engine_state.get_config();
to_md(input, pretty, per_element, config, head)
}

View File

@ -53,20 +53,18 @@ impl Command for ToNuon {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let raw = call.has_flag("raw");
let use_tabs = call.has_flag("tabs");
let use_indent = call.has_flag("indent");
let raw = call.has_flag(engine_state, stack, "raw")?;
let tabs: Option<usize> = call.get_flag(engine_state, stack, "tabs")?;
let indent: Option<usize> = call.get_flag(engine_state, stack, "indent")?;
let span = call.head;
let value = input.into_value(span);
let nuon_result = if raw {
value_to_string(&value, span, 0, None)
} else if use_tabs {
let tab_count: usize = call.get_flag(engine_state, stack, "tabs")?.unwrap_or(1);
} else if let Some(tab_count) = tabs {
value_to_string(&value, span, 0, Some(&"\t".repeat(tab_count)))
} else if use_indent {
let indent: usize = call.get_flag(engine_state, stack, "indent")?.unwrap_or(2);
} else if let Some(indent) = indent {
value_to_string(&value, span, 0, Some(&" ".repeat(indent)))
} else {
value_to_string(&value, span, 0, None)

View File

@ -1,4 +1,5 @@
use crate::formats::to::delimited::to_delimited_data;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -49,12 +50,12 @@ impl Command for ToTsv {
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let noheaders = call.has_flag("noheaders");
let noheaders = call.has_flag(engine_state, stack, "noheaders")?;
let config = engine_state.get_config();
to_tsv(input, noheaders, head, config)
}