Skip to content

Commit

Permalink
feat(turbo): update changemapper to return name and path of packages (#…
Browse files Browse the repository at this point in the history
…7336)

Co-authored-by: Chris Olszewski <chris.olszewski@vercel.com>
Co-authored-by: Nicholas Yang <nicholas.yang@vercel.com>
  • Loading branch information
3 people committed Feb 9, 2024
1 parent e06eb59 commit 178a294
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
5 changes: 4 additions & 1 deletion crates/turborepo-lib/src/run/scope/change_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ impl<'a> GitChangeDetector for ScopeChangeDetector<'a> {
.workspaces()
.map(|(name, _)| name.to_owned())
.collect()),
PackageChanges::Some(packages) => Ok(packages),
PackageChanges::Some(packages) => Ok(packages
.iter()
.map(|package| package.name.to_owned())
.collect()),
}
}
}
15 changes: 9 additions & 6 deletions crates/turborepo-repository/src/change_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashSet;
use turbopath::{AbsoluteSystemPath, AnchoredSystemPath, AnchoredSystemPathBuf};
use wax::Program;

use crate::package_graph::{ChangedPackagesError, PackageGraph, WorkspaceName};
use crate::package_graph::{ChangedPackagesError, PackageGraph, WorkspaceName, WorkspacePackage};

// We may not be able to load the lockfile contents, but we
// still want to be able to express a generic change.
Expand All @@ -17,7 +17,7 @@ pub enum LockfileChange {

pub enum PackageChanges {
All,
Some(HashSet<WorkspaceName>),
Some(HashSet<WorkspacePackage>),
}

pub struct ChangeMapper<'a> {
Expand Down Expand Up @@ -102,7 +102,7 @@ impl<'a> ChangeMapper<'a> {
&self,
files: impl Iterator<Item = &'b AnchoredSystemPathBuf>,
graph: &PackageGraph,
) -> Result<HashSet<WorkspaceName>, turborepo_scm::Error> {
) -> Result<HashSet<WorkspacePackage>, turborepo_scm::Error> {
let mut changed_packages = HashSet::new();
for file in files {
let mut found = false;
Expand All @@ -112,15 +112,18 @@ impl<'a> ChangeMapper<'a> {
}
if let Some(package_path) = entry.package_json_path.parent() {
if Self::is_file_in_package(file, package_path) {
changed_packages.insert(name.to_owned());
changed_packages.insert(WorkspacePackage {
name: name.clone(),
path: entry.package_path().to_owned(),
});
found = true;
break;
}
}
}
if !found {
// if the file is not in any package, it must be in the root package
changed_packages.insert(WorkspaceName::Root);
changed_packages.insert(WorkspacePackage::root());
}
}

Expand All @@ -136,7 +139,7 @@ impl<'a> ChangeMapper<'a> {
fn get_changed_packages_from_lockfile(
&self,
lockfile_content: Vec<u8>,
) -> Result<Vec<WorkspaceName>, ChangeMapError> {
) -> Result<Vec<WorkspacePackage>, ChangeMapError> {
let previous_lockfile = self
.pkg_graph
.package_manager()
Expand Down
51 changes: 45 additions & 6 deletions crates/turborepo-repository/src/package_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ pub struct PackageGraph {
lockfile: Option<Box<dyn Lockfile>>,
}

/// The WorkspacePackage follows the Vercel glossary of terms where "Workspace"
/// is the collection of packages and "Package" is a single package within the
/// workspace. https://vercel.com/docs/vercel-platform/glossary
/// There are other structs in this module that have "Workspace" in the name,
/// but they do NOT follow the glossary, and instead mean "package" when they
/// say Workspace. Some of these are labeled as such.
#[derive(Eq, PartialEq, Hash)]
pub struct WorkspacePackage {
pub name: WorkspaceName,
pub path: AnchoredSystemPathBuf,
}

impl WorkspacePackage {
pub fn root() -> Self {
Self {
name: WorkspaceName::Root,
path: AnchoredSystemPathBuf::default(),
}
}
}

/// WorkspaceInfo represents a package within the workspace.
/// TODO: The name WorkspaceInfo should be changed to PackageInfo to follow the
/// Vercel glossary.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WorkspaceInfo {
pub package_json: PackageJson,
Expand Down Expand Up @@ -61,7 +85,8 @@ impl WorkspaceInfo {
type PackageName = String;
type PackageVersion = String;

/// Name of workspaces with a special marker for the workspace root
/// WorkspaceName refers to the name of a *package* within a workspace.
/// TODO: rename "WorkspaceName" to PackageName.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum WorkspaceName {
Root,
Expand Down Expand Up @@ -310,7 +335,7 @@ impl PackageGraph {
pub fn changed_packages_from_lockfile(
&self,
previous: &dyn Lockfile,
) -> Result<Vec<WorkspaceName>, ChangedPackagesError> {
) -> Result<Vec<WorkspacePackage>, ChangedPackagesError> {
let current = self.lockfile().ok_or(ChangedPackagesError::NoLockfile)?;

let external_deps = self
Expand Down Expand Up @@ -340,16 +365,30 @@ impl PackageGraph {
closures.get(info.package_path().to_unix().as_str())
!= info.transitive_dependencies.as_ref()
})
.map(|(name, _info)| match name {
WorkspaceName::Other(n) => Some(WorkspaceName::Other(n.to_owned())),
.map(|(name, info)| match name {
WorkspaceName::Other(n) => {
let w_name = WorkspaceName::Other(n.to_owned());
Some(WorkspacePackage {
name: w_name.clone(),
path: info.package_path().to_owned(),
})
}
// if the root package has changed, then we should report `None`
// since all packages need to be revalidated
WorkspaceName::Root => None,
})
.collect::<Option<Vec<WorkspaceName>>>()
.collect::<Option<Vec<WorkspacePackage>>>()
};

Ok(changed.unwrap_or_else(|| self.workspaces.keys().cloned().collect()))
Ok(changed.unwrap_or_else(|| {
self.workspaces
.iter()
.map(|(name, info)| WorkspacePackage {
name: name.clone(),
path: info.package_path().to_owned(),
})
.collect()
}))
}

#[allow(dead_code)]
Expand Down

0 comments on commit 178a294

Please sign in to comment.