Merge pull request #18802 from github/redsun82/rust-glob-members

Rust: support glob members in workspaces
This commit is contained in:
Paolo Tranquilli
2025-02-19 13:30:58 +01:00
committed by GitHub
12 changed files with 68 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ use ra_ap_base_db::SourceDatabase;
use ra_ap_hir::Semantics;
use ra_ap_ide_db::RootDatabase;
use ra_ap_load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
use ra_ap_paths::Utf8PathBuf;
use ra_ap_paths::{AbsPath, Utf8PathBuf};
use ra_ap_project_model::ProjectManifest;
use ra_ap_project_model::{CargoConfig, ManifestPath};
use ra_ap_span::Edition;
@@ -136,6 +136,8 @@ impl<'a> RustAnalyzer<'a> {
struct CargoManifestMembersSlice {
#[serde(default)]
members: Vec<String>,
#[serde(default)]
exclude: Vec<String>,
}
#[derive(Deserialize)]
@@ -171,6 +173,12 @@ impl TomlReader {
}
}
fn workspace_members_match(workspace_dir: &AbsPath, members: &[String], target: &AbsPath) -> bool {
members.iter().any(|p| {
glob::Pattern::new(workspace_dir.join(p).as_str()).is_ok_and(|p| p.matches(target.as_str()))
})
}
fn find_workspace(reader: &mut TomlReader, manifest: &ProjectManifest) -> Option<ProjectManifest> {
let ProjectManifest::CargoToml(cargo) = manifest else {
return None;
@@ -200,9 +208,12 @@ fn find_workspace(reader: &mut TomlReader, manifest: &ProjectManifest) -> Option
if cargo.starts_with(other.parent())
&& reader.read(other).is_ok_and(|it| {
it.workspace.as_ref().is_some_and(|w| {
w.members
.iter()
.any(|m| other.parent().join(m) == cargo.parent())
workspace_members_match(other.parent(), &w.members, cargo.parent())
&& !workspace_members_match(
other.parent(),
&w.exclude,
cargo.parent(),
)
})
}) =>
{

View File

@@ -0,0 +1,4 @@
[workspace]
members = [ "*" ]
exclude = [ "other" ]
resolver = "2"

View File

@@ -0,0 +1,7 @@
[package]
name = "exe"
version = "0.1.0"
edition = "2021"
[dependencies]
lib = { path = "../lib" }

View File

@@ -0,0 +1,5 @@
use lib::hello;
fn main() {
hello();
}

View File

@@ -0,0 +1,6 @@
[package]
name = "lib"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@@ -0,0 +1,3 @@
pub fn hello() {
println!("Hello, world!");
}

View File

@@ -0,0 +1,6 @@
[package]
name = "other"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@@ -0,0 +1,3 @@
exe/src/main.rs
lib/src/lib.rs
other/src/lib.rs

View File

@@ -0,0 +1,10 @@
| Cargo.toml:0:0:0:0 | LoadManifest(Cargo.toml) |
| exe/src/main.rs:0:0:0:0 | Extract(exe/src/main.rs) |
| exe/src/main.rs:0:0:0:0 | Parse(exe/src/main.rs) |
| file://:0:0:0:0 | FindManifests |
| lib/src/lib.rs:0:0:0:0 | Extract(lib/src/lib.rs) |
| lib/src/lib.rs:0:0:0:0 | Parse(lib/src/lib.rs) |
| other/Cargo.toml:0:0:0:0 | LoadManifest(other/Cargo.toml) |
| other/src/lib.rs:0:0:0:0 | Extract(other/src/lib.rs) |
| other/src/lib.rs:0:0:0:0 | LoadSource(other/src/lib.rs) |
| other/src/lib.rs:0:0:0:0 | Parse(other/src/lib.rs) |

View File

@@ -0,0 +1,4 @@
import codeql.rust.elements.internal.ExtractorStep
from ExtractorStep step
select step

View File

@@ -0,0 +1,5 @@
import pytest
def test_cargo(codeql, rust, check_source_archive):
codeql.database.create()