From e319f39a201416db33c02505c8c441da1a527117 Mon Sep 17 00:00:00 2001 From: maciektr Date: Fri, 2 Feb 2024 15:21:37 +0100 Subject: [PATCH] Allow compilation of packages with `no-core` flag enabled. commit-id:5a5989db --- scarb/src/compiler/compilation_unit.rs | 12 ++++++++---- scarb/src/compiler/db.rs | 6 +++--- scarb/tests/build.rs | 23 ++++++++++++++++++++++- utils/scarb-test-support/src/command.rs | 1 + 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/scarb/src/compiler/compilation_unit.rs b/scarb/src/compiler/compilation_unit.rs index 946643f4a..68114c548 100644 --- a/scarb/src/compiler/compilation_unit.rs +++ b/scarb/src/compiler/compilation_unit.rs @@ -66,11 +66,15 @@ impl CompilationUnit { component } - pub fn core_package_component(&self) -> &CompilationUnitComponent { + pub fn core_package_component(&self) -> Option<&CompilationUnitComponent> { // NOTE: This uses the order invariant of `component` field. - let component = &self.components[1]; - assert!(component.package.id.is_core()); - component + if self.components.len() < 2 { + None + } else { + let component = &self.components[1]; + assert!(component.package.id.is_core()); + Some(component) + } } pub fn target(&self) -> &Target { diff --git a/scarb/src/compiler/db.rs b/scarb/src/compiler/db.rs index ac00bf3ee..5c3be339c 100644 --- a/scarb/src/compiler/db.rs +++ b/scarb/src/compiler/db.rs @@ -114,9 +114,9 @@ fn build_project_config(unit: &CompilationUnit) -> Result { ..Default::default() }; - let corelib = Some(Directory::Real( - unit.core_package_component().target.source_root().into(), - )); + let corelib = unit + .core_package_component() + .map(|core| Directory::Real(core.target.source_root().into())); let content = ProjectConfigContent { crate_roots, diff --git a/scarb/tests/build.rs b/scarb/tests/build.rs index f1b3b00c6..195af3e67 100644 --- a/scarb/tests/build.rs +++ b/scarb/tests/build.rs @@ -6,7 +6,8 @@ use indoc::indoc; use predicates::prelude::*; use scarb_build_metadata::CAIRO_VERSION; -use scarb_test_support::command::Scarb; +use scarb_metadata::Metadata; +use scarb_test_support::command::{CommandExt, Scarb}; use scarb_test_support::fsx::ChildPathEx; use scarb_test_support::project_builder::{Dep, DepBuilder, ProjectBuilder}; use scarb_test_support::workspace_builder::WorkspaceBuilder; @@ -819,3 +820,23 @@ fn warnings_can_be_disallowed() { error: could not compile [..] due to previous error "#}); } + +#[test] +#[ignore = "TODO(maciektr): Stop compiler from overwriting core crate settings."] +fn can_compile_no_core_package() { + let t = TempDir::new().unwrap(); + // Find path to corelib. + ProjectBuilder::start().name("hello").build(&t); + let metadata = Scarb::quick_snapbox() + .args(["--json", "metadata", "--format-version", "1"]) + .current_dir(&t) + .stdout_json::(); + let core = metadata.packages.iter().find(|p| p.name == "core").unwrap(); + let core = core.root.clone(); + // Compile corelib. + Scarb::quick_snapbox() + .arg("build") + .current_dir(core) + .assert() + .success(); +} diff --git a/utils/scarb-test-support/src/command.rs b/utils/scarb-test-support/src/command.rs index 349125172..437f2331c 100644 --- a/utils/scarb-test-support/src/command.rs +++ b/utils/scarb-test-support/src/command.rs @@ -136,6 +136,7 @@ impl CommandExt for SnapboxCommand { Err(_) => continue, } } + // help: make sure that the command outputs NDJSON (`--json` flag). panic!("Failed to deserialize stdout to JSON"); } }