Skip to content

Commit b1e9bcd

Browse files
committed
Auto merge of #12984 - Veykril:keep-going, r=Veykril
Use `--keep-going` cargo flag when building build scripts See #12973 (comment)
2 parents e70681f + 25d4fbe commit b1e9bcd

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

crates/project-model/src/build_scripts.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use cargo_metadata::{camino::Utf8Path, Message};
1212
use la_arena::ArenaMap;
1313
use paths::AbsPathBuf;
1414
use rustc_hash::FxHashMap;
15+
use semver::Version;
1516
use serde::Deserialize;
1617

1718
use crate::{cfg_flag::CfgFlag, CargoConfig, CargoWorkspace, Package};
@@ -77,9 +78,32 @@ impl WorkspaceBuildScripts {
7778
config: &CargoConfig,
7879
workspace: &CargoWorkspace,
7980
progress: &dyn Fn(String),
81+
toolchain: &Option<Version>,
8082
) -> io::Result<WorkspaceBuildScripts> {
81-
let mut cmd = Self::build_command(config);
83+
const RUST_1_62: Version = Version::new(1, 62, 0);
8284

85+
match Self::run_(Self::build_command(config), config, workspace, progress) {
86+
Ok(WorkspaceBuildScripts { error: Some(error), .. })
87+
if toolchain.as_ref().map_or(false, |it| *it >= RUST_1_62) =>
88+
{
89+
// building build scripts failed, attempt to build with --keep-going so
90+
// that we potentially get more build data
91+
let mut cmd = Self::build_command(config);
92+
cmd.args(&["-Z", "unstable-options", "--keep-going"]).env("RUSTC_BOOTSTRAP", "1");
93+
let mut res = Self::run_(cmd, config, workspace, progress)?;
94+
res.error = Some(error);
95+
Ok(res)
96+
}
97+
res => res,
98+
}
99+
}
100+
101+
fn run_(
102+
mut cmd: Command,
103+
config: &CargoConfig,
104+
workspace: &CargoWorkspace,
105+
progress: &dyn Fn(String),
106+
) -> io::Result<WorkspaceBuildScripts> {
83107
if config.wrap_rustc_in_build_scripts {
84108
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
85109
// that to compile only proc macros and build scripts during the initial

crates/project-model/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn load_cargo_with_overrides(file: &str, cfg_overrides: CfgOverrides) -> CrateGr
2828
rustc: None,
2929
rustc_cfg: Vec::new(),
3030
cfg_overrides,
31+
toolchain: None,
3132
};
3233
to_crate_graph(project_workspace)
3334
}

crates/project-model/src/workspace.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use base_db::{
1212
use cfg::{CfgDiff, CfgOptions};
1313
use paths::{AbsPath, AbsPathBuf};
1414
use rustc_hash::{FxHashMap, FxHashSet};
15+
use semver::Version;
1516
use stdx::always;
1617

1718
use crate::{
@@ -77,6 +78,7 @@ pub enum ProjectWorkspace {
7778
/// different target.
7879
rustc_cfg: Vec<CfgFlag>,
7980
cfg_overrides: CfgOverrides,
81+
toolchain: Option<Version>,
8082
},
8183
/// Project workspace was manually specified using a `rust-project.json` file.
8284
Json { project: ProjectJson, sysroot: Option<Sysroot>, rustc_cfg: Vec<CfgFlag> },
@@ -105,6 +107,7 @@ impl fmt::Debug for ProjectWorkspace {
105107
rustc,
106108
rustc_cfg,
107109
cfg_overrides,
110+
toolchain,
108111
} => f
109112
.debug_struct("Cargo")
110113
.field("root", &cargo.workspace_root().file_name())
@@ -116,6 +119,7 @@ impl fmt::Debug for ProjectWorkspace {
116119
)
117120
.field("n_rustc_cfg", &rustc_cfg.len())
118121
.field("n_cfg_overrides", &cfg_overrides.len())
122+
.field("toolchain", &toolchain)
119123
.finish(),
120124
ProjectWorkspace::Json { project, sysroot, rustc_cfg } => {
121125
let mut debug_struct = f.debug_struct("Json");
@@ -160,6 +164,9 @@ impl ProjectWorkspace {
160164
cmd.arg("--version");
161165
cmd
162166
})?;
167+
let toolchain = cargo_version
168+
.get("cargo ".len()..)
169+
.and_then(|it| Version::parse(it.split_whitespace().next()?).ok());
163170

164171
let meta = CargoWorkspace::fetch_metadata(
165172
&cargo_toml,
@@ -169,9 +176,9 @@ impl ProjectWorkspace {
169176
)
170177
.with_context(|| {
171178
format!(
172-
"Failed to read Cargo metadata from Cargo.toml file {}, {}",
179+
"Failed to read Cargo metadata from Cargo.toml file {}, {:?}",
173180
cargo_toml.display(),
174-
cargo_version
181+
toolchain
175182
)
176183
})?;
177184
let cargo = CargoWorkspace::new(meta);
@@ -219,6 +226,7 @@ impl ProjectWorkspace {
219226
rustc,
220227
rustc_cfg,
221228
cfg_overrides,
229+
toolchain,
222230
}
223231
}
224232
};
@@ -271,8 +279,8 @@ impl ProjectWorkspace {
271279
progress: &dyn Fn(String),
272280
) -> Result<WorkspaceBuildScripts> {
273281
match self {
274-
ProjectWorkspace::Cargo { cargo, .. } => {
275-
WorkspaceBuildScripts::run(config, cargo, progress).with_context(|| {
282+
ProjectWorkspace::Cargo { cargo, toolchain, .. } => {
283+
WorkspaceBuildScripts::run(config, cargo, progress, toolchain).with_context(|| {
276284
format!("Failed to run build scripts for {}", &cargo.workspace_root().display())
277285
})
278286
}
@@ -320,6 +328,7 @@ impl ProjectWorkspace {
320328
rustc_cfg: _,
321329
cfg_overrides: _,
322330
build_scripts,
331+
toolchain: _,
323332
} => {
324333
cargo
325334
.packages()
@@ -425,6 +434,7 @@ impl ProjectWorkspace {
425434
rustc_cfg,
426435
cfg_overrides,
427436
build_scripts,
437+
toolchain: _,
428438
} => cargo_to_crate_graph(
429439
rustc_cfg.clone(),
430440
cfg_overrides,

crates/rust-analyzer/src/reload.rs

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ impl GlobalState {
219219
cfg_overrides,
220220

221221
build_scripts: _,
222+
toolchain: _,
222223
} => Some((cargo, sysroot, rustc, rustc_cfg, cfg_overrides)),
223224
_ => None,
224225
};

0 commit comments

Comments
 (0)