Skip to content

fix(metadata set): return error when both --datasource-filepath and -datasource-ls are used - #16049

Merged
sholderbach merged 5 commits into
nushell:mainfrom
kumarUjjawal:refactor-command
Jul 2, 2025
Merged

sholderbach merged 5 commits into
nushell:mainfrom
kumarUjjawal:refactor-command

Conversation

@kumarUjjawal

Copy link
Copy Markdown
Contributor

Description

This PR improves the metadata set command by returning a clear error when both --datasource-filepath and --datasource-ls flags are used together. These flags are meant to be mutually exclusive, and previously this conflicting usage was silently ignored.

User-Facing Changes

  • Users will now see an error message if they use both --datasource-filepath and --datasource-ls together in metadata set.

Tests + Formatting

  • Added test at crates/nu-command/tests/commands/debug/metadata_set.rs to verify the error behavior.
  • Ran cargo fmt --all -- --check
  • Ran cargo clippy --workspace -- -D warnings -D clippy::unwrap_used
  • Ran cargo test --workspace

After Submitting

N/A

Comment thread crates/nu-command/src/debug/metadata.rs Resolved
Comment thread crates/nu-command/src/debug/metadata.rs Outdated
Comment on lines +59 to +65
if has_input {
return Ok(Value::record(
extend_record_with_metadata(Record::new(), input.metadata().as_ref(), call.head),
call.head,
)
.into_pipeline_data());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@132ikl does this part clash in logic with your change from #16014 (it doesn't overlap in the diff but early returns)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be the right approach?

         if has_input {
            let metadata = input.metadata();
            let val = input.into_value(call.head)?;
            return Ok(
                build_metadata_record_value(&val, metadata.as_ref(), call.head)
                    .into_pipeline_data(),
            );
        }

@132ikl 132ikl Jul 2, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly, I don't think the early return is necessary at all, we're already handling the case where the argument is None in the big match. i added a suggestion down below

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also my bad i deleted my github comment because github was showing it twice but that deleted both versions of my comment apparently, lol

Comment thread crates/nu-command/src/debug/metadata.rs Outdated
Comment on lines +46 to +65
let has_input = !matches!(input, PipelineData::Empty);

if let Some(arg_expr) = arg {
if has_input {
return Err(ShellError::IncompatibleParameters {
left_message: "pipeline input was provided".into(),
left_span: head,
right_message: "but a positional metadata expression was also given".into(),
right_span: arg_expr.span,
});
}
}

if has_input {
return Ok(Value::record(
extend_record_with_metadata(Record::new(), input.metadata().as_ref(), call.head),
call.head,
)
.into_pipeline_data());
}

@132ikl 132ikl Jul 2, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let has_input = !matches!(input, PipelineData::Empty);
if let Some(arg_expr) = arg {
if has_input {
return Err(ShellError::IncompatibleParameters {
left_message: "pipeline input was provided".into(),
left_span: head,
right_message: "but a positional metadata expression was also given".into(),
right_span: arg_expr.span,
});
}
}
if has_input {
return Ok(Value::record(
extend_record_with_metadata(Record::new(), input.metadata().as_ref(), call.head),
call.head,
)
.into_pipeline_data());
}
if !matches!(input, PipelineData::Empty) {
if let Some(arg_expr) = arg {
return Err(ShellError::IncompatibleParameters {
left_message: "pipeline input was provided".into(),
left_span: head,
right_message: "but a positional metadata expression was also given".into(),
right_span: arg_expr.span,
});
}
}

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for iterating @kumarUjjawal and thanks for the feedback @132ikl

@sholderbach
sholderbach merged commit 118857a into nushell:main Jul 2, 2025
@github-actions github-actions Bot added this to the v0.106.0 milestone Jul 2, 2025
fdncred added a commit that referenced this pull request Oct 16, 2026
…peline input instead of erroring (#19254)

## Description

`metadata` with an argument fails when the call also has pipeline input:

```nushell
[1] | each {|x| let s = (metadata $x).span; $s }
# Error: nu::shell::incompatible_parameters
#   pipeline input was provided / but a positional metadata expression was also given
```

The user never piped anything into `metadata` here. The first statement
of a block receives the block's input, which is intended (`"a" | do {
let x = (str uppercase); $x }` returns `A`), so `metadata` gets the
closure's input along with `$x` and refuses. The same check rejects `"a"
| metadata $in`, because `$in` stores the input in the variable and
still passes it to the call.

The check came in with another PR. A command can't tell implicit block
input from an explicit pipe at run time: both arrive as ordinary
`PipelineData` in the same register.

This PR removes the check. When `metadata` gets an argument, the result
describes only that argument: a record with its `span`. The input and
its pipeline metadata are ignored. Before #16049 the argument's span was
merged with the input's pipeline metadata, which attached the input's
metadata to an unrelated value:

```nushell
open Cargo.toml | metadata 5
# {span: ..., source: ".../Cargo.toml"}    <- input's source reported for the literal 5
ls | do { let m = (metadata $env.PWD); $m }
# {span: ..., path_columns: [name], ...}   <- ls's path columns reported for $env.PWD
```

`stor insert --data-record` and `stor update --update-record` had the
same input-versus-argument check and failed the same way inside a block:

```nushell
[{a: 1}] | each {|r| stor insert -t t -d $r }
# Error: Pipeline and Flag both being used
```

## User-facing changes (Release notes)

- `metadata <expression>` no longer errors when the call also has
pipeline input, such as the input a block's first statement receives
(`[1] | each {|x| let s = (metadata $x).span; $s }`) or `"a" | metadata
$in`. With an argument, `metadata` returns that argument's span and
ignores the input; `metadata` without an argument still reports the
input's pipeline metadata.
- `stor insert --data-record` and `stor update --update-record` no
longer error with "Pipeline and Flag both being used" when the call also
has pipeline input. The flag's record takes precedence and the input is
ignored.

## Additional notes

- Since the input is ignored whenever an argument is given, an explicit
pipe into these calls is now dropped without an error too (`ls |
metadata $x`, `ls | stor insert -t t -d {a: 1}`). Erroring only on an
explicit pipe would need the compiler to mark implicit block input,
which no command can see today.
- `metadata $in` returns only the span. The input's `path_columns`,
`content_type` and custom keys are not included; use `metadata` without
an argument for those. (`source` from `open` is already removed when the
input is collected into `$in`.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants