Skip to content

Commit e7bf5da

Browse files
authored
Run cargo fetch on workspace resolve (#1150)
commit-id:aeeb309e --- **Stack**: - #1161 - #1159 - #1157 - #1143 - #1155 - #1151 - #1150⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
1 parent c4cde23 commit e7bf5da

File tree

5 files changed

+77
-29
lines changed

5 files changed

+77
-29
lines changed

scarb/src/compiler/plugin/mod.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,35 @@ use std::fmt;
55
use anyhow::{anyhow, bail, Result};
66
use cairo_lang_semantic::plugin::PluginSuite;
77
use itertools::Itertools;
8+
use serde::{Deserialize, Serialize};
89

9-
use crate::core::PackageId;
10+
use crate::core::{Package, PackageId, TargetKind, Workspace};
1011

1112
use self::builtin::{BuiltinStarkNetPlugin, BuiltinTestPlugin};
1213

1314
pub mod builtin;
1415
pub mod proc_macro;
1516

17+
/// Properties that can be defined on Cairo plugin target.
18+
#[derive(Debug, Serialize, Deserialize, Default)]
19+
#[serde(rename_all = "kebab-case")]
20+
pub struct CairoPluginProps {
21+
/// Mark this macro plugin as builtin.
22+
/// Builtin plugins are assumed to be available in `CairoPluginRepository` for the whole Scarb execution.
23+
pub builtin: bool,
24+
}
25+
26+
pub fn fetch_cairo_plugin(package: &Package, ws: &Workspace<'_>) -> Result<()> {
27+
assert!(package.is_cairo_plugin());
28+
let target = package.fetch_target(&TargetKind::CAIRO_PLUGIN)?;
29+
let props: CairoPluginProps = target.props()?;
30+
// No need to fetch for buildin plugins.
31+
if !props.builtin {
32+
proc_macro::fetch_package(package, ws)?;
33+
}
34+
Ok(())
35+
}
36+
1637
pub trait CairoPlugin: Sync {
1738
fn id(&self) -> PackageId;
1839
fn instantiate(&self) -> Result<Box<dyn CairoPluginInstance>>;

scarb/src/compiler/plugin/proc_macro/compilation.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,24 @@ impl SharedLibraryProvider for Package {
4545
}
4646

4747
pub fn compile_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
48-
run_cargo(CargoAction::Build, unit, ws)
48+
let package = unit.components.first().unwrap().package.clone();
49+
run_cargo(CargoAction::Build, &package, ws)
4950
}
5051

5152
pub fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
52-
run_cargo(CargoAction::Check, unit, ws)
53+
let package = unit.components.first().unwrap().package.clone();
54+
run_cargo(CargoAction::Check, &package, ws)
5355
}
5456

55-
fn run_cargo(action: CargoAction, unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
56-
let main_package = unit.components.first().unwrap().package.clone();
57+
pub fn fetch_package(package: &Package, ws: &Workspace<'_>) -> Result<()> {
58+
run_cargo(CargoAction::Fetch, package, ws)
59+
}
60+
61+
fn run_cargo(action: CargoAction, package: &Package, ws: &Workspace<'_>) -> Result<()> {
5762
let cmd = CargoCommand {
5863
action,
59-
current_dir: main_package.root().to_path_buf(),
60-
target_dir: main_package
64+
current_dir: package.root().to_path_buf(),
65+
target_dir: package
6166
.target_path(ws.config())
6267
.path_unchecked()
6368
.to_path_buf(),
@@ -72,6 +77,7 @@ fn run_cargo(action: CargoAction, unit: CompilationUnit, ws: &Workspace<'_>) ->
7277
enum CargoAction {
7378
Build,
7479
Check,
80+
Fetch,
7581
}
7682

7783
struct CargoCommand {
@@ -85,12 +91,18 @@ impl From<CargoCommand> for Command {
8591
let mut cmd = Command::new("cargo");
8692
cmd.current_dir(args.current_dir);
8793
match args.action {
94+
CargoAction::Fetch => cmd.arg("fetch"),
8895
CargoAction::Build => cmd.arg("build"),
8996
CargoAction::Check => cmd.arg("check"),
9097
};
91-
cmd.arg("--release");
92-
cmd.arg("--target-dir");
93-
cmd.arg(args.target_dir);
98+
match args.action {
99+
CargoAction::Fetch => (),
100+
_ => {
101+
cmd.arg("--release");
102+
cmd.arg("--target-dir");
103+
cmd.arg(args.target_dir);
104+
}
105+
}
94106
cmd
95107
}
96108
}

scarb/src/compiler/plugin/proc_macro/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ pub mod compilation;
22
mod ffi;
33
mod host;
44

5-
pub use compilation::{check_unit, compile_unit};
5+
pub use compilation::{check_unit, compile_unit, fetch_package};
66
pub use ffi::*;
77
pub use host::*;

scarb/src/ops/resolve.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
use std::collections::HashMap;
2-
3-
use anyhow::{bail, Result};
4-
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
5-
use futures::TryFutureExt;
6-
use indoc::formatdoc;
7-
use itertools::Itertools;
8-
use serde::{Deserialize, Serialize};
9-
1+
use crate::compiler::plugin::{fetch_cairo_plugin, CairoPluginProps};
102
use crate::compiler::{CompilationUnit, CompilationUnitCairoPlugin, CompilationUnitComponent};
113
use crate::core::lockfile::Lockfile;
124
use crate::core::package::{Package, PackageClass, PackageId};
@@ -24,6 +16,12 @@ use crate::core::{
2416
use crate::internal::to_version::ToVersion;
2517
use crate::ops::lockfile::{read_lockfile, write_lockfile};
2618
use crate::{resolver, DEFAULT_SOURCE_PATH};
19+
use anyhow::{bail, Result};
20+
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
21+
use futures::TryFutureExt;
22+
use indoc::formatdoc;
23+
use itertools::Itertools;
24+
use std::collections::HashMap;
2725

2826
pub struct WorkspaceResolve {
2927
pub resolve: Resolve,
@@ -122,6 +120,12 @@ pub fn resolve_workspace_with_opts(
122120

123121
let packages = collect_packages_from_resolve_graph(&resolve, &patched).await?;
124122

123+
packages
124+
.values()
125+
.filter(|p| p.is_cairo_plugin())
126+
.map(|p| fetch_cairo_plugin(p, ws))
127+
.collect::<Result<Vec<()>>>()?;
128+
125129
Ok(WorkspaceResolve { resolve, packages })
126130
}
127131
.into_future(),
@@ -298,15 +302,6 @@ fn generate_cairo_compilation_units(
298302
.collect::<Result<Vec<CompilationUnit>>>()
299303
}
300304

301-
/// Properties that can be defined on Cairo plugin target.
302-
#[derive(Debug, Serialize, Deserialize, Default)]
303-
#[serde(rename_all = "kebab-case")]
304-
struct CairoPluginProps {
305-
/// Mark this macro plugin as builtin.
306-
/// Builtin plugins are assumed to be available in `CairoPluginRepository` for the whole Scarb execution.
307-
pub builtin: bool,
308-
}
309-
310305
pub struct PackageSolutionCollector<'a> {
311306
member: &'a Package,
312307
resolve: &'a WorkspaceResolve,

scarb/tests/build_cairo_plugin.rs

+20
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ fn check_cairo_plugin() {
145145
assert_matches(r#"[..]Finished release [optimized] target(s) in[..]"#, last);
146146
}
147147

148+
#[test]
149+
fn resolve_fetched_plugins() {
150+
let t = TempDir::new().unwrap();
151+
simple_project(&t);
152+
assert!(!t.child("Cargo.lock").exists());
153+
let output = Scarb::quick_snapbox()
154+
.arg("fetch")
155+
// Disable colors in Cargo output.
156+
.env("CARGO_TERM_COLOR", "never")
157+
.current_dir(&t)
158+
.output()
159+
.unwrap();
160+
assert!(
161+
output.status.success(),
162+
"{}",
163+
String::from_utf8_lossy(&output.stderr)
164+
);
165+
assert!(t.child("Cargo.lock").exists())
166+
}
167+
148168
#[test]
149169
fn compile_cairo_plugin_with_lib_target() {
150170
let t = TempDir::new().unwrap();

0 commit comments

Comments
 (0)