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

Read environment variables from .cargo/config.toml's [env] section #233

Closed
wants to merge 2 commits into from

Conversation

MarijnS95
Copy link
Member

@MarijnS95 MarijnS95 commented Mar 1, 2022

Closes #230

Environment variables can be set and optionally override the process environment through .cargo/config.toml's [env] section: https://doc.rust-lang.org/cargo/reference/config.html#env

These config variables have specific precedence rules with regards to overriding the environment set in the process, and can optionally represent paths relative to the parent of the containing .cargo/ folder. The entire handling of these variables is implemented in rust-mobile/cargo-subcommand#12 which can be read as-is from cargo-apk and ndk-build, through a trait to keep ndk-build oblivious for cargo-subcommand.

cargo-apk/src/apk.rs Show resolved Hide resolved
Some(config) => config
.resolve_env(key)
// IO error is currently only returned when path canonicalization fails
.expect("Failed to resolve environment variable")
Copy link
Contributor

Choose a reason for hiding this comment

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

intergrating the queried key into the error msg would be helpful imo

ndk-build/src/cargo.rs Outdated Show resolved Hide resolved
@MarijnS95 MarijnS95 force-pushed the env branch 2 times, most recently from 6735098 to e2ed7da Compare March 2, 2022 14:25
@MarijnS95 MarijnS95 requested a review from msiglreith March 2, 2022 14:27
Environment variables can be set and optionally override the process
environment through `.cargo/config.toml`'s `[env]` section:
https://doc.rust-lang.org/cargo/reference/config.html#env

These config variables have specific precedence rules with regards to
overriding the environment set in the process, and can optionally
represent paths relative to the parent of the containing `.cargo/`
folder.  The entire handling of these variables is implemented in
rust-mobile/cargo-subcommand#12 which can be read
as-is from cargo-apk and ndk-build, through a trait to keep ndk-build
oblivious for cargo-subcommand.
@dvc94ch
Copy link
Contributor

dvc94ch commented Mar 4, 2022

mmh, don't necessarily like that this env thing needs to be passed in to the Ndk. wouldn't it be better to do what cargo does and have cargo-subcommand set the environment variables?

@dvc94ch
Copy link
Contributor

dvc94ch commented Mar 4, 2022

sorry for being a bit late to the game, hadn't looked at this PR yet.

@MarijnS95
Copy link
Member Author

MarijnS95 commented Mar 4, 2022

mmh, don't necessarily like that this env thing needs to be passed in to the Ndk. wouldn't it be better to do what cargo does and have cargo-subcommand set the environment variables?

I hadn't really even considered that. Is it something we'd like cargo-subcommand to do instead of forcing everyone to implement this environment search?

EDIT: That way this PR likely becomes a no-op and can be closed.

@dvc94ch
Copy link
Contributor

dvc94ch commented Mar 4, 2022

I think so yes. I still think that down the road we want to remove arg parsing and instead have something called CargoWorkspace that could expose something like set_env or artifact returning the path of an artifact and whatever other useful stuf cargo-subcommand does.

@dvc94ch
Copy link
Contributor

dvc94ch commented Mar 4, 2022

Wondering what happens if you run the algorithm twice. set_env is idempotent? Running it twice will give the same result as running it once? cargo build will perform the setup again, so we don't want the results to be altered unexpectedly

@MarijnS95
Copy link
Member Author

set_env should be idempotent, and we won't modify any data in the environment, just overwrite it. Updating the process environment with stuff from [env] in cargo-subcommand should also be idempotent, though I'm curious how merging multiple .configs works, since you can then have different definitions for force=true (was it set in the process environment from the get-go, or because a previous .config set it?). That's probably documented though, will read it when we get to it.

@MarijnS95
Copy link
Member Author

I still think that down the road we want to remove arg parsing

Isn't cargo-subcommand still quite heavily reliant on cmdline arguments? Do we then let the caller parse it and pass us the arguments, or we parse the cmdline in cargo-subcommand and let the caller parse it for themselves?

@dvc94ch
Copy link
Contributor

dvc94ch commented Mar 4, 2022

well we can have a config struct with all the cmdline arguments. Something like this:

#[cfg_attr(feature = "clap", derive(Parser))]
struct Args {
    target_dir: Option<PathBuf>,
    package: Option<String>,
    examples: Option<bool>,
    example: Option<String>,
}

If a subcommand uses clap and uses all the supported args it can just #[clap(flatten)] cargo_args: cargo_subcommand::Args and otherwise just construct the Args struct manually.

@dvc94ch
Copy link
Contributor

dvc94ch commented Mar 4, 2022

Closing as per discussion

@dvc94ch dvc94ch closed this Mar 4, 2022
@MarijnS95
Copy link
Member Author

@dvc94ch Yeah that seems like a really neat solution, but isn't really relevant for this PR.

Not sure about preliminarily closing this before we have implemented the set_env() alternative in cargo-subcommand though, but I won't be able to get to it for a while unfortunately :(

@dvc94ch
Copy link
Contributor

dvc94ch commented Mar 4, 2022

I guess we can keep it open for now

@dvc94ch dvc94ch reopened this Mar 4, 2022
MarijnS95 added a commit to rust-mobile/cargo-subcommand that referenced this pull request Mar 22, 2022
…nment

Leaving the caller to read and merge config environment values with the
process environment is not only cumbersome as seen in
[rust-mobile/ndk#233], but these values need to be
propagated into the process environment anyway to be usable by any
subprocess spawned by the caller (ie. `cargo-apk` spawning `cargo
rustc`, which can internally spawn a bunch of compilers and linkers
too).

Instead, do this automatically when the caller invokes
`Subcommand::new` while still leaving the door open for crates to read
the environment from the public `subcommand.manifest.env` member.

[rust-mobile/ndk#233]: rust-mobile/ndk#233
@MarijnS95
Copy link
Member Author

This is finally unnecessary now that rust-mobile/cargo-subcommand#16 is open 🥳!

@MarijnS95 MarijnS95 closed this Mar 22, 2022
@MarijnS95 MarijnS95 deleted the env branch March 22, 2022 16:37
MarijnS95 added a commit to rust-mobile/cargo-subcommand that referenced this pull request Mar 22, 2022
…nment (#16)

Leaving the caller to read and merge config environment values with the
process environment is not only cumbersome as seen in
[rust-mobile/ndk#233], but these values need to be
propagated into the process environment anyway to be usable by any
subprocess spawned by the caller (ie. `cargo-apk` spawning `cargo
rustc`, which can internally spawn a bunch of compilers and linkers
too).

Instead, do this automatically when the caller invokes
`Subcommand::new` while still leaving the door open for crates to read
the environment from the public `subcommand.manifest.env` member.

[rust-mobile/ndk#233]: rust-mobile/ndk#233
@MarijnS95 MarijnS95 linked an issue Mar 28, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants