Skip to content

Commit

Permalink
closes #3054
Browse files Browse the repository at this point in the history
passes cargo args to idl generation
  • Loading branch information
biryukovmaxim committed Jun 27, 2024
1 parent 70d4f48 commit 3063167
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
38 changes: 28 additions & 10 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ pub enum IdlCommand {
/// Do not check for safety comments
#[clap(long)]
skip_lint: bool,
/// Arguments to pass to the underlying `cargo test` command
#[clap(required = false, last = true)]
cargo_args: Vec<String>,
},
/// Fetches an IDL for the given address from a cluster.
/// The address can be a program, IDL account, or IDL buffer.
Expand Down Expand Up @@ -1274,7 +1277,6 @@ pub fn build(
if let Some(program_name) = program_name.as_ref() {
cd_member(cfg_override, program_name)?;
}

let cfg = Config::discover(cfg_override)?.expect("Not in workspace.");
let cfg_parent = cfg.path().parent().expect("Invalid Anchor.toml");

Expand Down Expand Up @@ -1523,7 +1525,7 @@ fn build_cwd_verifiable(
stdout,
stderr,
env_vars,
cargo_args,
cargo_args.clone(),
arch,
);

Expand All @@ -1534,7 +1536,7 @@ fn build_cwd_verifiable(
Ok(_) => {
// Build the idl.
println!("Extracting the IDL");
let idl = generate_idl(cfg, skip_lint, no_docs)?;
let idl = generate_idl(cfg, skip_lint, no_docs, &cargo_args)?;
// Write out the JSON file.
println!("Writing the IDL file");
let out_file = workspace_dir.join(format!("target/idl/{}.json", idl.metadata.name));
Expand Down Expand Up @@ -1820,18 +1822,17 @@ fn _build_rust_cwd(
let subcommand = arch.build_subcommand();
let exit = std::process::Command::new("cargo")
.arg(subcommand)
.args(cargo_args)
.args(cargo_args.clone())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.map_err(|e| anyhow::format_err!("{}", e.to_string()))?;
if !exit.status.success() {
std::process::exit(exit.status.code().unwrap_or(1));
}

// Generate IDL
if !no_idl {
let idl = generate_idl(cfg, skip_lint, no_docs)?;
let idl = generate_idl(cfg, skip_lint, no_docs, &cargo_args)?;

// JSON out path.
let out = match idl_out {
Expand Down Expand Up @@ -1984,7 +1985,7 @@ fn verify(
None,
None,
env_vars,
cargo_args,
cargo_args.clone(),
false,
arch,
)?;
Expand All @@ -2008,7 +2009,7 @@ fn verify(
}

// Verify IDL (only if it's not a buffer account).
let local_idl = generate_idl(&cfg, true, false)?;
let local_idl = generate_idl(&cfg, true, false, &cargo_args)?;
if bin_ver.state != BinVerificationState::Buffer {
let deployed_idl = fetch_idl(cfg_override, program_id)?;
if local_idl != deployed_idl {
Expand Down Expand Up @@ -2215,7 +2216,16 @@ fn idl(cfg_override: &ConfigOverride, subcmd: IdlCommand) -> Result<()> {
out_ts,
no_docs,
skip_lint,
} => idl_build(cfg_override, program_name, out, out_ts, no_docs, skip_lint),
cargo_args,
} => idl_build(
cfg_override,
program_name,
out,
out_ts,
no_docs,
skip_lint,
cargo_args,
),
IdlCommand::Fetch { address, out } => idl_fetch(cfg_override, address, out),
IdlCommand::Convert { path, out } => idl_convert(path, out),
IdlCommand::Type { path, out } => idl_type(path, out),
Expand Down Expand Up @@ -2657,6 +2667,7 @@ fn idl_build(
out_ts: Option<String>,
no_docs: bool,
skip_lint: bool,
cargo_args: Vec<String>,
) -> Result<()> {
let cfg = Config::discover(cfg_override)?.expect("Not in workspace");
let program_path = match program_name {
Expand All @@ -2675,6 +2686,7 @@ fn idl_build(
cfg.features.resolution,
cfg.features.skip_lint || skip_lint,
no_docs,
&cargo_args,
)?;
let out = match out {
Some(path) => OutFile::File(PathBuf::from(path)),
Expand All @@ -2690,7 +2702,12 @@ fn idl_build(
}

/// Generate IDL with method decided by whether manifest file has `idl-build` feature or not.
fn generate_idl(cfg: &WithPath<Config>, skip_lint: bool, no_docs: bool) -> Result<Idl> {
fn generate_idl(
cfg: &WithPath<Config>,
skip_lint: bool,
no_docs: bool,
cargo_args: &[String],
) -> Result<Idl> {
// Check whether the manifest has `idl-build` feature
let manifest = Manifest::discover()?.ok_or_else(|| anyhow!("Cargo.toml not found"))?;
let is_idl_build = manifest
Expand Down Expand Up @@ -2721,6 +2738,7 @@ in `{path}`."#
cfg.features.resolution,
cfg.features.skip_lint || skip_lint,
no_docs,
cargo_args,
)
}

Expand Down
38 changes: 25 additions & 13 deletions idl/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ pub fn build_idl(
resolution: bool,
skip_lint: bool,
no_docs: bool,
cargo_args: &[String],
) -> Result<Idl> {
let idl = build(program_path.as_ref(), resolution, skip_lint, no_docs)?;
let idl = build(
program_path.as_ref(),
resolution,
skip_lint,
no_docs,
cargo_args,
)?;
let idl = convert_module_paths(idl);
let idl = sort(idl);
verify(&idl)?;
Expand All @@ -59,25 +66,30 @@ pub fn build_idl(
}

/// Build IDL.
fn build(program_path: &Path, resolution: bool, skip_lint: bool, no_docs: bool) -> Result<Idl> {
fn build(
program_path: &Path,
resolution: bool,
skip_lint: bool,
no_docs: bool,
cargo_args: &[String],
) -> Result<Idl> {
// `nightly` toolchain is currently required for building the IDL.
let toolchain = std::env::var("RUSTUP_TOOLCHAIN")
.map(|toolchain| format!("+{}", toolchain))
.unwrap_or_else(|_| "+nightly".to_string());

install_toolchain_if_needed(&toolchain)?;

let mut args = vec![
&toolchain,
"test",
"__anchor_private_print_idl",
"--features",
"idl-build",
];
args.extend(cargo_args.iter().map(String::as_str));
args.extend(["--", "--show-output", "--quiet"]);
let output = Command::new("cargo")
.args([
&toolchain,
"test",
"__anchor_private_print_idl",
"--features",
"idl-build",
"--",
"--show-output",
"--quiet",
])
.args(args)
.env(
"ANCHOR_IDL_BUILD_NO_DOCS",
if no_docs { "TRUE" } else { "FALSE" },
Expand Down

0 comments on commit 3063167

Please sign in to comment.