From d4ae08d21c5a22b711962f25057048a1c1249ffc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 14 Jan 2026 00:05:48 +0000 Subject: [PATCH 1/5] Initial plan From ca790691a382dc7d0576f2878a9052b2dd49e398 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 14 Jan 2026 01:11:31 +0000 Subject: [PATCH 2/5] Add Windows MSVC linker flag for ABI generation Co-authored-by: r-near <163825889+r-near@users.noreply.github.com> --- cargo-near-build/src/near/abi/generate/mod.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cargo-near-build/src/near/abi/generate/mod.rs b/cargo-near-build/src/near/abi/generate/mod.rs index 74e96f8c..c207ddd7 100644 --- a/cargo-near-build/src/near/abi/generate/mod.rs +++ b/cargo-near-build/src/near/abi/generate/mod.rs @@ -70,12 +70,23 @@ pub fn procedure( pretty_print::step("Generating ABI"); let compile_env = { - let compile_env = vec![ + #[allow(unused_mut)] + let mut compile_env = vec![ ("CARGO_PROFILE_DEV_OPT_LEVEL", "0"), ("CARGO_PROFILE_DEV_DEBUG", "0"), ("CARGO_PROFILE_DEV_LTO", "off"), (env_keys::BUILD_RS_ABI_STEP_HINT, "true"), ]; + + // On Windows MSVC, the linker doesn't allow unresolved symbols by default. + // When building a dylib for ABI generation, near-sdk's runtime functions + // (from near-sys) won't be available since they're provided by the NEAR VM + // in wasm builds. We use /FORCE:UNRESOLVED to allow the build to succeed. + #[cfg(windows)] + { + compile_env.push((env_keys::RUSTFLAGS, "-C link-arg=/FORCE:UNRESOLVED")); + } + [&compile_env, env].concat() }; let dylib_artifact = cargo_native::compile::run::( From 853e409d3c4d0bc72b3e28e972381120bd893119 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 14 Jan 2026 01:13:42 +0000 Subject: [PATCH 3/5] Fix RUSTFLAGS handling to append instead of replace Co-authored-by: r-near <163825889+r-near@users.noreply.github.com> --- cargo-near-build/src/cargo_native/compile.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cargo-near-build/src/cargo_native/compile.rs b/cargo-near-build/src/cargo_native/compile.rs index 1a2e891c..1efcdefd 100644 --- a/cargo-near-build/src/cargo_native/compile.rs +++ b/cargo-near-build/src/cargo_native/compile.rs @@ -25,7 +25,14 @@ where let final_env = { let mut env: BTreeMap<_, _> = env.into_iter().collect(); if hide_warnings { - env.insert(crate::env_keys::RUSTFLAGS, "-Awarnings"); + // Append -Awarnings to existing RUSTFLAGS if present + let existing_rustflags = env.get(&crate::env_keys::RUSTFLAGS).copied(); + let new_rustflags = match existing_rustflags { + Some(existing) => format!("{} -Awarnings", existing), + None => "-Awarnings".to_string(), + }; + // We need to leak the string to get a 'static str for the BTreeMap + env.insert(crate::env_keys::RUSTFLAGS, Box::leak(new_rustflags.into_boxed_str())); } env }; From 3a431df9e303fd78c39ad2ea3ffa5f1e5b33668a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 14 Jan 2026 01:18:14 +0000 Subject: [PATCH 4/5] Apply code formatting Co-authored-by: r-near <163825889+r-near@users.noreply.github.com> --- cargo-near-build/src/cargo_native/compile.rs | 5 ++++- cargo-near-build/src/near/abi/generate/mod.rs | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cargo-near-build/src/cargo_native/compile.rs b/cargo-near-build/src/cargo_native/compile.rs index 1efcdefd..c309c033 100644 --- a/cargo-near-build/src/cargo_native/compile.rs +++ b/cargo-near-build/src/cargo_native/compile.rs @@ -32,7 +32,10 @@ where None => "-Awarnings".to_string(), }; // We need to leak the string to get a 'static str for the BTreeMap - env.insert(crate::env_keys::RUSTFLAGS, Box::leak(new_rustflags.into_boxed_str())); + env.insert( + crate::env_keys::RUSTFLAGS, + Box::leak(new_rustflags.into_boxed_str()), + ); } env }; diff --git a/cargo-near-build/src/near/abi/generate/mod.rs b/cargo-near-build/src/near/abi/generate/mod.rs index c207ddd7..97b2aa8a 100644 --- a/cargo-near-build/src/near/abi/generate/mod.rs +++ b/cargo-near-build/src/near/abi/generate/mod.rs @@ -77,7 +77,7 @@ pub fn procedure( ("CARGO_PROFILE_DEV_LTO", "off"), (env_keys::BUILD_RS_ABI_STEP_HINT, "true"), ]; - + // On Windows MSVC, the linker doesn't allow unresolved symbols by default. // When building a dylib for ABI generation, near-sdk's runtime functions // (from near-sys) won't be available since they're provided by the NEAR VM @@ -86,7 +86,7 @@ pub fn procedure( { compile_env.push((env_keys::RUSTFLAGS, "-C link-arg=/FORCE:UNRESOLVED")); } - + [&compile_env, env].concat() }; let dylib_artifact = cargo_native::compile::run::( From 78011ccfadd35749c42574187d93b6c93d889a67 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 14 Jan 2026 01:20:17 +0000 Subject: [PATCH 5/5] Refactor RUSTFLAGS handling to avoid memory leak Co-authored-by: r-near <163825889+r-near@users.noreply.github.com> --- CHANGELOG.md | 5 +++ cargo-near-build/src/cargo_native/compile.rs | 33 +++++++++++--------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4234217b..3a56f7f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fixed Windows (without WSL) build failure on ABI generation step by adding `/FORCE:UNRESOLVED` linker flag for MSVC +- Fixed RUSTFLAGS handling in cargo_native::compile to append flags instead of replacing them + ## [0.19.0](https://github.com/near/cargo-near/compare/cargo-near-v0.18.0...cargo-near-v0.19.0) - 2026-01-12 ### Added diff --git a/cargo-near-build/src/cargo_native/compile.rs b/cargo-near-build/src/cargo_native/compile.rs index c309c033..c7c0907f 100644 --- a/cargo-near-build/src/cargo_native/compile.rs +++ b/cargo-near-build/src/cargo_native/compile.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, ffi::OsStr, marker::PhantomData, process::Command, thread}; +use std::{ffi::OsStr, marker::PhantomData, process::Command, thread}; use camino::Utf8Path; use cargo_metadata::{Artifact, Message}; @@ -23,19 +23,24 @@ where T: ArtifactType, { let final_env = { - let mut env: BTreeMap<_, _> = env.into_iter().collect(); + // Convert to owned strings to allow modification + let mut env: Vec<(String, String)> = env + .into_iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(); if hide_warnings { - // Append -Awarnings to existing RUSTFLAGS if present - let existing_rustflags = env.get(&crate::env_keys::RUSTFLAGS).copied(); - let new_rustflags = match existing_rustflags { - Some(existing) => format!("{} -Awarnings", existing), - None => "-Awarnings".to_string(), - }; - // We need to leak the string to get a 'static str for the BTreeMap - env.insert( - crate::env_keys::RUSTFLAGS, - Box::leak(new_rustflags.into_boxed_str()), - ); + // Check if RUSTFLAGS already exists and append -Awarnings + if let Some(pos) = env + .iter() + .position(|(k, _)| k == crate::env_keys::RUSTFLAGS) + { + env[pos].1.push_str(" -Awarnings"); + } else { + env.push(( + crate::env_keys::RUSTFLAGS.to_string(), + "-Awarnings".to_string(), + )); + } } env }; @@ -46,7 +51,7 @@ where "build", [&["--message-format=json-render-diagnostics"], args].concat(), manifest_path.directory().ok(), - final_env.iter(), + final_env.iter().map(|(k, v)| (k.as_str(), v.as_str())), &removed_env, color, )?;