From 4cf3aa6cee3c3ba79a88abde7213644658681c6f Mon Sep 17 00:00:00 2001 From: Simon Friis Vindum Date: Tue, 13 Oct 2026 14:22:31 +0200 Subject: [PATCH] Rust: Pass --ignore-rust-version to Cargo --- rust/extractor/src/config.rs | 12 +++++++++++- rust/ql/integration-tests/rust-version/Cargo.toml | 11 +++++++++++ rust/ql/integration-tests/rust-version/build.rs | 9 +++++++++ .../rust-version/functions.expected | 2 ++ rust/ql/integration-tests/rust-version/functions.ql | 5 +++++ .../integration-tests/rust-version/macros/Cargo.toml | 7 +++++++ .../integration-tests/rust-version/macros/src/lib.rs | 6 ++++++ rust/ql/integration-tests/rust-version/src/main.rs | 7 +++++++ .../rust-version/test_rust_version.py | 10 ++++++++++ 9 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 rust/ql/integration-tests/rust-version/Cargo.toml create mode 100644 rust/ql/integration-tests/rust-version/build.rs create mode 100644 rust/ql/integration-tests/rust-version/functions.expected create mode 100644 rust/ql/integration-tests/rust-version/functions.ql create mode 100644 rust/ql/integration-tests/rust-version/macros/Cargo.toml create mode 100644 rust/ql/integration-tests/rust-version/macros/src/lib.rs create mode 100644 rust/ql/integration-tests/rust-version/src/main.rs create mode 100644 rust/ql/integration-tests/rust-version/test_rust_version.py diff --git a/rust/extractor/src/config.rs b/rust/extractor/src/config.rs index 71d57049b7ed..81e42c552c82 100644 --- a/rust/extractor/src/config.rs +++ b/rust/extractor/src/config.rs @@ -189,6 +189,16 @@ impl Config { pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) { let sysroot = self.sysroot(dir); + let mut extra_args = self.cargo_extra_args.clone(); + // The fixed toolchain may be older than the project's declared minimum. + // If that's the case `cargo` will error. Passing the flag below ensures + // that the project's speified Rust version is ignored. With this + // failures will only occur if the project actually uses Rust features + // that rust-analyzer and/or the fixed toolchain is incompatible with + // (which is the best we can hope for). + if !extra_args.iter().any(|arg| arg == "--ignore-rust-version") { + extra_args.push("--ignore-rust-version".to_owned()); + } ( CargoConfig { all_targets: self.cargo_all_targets, @@ -205,7 +215,7 @@ impl Config { .map(RustLibSource::Path), extra_env: self.get_extra_env(), - extra_args: self.cargo_extra_args.clone(), + extra_args, extra_includes: self .extra_includes .iter() diff --git a/rust/ql/integration-tests/rust-version/Cargo.toml b/rust/ql/integration-tests/rust-version/Cargo.toml new file mode 100644 index 000000000000..a723b83f36e8 --- /dev/null +++ b/rust/ql/integration-tests/rust-version/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rust-version" +version = "0.1.0" +edition = "2024" +rust-version = "1.999.0" + +[dependencies] +macros = { path = "macros" } + +[workspace] +members = ["macros"] diff --git a/rust/ql/integration-tests/rust-version/build.rs b/rust/ql/integration-tests/rust-version/build.rs new file mode 100644 index 000000000000..524bc147a5e7 --- /dev/null +++ b/rust/ql/integration-tests/rust-version/build.rs @@ -0,0 +1,9 @@ +fn main() { + let out_dir = std::env::var("OUT_DIR").unwrap(); + std::fs::write( + std::path::Path::new(&out_dir).join("generated.rs"), + "pub fn from_build_script() -> u32 { 42 }\n", + ) + .unwrap(); + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/rust/ql/integration-tests/rust-version/functions.expected b/rust/ql/integration-tests/rust-version/functions.expected new file mode 100644 index 000000000000..120f73681b6f --- /dev/null +++ b/rust/ql/integration-tests/rust-version/functions.expected @@ -0,0 +1,2 @@ +| from_build_script | +| from_proc_macro | diff --git a/rust/ql/integration-tests/rust-version/functions.ql b/rust/ql/integration-tests/rust-version/functions.ql new file mode 100644 index 000000000000..cf3a29569257 --- /dev/null +++ b/rust/ql/integration-tests/rust-version/functions.ql @@ -0,0 +1,5 @@ +import rust + +from Function f +where f.fromSource() and f.getName().getText() = ["from_build_script", "from_proc_macro"] +select f.getName().getText() diff --git a/rust/ql/integration-tests/rust-version/macros/Cargo.toml b/rust/ql/integration-tests/rust-version/macros/Cargo.toml new file mode 100644 index 000000000000..a49335042bcd --- /dev/null +++ b/rust/ql/integration-tests/rust-version/macros/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "macros" +version = "0.1.0" +edition = "2024" + +[lib] +proc-macro = true diff --git a/rust/ql/integration-tests/rust-version/macros/src/lib.rs b/rust/ql/integration-tests/rust-version/macros/src/lib.rs new file mode 100644 index 000000000000..75f199d1b10c --- /dev/null +++ b/rust/ql/integration-tests/rust-version/macros/src/lib.rs @@ -0,0 +1,6 @@ +extern crate proc_macro; + +#[proc_macro] +pub fn generate(_: proc_macro::TokenStream) -> proc_macro::TokenStream { + "pub fn from_proc_macro() -> u32 { 43 }".parse().unwrap() +} diff --git a/rust/ql/integration-tests/rust-version/src/main.rs b/rust/ql/integration-tests/rust-version/src/main.rs new file mode 100644 index 000000000000..a51c9de834aa --- /dev/null +++ b/rust/ql/integration-tests/rust-version/src/main.rs @@ -0,0 +1,7 @@ +include!(concat!(env!("OUT_DIR"), "/generated.rs")); + +macros::generate!(); + +fn main() { + println!("{} {}", from_build_script(), from_proc_macro()); +} diff --git a/rust/ql/integration-tests/rust-version/test_rust_version.py b/rust/ql/integration-tests/rust-version/test_rust_version.py new file mode 100644 index 000000000000..d334187ca5a6 --- /dev/null +++ b/rust/ql/integration-tests/rust-version/test_rust_version.py @@ -0,0 +1,10 @@ +import pytest + + +@pytest.mark.parametrize("rust_edition", [2024]) +@pytest.mark.parametrize("extra_args", [None, "--offline", "--ignore-rust-version"]) +def test_rust_version(codeql, rust, cargo, extra_args): + env = {} + if extra_args is not None: + env["CODEQL_EXTRACTOR_RUST_OPTION_CARGO_EXTRA_ARGS"] = extra_args + codeql.database.create(_env=env)