Skip to content

Commit

Permalink
git(source-scan): resolve conflicts with main (#162)
Browse files Browse the repository at this point in the history
commit
ca13500
was lost in merge of #159, as it
was accidentally not pushed to remote before merge,
which was discovered when testing on
[contract](https://github.com/dj8yfo/sample_no_workspace/blob/d42d1c69a3883889c4bb688b11e0a8dffb384a15/Cargo.toml)

---

SIDENOTE: commit
787c3a4
fixes somewhat unrelated problem, also present in `main` .
It can be cherry-picked into a standalone pr to `main` :

as `cargo metadata` is called without any additional args (`--features`
or whatnot), the following diff:

```diff
-near-sdk = { version = "5.1.0", features = ["legacy"], git = "https://github.com/dj8yfo/near-sdk-rs.git", branch = "add_contract_metadata" }
+near-sdk = { version = "5.1.0", default-features = false, features = ["wee_alloc", "legacy"], git = "https://github.com/dj8yfo/near-sdk-rs.git", branch = "add_contract_metadata" }
```
results in 
```bash
Error:
   0: `near-sdk` dependency must have the `abi` feature enabled
```
which somewhat contradicts [no explicit `abi`
feature](https://github.com/near-examples/factory-rust/blob/main/Cargo.toml#L16)
, which is promoted for use by
[near-docs](https://github.com/near-examples/factory-rust/pull/2/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R74)

---------

Co-authored-by: Jacob Lindahl <encody@users.noreply.github.com>
  • Loading branch information
dj8yfo and encody committed May 13, 2024
1 parent bf8696b commit 55d9a4f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 84 deletions.
50 changes: 26 additions & 24 deletions cargo-near/src/commands/abi_command/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub(crate) fn generate_abi(
no_locked: bool,
generate_docs: bool,
hide_warnings: bool,
cargo_feature_args: &[&str],
color: ColorPreference,
) -> color_eyre::eyre::Result<AbiRoot> {
let root_node = crate_metadata
Expand Down Expand Up @@ -62,38 +63,32 @@ pub(crate) fn generate_abi(

for required_feature in ["__abi-generate", "__abi-embed"] {
if !near_sdk_dep.features.contains_key(required_feature) {
color_eyre::eyre::bail!("unsupported `near-sdk` version. expected 4.1.* or higher");
color_eyre::eyre::bail!(
"{}: {}",
format!(
"missing `{}` required feature for `near-sdk` dependency",
required_feature
),
"probably unsupported `near-sdk` version. expected 4.1.* or higher"
);
}
}

let near_sdk_metadata = crate_metadata
.root_package
.dependencies
.iter()
.find(|dep| dep.name == "near-sdk")
.wrap_err("`near-sdk` dependency not found")?;

// `Dependency::features` return value does not contain default features, so we have to check
// for default features separately.
if !near_sdk_metadata.uses_default_features
&& !near_sdk_metadata
.features
.iter()
.any(|feature| feature == "abi")
{
color_eyre::eyre::bail!("`near-sdk` dependency must have the `abi` feature enabled")
}
let cargo_args = {
let mut args = vec!["--features", "near-sdk/__abi-generate"];
args.extend_from_slice(cargo_feature_args);
if !no_locked {
args.push("--locked");
}

let mut cargo_args = vec!["--features", "near-sdk/__abi-generate"];
args
};

if !no_locked {
cargo_args.push("--locked");
}
util::print_step("Generating ABI");

let dylib_artifact = util::compile_project(
&crate_metadata.manifest_path,
&cargo_args,
cargo_args.as_slice(),
vec![
("CARGO_PROFILE_DEV_OPT_LEVEL", "0"),
("CARGO_PROFILE_DEV_DEBUG", "0"),
Expand Down Expand Up @@ -209,7 +204,14 @@ pub fn run(args: super::AbiCommand) -> near_cli_rs::CliResult {
} else {
AbiFormat::Json
};
let contract_abi = generate_abi(&crate_metadata, args.no_locked, !args.no_doc, false, color)?;
let contract_abi = generate_abi(
&crate_metadata,
args.no_locked,
!args.no_doc,
false,
&[],
color,
)?;
let AbiResult { path } =
write_to_file(&contract_abi, &crate_metadata, format, AbiCompression::NoOp)?;

Expand Down
16 changes: 16 additions & 0 deletions cargo-near/src/commands/build_command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ pub(super) fn run(

let mut build_env = vec![("RUSTFLAGS", "-C link-arg=-s")];
let mut cargo_args = vec!["--target", COMPILATION_TARGET];
let cargo_feature_args = {
let mut feat_args = vec![];
if let Some(features) = args.features.as_ref() {
feat_args.extend(&["--features", features]);
}

if args.no_default_features {
feat_args.push("--no-default-features");
}
feat_args
};

if !args.no_release {
cargo_args.push("--release");
}
Expand All @@ -69,6 +81,7 @@ pub(super) fn run(
args.no_locked,
!args.no_doc,
true,
&cargo_feature_args,
color.clone(),
)?;
contract_abi.metadata.build = Some(BuildInfo {
Expand All @@ -91,10 +104,13 @@ pub(super) fn run(
abi = Some(contract_abi);
}

cargo_args.extend(cargo_feature_args);

if let (false, Some(abi_path)) = (args.no_embed_abi, &min_abi_path) {
cargo_args.extend(&["--features", "near-sdk/__abi-embed"]);
build_env.push(("CARGO_NEAR_ABI_PATH", abi_path.as_str()));
}

util::print_step("Building contract");
let mut wasm_artifact = util::compile_project(
&crate_metadata.manifest_path,
Expand Down
37 changes: 25 additions & 12 deletions cargo-near/src/commands/build_command/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ impl super::BuildCommand {
manifest_command: Option<String>,
) -> color_eyre::eyre::Result<String> {
if let Some(cargo_cmd) = manifest_command {
if self.no_default_features {
return Err(color_eyre::eyre::eyre!(format!(
"`{}` {}",
"--no-default-features",
Self::BUILD_COMMAND_CLI_CONFIG_ERR
)));
}
if self.features.is_some() {
return Err(color_eyre::eyre::eyre!(format!(
"`{}` {}",
"--features",
Self::BUILD_COMMAND_CLI_CONFIG_ERR
)));
}
if self.no_abi {
return Err(color_eyre::eyre::eyre!(format!(
"`{}` {}",
Expand Down Expand Up @@ -221,6 +235,12 @@ impl super::BuildCommand {
);
let mut cargo_args = vec![];

if self.no_default_features {
cargo_args.push("--no-default-features");
}
if let Some(ref features) = self.features {
cargo_args.extend(&["--features", features]);
}
if self.no_abi {
cargo_args.push("--no-abi");
}
Expand Down Expand Up @@ -314,7 +334,7 @@ const RUST_LOG_EXPORT: &str = "RUST_LOG=cargo_near=info";

struct Nep330BuildInfo {
build_environment: String,
contract_path: Option<String>,
contract_path: String,
source_code_snapshot: source_id::SourceId,
}

Expand All @@ -330,11 +350,6 @@ impl Nep330BuildInfo {
.to_str()
.wrap_err("non UTF-8 unix path computed as contract path")?
.to_string();
let contract_path = if contract_path.is_empty() {
None
} else {
Some(contract_path)
};

let source_code_snapshot = source_id::SourceId::for_git(
&docker_build_meta.source_code_git_url,
Expand All @@ -360,12 +375,10 @@ impl Nep330BuildInfo {
),
];

if let Some(ref contract_path) = self.contract_path {
result.extend(vec![
"--env".to_string(),
format!("{}={}", CONTRACT_PATH_ENV_KEY, contract_path),
]);
}
result.extend(vec![
"--env".to_string(),
format!("{}={}", CONTRACT_PATH_ENV_KEY, self.contract_path),
]);

result
}
Expand Down
12 changes: 12 additions & 0 deletions cargo-near/src/commands/build_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ pub struct BuildCommand {
#[interactive_clap(long)]
#[interactive_clap(skip_interactive_input)]
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
/// Set compile-time feature flags.
#[interactive_clap(long)]
#[interactive_clap(skip_interactive_input)]
pub features: Option<String>,
/// Disables default feature flags.
#[interactive_clap(long)]
#[interactive_clap(skip_interactive_input)]
pub no_default_features: bool,
/// Coloring: auto, always, never
#[interactive_clap(long)]
#[interactive_clap(value_enum)]
Expand Down Expand Up @@ -116,6 +124,8 @@ impl From<CliBuildCommand> for BuildCommand {
no_abi: value.no_abi,
no_embed_abi: value.no_embed_abi,
no_doc: value.no_doc,
features: value.features,
no_default_features: value.no_default_features,
out_dir: value.out_dir,
manifest_path: value.manifest_path,
color: value.color,
Expand All @@ -140,6 +150,8 @@ impl BuildCommandlContext {
no_doc: scope.no_doc,
out_dir: scope.out_dir.clone(),
manifest_path: scope.manifest_path.clone(),
features: scope.features.clone(),
no_default_features: scope.no_default_features,
color: scope.color.clone(),
};
args.run(BuildContext::Build)?;
Expand Down
4 changes: 3 additions & 1 deletion cargo-near/src/types/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ fn get_cargo_metadata(
if !no_locked {
cmd.other_options(["--locked".to_string()]);
}
let metadata = cmd.manifest_path(&manifest_path.path).exec();
let cmd = cmd.manifest_path(&manifest_path.path);
log::debug!("metadata command: {:#?}", cmd.cargo_command());
let metadata = cmd.exec();
if let Err(cargo_metadata::Error::CargoMetadata { stderr }) = metadata.as_ref() {
if stderr.contains("remove the --locked flag") {
return Err(cargo_metadata::Error::CargoMetadata {
Expand Down
24 changes: 0 additions & 24 deletions integration-tests/templates/negative/_Cargo_no_abi_feature.toml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ crate-type = ["cdylib"]

[dependencies]
# NOTE: uncomment line 11 and comment 12, if it's desirable to switch to published `near-sdk` version
# near-sdk = { default-features = false, features = ["abi"], ::sdk-cratesio-version-toml:: }
near-sdk = { default-features = false, features = ["abi"], ::sdk-git-version-toml:: }
# near-sdk = { default-features = false, ::sdk-cratesio-version-toml:: }
near-sdk = { default-features = false, ::sdk-git-version-toml:: }
serde = { version = "1", features = ["derive"] }

[workspace]
Expand Down
22 changes: 1 addition & 21 deletions integration-tests/tests/abi/negative.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
use cargo_near_integration_tests::{generate_abi_fn, generate_abi_fn_with};
use function_name::named;

#[test]
#[named]
fn test_abi_feature_not_enabled() -> cargo_near::CliResult {
fn run_test() -> cargo_near::CliResult {
generate_abi_fn_with! {
Cargo: "/templates/negative/_Cargo_no_abi_feature.toml";
Code:
pub fn foo(&self, a: u32, b: u32) {}
};
Ok(())
}

assert_eq!(
run_test().map_err(|e| e.to_string()),
Err("`near-sdk` dependency must have the `abi` feature enabled".to_string())
);

Ok(())
}

#[test]
#[named]
fn test_abi_old_sdk() -> cargo_near::CliResult {
Expand All @@ -35,7 +15,7 @@ fn test_abi_old_sdk() -> cargo_near::CliResult {

assert_eq!(
run_test().map_err(|e| e.to_string()),
Err("unsupported `near-sdk` version. expected 4.1.* or higher".to_string())
Err("missing `__abi-generate` required feature for `near-sdk` dependency: probably unsupported `near-sdk` version. expected 4.1.* or higher".to_string())
);

Ok(())
Expand Down

0 comments on commit 55d9a4f

Please sign in to comment.