-
Notifications
You must be signed in to change notification settings - Fork 13.3k
io::process::Command: add fine-grained env builder #15353
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
Conversation
If I were to design the API from the ground up at this moment, I would tend to say the the shorter-named Cargo has a similar builder for the environment (predates pub fn env(mut self, key: &str, val: Option<&str>) -> ProcessBuilder; For us Hm, putting on the bikeshedding hat, what do you think of: fn env<T: ToCStr>(self, k: T, v: T) -> Self;
fn env_remove<T: ToCStr>(self, k: T) -> Self;
fn env_override<T: ToCStr>(self, k: &[(T, T)]) -> Self; Other than the naming (which may also be just fine), the rest of the patch looks good to me! |
@alexcrichton Updated, using I also updated some of the code using Let me know if you have any other suggestions! |
This commit changes the `io::process::Command` API to provide fine-grained control over the environment: * The `env` method now inserts/updates a key/value pair. * The `env_remove` method removes a key from the environment. * The old `env` method, which sets the entire environment in one shot, is renamed to `env_set_all`. It can be used in conjunction with the finer-grained methods. This renaming is a breaking change. To support these new methods, the internal `env` representation for `Command` has been changed to an optional `HashMap` holding owned `CString`s (to support non-utf8 data). The `HashMap` is only materialized if the environment is updated. The implementation does not try hard to avoid allocation, since the cost of launching a process will dwarf any allocation cost. This patch also adds `PartialOrd`, `Eq`, and `Hash` implementations for `CString`. [breaking-change]
@alexcrichton This is now passing on try. Take a quick look at the changes and send it to bors? |
@@ -574,7 +574,7 @@ fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) | |||
cmd.arg("./src/etc/lldb_batchmode.py") | |||
.arg(test_executable) | |||
.arg(debugger_script) | |||
.env([("PYTHONPATH", config.lldb_python_dir.clone().unwrap().as_slice())]); | |||
.env_set_all([("PYTHONPATH", config.lldb_python_dir.clone().unwrap().as_slice())]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like another candidate for env
over env_set_all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the existing code zaps the entire environment to contain just $PYTHONPATH
. Not sure if that was intended, but the new version keeps that behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I doubt that it was intended behavior, let's see what happens!
In general, it should probably continue to work, but it's also probably best to not nuke things like HOME and USER.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But not critical at all, of course.
I left two comments about where |
This commit adds `env_insert` and `env_remove` methods to the `Command` builder, easing updates to the environment variables for the child process. The existing method, `env`, is still available for overriding the entire environment in one shot (after which the `env_insert` and `env_remove` methods can be used to make further adjustments). To support these new methods, the internal `env` representation for `Command` has been changed to an optional `HashMap` holding owned `CString`s (to support non-utf8 data). The `HashMap` is only materialized if the environment is updated. The implementation does not try hard to avoid allocation, since the cost of launching a process will dwarf any allocation cost. This patch also adds `PartialOrd`, `Eq`, and `Hash` implementations for `CString`.
Quickfix for the build after rust-lang/rust#15353
Fixes build after rust-lang/rust#15353
Quickfix for the build after rust-lang/rust#15353
Add manual implementation of clone for tuples in mir interpreter And some other minor changes. Clone for tuple is not implemented in the std and it is magically implemented by the compiler, so we need this.
This commit adds
env_insert
andenv_remove
methods to theCommand
builder, easing updates to the environment variables for the child
process. The existing method,
env
, is still available for overridingthe entire environment in one shot (after which the
env_insert
andenv_remove
methods can be used to make further adjustments).To support these new methods, the internal
env
representation forCommand
has been changed to an optionalHashMap
holding ownedCString
s (to support non-utf8 data). TheHashMap
is onlymaterialized if the environment is updated. The implementation does not
try hard to avoid allocation, since the cost of launching a process will
dwarf any allocation cost.
This patch also adds
PartialOrd
,Eq
, andHash
implementations forCString
.