Skip to content

Commit

Permalink
new: Update moon dep-graph to support task by path. (#1084)
Browse files Browse the repository at this point in the history
* Remove scope ID.

* Add locator enum.

* Rename variants.

* Add tests.

* Bump version.

* Reuse working dir.
  • Loading branch information
milesj authored Sep 29, 2023
1 parent 022618b commit 7865ed2
Show file tree
Hide file tree
Showing 24 changed files with 200 additions and 82 deletions.
9 changes: 9 additions & 0 deletions .yarn/versions/41ec5248.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
releases:
"@moonrepo/cli": patch
"@moonrepo/core-linux-arm64-gnu": patch
"@moonrepo/core-linux-arm64-musl": patch
"@moonrepo/core-linux-x64-gnu": patch
"@moonrepo/core-linux-x64-musl": patch
"@moonrepo/core-macos-arm64": patch
"@moonrepo/core-macos-x64": patch
"@moonrepo/core-windows-x64-msvc": patch
6 changes: 3 additions & 3 deletions crates/cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use clap::Args;
use moon::generate_project_graph;
use moon_common::Id;
use moon_project::Project;
use moon_target::TargetLocator;
use moon_workspace::Workspace;
use starbase::system;
use std::env;
use std::sync::Arc;
use tracing::trace;

Expand Down Expand Up @@ -45,7 +45,7 @@ pub async fn check(
} else if args.ids.is_empty() {
trace!("Loading from path");

projects.push(project_graph.get_from_path(env::current_dir().unwrap())?);
projects.push(project_graph.get_from_path(None)?);
} else {
trace!(
"Running for specific projects: {}",
Expand All @@ -67,7 +67,7 @@ pub async fn check(
for project in projects {
for task in project.get_tasks()? {
if task.is_build_type() || task.is_test_type() {
targets.push(task.target.id.clone());
targets.push(TargetLocator::Qualified(task.target.clone()));
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions crates/cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use moon_workspace::Workspace;
use rustc_hash::FxHashMap;
use starbase::{system, AppResult};
use starbase_styles::color;
use std::env;
use std::fmt::Display;
use tracing::{debug, warn};

Expand Down Expand Up @@ -314,7 +313,6 @@ fn gather_variables(
pub async fn generate(args: ArgsRef<GenerateArgs>, workspace: ResourceRef<Workspace>) {
let generator = CodeGenerator::new(&workspace.root, &workspace.config.generator);
let theme = create_theme();
let cwd = env::current_dir().into_diagnostic()?;

// This is a special case for creating a new template with the generator itself!
if args.template {
Expand Down Expand Up @@ -364,15 +362,15 @@ pub async fn generate(args: ArgsRef<GenerateArgs>, workspace: ResourceRef<Worksp
.into_diagnostic()?
}
});
let dest = relative_dest.to_logical_path(&cwd);
let dest = relative_dest.to_logical_path(&workspace.working_dir);

debug!(dest = ?dest, "Destination path set");

// Gather variables and build context
let mut context = gather_variables(&template, &theme, args)?;
context.insert("dest_dir", &dest);
context.insert("dest_rel_dir", &relative_dest);
context.insert("working_dir", &cwd);
context.insert("working_dir", &workspace.working_dir);
context.insert("workspace_root", &workspace.root);

// Load template files and determine when to overwrite
Expand Down Expand Up @@ -454,7 +452,7 @@ pub async fn generate(args: ArgsRef<GenerateArgs>, workspace: ResourceRef<Worksp
},
color::muted_light(
file.dest_path
.strip_prefix(&cwd)
.strip_prefix(&workspace.working_dir)
.unwrap_or(&file.dest_path)
.to_str()
.unwrap()
Expand Down
13 changes: 6 additions & 7 deletions crates/cli/src/commands/graph/dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::commands::graph::utils::{dep_graph_repr, respond_to_request, setup_se
use clap::Args;
use miette::IntoDiagnostic;
use moon::{build_dep_graph, generate_project_graph};
use moon_target::Target;
use moon_target::TargetLocator;
use moon_workspace::Workspace;
use starbase::system;

#[derive(Args, Clone, Debug)]
pub struct DepGraphArgs {
#[arg(help = "Target to *only* graph")]
target: Option<String>,
target: Option<TargetLocator>,

#[arg(long, help = "Print the graph in DOT format")]
dot: bool,
Expand All @@ -24,11 +24,10 @@ pub async fn dep_graph(args: ArgsRef<DepGraphArgs>, workspace: ResourceMut<Works
let mut dep_builder = build_dep_graph(&project_graph);

// Focus a target and its dependencies/dependents
if let Some(id) = &args.target {
let target = Target::parse(id)?;

dep_builder.run_target(&target, None)?;
dep_builder.run_dependents_for_target(&target)?;
if let Some(locator) = args.target.clone() {
for target in dep_builder.run_targets_by_locator(&[locator], None)? {
dep_builder.run_dependents_for_target(&target)?;
}

// Show all targets and actions
} else {
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/src/commands/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use moon_rust_lang::CARGO;
use moon_terminal::{create_theme, safe_exit};
use moon_utils::path;
use moon_vcs::{Git, Vcs};
use moon_workspace::WorkspaceError;
use node::init_node;
use rust::init_rust;
use starbase::{system, AppResult};
Expand Down Expand Up @@ -166,7 +167,7 @@ pub async fn init(args: ArgsRef<InitArgs>) {
};

let theme = create_theme();
let working_dir = env::current_dir().expect("Failed to determine working directory.");
let working_dir = env::current_dir().map_err(|_| WorkspaceError::MissingWorkingDir)?;
let dest_path = PathBuf::from(&args.dest);
let dest_dir = if args.dest == "." {
working_dir
Expand Down
15 changes: 8 additions & 7 deletions crates/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use moon_action_context::{ActionContext, ProfileType};
use moon_action_pipeline::Pipeline;
use moon_common::is_test_env;
use moon_project_graph::ProjectGraph;
use moon_target::TargetLocator;
use moon_utils::is_ci;
use moon_workspace::Workspace;
use rustc_hash::FxHashSet;
Expand All @@ -22,8 +23,8 @@ const HEADING_DEBUGGING: &str = "Debugging";

#[derive(Args, Clone, Debug, Default)]
pub struct RunArgs {
#[arg(required = true, help = "List of targets (scope:task) to run")]
pub targets: Vec<String>,
#[arg(required = true, help = "List of targets to run")]
pub targets: Vec<TargetLocator>,

#[arg(
long,
Expand Down Expand Up @@ -103,7 +104,7 @@ pub fn is_local(args: &RunArgs) -> bool {
}

pub async fn run_target(
target_ids: &[String],
target_locators: &[TargetLocator],
args: &RunArgs,
concurrency: Option<usize>,
workspace: &Workspace,
Expand Down Expand Up @@ -142,8 +143,8 @@ pub async fn run_target(
}

// Run targets, optionally based on affected files
let primary_targets = dep_builder.run_targets_by_id(
target_ids,
let primary_targets = dep_builder.run_targets_by_locator(
target_locators,
if should_run_affected {
Some(&touched_files)
} else {
Expand All @@ -152,7 +153,7 @@ pub async fn run_target(
)?;

if primary_targets.is_empty() {
let targets_list = map_list(target_ids, |id| color::label(id));
let targets_list = map_list(target_locators, |id| color::label(id));

if should_run_affected {
let status_list = if args.status.is_empty() {
Expand Down Expand Up @@ -192,7 +193,7 @@ pub async fn run_target(
// Process all tasks in the graph
let context = ActionContext {
affected_only: should_run_affected,
initial_targets: FxHashSet::from_iter(target_ids.to_owned()),
initial_targets: FxHashSet::from_iter(target_locators.to_owned()),
interactive: args.interactive,
passthrough_args: args.passthrough.to_owned(),
primary_targets: FxHashSet::from_iter(primary_targets),
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/src/commands/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Args;
use console::Term;
use miette::{miette, IntoDiagnostic};
use moon::build_project_graph;
use moon_target::Target;
use moon_target::{Target, TargetScope};
use moon_terminal::{ExtendedTerm, Label};
use moon_workspace::Workspace;
use starbase::system;
Expand All @@ -19,15 +19,15 @@ pub struct TaskArgs {

#[system]
pub async fn task(args: ArgsRef<TaskArgs>, workspace: ResourceMut<Workspace>) {
let Some(project_locator) = args.target.scope_id.clone() else {
let TargetScope::Project(project_locator) = &args.target.scope else {
return Err(miette!("A project ID is required."));
};

let mut project_graph_builder = build_project_graph(workspace).await?;
project_graph_builder.load(&project_locator).await?;
project_graph_builder.load(project_locator).await?;

let project_graph = project_graph_builder.build().await?;
let project = project_graph.get(&project_locator)?;
let project = project_graph.get(project_locator)?;
let task = project.get_task(&args.target.task_id)?;

if args.json {
Expand Down
19 changes: 19 additions & 0 deletions crates/cli/tests/dep_graph_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ fn focused_by_target() {
assert_snapshot!(assert.output());
}

#[test]
fn focused_by_task_in_cwd() {
let (workspace_config, toolchain_config, tasks_config) = get_tasks_fixture_configs();

let sandbox = create_sandbox_with_config(
"tasks",
Some(workspace_config),
Some(toolchain_config),
Some(tasks_config),
);

let assert = sandbox.run_moon(|cmd| {
cmd.arg("dep-graph").arg("--dot").arg("lint");
cmd.current_dir(sandbox.path().join("basic"));
});

assert_snapshot!(assert.output());
}

#[test]
fn includes_dependencies_when_focused() {
let (workspace_config, toolchain_config, tasks_config) = get_tasks_fixture_configs();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/cli/tests/dep_graph_test.rs
expression: assert.output()
---
digraph {
0 [ label="SyncWorkspace" style=filled, shape=oval, fillcolor=gray, fontcolor=black]
1 [ label="SetupNodeTool(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black]
2 [ label="InstallNodeDeps(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black]
3 [ label="SyncNodeProject(basic)" style=filled, shape=oval, fillcolor=gray, fontcolor=black]
4 [ label="RunTarget(basic:lint)" style=filled, shape=oval, fillcolor=gray, fontcolor=black]
2 -> 1 [ arrowhead=box, arrowtail=box]
3 -> 1 [ arrowhead=box, arrowtail=box]
4 -> 2 [ arrowhead=box, arrowtail=box]
4 -> 3 [ arrowhead=box, arrowtail=box]
}



8 changes: 4 additions & 4 deletions crates/core/action-context/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::ValueEnum;
use moon_common::path::WorkspaceRelativePathBuf;
use moon_target::Target;
use moon_target::{Target, TargetLocator};
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
Expand Down Expand Up @@ -31,7 +31,7 @@ impl TargetState {
pub struct ActionContext {
pub affected_only: bool,

pub initial_targets: FxHashSet<String>,
pub initial_targets: FxHashSet<TargetLocator>,

pub interactive: bool,

Expand Down Expand Up @@ -62,8 +62,8 @@ impl ActionContext {
}

// :task == scope:task
for initial_target in &self.initial_targets {
if target.is_all_task(initial_target) {
for locator in &self.initial_targets {
if target.is_all_task(locator.as_str()) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/action-pipeline/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub async fn process_action(
ActionNode::RunTarget(runtime, target)
| ActionNode::RunInteractiveTarget(runtime, target)
| ActionNode::RunPersistentTarget(runtime, target) => {
let project = local_project_graph.get(target.scope_id.as_ref().unwrap())?;
let project = local_project_graph.get(target.get_project_id().unwrap())?;

local_emitter.emit(Event::TargetRunning { target }).await?;

Expand Down
43 changes: 19 additions & 24 deletions crates/core/dep-graph/src/dep_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use moon_platform::{PlatformManager, Runtime};
use moon_project::Project;
use moon_project_graph::ProjectGraph;
use moon_query::{build_query, Criteria};
use moon_target::{Target, TargetError, TargetScope};
use moon_target::{Target, TargetError, TargetLocator, TargetScope};
use moon_task::Task;
use petgraph::graph::NodeIndex;
use petgraph::Graph;
Expand Down Expand Up @@ -386,35 +386,30 @@ impl<'ws> DepGraphBuilder<'ws> {
Ok(indexes)
}

pub fn run_targets_by_id(
pub fn run_targets_by_locator(
&mut self,
target_ids: &[String],
target_locators: &[TargetLocator],
touched_files: Option<&TouchedFilePaths>,
) -> miette::Result<Vec<Target>> {
let mut qualified_targets = vec![];
let mut project_targets = vec![];

for target_id in target_ids {
// Target (with possible scope) provided
if target_id.contains(':') {
qualified_targets
.extend(self.run_target(Target::parse(target_id)?, touched_files)?.0);
// Task name provided, find closest project
} else {
project_targets.push(target_id);
}
}
let mut project = None;

for locator in target_locators {
let result = match locator {
TargetLocator::Qualified(target) => self.run_target(target, touched_files)?,
TargetLocator::TaskFromWorkingDir(task_id) => {
if project.is_none() {
project = Some(self.project_graph.get_from_path(None)?);
}

if !project_targets.is_empty() {
let cwd = std::env::current_dir().unwrap();
let project = self.project_graph.get_from_path(&cwd)?;
self.run_target(
Target::new(&project.as_ref().unwrap().id, task_id)?,
touched_files,
)?
}
};

for target_id in project_targets {
qualified_targets.extend(
self.run_target(Target::new(&project.id, target_id)?, touched_files)?
.0,
);
}
qualified_targets.extend(result.0);
}

Ok(qualified_targets)
Expand Down
1 change: 1 addition & 0 deletions crates/core/moon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub async fn create_project_graph_context(workspace: &Workspace) -> ProjectGraph
inherited_tasks: &workspace.tasks_config,
toolchain_config: &workspace.toolchain_config,
vcs: Some(&workspace.vcs),
working_dir: &workspace.working_dir,
workspace_config: &workspace.config,
workspace_root: &workspace.root,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/core/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl<'a> Runner<'a> {
}
}
TargetScope::Project(project_locator) => {
if let Some(owner_id) = &task.target.scope_id {
if let Some(owner_id) = task.target.get_project_id() {
if owner_id == project_locator && is_matching_task {
return Ok(true);
}
Expand Down
2 changes: 1 addition & 1 deletion nextgen/process/src/command_inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<'cmd> CommandInspector<'cmd> {
let command_line = self.get_command_line();
let workspace_root = env::var("MOON_WORKSPACE_ROOT")
.map(PathBuf::from)
.unwrap_or_else(|_| env::current_dir().unwrap());
.unwrap_or_else(|_| env::current_dir().unwrap_or_default());

if self.command.print_command {
println!(
Expand Down
Loading

0 comments on commit 7865ed2

Please sign in to comment.