Search code examples
linuxpowershellpowershell-corejson-patchpowershell-7.4

DLL works on Windows pwsh but not Linux?


I have the following script called Minimal.ps1:

Add-Type -LiteralPath "$PSScriptRoot/lib/JsonPatch.Net.dll"
$ErrorActionPreference = "Stop"
[System.Text.Json.JsonSerializer]::Deserialize[Json.Patch.JsonPatch]("[]")

In the lib directory I have is extracted the content of lib/net8.0/ from the JsonPatch.Net 3.2.3 Nuget package.

On windows (pwsh 7.4), the script works:

$ pwsh ./Minimal.ps1
Operations
----------
{}

When I try to run the same script on linux in docker it fails:

$ docker run --rm -it -v .:/test -w /test mcr.microsoft.com/dotnet/sdk:8.0.404 /usr/bin/pwsh ./Minimal.ps1
MethodInvocationException: /test/Minimal.ps1:6
Line |
   6 |  [System.Text.Json.JsonSerializer]::Deserialize[Json.Patch.JsonPatch]( …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Deserialize" with "1" argument(s): "Could not resolve type 'Json.Patch.PatchJsonConverter' in assembly 'JsonPatch.Net, Version=3.0.0.0, Culture=neutral,
     | PublicKeyToken=17ed63f672b0e278'."

I tried to create an instance of PatchJsonConverter explicitly but it returned the same error (could not resolve type).

I saw that JsonPatch.Net had dependencies on Json.More.Net, JsonPointer.Net, Humanizer.Net.

When trying to also load these dlls on linux with Add-Type, I get errors like this, except for Humanizer.Net that loads correctly.

  Add-Type -Verbose -LiteralPath "$PSScriptRoot/lib/Json.More.dll"
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Could not load file or assembly 'Json.More, Version=2.0.0.0, Culture=neutral, PublicKeyToken=17ed63f672b0e278'.

Solution

  • After some tests, it looks like the problem happens with the PowerShell version installed in the mcr.microsoft.com/dotnet/sdk:8.0.404 image (and all the other dotnet 8 images that I tested). The exact PowerShell version is 7.4.6.

    I'm not sure if the problem happens because of the PowerShell version or the way it's installed in the image, but everything works like expected in dotnet 9 images or when installing PowerShell manually in a Debian bookworm container.