Skip to content

Commit

Permalink
fix: Fix moon query projects issues. (#1688)
Browse files Browse the repository at this point in the history
* Fix query.

* Redo query.

* Slight rework.

* Fix tracking.
  • Loading branch information
milesj authored Oct 12, 2024
1 parent ff5274c commit e3a9746
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 49 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

- Added a new task option `merge`, that defines the same strategy for all applicable fields. Can be
overridden with `mergeX` task options.
- Added a `moon` setting to `.moon/toolchain.yml`, which can be used to customize the update check
process.

#### 🐞 Fixes

- Fixed `moon query projects` including dependency projects by default. Can be controlled with
`--upstream`.
- Fixed `moon query projects` regex patterns not working when used in conjunction with affected.
- Fixed Bash-based hooks being generated with the wrong path separators on Windows.
- Fixed an issue where an inherited task with merge strategy "replace" will accidentally remove task
args, deps, and env in inheriting tasks.
Expand Down
2 changes: 1 addition & 1 deletion crates/affected/src/affected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ impl fmt::Display for DownstreamScope {
// Dependencies
#[derive(Clone, Copy, Debug, Default, PartialEq, ValueEnum)]
pub enum UpstreamScope {
#[default]
None,
Direct,
#[default]
Deep,
}

Expand Down
8 changes: 4 additions & 4 deletions crates/affected/src/affected_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ impl<'app> AffectedTracker<'app> {
project_graph,
touched_files,
projects: FxHashMap::default(),
project_downstream: DownstreamScope::default(),
project_upstream: UpstreamScope::default(),
project_downstream: DownstreamScope::None,
project_upstream: UpstreamScope::Deep,
tasks: FxHashMap::default(),
task_downstream: DownstreamScope::default(),
task_upstream: UpstreamScope::default(),
task_downstream: DownstreamScope::None,
task_upstream: UpstreamScope::Deep,
}
}

Expand Down
67 changes: 24 additions & 43 deletions crates/app/src/queries/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use moon_project::Project;
use moon_project_graph::ProjectGraph;
use moon_task::Task;
use moon_vcs::BoxedVcs;
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::FxHashSet;
use serde::{Deserialize, Serialize};
use starbase::AppResult;
use starbase_utils::json;
Expand Down Expand Up @@ -57,7 +57,9 @@ fn convert_to_regex(field: &str, value: &Option<String>) -> AppResult<Option<reg
);

// case-insensitive by default
Ok(Some(regex::Regex::new(&format!("(?i){pattern}")).unwrap()))
let pat = regex::Regex::new(&format!("(?i){pattern}")).into_diagnostic()?;

Ok(Some(pat))
}
None => Ok(None),
}
Expand Down Expand Up @@ -101,32 +103,14 @@ pub async fn load_touched_files(vcs: &BoxedVcs) -> AppResult<FxHashSet<Workspace
Ok(result.files)
}

fn load_from_query(
project_graph: &ProjectGraph,
query: &str,
options: &QueryProjectsOptions,
) -> miette::Result<FxHashMap<Id, Arc<Project>>> {
Ok(project_graph
.query(moon_query::build_query(query)?)?
.into_iter()
.filter_map(|project| {
if options
.affected
.as_ref()
.is_some_and(|affected| !affected.is_project_affected(&project.id))
{
return None;
}

Some((project.id.clone(), project.to_owned()))
})
.collect::<FxHashMap<_, _>>())
fn load_with_query(project_graph: &ProjectGraph, query: &str) -> miette::Result<Vec<Arc<Project>>> {
project_graph.query(moon_query::build_query(query)?)
}

fn load_with_regex(
project_graph: &ProjectGraph,
options: &QueryProjectsOptions,
) -> miette::Result<FxHashMap<Id, Arc<Project>>> {
) -> miette::Result<Vec<Arc<Project>>> {
let alias_regex = convert_to_regex("alias", &options.alias)?;
let id_regex = convert_to_regex("id", &options.id)?;
let language_regex = convert_to_regex("language", &options.language)?;
Expand All @@ -135,17 +119,9 @@ fn load_with_regex(
let tags_regex = convert_to_regex("tags", &options.tags)?;
let tasks_regex = convert_to_regex("tasks", &options.tasks)?;
let type_regex = convert_to_regex("type", &options.type_of)?;
let mut projects = FxHashMap::default();
let mut filtered = vec![];

for project in project_graph.get_all()? {
if options
.affected
.as_ref()
.is_some_and(|affected| !affected.is_project_affected(&project.id))
{
continue;
}

if let Some(regex) = &id_regex {
if !regex.is_match(&project.id) {
continue;
Expand Down Expand Up @@ -203,10 +179,10 @@ fn load_with_regex(
}
}

projects.insert(project.id.clone(), project.to_owned());
filtered.push(project);
}

Ok(projects)
Ok(filtered)
}

pub async fn query_projects(
Expand All @@ -216,20 +192,25 @@ pub async fn query_projects(
debug!("Querying for projects");

let mut projects = if let Some(query) = &options.query {
load_from_query(project_graph, query, options)?
load_with_query(project_graph, query)?
} else {
load_with_regex(project_graph, options)?
};

if let Some(affected) = &options.affected {
debug!("Including affected projects");

for dep_id in affected.projects.keys() {
if !projects.contains_key(dep_id) {
projects.insert(dep_id.to_owned(), project_graph.get(dep_id)?);
}
}
debug!("Filtering based on affected");

projects = projects
.into_iter()
.filter_map(|project| {
if affected.is_project_affected(&project.id) {
Some(project)
} else {
None
}
})
.collect::<Vec<_>>();
}

Ok(projects.into_values().collect::<Vec<_>>())
Ok(projects)
}
2 changes: 1 addition & 1 deletion crates/cli/tests/query_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ mod projects {
let json: QueryProjectsResult = json::parse(assert.output()).unwrap();
let ids: Vec<String> = json.projects.iter().map(|p| p.id.to_string()).collect();

assert_eq!(ids, string_vec!["advanced", "noConfig"]);
assert_eq!(ids, string_vec!["noConfig"]);
assert!(json.options.affected.is_some());
}
}
Expand Down

0 comments on commit e3a9746

Please sign in to comment.