Subject: [PATCH v3 11/16] rust: fix linking binaries with cargo

From: "brian m. carlson" <[email-masked]>
Date: Sat, 7 Feb 2026 20:04:41 +0000

From: "brian m. carlson" <[email-masked]>

When Cargo links binaries with MSVC, it uses the link.exe linker from
PATH to do so.  However, when running under a shell from MSYS, such as
when building with the Git for Windows SDK, which we do in CI, the
/ming64/bin and /usr/bin entries are first in PATH.  That means that the
Unix link binary shows up first, which obviously does not work for
linking binaries in any useful way.

To solve this problem, adjust PATH to place those binaries at the end of
the list instead of the beginning.  This allows access to the normal
Unix tools, but link.exe will be the compiler's linker.  Make sure to
export PATH explicitly: while this should be the default, it's more
robust to not rely on the shell operating in a certain way.

The reason this has not shown up before is that we typically link our
binaries from the C compiler.  However, now that we're about to
introduce a Rust build script (build.rs file), Rust will end up linking
that script to further drive Cargo, in which case we'll invoke the
linker from it.  There are other solutions, such as using LLD, but this
one is simple and reliable and is most likely to work with existing
systems.

Signed-off-by: Ezekiel Newren <[email-masked]>
Signed-off-by: brian m. carlson <[email-masked]>
---
 src/cargo-meson.sh | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/src/cargo-meson.sh b/src/cargo-meson.sh
index 38728a3711..75f3cd1265 100755
--- a/src/cargo-meson.sh
+++ b/src/cargo-meson.sh
@@ -19,6 +19,18 @@ do
 	esac
 done
 
+case "$(cargo -vV | sed -n 's/^host: \(.*\)$/\1/p')" in
+	*-windows-msvc)
+		LIBNAME=gitcore.lib
+		PATH="$(echo "$PATH" | tr ':' '\n' | grep -Ev "^(/mingw64/bin|/usr/bin)$" | paste -sd: -):/mingw64/bin:/usr/bin"
+		export PATH
+		;;
+	*-windows-*)
+		LIBNAME=gitcore.lib;;
+	*)
+		LIBNAME=libgitcore.a;;
+esac
+
 cargo build --lib --quiet --manifest-path="$SOURCE_DIR/Cargo.toml" --target-dir="$BUILD_DIR" "$@"
 RET=$?
 if test $RET -ne 0
@@ -26,13 +38,6 @@ then
 	exit $RET
 fi
 
-case "$(cargo -vV | sed -n 's/^host: \(.*\)$/\1/p')" in
-	*-windows-*)
-		LIBNAME=gitcore.lib;;
-	*)
-		LIBNAME=libgitcore.a;;
-esac
-
 if ! cmp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a" >/dev/null 2>&1
 then
 	cp "$BUILD_DIR/$BUILD_TYPE/$LIBNAME" "$BUILD_DIR/libgitcore.a"