Skip to content

Rust: support glob members in workspaces #18802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions rust/extractor/src/rust_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,6 +136,8 @@ impl<'a> RustAnalyzer<'a> {
struct CargoManifestMembersSlice {
#[serde(default)]
members: Vec<String>,
#[serde(default)]
exclude: Vec<String>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
)
})
}) =>
{
Expand Down
4 changes: 4 additions & 0 deletions rust/ql/integration-tests/workspace-with-glob/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [ "*" ]
exclude = [ "other" ]
resolver = "2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "exe"
version = "0.1.0"
edition = "2021"

[dependencies]
lib = { path = "../lib" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use lib::hello;

fn main() {
hello();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "lib"
version = "0.1.0"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello() {
println!("Hello, world!");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "other"
version = "0.1.0"
edition = "2021"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exe/src/main.rs
lib/src/lib.rs
other/src/lib.rs
10 changes: 10 additions & 0 deletions rust/ql/integration-tests/workspace-with-glob/steps.expected
Original file line number Diff line number Diff line change
@@ -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) |
4 changes: 4 additions & 0 deletions rust/ql/integration-tests/workspace-with-glob/steps.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import codeql.rust.elements.internal.ExtractorStep

from ExtractorStep step
select step
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pytest


def test_cargo(codeql, rust, check_source_archive):
codeql.database.create()
Loading