Skip to content

Send environment variables from client, hash CARGO_ env vars in Rust#92

Merged
luser merged 2 commits into
mozilla:masterfrom
luser:send-env-vars
Mar 28, 2017
Merged

Send environment variables from client, hash CARGO_ env vars in Rust#92
luser merged 2 commits into
mozilla:masterfrom
luser:send-env-vars

Conversation

@luser

@luser luser commented Mar 27, 2017

Copy link
Copy Markdown
Contributor

This fixes a longstanding issue about not using the client's environment for compilation, and then uses that fix to include CARGO_ env vars in Rust hash generation.

@luser luser requested a review from alexcrichton March 27, 2017 23:34

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

Comment thread src/compiler/c.rs Outdated
executable: &str,
parsed_args: &ParsedArguments,
cwd: &str,
env_vars: &Vec<(String, String)>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps &[(String, String)]?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

...yeah that makes more sense. Fixed.

Comment thread src/compiler/rust.rs
cmd.args(&arguments)
.args(&["--print", "file-names"])
.env_clear()
.envs(env_vars.iter().map(|&(ref k, ref v)| (k, v)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like it's somewhat tricky to remember (clear the env before setting more env vars). I wonder if sccache could benefit from a Command look alike which allows introspection (we still need to add this to libstd...) to maybe package up arguments like the executable, cwd, env, and base arguments?

(just a random note for a future PR/refactoring)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's a sensible idea. I actually went back and changed this to add envs on RunCommand to make it slightly less verbose, but encapsulating all this commonality seems like a good idea.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I filed #95 on this.

Comment thread src/compiler/rust.rs
// we'll just hash the CARGO_ env vars and hope that's sufficient.
// Upstream Rust issue tracking getting information about env! usage:
// https://github.com/rust-lang/rust/issues/40364
for &(ref var, ref val) in env_vars.iter() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we'll want to be sure to sort these before hashing, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oops, good catch! Might have even caught that with the unit test I added if I had used more than one env var...

Comment thread src/protocol.rs Outdated
/// The commandline arguments passed to the compiler.
pub args: Vec<String>,
/// The environment variables present when the compiler was executed, as (var, val).
pub env_vars: Vec<(String, String)>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With the new serde version I think this could also start to be a OsString as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I pushed the serde bump to master and then fell down this rabbit hole. The only thing that I didn't have a great solution for was adding OsStrings to the hash, so I wrote a crappy equivalent of OsStringExt::as_bytes for Windows that just mem::transmutes. I suspect it will not ever be a problem in practice (and I added a unit test that should fail if it ever breaks) but it sure feels wrong.

luser added 2 commits March 28, 2017 11:48
…ozilla#89

These variables are very commonly used in env! invocations, so include them
in hash generation so we don't cache compilations improperly.
@luser luser merged commit 1cbd656 into mozilla:master Mar 28, 2017
@luser luser deleted the send-env-vars branch March 28, 2017 17:03
Comment thread src/compiler/c.rs
let cached_env_vars: HashSet<OsString> = CACHED_ENV_VARS.iter().map(|v| OsStr::new(v).to_os_string()).collect();
for &(ref var, ref val) in env_vars.iter() {
if cached_env_vars.contains(var) {
m.update(os_str_bytes(var));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This runs a risk of breaking in future rust versions, but could Hash for OsStr be used here instead?

alnr pushed a commit to alnr/sccache that referenced this pull request Aug 3, 2021
…s-workflow

feat/ci/action: add gh-pages deployment job
tottoto pushed a commit to tottoto/sccache that referenced this pull request Feb 6, 2026
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.

2 participants