Skip to content

Commit 56b3497

Browse files
committed
Auto merge of #9299 - ehuss:fix-config-include, r=alexcrichton
Fix config includes not working. The config-include feature wasn't working because the config values were being loaded before the call to `configure`, so the unstable flag was always false. I had to remove the unstable warning because it was a bit awkward to support, and I figure it's not that important.
2 parents 58a9613 + 1130bc0 commit 56b3497

File tree

2 files changed

+74
-21
lines changed

2 files changed

+74
-21
lines changed

src/cargo/util/config/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,15 @@ impl Config {
804804
self.cli_config = Some(cli_config.iter().map(|s| s.to_string()).collect());
805805
self.merge_cli_args()?;
806806
}
807+
if self.unstable_flags.config_include {
808+
// If the config was already loaded (like when fetching the
809+
// `[alias]` table), it was loaded with includes disabled because
810+
// the `unstable_flags` hadn't been set up, yet. Any values
811+
// fetched before this step will not process includes, but that
812+
// should be fine (`[alias]` is one of the only things loaded
813+
// before configure). This can be removed when stabilized.
814+
self.reload_rooted_at(self.cwd.clone())?;
815+
}
807816
let extra_verbose = verbose >= 2;
808817
let verbose = verbose != 0;
809818

@@ -956,10 +965,10 @@ impl Config {
956965
/// `seen` is used to check for cyclic includes.
957966
fn load_includes(&self, mut value: CV, seen: &mut HashSet<PathBuf>) -> CargoResult<CV> {
958967
// Get the list of files to load.
959-
let (includes, def) = match &mut value {
968+
let includes = match &mut value {
960969
CV::Table(table, _def) => match table.remove("include") {
961-
Some(CV::String(s, def)) => (vec![(s, def.clone())], def),
962-
Some(CV::List(list, def)) => (list, def),
970+
Some(CV::String(s, def)) => vec![(s, def.clone())],
971+
Some(CV::List(list, _def)) => list,
963972
Some(other) => bail!(
964973
"`include` expected a string or list, but found {} in `{}`",
965974
other.desc(),
@@ -973,8 +982,6 @@ impl Config {
973982
};
974983
// Check unstable.
975984
if !self.cli_unstable().config_include {
976-
self.shell().warn(format!("config `include` in `{}` ignored, the -Zconfig-include command-line flag is required",
977-
def))?;
978985
return Ok(value);
979986
}
980987
// Accumulate all values here.

tests/testsuite/config_include.rs

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
//! Tests for `include` config field.
22
3-
use super::config::{
4-
assert_error, assert_match, read_output, write_config, write_config_at, ConfigBuilder,
5-
};
6-
use cargo_test_support::{no_such_file_err_msg, paths};
3+
use super::config::{assert_error, write_config, write_config_at, ConfigBuilder};
4+
use cargo_test_support::{no_such_file_err_msg, paths, project};
75
use std::fs;
86

97
#[cargo_test]
108
fn gated() {
119
// Requires -Z flag.
1210
write_config("include='other'");
11+
write_config_at(
12+
".cargo/other",
13+
"
14+
othervalue = 1
15+
",
16+
);
1317
let config = ConfigBuilder::new().build();
14-
let output = read_output(config);
15-
let expected = "\
16-
warning: config `include` in `[..]/.cargo/config` ignored, \
17-
the -Zconfig-include command-line flag is required
18-
";
19-
assert_match(expected, &output);
18+
assert_eq!(config.get::<Option<i32>>("othervalue").unwrap(), None);
19+
let config = ConfigBuilder::new().unstable_flag("config-include").build();
20+
assert_eq!(config.get::<i32>("othervalue").unwrap(), 1);
2021
}
2122

2223
#[cargo_test]
@@ -43,6 +44,45 @@ fn simple() {
4344
assert_eq!(config.get::<i32>("key3").unwrap(), 4);
4445
}
4546

47+
#[cargo_test]
48+
fn works_with_cli() {
49+
write_config_at(
50+
".cargo/config.toml",
51+
"
52+
include = 'other.toml'
53+
[build]
54+
rustflags = ['-W', 'unused']
55+
",
56+
);
57+
write_config_at(
58+
".cargo/other.toml",
59+
"
60+
[build]
61+
rustflags = ['-W', 'unsafe-code']
62+
",
63+
);
64+
let p = project().file("src/lib.rs", "").build();
65+
p.cargo("build -v")
66+
.with_stderr(
67+
"\
68+
[COMPILING] foo v0.0.1 [..]
69+
[RUNNING] `rustc [..]-W unused`
70+
[FINISHED] [..]
71+
",
72+
)
73+
.run();
74+
p.cargo("build -v -Z config-include")
75+
.masquerade_as_nightly_cargo()
76+
.with_stderr(
77+
"\
78+
[COMPILING] foo v0.0.1 [..]
79+
[RUNNING] `rustc [..]-W unsafe-code -W unused`
80+
[FINISHED] [..]
81+
",
82+
)
83+
.run();
84+
}
85+
4686
#[cargo_test]
4787
fn left_to_right() {
4888
// How it merges multiple includes.
@@ -77,9 +117,11 @@ fn left_to_right() {
77117
fn missing_file() {
78118
// Error when there's a missing file.
79119
write_config("include='missing'");
80-
let config = ConfigBuilder::new().unstable_flag("config-include").build();
120+
let config = ConfigBuilder::new()
121+
.unstable_flag("config-include")
122+
.build_err();
81123
assert_error(
82-
config.get::<i32>("whatever").unwrap_err(),
124+
config.unwrap_err(),
83125
&format!(
84126
"\
85127
could not load Cargo configuration
@@ -103,9 +145,11 @@ fn cycle() {
103145
write_config_at(".cargo/config", "include='one'");
104146
write_config_at(".cargo/one", "include='two'");
105147
write_config_at(".cargo/two", "include='config'");
106-
let config = ConfigBuilder::new().unstable_flag("config-include").build();
148+
let config = ConfigBuilder::new()
149+
.unstable_flag("config-include")
150+
.build_err();
107151
assert_error(
108-
config.get::<i32>("whatever").unwrap_err(),
152+
config.unwrap_err(),
109153
"\
110154
could not load Cargo configuration
111155
@@ -147,9 +191,11 @@ fn cli_include() {
147191
fn bad_format() {
148192
// Not a valid format.
149193
write_config("include = 1");
150-
let config = ConfigBuilder::new().unstable_flag("config-include").build();
194+
let config = ConfigBuilder::new()
195+
.unstable_flag("config-include")
196+
.build_err();
151197
assert_error(
152-
config.get::<i32>("whatever").unwrap_err(),
198+
config.unwrap_err(),
153199
"\
154200
could not load Cargo configuration
155201

0 commit comments

Comments
 (0)