Skip to content

Commit

Permalink
extract common behavior for building FileManager and ParsedFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
anaPerezGhiglia committed Aug 7, 2024
1 parent 92e551d commit 8da3eef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
22 changes: 9 additions & 13 deletions tooling/nargo_cli/src/cli/dap_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use dap::types::Capabilities;
use serde_json::Value;

use super::debug_cmd::{compile_bin_package_for_debugging, compile_options_for_debugging};
use super::execution_helpers::prepare_package_for_debug;
use super::execution_helpers::{file_manager_and_files_from, prepare_package_for_debug};
use super::fs::inputs::read_inputs_from_file;
use super::test_cmd::{compile_no_check_for_debug, get_tests_in_package};
use crate::errors::CliError;
Expand Down Expand Up @@ -124,15 +124,13 @@ fn load_and_compile_project(
let compile_options =
compile_options_for_debugging(acir_mode, skip_instrumentation, CompileOptions::default());

let compiled_program = if test_name.is_some() {
let test_name = test_name.unwrap();
load_and_compile_test_function(test_name, workspace, &package, &compile_options)
} else {
let compiled = compile_bin_package_for_debugging(&workspace, &package, &compile_options)
.map_err(|_| LoadError::Generic("Failed to compile project".into()))?;
Ok(compiled)
}?;

let compiled_program = match test_name {
Some(test_name) => load_and_compile_test_function(test_name, workspace, &package, &compile_options)?,
None => {
compile_bin_package_for_debugging(&workspace, &package, &compile_options)
.map_err(|_| LoadError::Generic("Failed to compile project".into()))?
},
};
let compiled_program = nargo::ops::transform_program(compiled_program, expression_width);

let (inputs_map, _) =
Expand All @@ -154,9 +152,7 @@ fn load_and_compile_test_function(
package: &Package,
compile_options: &CompileOptions,
) -> Result<CompiledProgram, LoadError> {
let mut workspace_file_manager = file_manager_with_stdlib(&workspace.root_dir);
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
let mut parsed_files = parse_all(&workspace_file_manager);
let (workspace_file_manager, mut parsed_files) = file_manager_and_files_from(&workspace.root_dir, &workspace);

let test_functions = get_tests_in_package(
&workspace_file_manager,
Expand Down
6 changes: 2 additions & 4 deletions tooling/nargo_cli/src/cli/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use noirc_driver::{
use noirc_frontend::graph::CrateName;

use super::compile_cmd::get_target_width;
use super::execution_helpers::instrument_package_files;
use super::execution_helpers::{file_manager_and_files_from, instrument_package_files};
use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir};
use super::NargoConfig;
use crate::errors::CliError;
Expand Down Expand Up @@ -101,9 +101,7 @@ pub(crate) fn compile_bin_package_for_debugging(
package: &Package,
compile_options: &CompileOptions,
) -> Result<CompiledProgram, CompileError> {
let mut workspace_file_manager = file_manager_with_stdlib(std::path::Path::new(""));
insert_all_files_for_workspace_into_file_manager(workspace, &mut workspace_file_manager);
let mut parsed_files = parse_all(&workspace_file_manager);
let (workspace_file_manager, mut parsed_files) = file_manager_and_files_from(std::path::Path::new(""), workspace);

let compilation_result = if compile_options.instrument_debug {
let debug_state =
Expand Down
20 changes: 16 additions & 4 deletions tooling/nargo_cli/src/cli/execution_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use fm::FileManager;
use nargo::package::Package;
use nargo::prepare_package;
use noirc_driver::link_to_debug_crate;
use fm::{FileId, FileManager};
use nargo::{package::Package, workspace::Workspace};
use nargo::{insert_all_files_for_workspace_into_file_manager, parse_all, prepare_package};
use noirc_driver::{file_manager_with_stdlib, link_to_debug_crate};
use noirc_frontend::{
debug::DebugInstrumenter,
graph::CrateId,
hir::{Context, ParsedFiles},
};

use std::collections::HashMap;
use std::path::Path;

pub(crate) fn prepare_package_for_debug<'a>(
file_manager: &'a FileManager,
parsed_files: &'a mut ParsedFiles,
Expand Down Expand Up @@ -51,3 +54,12 @@ pub(crate) fn instrument_package_files(

debug_instrumenter
}

// TODO: should we create a type that englobe fileManager + parsed_files?
// all functions that need file_manager needs parsed_files as well
pub(crate) fn file_manager_and_files_from(root: &Path, workspace: &Workspace) -> (FileManager, ParsedFiles) {
let mut workspace_file_manager = file_manager_with_stdlib(root);
insert_all_files_for_workspace_into_file_manager(workspace, &mut workspace_file_manager);
let parsed_files = parse_all(&workspace_file_manager);
(workspace_file_manager, parsed_files)
}
6 changes: 2 additions & 4 deletions tooling/nargo_cli/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

use crate::{cli::check_cmd::check_crate_and_report_errors, errors::CliError};

use super::{execution_helpers::prepare_package_for_debug, NargoConfig};
use super::{execution_helpers::{file_manager_and_files_from, prepare_package_for_debug}, NargoConfig};

/// Run the tests for this program
#[derive(Debug, Clone, Args)]
Expand Down Expand Up @@ -72,9 +72,7 @@ pub(crate) fn run(args: TestCommand, config: NargoConfig) -> Result<(), CliError
)?;
let debug_mode = args.debug;

let mut workspace_file_manager = file_manager_with_stdlib(&workspace.root_dir);
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
let parsed_files = parse_all(&workspace_file_manager);
let (workspace_file_manager, parsed_files) = file_manager_and_files_from(&workspace.root_dir, &workspace);

let pattern = match &args.test_name {
Some(name) => {
Expand Down

0 comments on commit 8da3eef

Please sign in to comment.