Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate warnings #68

Merged
merged 3 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cargo-near/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) enum AbiCompression {
pub(crate) fn generate_abi(
crate_metadata: &CrateMetadata,
generate_docs: bool,
hide_warnings: bool,
) -> anyhow::Result<AbiRoot> {
let root_node = crate_metadata
.raw_metadata
Expand Down Expand Up @@ -82,6 +83,7 @@ pub(crate) fn generate_abi(
("CARGO_PROFILE_DEV_LTO", "off"),
],
util::dylib_extension(),
hide_warnings,
)?;

let mut contract_abi = util::handle_step("Extracting ABI...", || {
Expand Down Expand Up @@ -181,7 +183,7 @@ pub(crate) fn run(args: AbiCommand) -> anyhow::Result<()> {
} else {
AbiFormat::Json
};
let contract_abi = generate_abi(&crate_metadata, args.doc)?;
let contract_abi = generate_abi(&crate_metadata, args.doc, false)?;
let AbiResult { path } =
write_to_file(&contract_abi, &crate_metadata, format, AbiCompression::NoOp)?;

Expand Down
3 changes: 2 additions & 1 deletion cargo-near/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) fn run(args: BuildCommand) -> anyhow::Result<()> {
let mut pretty_abi_path = None;
let mut min_abi_path = None;
if !args.no_abi {
let contract_abi = abi::generate_abi(&crate_metadata, args.doc)?;
let contract_abi = abi::generate_abi(&crate_metadata, args.doc, true)?;
let AbiResult { path } = abi::write_to_file(
&contract_abi,
&crate_metadata,
Expand Down Expand Up @@ -71,6 +71,7 @@ pub(crate) fn run(args: BuildCommand) -> anyhow::Result<()> {
&cargo_args,
build_env,
"wasm",
false,
)?;

wasm_artifact.path = util::copy(&wasm_artifact.path, &out_dir)?;
Expand Down
47 changes: 36 additions & 11 deletions cargo-near/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cargo::manifest::CargoManifestPath;
use anyhow::{Context, Result};
use cargo_metadata::{Artifact, Message};
use std::collections::HashSet;
use std::collections::{BTreeMap, HashSet};
use std::ffi::OsStr;
use std::fs;
use std::io::{BufRead, BufReader};
Expand Down Expand Up @@ -31,23 +31,24 @@ pub(crate) const fn dylib_extension() -> &'static str {
/// If `working_dir` is set, cargo process will be spawned in the specified directory.
///
/// Returns execution standard output as a byte array.
pub(crate) fn invoke_cargo<I, S, P>(
pub(crate) fn invoke_cargo<A, P, E, S, EK, EV>(
command: &str,
args: I,
args: A,
working_dir: Option<P>,
env: Vec<(&str, &str)>,
env: E,
) -> Result<Vec<Artifact>>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
A: IntoIterator<Item = S>,
P: AsRef<Path>,
E: IntoIterator<Item = (EK, EV)>,
S: AsRef<OsStr>,
EK: AsRef<OsStr>,
EV: AsRef<OsStr>,
{
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let mut cmd = Command::new(cargo);

env.iter().for_each(|(env_key, env_val)| {
cmd.env(env_key, env_val);
});
cmd.envs(env);

if let Some(path) = working_dir {
log::debug!("Setting cargo working dir to '{}'", path.as_ref().display());
Expand Down Expand Up @@ -156,14 +157,38 @@ pub struct CompilationArtifact {
pub(crate) fn compile_project(
manifest_path: &CargoManifestPath,
args: &[&str],
env: Vec<(&str, &str)>,
mut env: Vec<(&str, &str)>,
artifact_extension: &str,
hide_warnings: bool,
) -> anyhow::Result<CompilationArtifact> {
let mut final_env = BTreeMap::new();

if hide_warnings {
env.push(("RUSTFLAGS", "-Awarnings"));
}

for (key, value) in env {
match key {
"RUSTFLAGS" => {
let rustflags: &mut String = final_env
.entry(key)
.or_insert_with(|| std::env::var(key).unwrap_or_default());
if !rustflags.is_empty() {
rustflags.push(' ');
}
rustflags.push_str(value);
}
_ => {
final_env.insert(key, value.to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious, why use a map instead of just a vec of (K, V) if you're not checking the duplication of keys on insert?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, actually, for RUSTFLAGS, we're checking to ensure we don't replace previous entries, but instead concatenate them. So it's here to allow us quickly index the map, instead of having to iterate the whole Vec<(K, V)>.

}
}
}

let artifacts = invoke_cargo(
"build",
[&["--message-format=json"], args].concat(),
manifest_path.directory().ok(),
env,
final_env.iter(),
)?;

// We find the last compiler artifact message which should contain information about the
Expand Down