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

feat: disallow nested packages #4407

Merged
merged 8 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 7 additions & 9 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::pkg::{manifest_file_missing, parsing_failed, wrong_program_type};
use anyhow::{anyhow, bail, Context, Result};
use forc_tracing::println_yellow_err;
use forc_util::{find_manifest_dir_in_parent, find_nested_manifest_dir, validate_name};
use forc_util::{find_nested_manifest_dir, find_parent_manifest_dir, validate_name};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap},
Expand Down Expand Up @@ -249,7 +249,7 @@ impl PackageManifestFile {
/// This is short for `PackageManifest::from_file`, but takes care of constructing the path to the
/// file.
pub fn from_dir(manifest_dir: &Path) -> Result<Self> {
let dir = forc_util::find_manifest_dir_in_parent(manifest_dir)
let dir = forc_util::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)
Expand Down Expand Up @@ -463,7 +463,7 @@ impl PackageManifest {
/// file.
pub fn from_dir(dir: &Path) -> Result<Self> {
let manifest_dir =
find_manifest_dir_in_parent(dir).ok_or_else(|| manifest_file_missing(dir))?;
find_parent_manifest_dir(dir).ok_or_else(|| manifest_file_missing(dir))?;
let file_path = manifest_dir.join(constants::MANIFEST_FILE_NAME);
Self::from_file(&file_path)
}
Expand Down Expand Up @@ -747,9 +747,8 @@ 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: &Path) -> Result<Self> {
let dir = forc_util::find_manifest_dir_in_parent_with_check(
manifest_dir,
|possible_manifest_dir| {
let dir =
forc_util::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
// package manifest file.
let possible_path = possible_manifest_dir.join(constants::MANIFEST_FILE_NAME);
Expand All @@ -765,9 +764,8 @@ impl WorkspaceManifestFile {
.err()
.map(|e| !e.to_string().contains("missing field `workspace`"))
.unwrap_or_else(|| true)
},
)
.ok_or_else(|| manifest_file_missing(manifest_dir))?;
})
.ok_or_else(|| manifest_file_missing(manifest_dir))?;
let path = dir.join(constants::MANIFEST_FILE_NAME);
Self::from_file(path)
}
Expand Down
6 changes: 3 additions & 3 deletions forc-plugins/forc-fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use taplo::formatter as taplo_fmt;
use tracing::{error, info};

use forc_tracing::{init_tracing_subscriber, println_green, println_red};
use forc_util::{find_manifest_dir_in_parent, is_sway_file};
use forc_util::{find_parent_manifest_dir, is_sway_file};
use sway_core::{BuildConfig, BuildTarget};
use sway_utils::{constants, get_sway_files};
use swayfmt::Formatter;
Expand Down Expand Up @@ -62,7 +62,7 @@ fn run() -> Result<()> {

// If we're formatting a single file, find the nearest manifest if within a project.
// Otherwise, we simply provide 'None' to format_file().
let manifest_file = find_manifest_dir_in_parent(file_path)
let manifest_file = find_parent_manifest_dir(file_path)
.map(|path| path.join(constants::MANIFEST_FILE_NAME));

if is_sway_file(file_path) {
Expand Down Expand Up @@ -252,7 +252,7 @@ fn format_manifest(app: &App, manifest_file: PathBuf) -> Result<bool> {

/// Format the package at the given directory.
fn format_pkg_at_dir(app: &App, dir: &Path, formatter: &mut Formatter) -> Result<()> {
match find_manifest_dir_in_parent(dir) {
match find_parent_manifest_dir(dir) {
Some(path) => {
let manifest_path = path.clone();
let manifest_file = manifest_path.join(constants::MANIFEST_FILE_NAME);
Expand Down
27 changes: 16 additions & 11 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,31 @@ pub fn format_log_receipts(receipts: &[fuel_tx::Receipt], pretty_print: bool) ->
}

/// Continually go down in the file tree until a Forc manifest file is found.
///
/// Given a pacakge's directory (which contains a manifest file) goes down in the file tree until
/// another manifest file is found.
///
/// Returns the directory that contains the nested manifest file.
pub fn find_nested_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
find_nested_dir_with_file(starter_path, constants::MANIFEST_FILE_NAME)
}

/// Continually go down in the file tree until a specified file is found.
///
/// Starts the search from child dirs of `starter_path`.
pub fn find_nested_dir_with_file(starter_path: &Path, file_name: &str) -> Option<PathBuf> {
use walkdir::WalkDir;
WalkDir::new(starter_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|entry| entry.file_name().to_string_lossy() == constants::MANIFEST_FILE_NAME)
.filter(|entry| entry.path() != starter_path.join(file_name))
kayagokalp marked this conversation as resolved.
Show resolved Hide resolved
.filter(|entry| entry.file_name().to_string_lossy() == file_name)
.map(|entry| {
let mut entry = entry.path().to_path_buf();
entry.pop();
entry
})
.nth(1)
.next()
}
kayagokalp marked this conversation as resolved.
Show resolved Hide resolved

/// Continually go up in the file tree until a specified file is found.
///
/// Starts the search from `starter_path`.
#[allow(clippy::branches_sharing_code)]
pub fn find_parent_dir_with_file(starter_path: &Path, file_name: &str) -> Option<PathBuf> {
let mut path = std::fs::canonicalize(starter_path).ok()?;
Expand All @@ -102,22 +107,22 @@ pub fn find_parent_dir_with_file(starter_path: &Path, file_name: &str) -> Option
None
}
/// Continually go up in the file tree until a Forc manifest file is found.
pub fn find_manifest_dir_in_parent(starter_path: &Path) -> Option<PathBuf> {
pub fn find_parent_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
find_parent_dir_with_file(starter_path, constants::MANIFEST_FILE_NAME)
}

/// Continually go up in the file tree until a Forc manifest file is found and given predicate
/// returns true.
pub fn find_manifest_dir_in_parent_with_check<F>(starter_path: &Path, f: F) -> Option<PathBuf>
pub fn find_parent_manifest_dir_with_check<F>(starter_path: &Path, f: F) -> Option<PathBuf>
where
F: Fn(&Path) -> bool,
{
find_manifest_dir_in_parent(starter_path).and_then(|manifest_dir| {
find_parent_manifest_dir(starter_path).and_then(|manifest_dir| {
// If given check satisifies return current dir otherwise start searching from the parent.
if f(&manifest_dir) {
Some(manifest_dir)
} else if let Some(parent_dir) = manifest_dir.parent() {
find_manifest_dir_in_parent_with_check(parent_dir, f)
find_parent_manifest_dir_with_check(parent_dir, f)
} else {
None
}
Expand Down
4 changes: 2 additions & 2 deletions forc/src/ops/forc_clean.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cli::CleanCommand;
use anyhow::{anyhow, bail, Result};
use forc_pkg::manifest::ManifestFile;
use forc_util::{default_output_directory, find_manifest_dir_in_parent};
use forc_util::{default_output_directory, find_parent_manifest_dir};
use std::path::PathBuf;
use sway_utils::MANIFEST_FILE_NAME;

Expand All @@ -15,7 +15,7 @@ pub fn clean(command: CleanCommand) -> Result<()> {
std::env::current_dir().map_err(|e| anyhow!("{:?}", e))?
};

let manifest_dir = match find_manifest_dir_in_parent(&this_dir) {
let manifest_dir = match find_parent_manifest_dir(&this_dir) {
Some(dir) => dir,
None => {
bail!(
Expand Down