-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Provide an API to extract fields from Command builder #44434
Comments
Related to #42200 |
Let's say I am ok to implement this, how should I proceed ? Directly with a PR or with an RFC first ? |
Seems reasonable. I would be interested in seeing this explored in a PR. |
I am working on a crate that would directly benefit from this. Is anyone still working on this feature? If not, would someone mind mentoring me to open a PR? |
Also, I have a tangential question not related to this issue, but might be raised as a result of introducing this feature: is there any way in the standard library to check whether a given |
Add accessors to Command. This adds some accessor methods to `Command` to provide a way to access the values set when building the `Command`. An example where this can be useful is to display the command to be executed. This is roughly based on the [`ProcessBuilder`](https://github.com/rust-lang/cargo/blob/13b73cdaf76b2d9182515c9cf26a8f68342d08ef/src/cargo/util/process_builder.rs#L105-L134) in Cargo. Possible concerns about the API: - Values with NULs on Unix will be returned as `"<string-with-nul>"`. I don't think it is practical to avoid this, since otherwise a whole separate copy of all the values would need to be kept in `Command`. - Does not handle `arg0` on Unix. This can be awkward to support in `get_args` and is rarely used. I figure if someone really wants it, it can be added to `CommandExt` as a separate method. - Does not offer a way to detect `env_clear`. I'm uncertain if it would be useful for anyone. - Does not offer a way to get an environment variable by name (`get_env`). I figure this can be added later if anyone really wants it. I think the motivation for this is weak, though. Also, the API could be a little awkward (return a `Option<Option<&OsStr>>`?). - `get_envs` could skip "cleared" entries and just return `&OsStr` values instead of `Option<&OsStr>`. I'm on the fence here. My use case is to display a shell command, and I only intend it to be roughly equivalent to the actual execution, and I probably won't display `None` entries. I erred on the side of providing extra information, but I suspect many situations will just filter out the `None`s. - Could implement more iterator stuff (like `DoubleEndedIterator`). I have not implemented new std items before, so I'm uncertain if the existing issue should be reused, or if a new tracking issue is needed. cc rust-lang#44434
Add accessors to Command. This adds some accessor methods to `Command` to provide a way to access the values set when building the `Command`. An example where this can be useful is to display the command to be executed. This is roughly based on the [`ProcessBuilder`](https://github.com/rust-lang/cargo/blob/13b73cdaf76b2d9182515c9cf26a8f68342d08ef/src/cargo/util/process_builder.rs#L105-L134) in Cargo. Possible concerns about the API: - Values with NULs on Unix will be returned as `"<string-with-nul>"`. I don't think it is practical to avoid this, since otherwise a whole separate copy of all the values would need to be kept in `Command`. - Does not handle `arg0` on Unix. This can be awkward to support in `get_args` and is rarely used. I figure if someone really wants it, it can be added to `CommandExt` as a separate method. - Does not offer a way to detect `env_clear`. I'm uncertain if it would be useful for anyone. - Does not offer a way to get an environment variable by name (`get_env`). I figure this can be added later if anyone really wants it. I think the motivation for this is weak, though. Also, the API could be a little awkward (return a `Option<Option<&OsStr>>`?). - `get_envs` could skip "cleared" entries and just return `&OsStr` values instead of `Option<&OsStr>`. I'm on the fence here. My use case is to display a shell command, and I only intend it to be roughly equivalent to the actual execution, and I probably won't display `None` entries. I erred on the side of providing extra information, but I suspect many situations will just filter out the `None`s. - Could implement more iterator stuff (like `DoubleEndedIterator`). I have not implemented new std items before, so I'm uncertain if the existing issue should be reused, or if a new tracking issue is needed. cc rust-lang#44434
This is now implemented and available on nightly, I have updated the original description with details. |
I feel this would be generally useful for debugging Command processes |
@rust-lang/libs Can we stabilize this? It seems like the remaining concerns listed would not require API break to change. |
cc @rust-lang/libs |
@rfcbot merge |
Team member @yaahc has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Checking off sfacklers checkbox on the FCP since he left the libs team. |
@rfcbot concern arg0 I'm concerned about the potential for confusion introduced by indexing args without account for arg0. |
On Wed, Jul 28, 2021 at 01:43:46PM -0700, Eric Huss wrote:
> I think it'd be entirely reasonable for the Debug impl on Command to print environment variables and anything else that has been set.
I tested that out with bootstrap for a while, and the experience was pretty terrible. The amount of verbosity was overwhelming, and the tool usually wants more fine-grained control over what is exactly displayed (like no `-v`, `-v`, `-vv`). I personally would not like to see the Debug impl change until a long while after something like these getter methods are stabilized to give authors a chance to implement a better display. There is a separate issue #42200 for changing the Debug impl.
That sounds completely fair; thanks for clarifying!
|
No (re-)parsing is necessary; adding a path to I don't think one should have to write a wrapper for this. This kind of usecase should be supported out-of-the-box.
We can provide at least 2 levels of verbosity with |
@rfcbot resolve use-case |
@rfcbot resolve preprocessing |
Am I right in thinking that |
I've resolved two of the three concerns. I also want to confirm, here: the only plan is to support As long as there's no API that accepts a numeric "index", I'll resolve the arg0 concern as well. I just want to make sure that anything accepting an "index" uses 0 to mean arg0, and 1 to mean the first command-line argument. |
My understanding is that we don't want to support any of these APIs because that is not the point of There is some desire to support an extended |
In building that function call incrementally (which is a common usecase for builder patterns), some amount of read access is required, as I laid down above. This has nothing to do with using |
@rfcbot resolve arg0 |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC will be merged soon. |
Tracking issue: rust-lang#44434
Not that it affects this PR, but something like this would be fine I think: struct ProcessEnvSetting<'a> {
Inherit,
Unset,
Set(&'a OsStr),
}
impl<'a> ProcessEnvSetting<'a> {
// Convenience method.
fn value_in_child(&self) -> Option<Cow<'a, OsStr>> {
// query current environment if `Inherit`
}
}
|
std: Stabilize command_access Tracking issue: rust-lang#44434 (not yet closed but the FCP is done so that should be soon).
std: Stabilize command_access Tracking issue: rust-lang#44434 (not yet closed but the FCP is done so that should be soon).
Triage: The feature has been stabilized by #88436, closing as complete. |
This is now implemented on nightly via #77029.
Summary
The following accessors are available on
Command
behind the#![feature(command_access)]
gate:Command::get_program
Command::get_args
Command::get_envs
Command::get_current_dir
Unresolved issues
Some concerns I had with the implementation, but probably not important:
"<string-with-nul>"
. I don't think it is practical to avoid this, since otherwise a whole separate copy of all the values would need to be kept inCommand
.arg0
on Unix. This can be awkward to support inget_args
and is rarely used. I figure if someone really wants it, it can be added toCommandExt
as a separate method.env_clear
. I'm uncertain if it would be useful for anyone.get_env
). I figure this can be added later if anyone really wants it. I think the motivation for this is weak, though. Also, the API could be a little awkward (return aOption<Option<&OsStr>>
?).get_envs
could skip "cleared" entries and just return&OsStr
values instead ofOption<&OsStr>
. I'm on the fence here. My use case is to display a shell command, and I only intend it to be roughly equivalent to the actual execution, and I probably won't displayNone
entries. I erred on the side of providing extra information, but I suspect many situations will just filter out theNone
s.DoubleEndedIterator
).Original issue below
The the
std::process::Command
builder is useful for building up a Command in a cross-platform way. I've found that it would be useful to extract the name, args, environment, etc. of aCommand
they have been set.There are at least two places in the Rust compiler that would benefit from such an API. Instead, the authors have had to resort to wrappers instead of using
Command
directly.https://github.com/rust-lang/rust/blob/master/src/tools/compiletest/src/runtest.rs#L1527
https://github.com/rust-lang/rust/blob/master/src/librustc_trans/back/command.rs
The text was updated successfully, but these errors were encountered: