Skip to content

Commit 449d672

Browse files
authored
Merge pull request #22814 from github/rust/ignore-toolchain-version
Rust: Pass `--ignore-rust-version` to Cargo
2 parents b6a9c9a + 4cf3aa6 commit 449d672

9 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎rust/extractor/src/config.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ impl Config {
189189

190190
pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) {
191191
let sysroot = self.sysroot(dir);
192+
let mut extra_args = self.cargo_extra_args.clone();
193+
// The fixed toolchain may be older than the project's declared minimum.
194+
// If that's the case `cargo` will error. Passing the flag below ensures
195+
// that the project's speified Rust version is ignored. With this
196+
// failures will only occur if the project actually uses Rust features
197+
// that rust-analyzer and/or the fixed toolchain is incompatible with
198+
// (which is the best we can hope for).
199+
if !extra_args.iter().any(|arg| arg == "--ignore-rust-version") {
200+
extra_args.push("--ignore-rust-version".to_owned());
201+
}
192202
(
193203
CargoConfig {
194204
all_targets: self.cargo_all_targets,
@@ -205,7 +215,7 @@ impl Config {
205215
.map(RustLibSource::Path),
206216

207217
extra_env: self.get_extra_env(),
208-
extra_args: self.cargo_extra_args.clone(),
218+
extra_args,
209219
extra_includes: self
210220
.extra_includes
211221
.iter()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "rust-version"
3+
version = "0.1.0"
4+
edition = "2024"
5+
rust-version = "1.999.0"
6+
7+
[dependencies]
8+
macros = { path = "macros" }
9+
10+
[workspace]
11+
members = ["macros"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let out_dir = std::env::var("OUT_DIR").unwrap();
3+
std::fs::write(
4+
std::path::Path::new(&out_dir).join("generated.rs"),
5+
"pub fn from_build_script() -> u32 { 42 }\n",
6+
)
7+
.unwrap();
8+
println!("cargo:rerun-if-changed=build.rs");
9+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| from_build_script |
2+
| from_proc_macro |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import rust
2+
3+
from Function f
4+
where f.fromSource() and f.getName().getText() = ["from_build_script", "from_proc_macro"]
5+
select f.getName().getText()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "macros"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
proc-macro = true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern crate proc_macro;
2+
3+
#[proc_macro]
4+
pub fn generate(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
5+
"pub fn from_proc_macro() -> u32 { 43 }".parse().unwrap()
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
2+
3+
macros::generate!();
4+
5+
fn main() {
6+
println!("{} {}", from_build_script(), from_proc_macro());
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
@pytest.mark.parametrize("rust_edition", [2024])
5+
@pytest.mark.parametrize("extra_args", [None, "--offline", "--ignore-rust-version"])
6+
def test_rust_version(codeql, rust, cargo, extra_args):
7+
env = {}
8+
if extra_args is not None:
9+
env["CODEQL_EXTRACTOR_RUST_OPTION_CARGO_EXTRA_ARGS"] = extra_args
10+
codeql.database.create(_env=env)

0 commit comments

Comments
 (0)