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

Re-repair Root Inference #4049

Merged
merged 8 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dialoguer = { version = "0.10.3", features = ["fuzzy-select"] }
dirs-next = "2.0.0"
dunce = "1.0"
env_logger = "0.10.0"
glob-match = "0.2.1"
hostname = "0.3.1"
indicatif = "0.17.3"
lazy_static = "1.4.0"
Expand Down
91 changes: 64 additions & 27 deletions crates/turborepo-lib/src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::{anyhow, Result};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct PnpmWorkspaces {
struct PnpmWorkspace {
pub packages: Vec<String>,
}

Expand Down Expand Up @@ -54,10 +54,29 @@ pub enum PackageManager {

#[derive(Debug)]
pub struct Globs {
#[allow(dead_code)]
inclusions: Vec<PathBuf>,
#[allow(dead_code)]
exclusions: Vec<PathBuf>,
pub inclusions: Vec<String>,
pub exclusions: Vec<String>,
}

impl Globs {
pub fn test(&self, root: PathBuf, target: PathBuf) -> Result<bool> {
let search_value = target
.strip_prefix(root)?
.to_str()
.ok_or_else(|| anyhow!("The relative path is not UTF8."))?;

let includes = &self
.inclusions
.iter()
.any(|inclusion| glob_match::glob_match(inclusion, search_value));

let excludes = &self
.exclusions
.iter()
.any(|exclusion| glob_match::glob_match(exclusion, search_value));

Ok(*includes && !excludes)
}
}

impl PackageManager {
Expand All @@ -70,36 +89,29 @@ impl PackageManager {
///
/// * `root_path`:
///
/// returns: Result<Globs, Error>
/// returns: Result<Option<Globs>, Error>
///
/// # Examples
///
/// ```
/// ```
pub fn get_workspace_globs(&self, root_path: &Path) -> Result<Globs> {
pub fn get_workspace_globs(&self, root_path: &Path) -> Result<Option<Globs>> {
let globs = match self {
PackageManager::Pnpm | PackageManager::Pnpm6 => {
let workspace_yaml = fs::read_to_string(root_path.join("pnpm-workspace.yaml"))?;
let workspaces: PnpmWorkspaces = serde_yaml::from_str(&workspace_yaml)?;
if workspaces.packages.is_empty() {
return Err(anyhow!(
"pnpm-workspace.yaml: no packages found. Turborepo requires pnpm \
workspaces and thus packages to be defined in the root \
pnpm-workspace.yaml"
));
let pnpm_workspace: PnpmWorkspace = serde_yaml::from_str(&workspace_yaml)?;
if pnpm_workspace.packages.is_empty() {
return Ok(None);
} else {
workspaces.packages
pnpm_workspace.packages
}
}
PackageManager::Berry | PackageManager::Npm | PackageManager::Yarn => {
let package_json_text = fs::read_to_string(root_path.join("package.json"))?;
let package_json: PackageJsonWorkspaces = serde_json::from_str(&package_json_text)?;

if package_json.workspaces.as_ref().is_empty() {
return Err(anyhow!(
"package.json: no packages found. Turborepo requires packages to be \
defined in the root package.json"
));
return Ok(None);
} else {
package_json.workspaces.into()
}
Expand All @@ -111,16 +123,16 @@ impl PackageManager {

for glob in globs {
if let Some(exclusion) = glob.strip_prefix('!') {
exclusions.push(PathBuf::from(exclusion.to_string()));
exclusions.push(exclusion.to_string());
} else {
inclusions.push(PathBuf::from(glob));
inclusions.push(glob);
}
}

Ok(Globs {
Ok(Some(Globs {
inclusions,
exclusions,
})
}))
}
}

Expand All @@ -135,12 +147,37 @@ mod tests {
let package_manager = PackageManager::Npm;
let globs = package_manager
.get_workspace_globs(Path::new("../../examples/with-yarn"))
.unwrap()
.unwrap();

assert_eq!(
globs.inclusions,
vec![PathBuf::from("apps/*"), PathBuf::from("packages/*")]
);
assert_eq!(globs.inclusions, vec!["apps/*", "packages/*"]);
}

#[test]
fn test_globs_test() {
struct TestCase {
globs: Globs,
root: PathBuf,
target: PathBuf,
output: Result<bool>,
}

let tests = [TestCase {
globs: Globs {
inclusions: vec!["d/**".to_string()],
exclusions: vec![],
},
root: PathBuf::from("/a/b/c"),
target: PathBuf::from("/a/b/c/d/e/f"),
output: Ok(true),
}];

for test in tests {
match test.globs.test(test.root, test.target) {
Ok(value) => assert_eq!(value, test.output.unwrap()),
Err(value) => assert_eq!(value.to_string(), test.output.unwrap_err().to_string()),
};
}
}

#[test]
Expand Down
Loading