Skip to content

Throw out error if external command in subexpression is failed to run#8204

Merged
kubouch merged 10 commits intonushell:mainfrom
WindSoilder:subexpression_with_external_failed
Mar 1, 2023
Merged

Throw out error if external command in subexpression is failed to run#8204
kubouch merged 10 commits intonushell:mainfrom
WindSoilder:subexpression_with_external_failed

Conversation

@WindSoilder
Copy link
Copy Markdown
Contributor

@WindSoilder WindSoilder commented Feb 25, 2023

Description

Fixes: #7690

User-Facing Changes

The following command:

❯ let x = (^cat asdf; "foo")
Error: nu::shell::external_command

  × External command failed
   ╭─[entry #1:1:1]
 1 │ let x = (^cat asdf; "foo")
   ·           ─┬─
   ·            ╰── External command failed
   ╰────
  help: cat: asdf: No such file or directory

As we can see, it will throws out error rather than return "foo".

I think it's fine, because the semantic of subexpression is eval a subexpression to a value, if the external command is failed to run, it means that we can't convert the result to value.

It's different to ^cat asdf, which runs the command directlry.

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 after the PR is merged, if necessary. This will help us keep the docs up to date.

@rgwood
Copy link
Copy Markdown
Contributor

rgwood commented Feb 25, 2023

Nice PR, thanks! This is an improvement, but the result of the subexpression is surprising to me:

〉let f = (^false; "foo")
〉$f$f | describe
string
〉(^false;"hello,world!") | describe
string

I'm not 100% sure what the desired behaviour is here, but returning an empty string seems wrong.

@WindSoilder
Copy link
Copy Markdown
Contributor Author

Hmm...Or the variable shouldn't defined?

And I'm not sure how it breaks test code, need to take a deeper look

@WindSoilder WindSoilder marked this pull request as draft February 25, 2023 09:13
@WindSoilder
Copy link
Copy Markdown
Contributor Author

Hi, refer to let assignment, sorry I don't have a perfect way....

Currently I have two options:

  1. Throws out error if external command runs into failed and then in the case let f = (^false; "foo"), variable f won't be defined. One shortcoming is that for pure expression (^cat asdf; "foo"), it still throws out error, which would make user confused, and the error message is not good.
❯ (^cat asdf; "foo")
cat: asdf: No such file or directory
Error: nu::shell::external_command

  × External command failed
   ╭─[entry #1:1:1]
 1 │ (^cat asdf; "foo")
   ·   ─┬─
   ·    ╰── External command failed
   ╰────
  help:
  1. Keep f to be empty string. And something like (^cat adsfa; "foo") works better

@WindSoilder WindSoilder marked this pull request as ready for review February 26, 2023 12:54
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 26, 2023

Codecov Report

Merging #8204 (19c3c35) into main (592e677) will increase coverage by 0.37%.
The diff coverage is 97.15%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8204      +/-   ##
==========================================
+ Coverage   67.72%   68.09%   +0.37%     
==========================================
  Files         621      620       -1     
  Lines       99312    99313       +1     
==========================================
+ Hits        67258    67629     +371     
+ Misses      32054    31684     -370     
Impacted Files Coverage Δ
crates/nu-command/src/default_context.rs 99.78% <ø> (-0.01%) ⬇️
crates/nu-protocol/src/pipeline_data.rs 77.45% <89.79%> (+0.46%) ⬆️
crates/nu-engine/src/eval.rs 75.59% <93.02%> (+0.59%) ⬆️
crates/nu-cli/src/print.rs 100.00% <100.00%> (ø)
crates/nu-cmd-lang/src/core_commands/alias.rs 100.00% <100.00%> (ø)
crates/nu-cmd-lang/src/core_commands/break_.rs 100.00% <100.00%> (ø)
...rates/nu-cmd-lang/src/core_commands/commandline.rs 54.68% <100.00%> (ø)
crates/nu-cmd-lang/src/core_commands/continue_.rs 100.00% <100.00%> (ø)
crates/nu-cmd-lang/src/core_commands/def.rs 100.00% <100.00%> (ø)
crates/nu-cmd-lang/src/core_commands/def_env.rs 100.00% <100.00%> (ø)
... and 176 more

@WindSoilder WindSoilder marked this pull request as draft February 26, 2023 13:42
@WindSoilder WindSoilder marked this pull request as ready for review February 26, 2023 13:43
@rgwood
Copy link
Copy Markdown
Contributor

rgwood commented Feb 26, 2023

I prefer 1. I think (^cat asdf; "foo") should fail if the call to cat fails.

@WindSoilder
Copy link
Copy Markdown
Contributor Author

Yeah, I like 1 too, and I've update title and descripiton to reflect latest changes

@WindSoilder WindSoilder changed the title Make semicolon works better in subcommand Throw out error if external command in subexpression is failed in run Feb 27, 2023
@rgwood
Copy link
Copy Markdown
Contributor

rgwood commented Feb 27, 2023

Nice. The new behaviour seems good to me 👍

Looks like the Windows virtualenv tests are failing with a stack overflow again 😭. Last time, @kubouch fixed this by refactoring code into a new function: #8198 Not sure if that's a viable solution here.

@WindSoilder WindSoilder changed the title Throw out error if external command in subexpression is failed in run Throw out error if external command in subexpression is failed to run Feb 27, 2023
@kubouch
Copy link
Copy Markdown
Contributor

kubouch commented Mar 1, 2023

Great the tests are working now!

One question: What value will be stored in x?

@WindSoilder
Copy link
Copy Markdown
Contributor Author

WindSoilder commented Mar 1, 2023

In this case x will remain undefined

@kubouch
Copy link
Copy Markdown
Contributor

kubouch commented Mar 1, 2023

Yeah, this is interesting, because parser still has the x definition (check $nu.scope.vars), but it's not in the Stack during evaluation. I think it's OK, but maybe good to keep in mind in case we see some weird behavior.

@kubouch kubouch merged commit dec0a25 into nushell:main Mar 1, 2023
WindSoilder added a commit to WindSoilder/nushell that referenced this pull request Mar 16, 2023
sophiajt pushed a commit that referenced this pull request Mar 16, 2023
…d to run (#8204)" (#8475)

This reverts commit dec0a25.

It breaks programs like `fzf`

# Description

Fixes: #8472 
Fixes:  #8313
Reopen: #7690 

# User-Facing Changes

_(List of all changes that impact the user experience here. This helps
us keep track of breaking 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

> **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
> ```

# 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.
sophiajt pushed a commit that referenced this pull request Mar 17, 2023
…d to run (#8204)" (#8475)

This reverts commit dec0a25.

It breaks programs like `fzf`

# Description

Fixes: #8472 
Fixes:  #8313
Reopen: #7690 

# User-Facing Changes

_(List of all changes that impact the user experience here. This helps
us keep track of breaking 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

> **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
> ```

# 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.
@WindSoilder WindSoilder deleted the subexpression_with_external_failed branch August 2, 2023 22:07
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.

SubExpression with external command failure does not stop command running

3 participants