Skip to content

Commit c473479

Browse files
committed
Add env-var to suppress rustc caching
1 parent 6b1dc52 commit c473479

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

src/cargo/util/config.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ pub struct Config {
6666
easy: LazyCell<RefCell<Easy>>,
6767
/// Cache of the `SourceId` for crates.io
6868
crates_io_source_id: LazyCell<SourceId>,
69+
/// If false, don't cache `rustc --version --verbose` invocations
70+
cache_rustc_info: bool,
71+
/// Creation time of this config, used to output the total build time
6972
creation_time: Instant,
7073
}
7174

@@ -82,6 +85,11 @@ impl Config {
8285
}
8386
});
8487

88+
let cache_rustc_info = match env::var("CARGO_CACHE_RUSTC_INFO") {
89+
Ok(cache) => cache != "0",
90+
_ => true,
91+
};
92+
8593
Config {
8694
home_path: Filesystem::new(homedir),
8795
shell: RefCell::new(shell),
@@ -103,6 +111,7 @@ impl Config {
103111
cli_flags: CliUnstable::default(),
104112
easy: LazyCell::new(),
105113
crates_io_source_id: LazyCell::new(),
114+
cache_rustc_info,
106115
creation_time: Instant::now(),
107116
}
108117
}
@@ -162,7 +171,11 @@ impl Config {
162171
Rustc::new(
163172
self.get_tool("rustc")?,
164173
self.maybe_get_tool("rustc_wrapper")?,
165-
cache_location,
174+
if self.cache_rustc_info {
175+
cache_location
176+
} else {
177+
None
178+
},
166179
)
167180
}
168181

src/doc/src/reference/environment-variables.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,31 @@ with them:
99
You can override these environment variables to change Cargo's behavior on your
1010
system:
1111

12-
* `CARGO_HOME` - Cargo maintains a local cache of the registry index and of git
12+
* `CARGO_HOME` Cargo maintains a local cache of the registry index and of git
1313
checkouts of crates. By default these are stored under `$HOME/.cargo`, but
1414
this variable overrides the location of this directory. Once a crate is cached
1515
it is not removed by the clean command.
16-
* `CARGO_TARGET_DIR` - Location of where to place all generated artifacts,
16+
* `CARGO_TARGET_DIR` Location of where to place all generated artifacts,
1717
relative to the current working directory.
18-
* `RUSTC` - Instead of running `rustc`, Cargo will execute this specified
18+
* `RUSTC` Instead of running `rustc`, Cargo will execute this specified
1919
compiler instead.
20-
* `RUSTC_WRAPPER` - Instead of simply running `rustc`, Cargo will execute this
20+
* `RUSTC_WRAPPER` Instead of simply running `rustc`, Cargo will execute this
2121
specified wrapper instead, passing as its commandline arguments the rustc
2222
invocation, with the first argument being rustc.
23-
* `RUSTDOC` - Instead of running `rustdoc`, Cargo will execute this specified
23+
* `RUSTDOC` Instead of running `rustdoc`, Cargo will execute this specified
2424
`rustdoc` instance instead.
25-
* `RUSTDOCFLAGS` - A space-separated list of custom flags to pass to all `rustdoc`
25+
* `RUSTDOCFLAGS` A space-separated list of custom flags to pass to all `rustdoc`
2626
invocations that Cargo performs. In contrast with `cargo rustdoc`, this is
2727
useful for passing a flag to *all* `rustdoc` instances.
28-
* `RUSTFLAGS` - A space-separated list of custom flags to pass to all compiler
28+
* `RUSTFLAGS` A space-separated list of custom flags to pass to all compiler
2929
invocations that Cargo performs. In contrast with `cargo rustc`, this is
3030
useful for passing a flag to *all* compiler instances.
31-
* `CARGO_INCREMENTAL` - If this is set to 1 then Cargo will force incremental
31+
* `CARGO_INCREMENTAL` If this is set to 1 then Cargo will force incremental
3232
compilation to be enabled for the current compilation, and when set to 0 it
3333
will force disabling it. If this env var isn't present then cargo's defaults
3434
will otherwise be used.
35+
* `CARGO_CACHE_RUSTC_INFO` — If this is set to 0 then Cargo will not try to cache
36+
compiler version information.
3537

3638
Note that Cargo will also read environment variables for `.cargo/config`
3739
configuration values, as described in [that documentation][config-env]

tests/testsuite/rustc_info_cache.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ fn rustc_info_cache() {
1919
.build();
2020

2121
let miss = "[..] rustc info cache miss[..]";
22-
let hit = "[..] rustc info cache hit[..]";
22+
let hit = "[..]rustc info cache hit[..]";
23+
let uncached = "[..]rustc info uncached[..]";
24+
let update = "[..]updated rustc info cache[..]";
2325

2426
assert_that(
2527
p.cargo("build").env("RUST_LOG", "cargo::util::rustc=info"),
2628
execs()
2729
.with_status(0)
2830
.with_stderr_contains("[..]failed to read rustc info cache[..]")
2931
.with_stderr_contains(miss)
30-
.with_stderr_does_not_contain(hit),
32+
.with_stderr_does_not_contain(hit)
33+
.with_stderr_contains(update),
3134
);
3235

3336
assert_that(
@@ -36,7 +39,19 @@ fn rustc_info_cache() {
3639
.with_status(0)
3740
.with_stderr_contains("[..]reusing existing rustc info cache[..]")
3841
.with_stderr_contains(hit)
39-
.with_stderr_does_not_contain(miss),
42+
.with_stderr_does_not_contain(miss)
43+
.with_stderr_does_not_contain(update),
44+
);
45+
46+
assert_that(
47+
p.cargo("build")
48+
.env("RUST_LOG", "cargo::util::rustc=info")
49+
.env("CARGO_CACHE_RUSTC_INFO", "0"),
50+
execs()
51+
.with_status(0)
52+
.with_stderr_contains("[..]rustc info cache disabled[..]")
53+
.with_stderr_contains(uncached)
54+
.with_stderr_does_not_contain(update),
4055
);
4156

4257
let other_rustc = {
@@ -80,7 +95,8 @@ fn rustc_info_cache() {
8095
.with_status(0)
8196
.with_stderr_contains("[..]different compiler, creating new rustc info cache[..]")
8297
.with_stderr_contains(miss)
83-
.with_stderr_does_not_contain(hit),
98+
.with_stderr_does_not_contain(hit)
99+
.with_stderr_contains(update),
84100
);
85101

86102
assert_that(
@@ -91,7 +107,8 @@ fn rustc_info_cache() {
91107
.with_status(0)
92108
.with_stderr_contains("[..]reusing existing rustc info cache[..]")
93109
.with_stderr_contains(hit)
94-
.with_stderr_does_not_contain(miss),
110+
.with_stderr_does_not_contain(miss)
111+
.with_stderr_does_not_contain(update),
95112
);
96113

97114
other_rustc.move_into_the_future();
@@ -104,7 +121,8 @@ fn rustc_info_cache() {
104121
.with_status(0)
105122
.with_stderr_contains("[..]different compiler, creating new rustc info cache[..]")
106123
.with_stderr_contains(miss)
107-
.with_stderr_does_not_contain(hit),
124+
.with_stderr_does_not_contain(hit)
125+
.with_stderr_contains(update),
108126
);
109127

110128
assert_that(
@@ -115,6 +133,7 @@ fn rustc_info_cache() {
115133
.with_status(0)
116134
.with_stderr_contains("[..]reusing existing rustc info cache[..]")
117135
.with_stderr_contains(hit)
118-
.with_stderr_does_not_contain(miss),
136+
.with_stderr_does_not_contain(miss)
137+
.with_stderr_does_not_contain(update),
119138
);
120139
}

0 commit comments

Comments
 (0)