leo_lang/cli/commands/
run.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use super::*;
18
19use snarkvm::cli::Run as SnarkVMRun;
20
21/// Build, Prove and Run Leo program with inputs
22#[derive(Parser, Debug)]
23pub struct LeoRun {
24    #[clap(name = "NAME", help = "The name of the program to run.", default_value = "main")]
25    pub(crate) name: String,
26    #[clap(name = "INPUTS", help = "The inputs to the program.")]
27    pub(crate) inputs: Vec<String>,
28    #[clap(flatten)]
29    pub(crate) compiler_options: BuildOptions,
30    #[clap(flatten)]
31    pub(crate) env_override: EnvOptions,
32}
33
34impl Command for LeoRun {
35    type Input = <LeoBuild as Command>::Output;
36    type Output = ();
37
38    fn log_span(&self) -> Span {
39        tracing::span!(tracing::Level::INFO, "Leo")
40    }
41
42    fn prelude(&self, context: Context) -> Result<Self::Input> {
43        (LeoBuild { options: self.compiler_options.clone(), env_override: self.env_override.clone() }).execute(context)
44    }
45
46    fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
47        handle_run(self, context)
48    }
49}
50
51// A helper function to handle the run command.
52fn handle_run(mut command: LeoRun, context: Context) -> Result<<LeoRun as Command>::Output> {
53    // Compose the `run` command.
54    // The argument "--" is the separator, used to make sure snarkvm doesn't try to parse negative
55    // values as CLI flags.
56    let mut arguments = vec![SNARKVM_COMMAND.to_string(), command.name, "--".to_string()];
57    arguments.append(&mut command.inputs);
58
59    // Open the Leo build/ directory
60    let path = context.dir()?;
61    let build_directory = path.join(leo_package::BUILD_DIRECTORY);
62
63    // Change the cwd to the Leo build/ directory to compile aleo files.
64    std::env::set_current_dir(&build_directory)
65        .map_err(|err| PackageError::failed_to_set_cwd(build_directory.display(), err))?;
66
67    // Unset the Leo panic hook
68    let _ = std::panic::take_hook();
69
70    // Call the `run` command.
71    println!();
72    let command = SnarkVMRun::try_parse_from(&arguments).map_err(CliError::failed_to_parse_run)?;
73    let res = command.parse().map_err(CliError::failed_to_execute_run)?;
74
75    // Log the output of the `run` command.
76    tracing::info!("{}", res);
77
78    Ok(())
79}