Skip to content

Commit

Permalink
new: Add new workspace builder. (#1697)
Browse files Browse the repository at this point in the history
* Add workspace crate.

* Add projects loading.

* Add build.

* Clean up build data.

* Flesh out build.

* Add caching.

* Add to session.

* Use focused graphs.

* Add workspace mocker.

* Delete old graph builder.

* Polish.

* Fix tests.

* Fix tests.
  • Loading branch information
milesj committed Nov 25, 2024
1 parent af44d7d commit 798d0a8
Show file tree
Hide file tree
Showing 38 changed files with 1,308 additions and 1,151 deletions.
30 changes: 26 additions & 4 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion crates/action-graph/tests/action_graph_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,9 @@ mod action_graph {
}

#[tokio::test]
#[should_panic(expected = "No project has been configured with the name or alias unknown.")]
#[should_panic(
expected = "No project has been configured with the identifier or alias unknown."
)]
async fn errors_for_unknown_project() {
let sandbox = create_sandbox("tasks");
let container = ActionGraphContainer::new(sandbox.path()).await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ expression: graph.to_dot()
digraph {
0 [ label="SyncWorkspace" ]
1 [ label="SetupToolchain(system)" ]
2 [ label="SyncProject(system, server)" ]
3 [ label="RunTask(server:build)" ]
2 [ label="SyncProject(system, base)" ]
3 [ label="RunTask(base:build)" ]
4 [ label="SyncProject(system, client)" ]
5 [ label="SyncProject(system, common)" ]
6 [ label="SyncProject(system, base)" ]
5 [ label="SyncProject(system, server)" ]
6 [ label="SyncProject(system, common)" ]
7 [ label="RunTask(client:build)" ]
8 [ label="RunTask(common:build)" ]
9 [ label="RunTask(base:build)" ]
9 [ label="RunTask(server:build)" ]
1 -> 0 [ ]
2 -> 1 [ ]
3 -> 2 [ ]
6 -> 1 [ ]
5 -> 1 [ ]
5 -> 6 [ ]
6 -> 1 [ ]
6 -> 2 [ ]
4 -> 1 [ ]
4 -> 2 [ ]
4 -> 5 [ ]
8 -> 5 [ ]
4 -> 6 [ ]
8 -> 6 [ ]
9 -> 5 [ ]
7 -> 4 [ ]
7 -> 8 [ ]
7 -> 3 [ ]
9 -> 6 [ ]
7 -> 9 [ ]
}
1 change: 1 addition & 0 deletions crates/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ moon_task = { path = "../task" }
moon_toolchain = { path = "../toolchain" }
moon_toolchain_plugin = { path = "../toolchain-plugin" }
moon_vcs = { path = "../vcs" }
moon_workspace = { path = "../workspace" }
async-recursion = { workspace = true }
async-trait = { workspace = true }
bytes = "1.7.2"
Expand Down
4 changes: 2 additions & 2 deletions crates/app/src/commands/docker/scaffold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ async fn scaffold_sources_project(
}

debug!(
id = project_id.as_str(),
project_id = project_id.as_str(),
globs = ?include_globs,
"Copying sources from project {}",
color::id(project_id),
Expand All @@ -318,7 +318,7 @@ async fn scaffold_sources_project(
// they can be explicit in config or on the command line!
if !dep_cfg.is_root_scope() {
debug!(
id = project_id.as_str(),
project_id = project_id.as_str(),
dep_id = dep_cfg.id.as_str(),
"Including dependency project"
);
Expand Down
5 changes: 2 additions & 3 deletions crates/app/src/commands/node/run_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ pub async fn run_script(session: CliSession, args: RunScriptArgs) -> AppResult {

// Otherwise try and find the project in the graph
} else if let Some(project_id) = &args.project {
let mut project_graph = session.build_project_graph().await?;
project_graph.load(project_id).await?;
let project_graph = session.get_project_graph().await?;

command.cwd(&project_graph.build().await?.get(project_id)?.root);
command.cwd(&project_graph.get(project_id)?.root);

// This should rarely happen...
} else {
Expand Down
8 changes: 4 additions & 4 deletions crates/app/src/commands/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub struct ProjectArgs {

#[instrument(skip_all)]
pub async fn project(session: CliSession, args: ProjectArgs) -> AppResult {
let mut project_graph_builder = session.build_project_graph().await?;
project_graph_builder.load(&args.id).await?;

let project_graph = project_graph_builder.build().await?;
let project_graph = session
.get_project_graph()
.await?
.into_focused(&args.id, false)?;
let project = project_graph.get(&args.id)?;
let config = &project.config;

Expand Down
5 changes: 1 addition & 4 deletions crates/app/src/commands/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ pub async fn task(session: CliSession, args: TaskArgs) -> AppResult {
return Err(AppError::ProjectIdRequired.into());
};

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

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

Expand Down
10 changes: 5 additions & 5 deletions crates/app/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use moon_action_context::ActionContext;
use moon_action_graph::ActionGraph;
use moon_action_pipeline::ActionPipeline;
use moon_platform::PlatformManager;
use moon_project_graph::{
use moon_workspace::{
ExtendProjectData, ExtendProjectEvent, ExtendProjectGraphData, ExtendProjectGraphEvent,
ProjectGraphBuilderContext,
WorkspaceBuilderContext,
};
use starbase_events::{Emitter, EventState};
use std::sync::Arc;
Expand Down Expand Up @@ -53,10 +53,10 @@ pub async fn run_action_pipeline(
Ok(results)
}

pub async fn create_project_graph_context(
pub async fn create_workspace_graph_context(
session: &CliSession,
) -> miette::Result<ProjectGraphBuilderContext> {
let context = ProjectGraphBuilderContext {
) -> miette::Result<WorkspaceBuilderContext> {
let context = WorkspaceBuilderContext {
config_loader: &session.config_loader,
extend_project: Emitter::<ExtendProjectEvent>::new(),
extend_project_graph: Emitter::<ExtendProjectGraphEvent>::new(),
Expand Down
31 changes: 16 additions & 15 deletions crates/app/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ use moon_console_reporter::DefaultReporter;
use moon_env::MoonEnvironment;
use moon_extension_plugin::*;
use moon_plugin::{PluginHostData, PluginId};
use moon_project_graph::{ProjectGraph, ProjectGraphBuilder};
use moon_project_graph::ProjectGraph;
use moon_toolchain_plugin::*;
use moon_vcs::{BoxedVcs, Git};
use moon_workspace::WorkspaceBuilder;
use once_cell::sync::OnceCell;
use proto_core::ProtoEnvironment;
use semver::Version;
Expand Down Expand Up @@ -89,10 +90,6 @@ impl CliSession {
ActionGraphBuilder::new(project_graph)
}

pub async fn build_project_graph(&self) -> AppResult<ProjectGraphBuilder> {
ProjectGraphBuilder::new(create_project_graph_context(self).await?).await
}

pub fn get_app_context(&self) -> AppResult<Arc<AppContext>> {
Ok(Arc::new(AppContext {
cli_version: self.cli_version.clone(),
Expand Down Expand Up @@ -140,18 +137,11 @@ impl CliSession {
}

pub async fn get_project_graph(&self) -> AppResult<Arc<ProjectGraph>> {
if let Some(item) = self.project_graph.get() {
return Ok(Arc::clone(item));
if self.project_graph.get().is_none() {
self.load_workspace_graph().await?;
}

let cache_engine = self.get_cache_engine()?;
let context = create_project_graph_context(self).await?;
let builder = ProjectGraphBuilder::generate(context, &cache_engine).await?;
let graph = Arc::new(builder.build().await?);

let _ = self.project_graph.set(Arc::clone(&graph));

Ok(graph)
Ok(self.project_graph.get().map(Arc::clone).unwrap())
}

pub async fn get_toolchain_registry(&self) -> AppResult<Arc<ToolchainRegistry>> {
Expand Down Expand Up @@ -207,6 +197,17 @@ impl CliSession {
Commands::Bin(_) | Commands::Docker { .. } | Commands::Node { .. } | Commands::Teardown
)
}

async fn load_workspace_graph(&self) -> AppResult<()> {
let cache_engine = self.get_cache_engine()?;
let context = create_workspace_graph_context(self).await?;
let builder = WorkspaceBuilder::new_with_cache(context, &cache_engine).await?;
let result = builder.build().await?;

let _ = self.project_graph.set(Arc::new(result.project_graph));

Ok(())
}
}

#[async_trait]
Expand Down
2 changes: 1 addition & 1 deletion crates/cache-item/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use cache_mode::*;
#[macro_export]
macro_rules! cache_item {
($item:item) => {
#[derive(Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[derive(Debug, Default, /* Eq, */ PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default, rename_all = "camelCase")]
$item
};
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/action_graph_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod action_graph {
let dot = assert.output();

// Snapshot is not deterministic
assert_eq!(dot.split('\n').count(), 450);
assert_eq!(dot.split('\n').count(), 448);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/docker_file_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod dockerfile {
});

assert.inner.stderr(predicate::str::contains(
"No project has been configured with the name or alias missing.",
"No project has been configured with the identifier or alias missing.",
));
}

Expand Down
4 changes: 2 additions & 2 deletions crates/cli/tests/snapshots/project_test__unknown_project.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
source: crates/cli/tests/project_test.rs
expression: get_assert_stderr_output(&assert.inner)
---
Error: project_graph::unknown_project
Error: project_graph::unknown_id

× No project has been configured with the name or alias unknown.
× No project has been configured with the identifier or alias unknown.



Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
source: crates/cli/tests/run_test.rs
expression: assert.output()
---
Error: project_graph::unknown_project
Error: project_graph::unknown_id

× No project has been configured with the name or alias unknown.
× No project has been configured with the identifier or alias unknown.



Loading

0 comments on commit 798d0a8

Please sign in to comment.