Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions src/MongoDB.Driver.Encryption/LibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ static LinuxLibrary()
var osArchitecture = RuntimeInformation.OSArchitecture.ToString().ToLower();
#pragma warning restore CA1304

var finalpath = IsAlpine() ? $"alpine/{osArchitecture}" : $"{osArchitecture}";
var runtimeSuffix = IsAlpine() ? $"musl-{osArchitecture}" : $"{osArchitecture}";

__suffixPaths = new []{
$"../../runtimes/linux/native/{finalpath}",
$"runtimes/linux/native/{finalpath}",
$"../../runtimes/linux-{runtimeSuffix}/native/",
$"runtimes/linux-{runtimeSuffix}/native/",
string.Empty
};

Expand Down Expand Up @@ -246,8 +246,8 @@ private class WindowsLibrary : ISharedLibraryLoader
{
private static readonly string[] __suffixPaths =
{
@"..\..\runtimes\win\native\",
@".\runtimes\win\native\",
@"..\..\runtimes\win-x64\native\",
@".\runtimes\win-x64\native\",
string.Empty
};

Expand Down
61 changes: 40 additions & 21 deletions src/MongoDB.Driver.Encryption/MongoDB.Driver.Encryption.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,60 @@
Targets="DownloadNativeBinary" />
</Target>

<Target Name="DownloadNativeBinaries_UbuntuX64" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/linux/native/x64/libmongocrypt.so')">
<Target Name="DownloadNativeBinaries_UbuntuX64" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/linux-x64/native/libmongocrypt.so')">
<PropertyGroup>
<LibMongoCryptSourceUrl>https://mciuploads.s3.amazonaws.com/libmongocrypt-release/ubuntu1804-64/$(LibMongoCryptVersion)/$(LibMongoCryptCommit)/libmongocrypt.tar.gz</LibMongoCryptSourceUrl>
<LibMongoCryptSourcePath>nocrypto/lib/libmongocrypt.so</LibMongoCryptSourcePath>
<LibMongoCryptPackagePath>runtimes/linux/native/x64</LibMongoCryptPackagePath>
<LibMongoCryptPackagePath>runtimes/linux-x64/native/</LibMongoCryptPackagePath>
</PropertyGroup>

<MSBuild Projects ="$(MSBuildProjectFullPath)"
Properties="TargetFramework=once;LibMongoCryptSourceUrl=$(LibMongoCryptSourceUrl);LibMongoCryptSourcePath=$(LibMongoCryptSourcePath);LibMongoCryptPackagePath=$(LibMongoCryptPackagePath)"
Targets="DownloadNativeBinary" />
</Target>

<Target Name="DownloadNativeBinaries_UbuntuARM64" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/linux/native/arm64/libmongocrypt.so')">
<Target Name="DownloadNativeBinaries_UbuntuARM64" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/linux-arm64/native/libmongocrypt.so')">
<PropertyGroup>
<LibMongoCryptSourceUrl>https://mciuploads.s3.amazonaws.com/libmongocrypt-release/ubuntu1804-arm64/$(LibMongoCryptVersion)/$(LibMongoCryptCommit)/libmongocrypt.tar.gz</LibMongoCryptSourceUrl>
<LibMongoCryptSourcePath>nocrypto/lib/libmongocrypt.so</LibMongoCryptSourcePath>
<LibMongoCryptPackagePath>runtimes/linux/native/arm64</LibMongoCryptPackagePath>
<LibMongoCryptPackagePath>runtimes/linux-arm64/native/</LibMongoCryptPackagePath>
</PropertyGroup>

<MSBuild Projects ="$(MSBuildProjectFullPath)"
Properties="TargetFramework=once;LibMongoCryptSourceUrl=$(LibMongoCryptSourceUrl);LibMongoCryptSourcePath=$(LibMongoCryptSourcePath);LibMongoCryptPackagePath=$(LibMongoCryptPackagePath)"
Targets="DownloadNativeBinary" />
</Target>

<Target Name="DownloadNativeBinaries_Alpine" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/linux/native/alpine/libmongocrypt.so')">

<Target Name="DownloadNativeBinaries_AlpineAMD64" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/linux-musl-x64/native/libmongocrypt.so')">
Copy link
Contributor Author

@adelinowona adelinowona Sep 30, 2025

Choose a reason for hiding this comment

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

Added explicit linux-musl-x64 RID mapping to ensure correct native library selection

During local testing, I discovered that MSBuild's RID fallback mechanism was causing incorrect library resolution. According to the RID compatibility graph, linux-musl-x64 is compatible with linux-x64, which caused MSBuild to use the linux-x64 native library as a fallback for linux-musl-x64 targets.

Since we provide a dedicated native library built specifically for linux-musl-x64, this change ensures that the platform-specific library is selected instead of relying on the fallback mechanism.

<PropertyGroup>
<LibMongoCryptSourceUrl>https://mciuploads.s3.amazonaws.com/libmongocrypt-release/alpine-amd64-earthly/$(LibMongoCryptVersion)/$(LibMongoCryptCommit)/libmongocrypt.tar.gz</LibMongoCryptSourceUrl>
<LibMongoCryptSourcePath>nocrypto/lib/libmongocrypt.so</LibMongoCryptSourcePath>
<LibMongoCryptPackagePath>runtimes/linux-musl-x64/native/</LibMongoCryptPackagePath>
</PropertyGroup>

<MSBuild Projects ="$(MSBuildProjectFullPath)"
Properties="TargetFramework=once;LibMongoCryptSourceUrl=$(LibMongoCryptSourceUrl);LibMongoCryptSourcePath=$(LibMongoCryptSourcePath);LibMongoCryptPackagePath=$(LibMongoCryptPackagePath)"
Targets="DownloadNativeBinary" />
</Target>

<Target Name="DownloadNativeBinaries_AlpineARM64" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/linux-musl-arm64/native/libmongocrypt.so')">
<PropertyGroup>
<LibMongoCryptSourceUrl>https://mciuploads.s3.amazonaws.com/libmongocrypt-release/alpine-arm64-earthly/$(LibMongoCryptVersion)/$(LibMongoCryptCommit)/libmongocrypt.tar.gz</LibMongoCryptSourceUrl>
<LibMongoCryptSourcePath>nocrypto/lib/libmongocrypt.so</LibMongoCryptSourcePath>
<LibMongoCryptPackagePath>runtimes/linux/native/alpine</LibMongoCryptPackagePath>
<LibMongoCryptPackagePath>runtimes/linux-musl-arm64/native/</LibMongoCryptPackagePath>
</PropertyGroup>

<MSBuild Projects ="$(MSBuildProjectFullPath)"
Properties="TargetFramework=once;LibMongoCryptSourceUrl=$(LibMongoCryptSourceUrl);LibMongoCryptSourcePath=$(LibMongoCryptSourcePath);LibMongoCryptPackagePath=$(LibMongoCryptPackagePath)"
Targets="DownloadNativeBinary" />
</Target>

<Target Name="DownloadNativeBinaries_Windows" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/win/native/mongocrypt.dll')">
<Target Name="DownloadNativeBinaries_WindowsX64" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)/runtimes/win-x64/native/mongocrypt.dll')">
<PropertyGroup>
<LibMongoCryptSourceUrl>https://mciuploads.s3.amazonaws.com/libmongocrypt-release/windows-test/$(LibMongoCryptVersion)/$(LibMongoCryptCommit)/libmongocrypt.tar.gz</LibMongoCryptSourceUrl>
<LibMongoCryptSourcePath>bin/mongocrypt.dll</LibMongoCryptSourcePath>
<LibMongoCryptPackagePath>runtimes/win/native</LibMongoCryptPackagePath>
<LibMongoCryptPackagePath>runtimes/win-x64/native</LibMongoCryptPackagePath>
</PropertyGroup>

<MSBuild Projects ="$(MSBuildProjectFullPath)"
Expand All @@ -91,33 +104,39 @@

<ItemGroup>
<Content Include="$(MSBuildProjectDirectory)/runtimes/osx/native/libmongocrypt.dylib">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>runtimes\osx\native</PackagePath>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The macOS paths remain unchanged because our packaged library is a universal binary that supports both arm64 and x64 architectures. According to MSBuild's RID compatibility fallback graph, both osx-x64 and osx-arm64 fall back to osx, so the current structure already handles both platforms correctly without requiring separate RID-specific paths.

</Content>

<Content Include="$(MSBuildProjectDirectory)/runtimes/linux/native/x64/libmongocrypt.so">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Content Include="$(MSBuildProjectDirectory)/runtimes/linux-x64/native/libmongocrypt.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>runtimes\linux-x64\native</PackagePath>
</Content>

<Content Include="$(MSBuildProjectDirectory)/runtimes/linux-arm64/native/libmongocrypt.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>runtimes\linux\native\x64</PackagePath>
<PackagePath>runtimes\linux-arm64\native</PackagePath>
</Content>

<Content Include="$(MSBuildProjectDirectory)/runtimes/linux/native/arm64/libmongocrypt.so">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Content Include="$(MSBuildProjectDirectory)/runtimes/linux-musl-arm64/native/libmongocrypt.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Copy link
Contributor Author

@adelinowona adelinowona Sep 30, 2025

Choose a reason for hiding this comment

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

Switched to PreserveNewest as it follows MSBuild best practices by only copying files when they've been modified, rather than copying on every build. While this should theoretically improve build performance, the impact will be negligible given our small number of native libraries. Made the change for consistency with recommended practices.

<Pack>true</Pack>
<PackagePath>runtimes\linux\native\arm64</PackagePath>
<PackagePath>runtimes\linux-musl-arm64\native</PackagePath>
</Content>

<Content Include="$(MSBuildProjectDirectory)/runtimes/linux/native/alpine/libmongocrypt.so">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Content Include="$(MSBuildProjectDirectory)/runtimes/linux-musl-x64/native/libmongocrypt.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>runtimes\linux\native\alpine</PackagePath>
<PackagePath>runtimes\linux-musl-x64\native</PackagePath>
</Content>

<Content Include="$(MSBuildProjectDirectory)/runtimes/win/native/mongocrypt.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Content Include="$(MSBuildProjectDirectory)/runtimes/win-x64/native/mongocrypt.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Pack>true</Pack>
<PackagePath>runtimes\win\native</PackagePath>
<PackagePath>runtimes\win-x64\native</PackagePath>
</Content>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition="$([MSBuild]::IsOsPlatform('Windows'))">
<Content Include="$(MSBuildThisFileDirectory)../runtimes/win/native/mongocrypt.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<ItemGroup Condition="'$(UsingMicrosoftNETSdk)' != 'true' AND $([MSBuild]::IsOsPlatform('Windows'))">
<Content Include="$(MSBuildThisFileDirectory)../runtimes/win-x64/native/mongocrypt.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>mongocrypt.dll</Link>
</Content>
</ItemGroup>
Comment on lines 1 to 8
Copy link
Contributor Author

@adelinowona adelinowona Sep 30, 2025

Choose a reason for hiding this comment

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

From my understanding this target file provides backward compatibility for older, non-SDK-style projects (e.g., .NET Framework with packages.config) that do not automatically handle native assets from the 'runtimes' folder.

The condition ensures this logic ONLY runs for non-SDK-style projects. Modern .NET SDK projects (which set 'UsingMicrosoftNETSdk' to 'true') will handle native asset copying automatically based on the RID.

Expand Down