Skip to content

Commit

Permalink
Require disabled gas in executable target compilation
Browse files Browse the repository at this point in the history
commit-id:671bf5e1
  • Loading branch information
maciektr committed Jan 29, 2025
1 parent ef4fb73 commit 55a1c95
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
6 changes: 6 additions & 0 deletions extensions/scarb-execute/tests/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ fn can_take_big_number_as_arg() {
.version("0.1.0")
.manifest_extra(indoc! {r#"
[executable]
[cairo]
enable-gas = false
"#})
.dep_cairo_execute()
.lib_cairo(indoc! {r#"
Expand Down Expand Up @@ -51,6 +54,9 @@ fn can_read_arguments_from_file() {
.version("0.1.0")
.manifest_extra(indoc! {r#"
[executable]
[cairo]
enable-gas = false
"#})
.dep_cairo_execute()
.lib_cairo(indoc! {r#"
Expand Down
14 changes: 10 additions & 4 deletions extensions/scarb-execute/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ fn executable_project_builder() -> ProjectBuilder {
.version("0.1.0")
.dep_cairo_execute()
.manifest_extra(indoc! {r#"
[executable]
"#})
[executable]
[cairo]
enable-gas = false
"#})
.lib_cairo(indoc! {r#"
#[executable]
fn main() -> felt252 {
Expand Down Expand Up @@ -136,8 +139,11 @@ fn fails_when_attr_missing() {
.version("0.1.0")
.dep_cairo_execute()
.manifest_extra(indoc! {r#"
[executable]
"#})
[executable]
[cairo]
enable-gas = false
"#})
.lib_cairo(indoc! {r#"
fn main() -> felt252 {
42
Expand Down
17 changes: 15 additions & 2 deletions scarb/src/compiler/compilers/executable.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::compiler::helpers::write_json;
use crate::compiler::helpers::{build_compiler_config, collect_main_crate_ids};
use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
use crate::core::{TargetKind, Workspace};
use anyhow::Result;
use crate::core::{TargetKind, Utf8PathWorkspaceExt, Workspace};
use anyhow::{ensure, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_executable::executable::Executable;
use indoc::formatdoc;
use tracing::trace_span;

pub struct ExecutableCompiler;
Expand All @@ -20,6 +21,18 @@ impl Compiler for ExecutableCompiler {
db: &mut RootDatabase,
ws: &Workspace<'_>,
) -> Result<()> {
ensure!(
!unit.compiler_config.enable_gas,
formatdoc! {r#"
executable target cannot be compiled with enabled gas calculation
help: if you want to diable gas calculation, consider adding following
excerpt to your package manifest
-> {scarb_toml}
[cairo]
enable-gas = false
"#, scarb_toml=unit.main_component().package.manifest_path().workspace_relative(ws)}
);

let target_dir = unit.target_dir(ws);
let main_crate_ids = collect_main_crate_ids(&unit, db);
let compiler_config = build_compiler_config(db, &unit, &main_crate_ids, ws);
Expand Down
43 changes: 43 additions & 0 deletions scarb/tests/build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,9 @@ fn test_executable_compiler_creates_output_files() {
.dep_cairo_execute()
.manifest_extra(indoc! {r#"
[[target.executable]]
[cairo]
enable-gas = false
"#})
.lib_cairo(indoc! {r#"
#[executable]
Expand Down Expand Up @@ -1130,6 +1133,9 @@ fn compile_executable_target_can_use_short_declaration() {
.dep_cairo_execute()
.manifest_extra(indoc! {r#"
[executable]
[cairo]
enable-gas = false
"#})
.lib_cairo(indoc! {r#"
#[executable]
Expand All @@ -1148,3 +1154,40 @@ fn compile_executable_target_can_use_short_declaration() {
t.child("target/dev/executable_test.executable.json")
.assert(predicates::path::exists());
}

#[test]
fn executable_target_requires_disabled_gas() {
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("executable_test")
.dep_cairo_execute()
.manifest_extra(indoc! {r#"
[executable]
"#})
.lib_cairo(indoc! {r#"
#[executable]
fn main() -> felt252 {
42
}
"#})
.build(&t);

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.failure()
.stdout_matches(indoc! {r#"
[..]Compiling executable_test v1.0.0 ([..]Scarb.toml)
error: executable target cannot be compiled with enabled gas calculation
help: if you want to diable gas calculation, consider adding following
excerpt to your package manifest
-> Scarb.toml
[cairo]
enable-gas = false
error: could not compile `executable_test` due to previous error
"#});

t.child("target/dev/executable_test.executable.json")
.assert(predicates::path::exists().not());
}

0 comments on commit 55a1c95

Please sign in to comment.