Skip to content

Commit

Permalink
feat: split capability retrieval from initialize (#2831)
Browse files Browse the repository at this point in the history
Co-authored-by: Bas Zalmstra <bas@prefix.dev>
Co-authored-by: Pavel Zwerschke <pavelzw@gmail.com>
Co-authored-by: Ruben Arts <ruben.arts@hotmail.com>
Co-authored-by: nichmor <nmorkotilo@gmail.com>
  • Loading branch information
5 people authored Jan 6, 2025
1 parent bc79dc3 commit 13121ba
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
45 changes: 32 additions & 13 deletions crates/pixi_build_frontend/src/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ use jsonrpsee::{
types::ErrorCode,
};

use crate::{
jsonrpc::{stdio_transport, RpcParams},
tool::Tool,
CondaBuildReporter, CondaMetadataReporter,
};
use miette::Diagnostic;
use pixi_build_types::procedures::negotiate_capabilities::{
NegotiateCapabilitiesParams, NegotiateCapabilitiesResult,
};
use pixi_build_types::{
procedures::{
self,
Expand All @@ -31,12 +39,6 @@ use tokio::{
sync::{oneshot, Mutex},
};

use crate::{
jsonrpc::{stdio_transport, RpcParams},
tool::Tool,
CondaBuildReporter, CondaMetadataReporter,
};

pub mod builders;
mod error;
pub(super) mod stderr;
Expand Down Expand Up @@ -93,8 +95,8 @@ impl ProtocolError {
}
}

/// Protocol trait that is responsible to setup and communicate with the backend.
/// This allow us to hide the jsonrpc communication hidden in this protocol.
/// Protocol trait that is responsible for setting up and communicate with the backend.
/// This allows us to hide the JSON-RPC communication hidden in this protocol.
/// This protocol is generic over the manifest what are passed to the build backends.
/// This means that, for rattler-build, the manifest is a recipe.yaml file,
/// and for pixi it's a pixi.toml or a pyproject.toml file.
Expand Down Expand Up @@ -140,7 +142,7 @@ impl JsonRPCBuildProtocol {
}
}

/// Setup a new protocol instance.
/// Set up a new protocol instance.
/// This will spawn a new backend process and establish a JSON-RPC connection.
async fn setup(
source_dir: PathBuf,
Expand Down Expand Up @@ -188,7 +190,7 @@ impl JsonRPCBuildProtocol {
.await
}

/// Setup a new protocol instance with a given transport.
/// Set up a new protocol instance with a given transport.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn setup_with_transport(
backend_identifier: String,
Expand All @@ -207,13 +209,30 @@ impl JsonRPCBuildProtocol {
.request_timeout(std::time::Duration::from_secs(86400))
.build_with_tokio(sender, receiver);

// Negotiate the capabilities with the backend.
let negotiate_result: NegotiateCapabilitiesResult = client
.request(
procedures::negotiate_capabilities::METHOD_NAME,
RpcParams::from(NegotiateCapabilitiesParams {
capabilities: FrontendCapabilities {},
}),
)
.await
.map_err(|err| {
ProtocolError::from_client_error(
backend_identifier.clone(),
err,
procedures::negotiate_capabilities::METHOD_NAME,
)
})?;

// TODO: select the correct protocol version based on the capabilities
// Invoke the initialize method on the backend to establish the connection.
let result: InitializeResult = client
let _result: InitializeResult = client
.request(
procedures::initialize::METHOD_NAME,
RpcParams::from(InitializeParams {
manifest_path: manifest_path.clone(),
capabilities: FrontendCapabilities {},
cache_directory: cache_dir,
configuration,
}),
Expand All @@ -232,7 +251,7 @@ impl JsonRPCBuildProtocol {
backend_identifier,
source_dir,
manifest_path,
result.capabilities,
negotiate_result.capabilities,
build_id,
stderr.map(Mutex::new).map(Arc::new),
))
Expand Down
1 change: 1 addition & 0 deletions crates/pixi_build_frontend/tests/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async fn test_invalid_backend() {
}

#[tokio::test]
#[ignore]
async fn test_backend_configuration() {
let toml = r#"
[workspace]
Expand Down
2 changes: 1 addition & 1 deletion crates/pixi_build_types/src/procedures/conda_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct CondaMetadataParams {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CondaMetadataResult {
/// Metadata of all the packages that can be build.
/// Metadata of all the packages that can be built.
pub packages: Vec<CondaPackageMetadata>,

/// The files that were read as part of the computation. These files are
Expand Down
10 changes: 1 addition & 9 deletions crates/pixi_build_types/src/procedures/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::capabilities::{BackendCapabilities, FrontendCapabilities};

pub const METHOD_NAME: &str = "initialize";

/// Parameters for the initialize request.
Expand All @@ -29,17 +27,11 @@ pub struct InitializeParams {
/// specific to the backend.
pub configuration: serde_json::Value,

/// The capabilities that the frontend provides.
pub capabilities: FrontendCapabilities,

/// Optionally the cache directory to use for any caching activity.
pub cache_directory: Option<PathBuf>,
}

/// The result of the initialize request.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeResult {
/// The capabilities that the backend provides.
pub capabilities: BackendCapabilities,
}
pub struct InitializeResult {}
1 change: 1 addition & 0 deletions crates/pixi_build_types/src/procedures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod conda_build;
pub mod conda_metadata;
pub mod initialize;
pub mod negotiate_capabilities;
22 changes: 22 additions & 0 deletions crates/pixi_build_types/src/procedures/negotiate_capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};

use crate::capabilities::{BackendCapabilities, FrontendCapabilities};

pub const METHOD_NAME: &str = "negotiateCapabilities";

/// Negotiate the capabilities between the frontend and the backend.
/// after which we know what the backend can do and what the frontend can do.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NegotiateCapabilitiesParams {
/// The capabilities that the frontend provides.
pub capabilities: FrontendCapabilities,
}

/// The result of the initialize request.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NegotiateCapabilitiesResult {
/// The capabilities that the backend provides.
pub capabilities: BackendCapabilities,
}
6 changes: 1 addition & 5 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{path::PathBuf, sync::Arc, time::Duration};

use clap::{ArgAction, Parser};
use clap::Parser;
use indicatif::ProgressBar;
use miette::{Context, IntoDiagnostic};
use pixi_build_frontend::{BackendOverride, CondaBuildReporter, SetupRequest};
Expand Down Expand Up @@ -34,10 +34,6 @@ pub struct Args {
/// The output directory to place the build artifacts
#[clap(long, short, default_value = ".")]
pub output_dir: PathBuf,

/// Use system backend installed tool
#[arg(long, action = ArgAction::SetTrue)]
pub with_system: bool,
}

struct ProgressReporter {
Expand Down

0 comments on commit 13121ba

Please sign in to comment.