Skip to content

Commit

Permalink
Allow compilation of packages with no-core flag enabled.
Browse files Browse the repository at this point in the history
commit-id:5a5989db
  • Loading branch information
maciektr committed Feb 5, 2024
1 parent 46d5d5c commit e319f39
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
12 changes: 8 additions & 4 deletions scarb/src/compiler/compilation_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions scarb/src/compiler/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ fn build_project_config(unit: &CompilationUnit) -> Result<ProjectConfig> {
..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,
Expand Down
23 changes: 22 additions & 1 deletion scarb/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Metadata>();
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();
}
1 change: 1 addition & 0 deletions utils/scarb-test-support/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit e319f39

Please sign in to comment.