diff --git a/Cargo.lock b/Cargo.lock index a1fbd30f0ed..d8b3e4edf84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3177,6 +3177,7 @@ version = "0.7.0" dependencies = [ "async-trait", "dashmap 4.0.2", + "forc-util", "futures", "ropey", "serde_json", diff --git a/forc-util/src/lib.rs b/forc-util/src/lib.rs index cbd5b424586..e65dd71bdc0 100644 --- a/forc-util/src/lib.rs +++ b/forc-util/src/lib.rs @@ -17,6 +17,32 @@ pub mod restricted; pub const DEFAULT_OUTPUT_DIRECTORY: &str = "out"; +/// Continually go up in the file tree until a specified file is found. +#[allow(clippy::branches_sharing_code)] +pub fn find_parent_dir_with_file(starter_path: &Path, file_name: &str) -> Option { + let mut path = std::fs::canonicalize(starter_path).ok()?; + let empty_path = PathBuf::from("/"); + while path != empty_path { + path.push(file_name); + if path.exists() { + path.pop(); + return Some(path); + } else { + path.pop(); + path.pop(); + } + } + None +} +/// Continually go up in the file tree until a Forc manifest file is found. +pub fn find_manifest_dir(starter_path: &Path) -> Option { + find_parent_dir_with_file(starter_path, constants::MANIFEST_FILE_NAME) +} +/// Continually go up in the file tree until a Cargo manifest file is found. +pub fn find_cargo_manifest_dir(starter_path: &Path) -> Option { + find_parent_dir_with_file(starter_path, "Cargo.toml") +} + pub fn is_sway_file(file: &Path) -> bool { let res = file.extension(); Some(OsStr::new(constants::SWAY_EXTENSION)) == res diff --git a/forc/src/ops/forc_build.rs b/forc/src/ops/forc_build.rs index 98e60090e2a..9b8915f99b2 100644 --- a/forc/src/ops/forc_build.rs +++ b/forc/src/ops/forc_build.rs @@ -1,12 +1,12 @@ use crate::cli::BuildCommand; use anyhow::{anyhow, bail, Result}; use forc_pkg::{self as pkg, lock, Lock, Manifest}; -use forc_util::{default_output_directory, lock_path}; +use forc_util::{default_output_directory, find_manifest_dir, lock_path}; use std::{ fs::{self, File}, path::PathBuf, }; -use sway_utils::{find_manifest_dir, MANIFEST_FILE_NAME}; +use sway_utils::MANIFEST_FILE_NAME; pub fn build(command: BuildCommand) -> Result { let BuildCommand { diff --git a/forc/src/ops/forc_clean.rs b/forc/src/ops/forc_clean.rs index 971c7db9293..17f51cb57a8 100644 --- a/forc/src/ops/forc_clean.rs +++ b/forc/src/ops/forc_clean.rs @@ -1,8 +1,8 @@ use crate::cli::CleanCommand; use anyhow::{anyhow, bail, Result}; -use forc_util::default_output_directory; +use forc_util::{default_output_directory, find_cargo_manifest_dir, find_manifest_dir}; use std::{path::PathBuf, process}; -use sway_utils::{find_cargo_manifest_dir, find_manifest_dir, MANIFEST_FILE_NAME}; +use sway_utils::MANIFEST_FILE_NAME; pub fn clean(command: CleanCommand) -> Result<()> { let CleanCommand { path } = command; diff --git a/forc/src/ops/forc_deploy.rs b/forc/src/ops/forc_deploy.rs index 2590d1be53e..806fa8be3b7 100644 --- a/forc/src/ops/forc_deploy.rs +++ b/forc/src/ops/forc_deploy.rs @@ -3,12 +3,13 @@ use crate::ops::forc_build; use crate::utils::cli_error::CliError; use anyhow::Result; use forc_pkg::Manifest; +use forc_util::find_manifest_dir; use fuel_gql_client::client::FuelClient; use fuel_tx::{Output, Salt, Transaction}; use fuel_vm::prelude::*; use std::path::PathBuf; use sway_core::{parse, TreeType}; -use sway_utils::{constants::*, find_manifest_dir}; +use sway_utils::constants::*; pub async fn deploy(command: DeployCommand) -> Result { let curr_dir = if let Some(ref path) = command.path { diff --git a/forc/src/ops/forc_fmt.rs b/forc/src/ops/forc_fmt.rs index b13b8f559a2..7ddc1ad3e36 100644 --- a/forc/src/ops/forc_fmt.rs +++ b/forc/src/ops/forc_fmt.rs @@ -1,11 +1,11 @@ use crate::cli::{BuildCommand, FormatCommand}; use crate::ops::forc_build; -use forc_util::{println_green, println_red}; +use forc_util::{find_manifest_dir, println_green, println_red}; use prettydiff::{basic::DiffOp, diff_lines}; use std::default::Default; use std::{fmt, fs, io, path::Path, sync::Arc}; use sway_fmt::{get_formatted_data, FormattingOptions}; -use sway_utils::{constants, find_manifest_dir, get_sway_files}; +use sway_utils::{constants, get_sway_files}; use taplo::formatter as taplo_fmt; pub fn format(command: FormatCommand) -> Result<(), FormatError> { diff --git a/forc/src/ops/forc_run.rs b/forc/src/ops/forc_run.rs index c5304639711..6ce4b85e0f4 100644 --- a/forc/src/ops/forc_run.rs +++ b/forc/src/ops/forc_run.rs @@ -2,13 +2,14 @@ use crate::cli::{BuildCommand, RunCommand}; use crate::ops::forc_build; use crate::utils::cli_error::CliError; use forc_pkg::Manifest; +use forc_util::find_manifest_dir; use fuel_gql_client::client::FuelClient; use fuel_tx::Transaction; use futures::TryFutureExt; use std::path::PathBuf; use std::str::FromStr; use sway_core::{parse, TreeType}; -use sway_utils::{constants::*, find_manifest_dir}; +use sway_utils::constants::*; use tokio::process::Child; pub async fn run(command: RunCommand) -> Result<(), CliError> { diff --git a/forc/src/ops/forc_update.rs b/forc/src/ops/forc_update.rs index ed119da9d54..2e5ac8bcbab 100644 --- a/forc/src/ops/forc_update.rs +++ b/forc/src/ops/forc_update.rs @@ -1,9 +1,8 @@ use crate::cli::UpdateCommand; use anyhow::{anyhow, Result}; use forc_pkg::{self as pkg, lock, Lock, Manifest}; -use forc_util::lock_path; +use forc_util::{find_manifest_dir, lock_path}; use std::{fs, path::PathBuf}; -use sway_utils::find_manifest_dir; /// Running `forc update` will check for updates for the entire dependency graph and commit new /// semver-compatible versions to the `Forc.lock` file. For git dependencies, the commit is updated diff --git a/sway-lsp/Cargo.toml b/sway-lsp/Cargo.toml index 4f2a7acff48..51823c2bb16 100644 --- a/sway-lsp/Cargo.toml +++ b/sway-lsp/Cargo.toml @@ -10,6 +10,7 @@ description = "LSP server for Sway." [dependencies] dashmap = "4.0.2" +forc-util = { version = "0.7.0", path = "../forc-util" } tower-lsp = "0.16.0" ropey = "1.2" serde_json = "1.0.60" diff --git a/sway-lsp/src/server.rs b/sway-lsp/src/server.rs index 2e8ab22d40e..1732d7b42e1 100644 --- a/sway-lsp/src/server.rs +++ b/sway-lsp/src/server.rs @@ -3,8 +3,9 @@ use crate::core::{ document::{DocumentError, TextDocument}, session::Session, }; +use forc_util::find_manifest_dir; use std::sync::Arc; -use sway_utils::helpers::{find_manifest_dir, get_sway_files}; +use sway_utils::helpers::get_sway_files; use tower_lsp::lsp_types::*; use tower_lsp::{jsonrpc, Client, LanguageServer}; diff --git a/sway-utils/src/helpers.rs b/sway-utils/src/helpers.rs index da052530de7..0b6f3cda7a8 100644 --- a/sway-utils/src/helpers.rs +++ b/sway-utils/src/helpers.rs @@ -4,31 +4,6 @@ use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; -/// Continually go up in the file tree until a specified file is found. -#[allow(clippy::branches_sharing_code)] -pub fn find_parent_dir_with_file(starter_path: &Path, file_name: &str) -> Option { - let mut path = std::fs::canonicalize(starter_path).ok()?; - let empty_path = PathBuf::from("/"); - while path != empty_path { - path.push(file_name); - if path.exists() { - path.pop(); - return Some(path); - } else { - path.pop(); - path.pop(); - } - } - None -} -/// Continually go up in the file tree until a Forc manifest file is found. -pub fn find_manifest_dir(starter_path: &Path) -> Option { - find_parent_dir_with_file(starter_path, constants::MANIFEST_FILE_NAME) -} -/// Continually go up in the file tree until a Cargo manifest file is found. -pub fn find_cargo_manifest_dir(starter_path: &Path) -> Option { - find_parent_dir_with_file(starter_path, "Cargo.toml") -} pub fn get_sway_files(path: PathBuf) -> Vec { let mut files = vec![]; let mut dir_entries = vec![path];