From 41b8f873c0e1655007b65e92410fa2a12a0def2c Mon Sep 17 00:00:00 2001 From: Piotr Magiera <56825108+piotmag769@users.noreply.github.com> Date: Tue, 27 Aug 2024 17:04:36 +0200 Subject: [PATCH] LS: fix metadata hack (#6296) --- .../src/project/scarb.rs | 33 +++++-------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/crates/cairo-lang-language-server/src/project/scarb.rs b/crates/cairo-lang-language-server/src/project/scarb.rs index 425c954559d..2575b0af846 100644 --- a/crates/cairo-lang-language-server/src/project/scarb.rs +++ b/crates/cairo-lang-language-server/src/project/scarb.rs @@ -5,8 +5,7 @@ use std::path::Path; use anyhow::{bail, ensure, Context, Result}; use cairo_lang_filesystem::db::{CrateSettings, Edition, ExperimentalFeaturesConfig}; use itertools::Itertools; -use scarb_metadata::{CompilationUnitMetadata, Metadata, PackageMetadata}; -use serde_json::Value; +use scarb_metadata::{Metadata, PackageMetadata}; use tracing::{debug, error, warn}; use crate::lang::db::AnalysisDatabase; @@ -41,26 +40,16 @@ pub fn update_crate_roots(metadata: &Metadata, db: &mut AnalysisDatabase) { let mut package = metadata.packages.iter().find(|package| package.id == component.package); - // TODO(pmagiera): this is a hack, remove this when `scarb metadata` is fixed. - if package.is_none() - && is_integration_test_cu(compilation_unit) - && compilation_unit.package == component.package - { - let human_readable_member_name = component - .package - .repr - .split("_integrationtest") - .collect::>() - .first() - // NOTE: this `unwrap` cannot fail since `split` always returns a non-empty - // vector. - .unwrap() - .to_string(); - + // For some compilation units corresponding to integration tests, + // a main package of the compilation unit may not be found in a list of packages + // (metadata hack, intended by scarb). + // We instead find a package that specifies the target of the compilation unit + // and later use it to extract edition and experimental features. + if package.is_none() && compilation_unit.package == component.package { package = metadata .packages .iter() - .find(|package| package.id.repr.starts_with(&human_readable_member_name)); + .find(|p| p.targets.iter().any(|t| t.name == compilation_unit.target.name)); } if package.is_none() { @@ -268,9 +257,3 @@ fn scarb_package_experimental_features( coupons: contains("coupons"), } } - -fn is_integration_test_cu(compilation_unit: &CompilationUnitMetadata) -> bool { - compilation_unit.target.kind == "test" - && compilation_unit.target.params.get("test-type").and_then(Value::as_str) - == Some("integration") -}