Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error if unset argument starts with '=' for env command #6172

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

Carbrex
Copy link
Contributor

@Carbrex Carbrex commented Apr 1, 2024

#6165
#6166

Output

Before:

$ cargo run env | grep ZZZ 
ZZZ=zzz
$ cargo run env -u=ZZZ | grep ZZZ

(No output because ZZZ was unset)

After:

$ cargo run env | grep ZZZ
ZZZ=zzz
$ cargo run env -u=ZZZ | grep ZZZ
env: cannot unset '=ZZZ': Invalid argument
Try 'target/debug/coreutils env --help' for more information.

Tests:

Before:

$ cargo test test_env_with_gnu_reference_unset_invalid_variables

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 70 filtered out; finished in 0.00s


running 1 test
test test_env::test_env_with_gnu_reference_unset_invalid_variables ... FAILED

failures:

---- test_env::test_env_with_gnu_reference_unset_invalid_variables stdout ----
run: /home/geet/debian-gsoc/coreutils/target/debug/coreutils env -u=2EKt
thread 'test_env::test_env_with_gnu_reference_unset_invalid_variables' panicked at tests/by-util/test_env.rs:578:10:
Command was expected to fail.
stdout = LC_ALL=C
TZ=UTC

 stderr = 
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    test_env::test_env_with_gnu_reference_unset_invalid_variables

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 2875 filtered out; finished in 0.00s

After:

$ cargo test test_env_with_gnu_reference_unset_invalid_variables

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 70 filtered out; finished in 0.00s


running 1 test
test test_env::test_env_with_gnu_reference_unset_invalid_variables ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 2875 filtered out; finished in 0.01s

src/uu/env/src/env.rs Outdated Show resolved Hide resolved
@BenWiederhake
Copy link
Collaborator

Hi again! I made a mistake and merged your other PR a bit too quickly, which is why the other maintainers had to do a force-push. Sorry ^^'

I'd be happy to help you sort out this mess in case you get stuck with git rebase. I'm available to chat in the uutils Discord chat.

@Carbrex
Copy link
Contributor Author

Carbrex commented Apr 1, 2024

Hi again! I made a mistake and merged your other PR a bit too quickly, which is why the other maintainers had to do a force-push. Sorry ^^'

I'd be happy to help you sort out this mess in case you get stuck with git rebase. I'm available to chat in the uutils Discord chat.

Thanks for your help I will try to fix it and if I fail I will certainly ask you on discord.

@Carbrex Carbrex marked this pull request as draft April 1, 2024 15:37
@sylvestre
Copy link
Sponsor Contributor

Please rebase your work
Thanks

@Carbrex Carbrex force-pushed the patch-2-6165 branch 4 times, most recently from bd361f7 to 403da10 Compare April 1, 2024 15:58
@Carbrex Carbrex marked this pull request as ready for review April 1, 2024 15:58
@Carbrex
Copy link
Contributor Author

Carbrex commented Apr 1, 2024

Thanks for your patience! Its ready to be reviewed now.
I was not able to remove this Add test for ignoring time-style if time not defined commit using rebase as it shows some error.

src/uu/env/src/env.rs Outdated Show resolved Hide resolved
Fix clippy error

Fix formatting

Simplify unset prefix handling

Also include '=' in invalid_arg

cargo fmt
@sylvestre
Copy link
Sponsor Contributor

please fix the lint

@Carbrex
Copy link
Contributor Author

Carbrex commented Apr 1, 2024

please fix the lint

I think all the lint tests have passed, haven't they?

@@ -7,6 +7,7 @@
#[cfg(target_os = "linux")]
use crate::common::util::expected_result;
use crate::common::util::TestScenario;
#[rustfmt::skip]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm very surprised that this is necessary, and can't reproduce it. In fact, all CI so far always was green, so this is not a systemic issue. Also, removing the leading :: would break the code, so any program that suggests just removing them without replacement is obviously wrong.

Which rustfmt version are you using, and how exactly do you invoke it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am doing cargo fmt --all and when I run that it removes :: in front of env this resulting in error.

$ rustfmt --version
rustfmt 1.7.0-stable ( )

Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

please remove these skips :)

if needed, we will fix them into a different PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh, that is very weird.

$ cargo +stable fmt --version # I have the same version as you
rustfmt 1.7.0-stable (07dca48 2024-02-04)
$ cargo +stable fmt --all --check # It does not report anything
$ cargo +stable fmt --all # And running it in the same manner …
$ git diff # … does not cause any file changes.
$

If the formatting issues cause pre-commit to fail, you can skip it completely via git commit --no-verify. However, that's just a temporary solution.

@sylvestre
Copy link
Sponsor Contributor

Seems that it needs more work:
With this PR:

./target/debug/coreutils env -i -0 -u=kQ4dALb1 A=0d CZj=lYr
env: cannot unset '=kQ4dALb1': Invalid argument

With GNU:

$ /usr/bin/env -i -0 -u=kQ4dALb1 A=0d CZj=lYr
A=0dCZj=lYr%      

@Carbrex
Copy link
Contributor Author

Carbrex commented Apr 1, 2024

Seems that it needs more work: With this PR:

./target/debug/coreutils env -i -0 -u=kQ4dALb1 A=0d CZj=lYr
env: cannot unset '=kQ4dALb1': Invalid argument

With GNU:

$ /usr/bin/env -i -0 -u=kQ4dALb1 A=0d CZj=lYr
A=0dCZj=lYr%      

Ok thanks I will look into it.

@Carbrex
Copy link
Contributor Author

Carbrex commented Apr 1, 2024

These are some other results with GNU that don't match:

$ env -0 -u=kQ4dALb1 - A=0d CZj=lYr
A=0dCZj=lYr
$ env -0 -u=kQ4dALb1 - A=0d CZj=lYr 
A=0dCZj=lYr 
$ env -u=kQ4dALb1 - A=0d CZj=lYr 
A=0d
CZj=lYr
$ env -i -u=kQ4dALb1  A=0d CZj=lYr
A=0d
CZj=lYr

@Carbrex
Copy link
Contributor Author

Carbrex commented Apr 1, 2024

I am converting this to a draft pr until I fix these.

@Carbrex Carbrex marked this pull request as draft April 1, 2024 19:20
// clap automatically removes '=' from the beginning of the argument, so we need to check for it
for arg in &original_args {
let arg = arg.to_string_lossy();
let prefix = "-u=";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't that break something like env echo gotcha -u=hello ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, it definitely wouldn't catch env -vu=hello.

Suggestion: Instead of trying to parse the arguments again, is there a way to examine matches to determine what the exact argument was that leads clap to believe something? This should be much more robust.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, it really breaks it. Also, I have found some more cases where it breaks things. I am not sure of what approach I should do, because it looks like I am trying to shoehorn too much of if conditions to match the gnu behavior. Some help would be appreciated.

Copy link
Contributor Author

@Carbrex Carbrex Apr 2, 2024

Choose a reason for hiding this comment

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

Also, it definitely wouldn't catch env -vu=hello.

Suggestion: Instead of trying to parse the arguments again, is there a way to examine matches to determine what the exact argument was that leads clap to believe something? This should be much more robust.

I tried doing something with clap but it turns out that clap removes '=' before the argument by itself.
$ cargo run env -u=kQ4dALb1 A=0d CZj=lYr

This is what matches print:

matches: ArgMatches { valid_args: ["ignore-environment", "chdir", "null", "file", "unset", "debug", "split-string", "vars", "help", "version"], valid_subcommands: [], args: FlatMap { keys: ["unset", "vars", "ignore-environment", "null", "debug"], values: [MatchedArg { source: Some(CommandLine), indices: [2], type_id: Some(std::ffi::os_str::OsString), vals: [[AnyValue { inner: std::ffi::os_str::OsString }]], raw_vals: [["kQ4dALb1"]], ignore_case: false }, MatchedArg { source: Some(CommandLine), indices: [3, 4], type_id: Some(std::ffi::os_str::OsString), vals: [[AnyValue { inner: std::ffi::os_str::OsString }], [AnyValue { inner: std::ffi::os_str::OsString }]], raw_vals: [["A=0d"], ["CZj=lYr"]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [5], type_id: Some(bool), vals: [[AnyValue { inner: bool }]], raw_vals: [["false"]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [6], type_id: Some(bool), vals: [[AnyValue { inner: bool }]], raw_vals: [["false"]], ignore_case: false }, MatchedArg { source: Some(DefaultValue), indices: [7], type_id: Some(bool), vals: [[AnyValue { inner: bool }]], raw_vals: [["false"]], ignore_case: false }] }, subcommand: None }

@Carbrex
Copy link
Contributor Author

Carbrex commented Apr 2, 2024

Some more deviations from gnu are

$ env D=ddsa - A=0d CZj=lYr  
env: ‘-’: No such file or directory
                                                                                       
$ cargo run env D=ddsa - A=0d CZj=lYr 
D=ddsa
A=0d
CZj=lYr

$ env -- -u=kQ4dALb1 A=0d  CZj=lYr      
(Prints all environment variables)

After some testing I have found that gnu works if there is -i or '-' before or after '-u=adsfas'.
Also, when there is '--' before '-u=adfs'.
Some help would be appreciated on what should I do.

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/timeout/timeout is no longer failing!

@sylvestre
Copy link
Sponsor Contributor

is it ready to be reviewed? Thanks

@Carbrex
Copy link
Contributor Author

Carbrex commented Jun 4, 2024

No, sorry it isn't ready for review. This pr wont work because of so many weird behavior by gnu as mentioned in my previous comments.
But I guess the tests might be useful. I might write some more tests if needed?

@sylvestre
Copy link
Sponsor Contributor

Sure sounds good :)

@Carbrex
Copy link
Contributor Author

Carbrex commented Jun 5, 2024

I think I have added enough tests.

@Carbrex Carbrex marked this pull request as ready for review June 5, 2024 08:51
Copy link
Collaborator

@BenWiederhake BenWiederhake left a comment

Choose a reason for hiding this comment

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

Thanks for collecting and implementing the test cases! Currently, CI fails because the tests are run (and fail), therefore this cannot be merged. Also, I have the impression that some of these commands should behave differently, are you sure about the expected results? (See below)

tests/by-util/test_env.rs Outdated Show resolved Hide resolved
tests/by-util/test_env.rs Outdated Show resolved Hide resolved
tests/by-util/test_env.rs Show resolved Hide resolved
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.

None yet

3 participants