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

env: support string args by "-S", "-vS" or "--split-strings" #5801

Merged

Conversation

cre4ture
Copy link
Contributor

@cre4ture cre4ture commented Jan 7, 2024

addresses #5710

This implements all the functionality tested by gnu/tests/env/env-S.pl.
I had to patch the test due to more powerfull implementation and differencences in how parser errors are reported.
I Introduced a mechanism to patch the test with patch-files as the sed-stuff I could not make run for me.

The feature is quite complex, as the tool needs do parsing and evaluation (quoting, escape sequences, environmental variables) of the string on a similar level as a shell:
env -S<single full featured shell command>

I used "shell-words" create (MIT license) as a basis. I did heavy modifications, such that I directly commit the sources to this repo. EDIT: Additionally I extended shell-words with a variable expanstion functionality as needed by env.
The idea to use subst was rejected after discussions with the subst maintainer.

@sylvestre
Copy link
Contributor

I really don't like forking. Did you try to contact upstream to propose this behavior?

Also, please squash your changes before you push them. Thanks

@cre4ture
Copy link
Contributor Author

cre4ture commented Jan 7, 2024

I think the changes for subst are small and probably acceptable by upstream. I'll do a pull request for this and try to get it in.
Regarding shell-words I'm not so optimistic. The needed changes where really against many of the existing tests there. And then I started to do refactorings and integrated the variable substitution directly... I hope your fine with doing a "fork" there. I think this is less of an issue as the original implementation in only one single file with ~500 lines of code. What do you think?

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from 3efb40a to 52077cb Compare January 7, 2024 14:09
Copy link

github-actions bot commented Jan 7, 2024

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/env/env-S is no longer failing!

@sylvestre
Copy link
Contributor

you forked this file, right ?
https://github.com/tmiasko/shell-words/blob/master/src/lib.rs

@cre4ture
Copy link
Contributor Author

cre4ture commented Jan 8, 2024

yes. its content is partly in new file split_iterator.rs and the tests I moved into the test_env.rs test suite

Copy link

github-actions bot commented Jan 8, 2024

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch 2 times, most recently from 80a3291 to d76ee6b Compare January 10, 2024 21:11
Copy link

GNU testsuite comparison:

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

Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

src/uu/env/Cargo.toml Outdated Show resolved Hide resolved
@cakebaker cakebaker linked an issue Jan 11, 2024 that may be closed by this pull request
@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from 06d603a to a4a3ba1 Compare January 12, 2024 20:29
Copy link

GNU testsuite comparison:

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

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from 84448b4 to 299decf Compare January 12, 2024 21:23
Copy link

GNU testsuite comparison:

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

Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

1 similar comment
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

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/env/env-S is no longer failing!

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from b995a0d to c5eebe6 Compare January 14, 2024 12:47
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/env/env-S is no longer failing!

Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

2 similar comments
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

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/env/env-S is no longer failing!

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from 514a489 to ab50934 Compare January 15, 2024 21:29
@cre4ture cre4ture requested a review from sylvestre January 15, 2024 21:30
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

1 similar comment
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from ab5b5b8 to b980a88 Compare February 27, 2024 15:49
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from b980a88 to 2bd2e58 Compare March 8, 2024 22:04
Copy link

github-actions bot commented Mar 8, 2024

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/env/env-S is no longer failing!

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from 2bd2e58 to 7271b35 Compare March 14, 2024 18:38
@cre4ture
Copy link
Contributor Author

I implemented it now by using u8 byte arrays on linux and u16 word arrays on windows.
This way, it should be fully compatible with non-utf encoded sequences on both operating system types.

Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!
Congrats! The gnu test tests/mv/backup-dir is no longer failing!

@sylvestre
Copy link
Contributor

is it ready for review ? :)
thanks

@cre4ture cre4ture force-pushed the feature/env_string_args_try_with_shell_words branch from 7271b35 to c62ba55 Compare March 18, 2024 23:45
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/env/env-S is no longer failing!

@cre4ture
Copy link
Contributor Author

@sylvestre yes its ready. thanks

}

// NOTE: we manually set and unset the env vars below rather than using Command::env() to more
// easily handle the case where no command is given
#[allow(clippy::cognitive_complexity)]
Copy link
Contributor

Choose a reason for hiding this comment

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

for a future PR, it would be nice to address this warning

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is moved code. But I'll address it anyway.

program: vec![],
};

// change directory
Copy link
Contributor

Choose a reason for hiding this comment

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

explain the why :)

Copy link
Contributor Author

@cre4ture cre4ture Mar 22, 2024

Choose a reason for hiding this comment

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

this is moved code. But I'll address it anyway by extraction to function

// load .env-style config file prior to those given on the command-line
load_config_file(&mut opts)?;

// unset specified env vars
Copy link
Contributor

Choose a reason for hiding this comment

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

same, explain why we unset these variables.
Security?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is moved code. But I'll address it anyway by extraction to function.
the variables are unset because the corresponding command line argument to unset the variables is provided.

// `exit.code()` returns `None` on Unix when the process is terminated by a signal.
// See std::os::unix::process::ExitStatusExt for more information. This prints out
// the interrupted process and the signal it received.
let signal_code = exit.signal().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

should we handle the potential errors ? (same question for the next line)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is moved code.
I validated that the unwraps are save. So I think there is nothing to address.

// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// This module contains classes and functions for dealing with the differences
Copy link
Contributor

Choose a reason for hiding this comment

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

this can't be used elsewhere in the coreutils ?

Copy link
Contributor

Choose a reason for hiding this comment

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

if so, maybe it could be moved into uucore
cc @cakebaker @tertsdiepraam

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 was planning to move it as soon as I found some other usecase for it.
But If you wish, I move it also in advance.

@sylvestre
Copy link
Contributor

Some minor comments, it is impressive work btw :)

@sylvestre sylvestre merged commit 08172c2 into uutils:main Mar 19, 2024
64 checks passed
@sylvestre
Copy link
Contributor

given the size of the work, let's land it now and fix my comments later :)
well done again

@cre4ture
Copy link
Contributor Author

given the size of the work, let's land it now and fix my comments later :) well done again

thanks. :-)
I'll create a new PR for the findings then.

@sylvestre
Copy link
Contributor

@cre4ture seems that v9.5 of coreutils made this env-S test fails
#6139

@cre4ture
Copy link
Contributor Author

its not related to "-S" as its in the env.sh.
But I investigated it already. It seems that there was a new feature added to env:

‘-a arg’
‘--argv0=arg’

    Override the zeroth argument passed to the command being executed. Without this option a default value of command is used.

if rust std-library allows to override this then it should be an easy thing. If not, it's difficult.

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.

env: split-string implementation
3 participants