diff --git a/forc-pkg/src/manifest.rs b/forc-pkg/src/manifest.rs index 2f2676b423f..789e616b0ee 100644 --- a/forc-pkg/src/manifest.rs +++ b/forc-pkg/src/manifest.rs @@ -23,21 +23,50 @@ pub type MemberName = String; /// A manifest for each workspace member, or just one manifest if working with a single package pub type MemberManifestFiles = BTreeMap; +pub trait GenericManifestFile { + fn from_file>(path: P) -> Result + where + Self: Sized; + fn from_dir>(dir: P) -> Result + where + Self: Sized; + + /// The path to the `Forc.toml` from which this manifest was loaded. + /// + /// This will always be a canonical path. + fn path(&self) -> &Path; + + /// The path to the directory containing the `Forc.toml` from which this manifest was loaded. + /// + /// This will always be a canonical path. + fn dir(&self) -> &Path { + self.path() + .parent() + .expect("failed to retrieve manifest directory") + } + + /// Returns the path of the `Forc.lock` file. + fn lock_path(&self) -> Result; + + /// Returns a mapping of member member names to package manifest files. + fn member_manifests(&self) -> Result; +} + #[derive(Clone, Debug)] pub enum ManifestFile { Package(Box), Workspace(WorkspaceManifestFile), } -impl ManifestFile { +impl GenericManifestFile for ManifestFile { /// Returns a `PackageManifestFile` if the path is within a package directory, otherwise /// returns a `WorkspaceManifestFile` if within a workspace directory. - pub fn from_dir(manifest_dir: &Path) -> Result { - let maybe_pkg_manifest = PackageManifestFile::from_dir(manifest_dir); + fn from_dir>(path: P) -> Result { + let maybe_pkg_manifest = PackageManifestFile::from_dir(path.as_ref()); let manifest_file = if let Err(e) = maybe_pkg_manifest { if e.to_string().contains("missing field `project`") { // This might be a workspace manifest file - let workspace_manifest_file = WorkspaceManifestFile::from_dir(manifest_dir)?; + let workspace_manifest_file = WorkspaceManifestFile::from_dir(path.as_ref())?; ManifestFile::Workspace(workspace_manifest_file) } else { bail!("{}", e) @@ -45,19 +74,22 @@ impl ManifestFile { } else if let Ok(pkg_manifest) = maybe_pkg_manifest { ManifestFile::Package(Box::new(pkg_manifest)) } else { - bail!("Cannot find a valid `Forc.toml` at {:?}", manifest_dir) + bail!( + "Cannot find a valid `Forc.toml` at {}", + path.as_ref().to_string_lossy() + ) }; Ok(manifest_file) } /// Returns a `PackageManifestFile` if the path is pointing to package manifest, otherwise /// returns a `WorkspaceManifestFile` if it is pointing to a workspace manifest. - pub fn from_file(path: PathBuf) -> Result { - let maybe_pkg_manifest = PackageManifestFile::from_file(path.clone()); + fn from_file>(path: P) -> Result { + let maybe_pkg_manifest = PackageManifestFile::from_file(path.as_ref()); let manifest_file = if let Err(e) = maybe_pkg_manifest { if e.to_string().contains("missing field `project`") { // This might be a workspace manifest file - let workspace_manifest_file = WorkspaceManifestFile::from_file(path)?; + let workspace_manifest_file = WorkspaceManifestFile::from_file(path.as_ref())?; ManifestFile::Workspace(workspace_manifest_file) } else { bail!("{}", e) @@ -65,7 +97,10 @@ impl ManifestFile { } else if let Ok(pkg_manifest) = maybe_pkg_manifest { ManifestFile::Package(Box::new(pkg_manifest)) } else { - bail!("Cannot find a valid `Forc.toml` at {:?}", path) + bail!( + "Cannot find a valid `Forc.toml` at {}", + path.as_ref().to_string_lossy() + ) }; Ok(manifest_file) } @@ -73,83 +108,27 @@ impl ManifestFile { /// The path to the `Forc.toml` from which this manifest was loaded. /// /// This will always be a canonical path. - pub fn path(&self) -> &Path { + fn path(&self) -> &Path { match self { ManifestFile::Package(pkg_manifest_file) => pkg_manifest_file.path(), ManifestFile::Workspace(workspace_manifest_file) => workspace_manifest_file.path(), } } - /// The path to the directory containing the `Forc.toml` from which this manifest was loaded. - /// - /// This will always be a canonical path. - pub fn dir(&self) -> &Path { - self.path() - .parent() - .expect("failed to retrieve manifest directory") - } - - /// Returns manifest file map from member name to the corresponding package manifest file - pub fn member_manifests(&self) -> Result { - let mut member_manifest_files = BTreeMap::new(); + fn member_manifests(&self) -> Result { match self { - ManifestFile::Package(pkg_manifest_file) => { - // Check if this package is in a workspace, in that case insert all member manifests - if let Some(workspace_manifest_file) = pkg_manifest_file.workspace()? { - for member_manifest in workspace_manifest_file.member_pkg_manifests()? { - let member_manifest = - member_manifest.with_context(|| "Invalid member manifest")?; - member_manifest_files - .insert(member_manifest.project.name.clone(), member_manifest); - } - } else { - let member_name = &pkg_manifest_file.project.name; - member_manifest_files.insert(member_name.clone(), *pkg_manifest_file.clone()); - } - } + ManifestFile::Package(pkg_manifest_file) => pkg_manifest_file.member_manifests(), ManifestFile::Workspace(workspace_manifest_file) => { - for member_manifest in workspace_manifest_file.member_pkg_manifests()? { - let member_manifest = - member_manifest.with_context(|| "Invalid member manifest")?; - member_manifest_files - .insert(member_manifest.project.name.clone(), member_manifest); - } + workspace_manifest_file.member_manifests() } } - Ok(member_manifest_files) } /// Returns the path of the lock file for the given ManifestFile - pub fn lock_path(&self) -> Result { + fn lock_path(&self) -> Result { match self { ManifestFile::Package(pkg_manifest) => pkg_manifest.lock_path(), - ManifestFile::Workspace(workspace_manifest) => Ok(workspace_manifest.lock_path()), - } - } -} - -impl TryInto for ManifestFile { - type Error = anyhow::Error; - - fn try_into(self) -> Result { - match self { - ManifestFile::Package(pkg_manifest_file) => Ok(*pkg_manifest_file), - ManifestFile::Workspace(_) => { - bail!("Cannot convert workspace manifest to package manifest") - } - } - } -} - -impl TryInto for ManifestFile { - type Error = anyhow::Error; - - fn try_into(self) -> Result { - match self { - ManifestFile::Package(_) => { - bail!("Cannot convert package manifest to workspace manifest") - } - ManifestFile::Workspace(workspace_manifest_file) => Ok(workspace_manifest_file), + ManifestFile::Workspace(workspace_manifest) => workspace_manifest.lock_path(), } } } @@ -307,29 +286,13 @@ impl Dependency { } impl PackageManifestFile { - /// Given a path to a `Forc.toml`, read it and construct a `PackageManifest`. - /// - /// This also `validate`s the manifest, returning an `Err` in the case that invalid names, - /// fields were used. - /// - /// If `core` and `std` are unspecified, `std` will be added to the `dependencies` table - /// implicitly. In this case, the git tag associated with the version of this crate is used to - /// specify the pinned commit at which we fetch `std`. - pub fn from_file>(path: P) -> Result { - let path = path.as_ref().canonicalize()?; - let manifest = PackageManifest::from_file(&path)?; - let manifest_file = Self { manifest, path }; - manifest_file.validate()?; - Ok(manifest_file) - } - /// Returns an iterator over patches defined in underlying `PackageManifest` if this is a /// standalone package. /// /// If this package is a member of a workspace, patches are fetched from /// the workspace manifest file, ignoring any patch defined in the package /// manifest file, even if a patch section is not defined in the namespace. - pub fn resolve_patches(&self) -> Result> { + fn resolve_patches(&self) -> Result> { if let Some(workspace) = self.workspace().ok().flatten() { // If workspace is defined, passing a local patch is a warning, but the global patch is used if self.patch.is_some() { @@ -362,68 +325,6 @@ impl PackageManifestFile { .map(|(_, patch)| patch)) } - /// Read the manifest from the `Forc.toml` in the directory specified by the given `path` or - /// any of its parent directories. - /// - /// This is short for `PackageManifest::from_file`, but takes care of constructing the path to the - /// file. - pub fn from_dir>(manifest_dir: P) -> Result { - let manifest_dir = manifest_dir.as_ref(); - let dir = find_parent_manifest_dir(manifest_dir) - .ok_or_else(|| manifest_file_missing(manifest_dir))?; - let path = dir.join(constants::MANIFEST_FILE_NAME); - Self::from_file(path) - } - - /// Validate the `PackageManifestFile`. - /// - /// This checks: - /// 1. Validity of the underlying `PackageManifest`. - /// 2. Existence of the entry file. - pub fn validate(&self) -> Result<()> { - self.manifest.validate()?; - let mut entry_path = self.path.clone(); - entry_path.pop(); - let entry_path = entry_path - .join(constants::SRC_DIR) - .join(&self.project.entry); - if !entry_path.exists() { - bail!( - "failed to validate path from entry field {:?} in Forc manifest file.", - self.project.entry - ) - } - - // Check for nested packages. - // - // `path` is the path to manifest file. To start nested package search we need to start - // from manifest's directory. So, last part of the path (the filename, "/forc.toml") needs - // to be removed. - let mut pkg_dir = self.path.to_path_buf(); - pkg_dir.pop(); - if let Some(nested_package) = find_nested_manifest_dir(&pkg_dir) { - // remove file name from nested_package_manifest - bail!("Nested packages are not supported, please consider seperating the nested package at {} from the package at {}, or if it makes sense consider creating a workspace.", nested_package.display(), pkg_dir.display()) - } - Ok(()) - } - - /// The path to the `Forc.toml` from which this manifest was loaded. - /// - /// This will always be a canonical path. - pub fn path(&self) -> &Path { - &self.path - } - - /// The path to the directory containing the `Forc.toml` from which this manfiest was loaded. - /// - /// This will always be a canonical path. - pub fn dir(&self) -> &Path { - self.path() - .parent() - .expect("failed to retrieve manifest directory") - } - /// Given the directory in which the file associated with this `PackageManifest` resides, produce the /// path to the entry file as specified in the manifest. /// @@ -514,24 +415,108 @@ impl PackageManifestFile { } } + /// Returns an immutable reference to the project name that this manifest file describes. + pub fn project_name(&self) -> &str { + &self.project.name + } + + /// Validate the `PackageManifestFile`. + /// + /// This checks: + /// 1. Validity of the underlying `PackageManifest`. + /// 2. Existence of the entry file. + pub fn validate(&self) -> Result<()> { + self.manifest.validate()?; + let mut entry_path = self.path.clone(); + entry_path.pop(); + let entry_path = entry_path + .join(constants::SRC_DIR) + .join(&self.project.entry); + if !entry_path.exists() { + bail!( + "failed to validate path from entry field {:?} in Forc manifest file.", + self.project.entry + ) + } + + // Check for nested packages. + // + // `path` is the path to manifest file. To start nested package search we need to start + // from manifest's directory. So, last part of the path (the filename, "/forc.toml") needs + // to be removed. + let mut pkg_dir = self.path.to_path_buf(); + pkg_dir.pop(); + if let Some(nested_package) = find_nested_manifest_dir(&pkg_dir) { + // remove file name from nested_package_manifest + bail!("Nested packages are not supported, please consider seperating the nested package at {} from the package at {}, or if it makes sense consider creating a workspace.", nested_package.display(), pkg_dir.display()) + } + Ok(()) + } +} + +impl GenericManifestFile for PackageManifestFile { + /// Given a path to a `Forc.toml`, read it and construct a `PackageManifest`. + /// + /// This also `validate`s the manifest, returning an `Err` in the case that invalid names, + /// fields were used. + /// + /// If `core` and `std` are unspecified, `std` will be added to the `dependencies` table + /// implicitly. In this case, the git tag associated with the version of this crate is used to + /// specify the pinned commit at which we fetch `std`. + fn from_file>(path: P) -> Result { + let path = path.as_ref().canonicalize()?; + let manifest = PackageManifest::from_file(&path)?; + let manifest_file = Self { manifest, path }; + manifest_file.validate()?; + Ok(manifest_file) + } + + /// Read the manifest from the `Forc.toml` in the directory specified by the given `path` or + /// any of its parent directories. + /// + /// This is short for `PackageManifest::from_file`, but takes care of constructing the path to the + /// file. + fn from_dir>(manifest_dir: P) -> Result { + let manifest_dir = manifest_dir.as_ref(); + let dir = find_parent_manifest_dir(manifest_dir) + .ok_or_else(|| manifest_file_missing(manifest_dir))?; + let path = dir.join(constants::MANIFEST_FILE_NAME); + Self::from_file(path) + } + + fn path(&self) -> &Path { + &self.path + } + /// Returns the location of the lock file for `PackageManifestFile`. /// Checks if this PackageManifestFile corresponds to a workspace member and if that is the case /// returns the workspace level lock file's location. /// /// This will always be a canonical path. - pub fn lock_path(&self) -> Result { + fn lock_path(&self) -> Result { // Check if this package is in a workspace let workspace_manifest = self.workspace()?; if let Some(workspace_manifest) = workspace_manifest { - Ok(workspace_manifest.lock_path()) + workspace_manifest.lock_path() } else { Ok(self.dir().to_path_buf().join(constants::LOCK_FILE_NAME)) } } - /// Returns an immutable reference to the project name that this manifest file describes. - pub fn project_name(&self) -> &str { - &self.project.name + fn member_manifests(&self) -> Result { + let mut member_manifest_files = BTreeMap::new(); + // Check if this package is in a workspace, in that case insert all member manifests + if let Some(workspace_manifest_file) = self.workspace()? { + for member_manifest in workspace_manifest_file.member_pkg_manifests()? { + let member_manifest = member_manifest.with_context(|| "Invalid member manifest")?; + member_manifest_files.insert(member_manifest.project.name.clone(), member_manifest); + } + } else { + let member_name = &self.project.name; + member_manifest_files.insert(member_name.clone(), self.clone()); + } + + Ok(member_manifest_files) } } @@ -887,12 +872,52 @@ pub struct Workspace { } impl WorkspaceManifestFile { + /// Produce an iterator yielding all listed patches. + pub fn patches(&self) -> impl Iterator { + self.patch + .as_ref() + .into_iter() + .flat_map(|patches| patches.iter()) + } + + /// Returns an iterator over relative paths of workspace members. + pub fn members(&self) -> impl Iterator + '_ { + self.workspace.members.iter() + } + + /// Returns an iterator over workspace member root directories. + /// + /// This will always return canonical paths. + pub fn member_paths(&self) -> Result + '_> { + Ok(self + .workspace + .members + .iter() + .map(|member| self.dir().join(member))) + } + + /// Returns an iterator over workspace member package manifests. + pub fn member_pkg_manifests( + &self, + ) -> Result> + '_> { + let member_paths = self.member_paths()?; + let member_pkg_manifests = member_paths.map(PackageManifestFile::from_dir); + Ok(member_pkg_manifests) + } + + /// Check if given path corresponds to any workspace member's path + pub fn is_member_path(&self, path: &Path) -> Result { + Ok(self.member_paths()?.any(|member_path| member_path == path)) + } +} + +impl GenericManifestFile for WorkspaceManifestFile { /// Given a path to a `Forc.toml`, read it and construct a `PackageManifest` /// /// This also `validate`s the manifest, returning an `Err` in the case that given members are /// not present in the manifest dir. - pub fn from_file(path: PathBuf) -> Result { - let path = path.canonicalize()?; + fn from_file>(path: P) -> Result { + let path = path.as_ref().canonicalize()?; let parent = path .parent() .ok_or_else(|| anyhow!("Cannot get parent dir of {:?}", path))?; @@ -906,7 +931,7 @@ impl WorkspaceManifestFile { /// /// This is short for `PackageManifest::from_file`, but takes care of constructing the path to the /// file. - pub fn from_dir>(manifest_dir: T) -> Result { + fn from_dir>(manifest_dir: P) -> Result { let manifest_dir = manifest_dir.as_ref(); let dir = find_parent_manifest_dir_with_check(manifest_dir, |possible_manifest_dir| { // Check if the found manifest file is a workspace manifest file or a standalone @@ -930,65 +955,25 @@ impl WorkspaceManifestFile { Self::from_file(path) } - /// Returns an iterator over relative paths of workspace members. - pub fn members(&self) -> impl Iterator + '_ { - self.workspace.members.iter() - } - - /// Returns an iterator over workspace member root directories. - /// - /// This will always return canonical paths. - pub fn member_paths(&self) -> Result + '_> { - Ok(self - .workspace - .members - .iter() - .map(|member| self.dir().join(member))) - } - - /// Returns an iterator over workspace member package manifests. - pub fn member_pkg_manifests( - &self, - ) -> Result> + '_> { - let member_paths = self.member_paths()?; - let member_pkg_manifests = member_paths.map(PackageManifestFile::from_dir); - Ok(member_pkg_manifests) - } - - /// The path to the `Forc.toml` from which this manifest was loaded. - /// - /// This will always be a canonical path. - pub fn path(&self) -> &Path { + fn path(&self) -> &Path { &self.path } - /// The path to the directory containing the `Forc.toml` from which this manifest was loaded. - /// - /// This will always be a canonical path. - pub fn dir(&self) -> &Path { - self.path() - .parent() - .expect("failed to retrieve manifest directory") - } - - /// Check if given path corresponds to any workspace member's path - pub fn is_member_path(&self, path: &Path) -> Result { - Ok(self.member_paths()?.any(|member_path| member_path == path)) - } - /// Returns the location of the lock file for `WorkspaceManifestFile`. /// /// This will always be a canonical path. - pub fn lock_path(&self) -> PathBuf { - self.dir().to_path_buf().join(constants::LOCK_FILE_NAME) + fn lock_path(&self) -> Result { + Ok(self.dir().to_path_buf().join(constants::LOCK_FILE_NAME)) } - /// Produce an iterator yielding all listed patches. - pub fn patches(&self) -> impl Iterator { - self.patch - .as_ref() - .into_iter() - .flat_map(|patches| patches.iter()) + fn member_manifests(&self) -> Result { + let mut member_manifest_files = BTreeMap::new(); + for member_manifest in self.member_pkg_manifests()? { + let member_manifest = member_manifest.with_context(|| "Invalid member manifest")?; + member_manifest_files.insert(member_manifest.project.name.clone(), member_manifest); + } + + Ok(member_manifest_files) } } diff --git a/forc-pkg/src/pkg.rs b/forc-pkg/src/pkg.rs index a041ad08f35..bda3883a37c 100644 --- a/forc-pkg/src/pkg.rs +++ b/forc-pkg/src/pkg.rs @@ -1,3 +1,4 @@ +use crate::manifest::GenericManifestFile; use crate::{ lock::Lock, manifest::{ @@ -580,7 +581,7 @@ impl BuildPlan { std::env::current_dir()? }; - let manifest_file = ManifestFile::from_dir(&manifest_dir)?; + let manifest_file = ManifestFile::from_dir(manifest_dir)?; let member_manifests = manifest_file.member_manifests()?; // Check if we have members to build so that we are not trying to build an empty workspace. if member_manifests.is_empty() { @@ -2773,7 +2774,7 @@ mod test { .parent() .unwrap() .join("test/src/e2e_vm_tests/test_programs/should_pass/forc/workspace_building/"); - let manifest_file = ManifestFile::from_dir(&manifest_dir).unwrap(); + let manifest_file = ManifestFile::from_dir(manifest_dir).unwrap(); let member_manifests = manifest_file.member_manifests().unwrap(); let lock_path = manifest_file.lock_path().unwrap(); BuildPlan::from_lock_and_manifests( diff --git a/forc-pkg/src/source/git/mod.rs b/forc-pkg/src/source/git/mod.rs index d55da27574e..6f0cb694cda 100644 --- a/forc-pkg/src/source/git/mod.rs +++ b/forc-pkg/src/source/git/mod.rs @@ -1,4 +1,6 @@ mod auth; + +use crate::manifest::GenericManifestFile; use crate::{ manifest::{self, PackageManifestFile}, source, diff --git a/forc-pkg/src/source/ipfs.rs b/forc-pkg/src/source/ipfs.rs index 64df35d9f3d..70dc988588d 100644 --- a/forc-pkg/src/source/ipfs.rs +++ b/forc-pkg/src/source/ipfs.rs @@ -1,3 +1,4 @@ +use crate::manifest::GenericManifestFile; use crate::{ manifest::{self, PackageManifestFile}, source, diff --git a/forc-pkg/src/source/member.rs b/forc-pkg/src/source/member.rs index 11664f80296..48ff4ff5f36 100644 --- a/forc-pkg/src/source/member.rs +++ b/forc-pkg/src/source/member.rs @@ -1,3 +1,4 @@ +use crate::manifest::GenericManifestFile; use crate::{manifest::PackageManifestFile, source}; use serde::{Deserialize, Serialize}; use std::{ diff --git a/forc-pkg/src/source/mod.rs b/forc-pkg/src/source/mod.rs index df2fea8de67..76533788151 100644 --- a/forc-pkg/src/source/mod.rs +++ b/forc-pkg/src/source/mod.rs @@ -14,6 +14,7 @@ pub mod path; mod reg; use self::git::Url; +use crate::manifest::GenericManifestFile; use crate::{ manifest::{self, MemberManifestFiles, PackageManifestFile}, pkg::{ManifestMap, PinnedId}, diff --git a/forc-pkg/src/source/path.rs b/forc-pkg/src/source/path.rs index eb5ff20d3c3..4602b3d8862 100644 --- a/forc-pkg/src/source/path.rs +++ b/forc-pkg/src/source/path.rs @@ -1,3 +1,4 @@ +use crate::manifest::GenericManifestFile; use crate::{manifest::PackageManifestFile, pkg::PinnedId, source}; use serde::{Deserialize, Serialize}; use std::{ diff --git a/forc-plugins/forc-client/src/op/deploy.rs b/forc-plugins/forc-client/src/op/deploy.rs index a2c03acca36..bd1079e7914 100644 --- a/forc-plugins/forc-client/src/op/deploy.rs +++ b/forc-plugins/forc-client/src/op/deploy.rs @@ -8,6 +8,7 @@ use crate::{ }, }; use anyhow::{bail, Context, Result}; +use forc_pkg::manifest::GenericManifestFile; use forc_pkg::{self as pkg, PackageManifestFile}; use forc_tracing::println_warning; use forc_util::default_output_directory; diff --git a/forc-plugins/forc-client/src/util/pkg.rs b/forc-plugins/forc-client/src/util/pkg.rs index c72a740cdfd..5706da27413 100644 --- a/forc-plugins/forc-client/src/util/pkg.rs +++ b/forc-plugins/forc-client/src/util/pkg.rs @@ -1,8 +1,8 @@ -use std::{collections::HashMap, path::Path, sync::Arc}; - use anyhow::Result; +use forc_pkg::manifest::GenericManifestFile; use forc_pkg::{self as pkg, manifest::ManifestFile, BuildOpts, BuildPlan}; use pkg::{build_with_options, BuiltPackage}; +use std::{collections::HashMap, path::Path, sync::Arc}; pub(crate) fn built_pkgs(path: &Path, build_opts: BuildOpts) -> Result>> { let manifest_file = ManifestFile::from_dir(path)?; diff --git a/forc-plugins/forc-debug/src/server/handlers/handle_launch.rs b/forc-plugins/forc-debug/src/server/handlers/handle_launch.rs index d87cb0efe30..b8b55fc0c1a 100644 --- a/forc-plugins/forc-debug/src/server/handlers/handle_launch.rs +++ b/forc-plugins/forc-debug/src/server/handlers/handle_launch.rs @@ -1,6 +1,7 @@ use crate::server::{AdapterError, DapServer}; use crate::types::Instruction; -use forc_pkg::{self, BuildProfile, Built, BuiltPackage, PackageManifestFile}; +use forc_pkg::manifest::GenericManifestFile; +use forc_pkg::{self, BuildProfile, Built, BuiltPackage}; use forc_test::execute::TestExecutor; use forc_test::setup::TestSetup; use forc_test::BuiltTests; @@ -53,28 +54,26 @@ impl DapServer { } // 1. Build the packages - let manifest_file = forc_pkg::manifest::ManifestFile::from_dir(&self.state.program_path) + let pkg_manifest = forc_pkg::manifest::PackageManifestFile::from_dir( + &self.state.program_path, + ) + .map_err(|err| AdapterError::BuildFailed { + reason: format!("read manifest file: {:?}", err), + })?; + + let lock_path = pkg_manifest + .lock_path() .map_err(|err| AdapterError::BuildFailed { - reason: format!("read manifest file: {:?}", err), + reason: format!("lock path: {:?}", err), })?; - let pkg_manifest: PackageManifestFile = - manifest_file - .clone() - .try_into() - .map_err(|err: anyhow::Error| AdapterError::BuildFailed { - reason: format!("package manifest: {:?}", err), - })?; + let member_manifests = - manifest_file + pkg_manifest .member_manifests() .map_err(|err| AdapterError::BuildFailed { reason: format!("member manifests: {:?}", err), })?; - let lock_path = manifest_file - .lock_path() - .map_err(|err| AdapterError::BuildFailed { - reason: format!("lock path: {:?}", err), - })?; + let build_plan = forc_pkg::BuildPlan::from_lock_and_manifests( &lock_path, &member_manifests, diff --git a/forc-plugins/forc-doc/src/main.rs b/forc-plugins/forc-doc/src/main.rs index 4f6cfa45036..b9d3c79fba2 100644 --- a/forc-plugins/forc-doc/src/main.rs +++ b/forc-plugins/forc-doc/src/main.rs @@ -8,6 +8,7 @@ use clap::Parser; use cli::Command; use colored::*; use forc_pkg as pkg; +use forc_pkg::manifest::GenericManifestFile; use forc_util::default_output_directory; use include_dir::{include_dir, Dir}; use pkg::{manifest::ManifestFile, PackageManifestFile}; @@ -175,7 +176,7 @@ pub fn compile_html( } else { std::env::current_dir()? }; - let manifest = ManifestFile::from_dir(&dir)?; + let manifest = ManifestFile::from_dir(dir)?; let pkg_manifest = if let ManifestFile::Package(pkg_manifest) = &manifest { pkg_manifest } else { diff --git a/forc-plugins/forc-fmt/src/main.rs b/forc-plugins/forc-fmt/src/main.rs index 28ff0330f3b..d88a38ffd9e 100644 --- a/forc-plugins/forc-fmt/src/main.rs +++ b/forc-plugins/forc-fmt/src/main.rs @@ -2,7 +2,10 @@ use anyhow::{bail, Result}; use clap::Parser; -use forc_pkg::{manifest::ManifestFile, WorkspaceManifestFile}; +use forc_pkg::{ + manifest::{GenericManifestFile, ManifestFile}, + WorkspaceManifestFile, +}; use prettydiff::{basic::DiffOp, diff_lines}; use std::{ default::Default, diff --git a/forc/src/ops/forc_check.rs b/forc/src/ops/forc_check.rs index fc7e69166cf..457d8de7370 100644 --- a/forc/src/ops/forc_check.rs +++ b/forc/src/ops/forc_check.rs @@ -1,6 +1,7 @@ use crate::cli::CheckCommand; use anyhow::Result; use forc_pkg as pkg; +use forc_pkg::manifest::GenericManifestFile; use pkg::manifest::ManifestFile; use std::path::PathBuf; use sway_core::{language::ty, Engines}; @@ -22,7 +23,7 @@ pub fn check(command: CheckCommand, engines: &Engines) -> Result<(Option Result<()> { ) } }; - let manifest = ManifestFile::from_dir(&manifest_dir)?; + let manifest = ManifestFile::from_dir(manifest_dir)?; // If this is a workspace collect all member paths and clean each of them. let paths: Vec = match manifest { ManifestFile::Package(_) => std::iter::once(this_dir).collect(), diff --git a/forc/src/ops/forc_update.rs b/forc/src/ops/forc_update.rs index ecd2458f5f8..731bfd2aee5 100644 --- a/forc/src/ops/forc_update.rs +++ b/forc/src/ops/forc_update.rs @@ -1,5 +1,6 @@ use crate::cli::UpdateCommand; use anyhow::{anyhow, Result}; +use forc_pkg::manifest::GenericManifestFile; use forc_pkg::{self as pkg, lock, Lock}; use forc_util::lock_path; use pkg::manifest::ManifestFile; @@ -33,7 +34,7 @@ pub async fn update(command: UpdateCommand) -> Result<()> { None => std::env::current_dir()?, }; - let manifest = ManifestFile::from_dir(&this_dir)?; + let manifest = ManifestFile::from_dir(this_dir)?; let lock_path = lock_path(manifest.dir()); let old_lock = Lock::from_path(&lock_path).ok().unwrap_or_default(); let offline = false; diff --git a/sway-lsp/src/core/session.rs b/sway-lsp/src/core/session.rs index e208b0e0c52..7258324bdbf 100644 --- a/sway-lsp/src/core/session.rs +++ b/sway-lsp/src/core/session.rs @@ -23,7 +23,10 @@ use lsp_types::{ TextDocumentContentChangeEvent, TextEdit, Url, }; use parking_lot::RwLock; -use pkg::{manifest::ManifestFile, BuildPlan}; +use pkg::{ + manifest::{GenericManifestFile, ManifestFile}, + BuildPlan, +}; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use std::{ path::PathBuf, @@ -323,7 +326,7 @@ impl Session { pub(crate) fn build_plan(uri: &Url) -> Result { let manifest_dir = PathBuf::from(uri.path()); let manifest = - ManifestFile::from_dir(&manifest_dir).map_err(|_| DocumentError::ManifestFileNotFound { + ManifestFile::from_dir(manifest_dir).map_err(|_| DocumentError::ManifestFileNotFound { dir: uri.path().into(), })?; let member_manifests = diff --git a/sway-lsp/src/core/sync.rs b/sway-lsp/src/core/sync.rs index b288b5cadb7..c7a2e6c5c40 100644 --- a/sway-lsp/src/core/sync.rs +++ b/sway-lsp/src/core/sync.rs @@ -3,6 +3,7 @@ use crate::{ utils::document::{get_path_from_url, get_url_from_path, get_url_from_span}, }; use dashmap::DashMap; +use forc_pkg::manifest::GenericManifestFile; use forc_pkg::{manifest::Dependency, PackageManifestFile}; use indexmap::IndexMap; use lsp_types::Url; diff --git a/sway-lsp/src/server_state.rs b/sway-lsp/src/server_state.rs index 89558005e43..29f8fc100c2 100644 --- a/sway-lsp/src/server_state.rs +++ b/sway-lsp/src/server_state.rs @@ -9,6 +9,7 @@ use crate::{ }; use crossbeam_channel::{Receiver, Sender}; use dashmap::DashMap; +use forc_pkg::manifest::GenericManifestFile; use forc_pkg::PackageManifestFile; use lsp_types::{Diagnostic, Url}; use parking_lot::RwLock;