-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(turborepo): Calculate package inference (#5406)
Co-authored-by: Greg Soltis <Greg Soltis> Co-authored-by: Nicholas Yang <nicholas.yang@vercel.com> Co-authored-by: Alexander Lyon <arlyon@me.com>
- Loading branch information
1 parent
a2b1fd4
commit 4314d1d
Showing
6 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use tracing::debug; | ||
use turbopath::{AbsoluteSystemPath, AnchoredSystemPathBuf}; | ||
|
||
use crate::package_graph; | ||
|
||
pub struct PackageInference { | ||
package_name: Option<String>, | ||
directory_root: AnchoredSystemPathBuf, | ||
} | ||
|
||
impl PackageInference { | ||
// calculate, based on the directory that global turbo was invoked in, | ||
// the pieces of a filter spec that we will infer. If turbo was invoked | ||
// somewhere between the root and packages, scope turbo invocations to the | ||
// packages below where turbo was invoked. If turbo was invoked at or within | ||
// a particular package, scope the turbo invocation to just that package. | ||
pub fn calculate( | ||
turbo_root: &AbsoluteSystemPath, | ||
pkg_inference_path: &AnchoredSystemPathBuf, | ||
pkg_graph: &package_graph::PackageGraph, | ||
) -> Self { | ||
debug!( | ||
"Using {} as a basis for selecting pacakges", | ||
pkg_inference_path | ||
); | ||
let full_inference_path = turbo_root.resolve(pkg_inference_path); | ||
for (workspace_name, workspace_entry) in pkg_graph.workspaces() { | ||
let pkg_path = turbo_root.resolve(&workspace_entry.package_json_path()); | ||
let inferred_path_is_below = pkg_path.contains(&full_inference_path); | ||
// We skip over the root package as the inferred path will always be below it | ||
if inferred_path_is_below && (&pkg_path as &AbsoluteSystemPath) != turbo_root { | ||
// set both. The user might have set a parent directory filter, | ||
// in which case we *should* fail to find any packages, but we should | ||
// do so in a consistent manner | ||
return Self { | ||
package_name: Some(workspace_name.to_string()), | ||
directory_root: workspace_entry.package_json_path().clone(), | ||
}; | ||
} | ||
let inferred_path_is_between_root_and_pkg = full_inference_path.contains(&pkg_path); | ||
if inferred_path_is_between_root_and_pkg { | ||
// we've found *some* package below our inference directory. We can stop now and | ||
// conclude that we're looking for all packages in a | ||
// subdirectory | ||
break; | ||
} | ||
} | ||
Self { | ||
package_name: None, | ||
directory_root: pkg_inference_path.to_owned(), | ||
} | ||
} | ||
} |
12 changes: 9 additions & 3 deletions
12
crates/turborepo-lib/src/run/scope.rs → crates/turborepo-lib/src/run/scope/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
mod filter; | ||
|
||
use std::collections::HashSet; | ||
|
||
use anyhow::Result; | ||
use filter::PackageInference; | ||
use tracing::warn; | ||
use turborepo_scm::SCM; | ||
|
||
use crate::{commands::CommandBase, opts::ScopeOpts, package_graph}; | ||
|
||
pub fn resolve_packages( | ||
_opts: &ScopeOpts, | ||
_base: &CommandBase, | ||
_ctx: &package_graph::PackageGraph, | ||
opts: &ScopeOpts, | ||
base: &CommandBase, | ||
pkg_graph: &package_graph::PackageGraph, | ||
_scm: &SCM, | ||
) -> Result<HashSet<String>> { | ||
let _pkg_inference = opts.pkg_inference_root.as_ref().map(|pkg_inference_path| { | ||
PackageInference::calculate(&base.repo_root, &pkg_inference_path, pkg_graph) | ||
}); | ||
warn!("resolve packages not implemented yet"); | ||
Ok(HashSet::new()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters