Skip to content
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

Require disabled gas in executable target compilation #1929

Closed
wants to merge 1 commit into from
Closed
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
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());
}
Loading