Project

Profile

Help

SaxonCS on MacOs ARM with .NET 6?

Added by Martin Honnen over 2 years ago

Has anyone here tried or an insight on using .NET 6 on MacOs ARM like e.g. an Mac Mini M1? The latest two Visual Studio 2022 Previews say they support that (https://devblogs.microsoft.com/visualstudio/visual-studio-2022-for-mac-preview-6/) and indeed I can install the VS 2022 program fine and create and run a simple .NET 6 console app that identifies the Environment.Version as 6.0.2.

Then it lets me add SaxonCS 11.2 as a NuGet package without complaining, but on trying to run some code that simply tries to create the Saxon Processor it fails with a message "BadImageFormatException: Could not load file or assembly SaxonCS .. An Attempt was made to load a program with an incorrect format.".

Is anyone of the Saxon devs on MacOs ARM running SaxonCS with .NET 6? Or does anyone else know what causes the BadImageFormatException when trying to use the NuGet SaxonCS 11.2 package with .NET 6 on MacOS ARM/Mac Mini M1 with VS 2022 Preview?


Replies (16)

Please register to reply

SaxonCS on MacOs ARM with .NET 6? - Added by Norm Tovey-Walsh over 2 years ago

Hi Martin,

Is anyone of the Saxon devs on MacOs ARM running SaxonCS with .NET 6?
Or does anyone else know what causes the BadImageFormatException when
trying to use the NuGet SaxonCS 11.2 package with .NET 6 on MacOS
ARM/Mac Mini M1 with VS 2022

The SaxonCS NuGet package was built with .NET 5. It was, as it happens,
built on an M1 machine, but my understanding is that the artifact we’re
building for NuGet is supposed to be architecture independent.

My best guess is that what you’re seeing is a .NET 5 vs .NET 6 problem.

I have got .NET 6 now in my ongoing and utterly frustrating attempts to
sort out how to sign the SaxonCS application for MacOS such that I can
get it notarized. If you’d like me to build you a .NET 6 artifact to
test, I can probably do that.

https://github.com/dotnet/sdk/issues/24337

Be seeing you,
norm

--
Norm Tovey-Walsh
Saxonica

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen over 2 years ago

I have never had problems using .NET 6 instead of .NET 5 with SaxonCS on Windows so I thought it might be that M1 architecture and VS 2022 Preview with .NET 6 trying to be "native" to that architecture, I think, instead of relying on Rosetta, as I (hope to) understand VS 2019 does. But as you say you build that stuff on an M1, I might be wrong. I also agree that .NET Core/.NET 5/6 is usually architecture independent.

I did the first install yesterday of that VS 2022 preview and only that single test, I will see whether I can figure out better what that error means and how to resolve it. The whole installation seem instable somehow as I also got messages/warnings about dotnet crashing while using VS 2022.

If you can test on your M1 if you can run SaxonCS from NuGet with .NET 6 the same as with .NET 5, now that you have .NET 6, that would help.

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen over 2 years ago

I did some further tests with VS 2022 Preview on a Mac Mini M1 and it indeed seems it can run SaxonCS 11.2 from NuGet with .NET 5 without problems while I always get that BadImageFormatException crash when trying .NET 6. Still not sure whether that means SaxonCS needs to be rebuilt for .NET 6 on such a Mac (as I said, on Windows I can easily run SaxonCS 11 with both .NET 5 and 6) or whether that preview is not working right and needs to be fixed.

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen over 2 years ago

Some output for details:

dotnet --info
.NET SDK (gemäß "global.json"):
 Version:   6.0.200
 Commit:    4c30de7899

Laufzeitumgebung:
 OS Name:     Mac OS X
 OS Version:  11.6
 OS Platform: Darwin
 RID:         osx.11.0-arm64
 Base Path:   /usr/local/share/dotnet/sdk/6.0.200/

Host (useful for support):
  Version: 6.0.2
  Commit:  839cdfb0ec

.NET SDKs installed:
  6.0.200 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

When I run the app referencing SaxonCS 11.2 from NuGet it gives

dotnet NET6ConsoleApp1.dll
Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'SaxonCS, Version=11.2.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.

File name: 'SaxonCS, Version=11.2.0.0, Culture=neutral, PublicKeyToken=null'
zsh: abort      dotnet NET6ConsoleApp1.dll

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen over 2 years ago

I have tried to produce the problem without SaxonCS and I have managed to do that by creating a .NET 5 library on the Mac M1 with compiler target x64 and then using it in a default (any CPU) .NET 6 console app written and run on the same Mac:

Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'NET5LibTest1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.

File name: 'NET5LibTest1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

So the whole problem might be related to the spurious warning I found in https://saxonica.plan.io/boards/3/topics/8322.

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen over 2 years ago

I did that compiler target setting in the VS 2019 IDE but it translates into a project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <WarningLevel>4</WarningLevel>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>
</Project>

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Norm Tovey-Walsh over 2 years ago

I can't find anything in our build, or in the command lines used to build, that suggests we're trying to use a particular platform target or architecture. The SaxonCS application specifies some options that might limit its platform, but those aren't used in the nuget package target(s).

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen over 2 years ago

All very odd, I have also asked on StackOverflow on the BadImageFormatException but so far only one commenter has shown up saying that the BadImageFormatException normally occurs for people using a 32bit library on a 64bit machine or the other way round, I don't have any clear understanding so far why on the Mac M1 .NET 6 fails with the SaxonCS .NET 5 library as all warning I have got (on Windows) so far pointed to that 64bit target, kind of seems the ARM64 architecture should be able to handle that.

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen over 2 years ago

I guess I can't understand a longish build script but https://saxonica.plan.io/projects/saxonmirrorhe/repository/he/revisions/saxon11/entry/build-cs.gradle at the beginning used x64 to define its csrid

if (systemOs.isWindows()) {
  csplatform = "Windows"
  csrid = "win-x64"
}
if (systemOs.isMacOsX()) {
  csplatform = "MacOS"
  csrid = "osx-x64"
}
if (systemOs.isLinux()) {
  csplatform = "Linux"
  csrid= "linux-x64"
}

As far as I understand it, .NET 5 on Mac is indeed restricted to create and run x64 code which can be run on a Mac M1 with ARM64 through Rosetta and .NET 6 on the Mac is the first version to support arm64 code creation and running.

I guess I have to wait until a .NET 6 release of SaxonCS is published. What are the plans for 11.3, to continue with .NET 5?

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Norm Tovey-Walsh over 2 years ago

That csrid is only used in the target that builds the SaxonCS executable. It's not used to build the NuGet package. The NuGet package is built with (effectively):

dotnet build SaxonCS.sln --verbosity Normal --configuration Release \
    /nodereuse:false --no-restore -p:Version=11.2.0 -p:PackageVersion=11.2.0

I believe the plan is to continue to use .NET 5 so that we don't force everyone who wants to use the library to upgrade to .NET 6.

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen over 2 years ago

I can understand the reasons to continue with .NET 5 but as .NET 6 is the long term supported version from Microsoft it would be nice to have .NET 6 releases as well, given that other packages like MAUI target .NET 6 and not .NET 5 and given that the .NET 5 SaxonCS NuGet package isn't usable with .NET 6 on a Mac M1 /ARM 64.

Earlier you wrote:

Norm Tovey-Walsh wrote:

I have got .NET 6 now in my ongoing and utterly frustrating attempts to
sort out how to sign the SaxonCS application for MacOS such that I can
get it notarized. If you’d like me to build you a .NET 6 artifact to
test, I can probably do that.

I think I am ready now to test a .NET 6 artifact on the Mac (or even perhaps on Android) so if you can make a .NET 6 artifact to test available that would be nice.

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen about 2 years ago

Now being on MacOs 12 instead of 11, using the latest VS 2022 Preview (I think version is 8) I, to my surprise, find SaxonCS 11.3 from NuGet working with .NET 6:

.NET 6.0.3 Unix 12.3.1
SAXON-CS-EE 11.3 from Saxonica 11.3
Hello from SaxonCS: 2022-04-09T20:19:31.416+02:00

I am not sure why things are working suddenly.

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen about 2 years ago

Even 11.2 works in this environment, no idea what (Mac OS 12?) makes the difference:

6.0.3 Unix 12.3.1 /usr/local/share/dotnet/dotnet
11.2 SAXON-CS-EE 11.2 from Saxonica

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Norm Tovey-Walsh about 2 years ago

Saxonica Developer Community writes:

Even 11.2 works in this environment, no idea what (Mac OS 12?) makes
the difference:

Huh. Well, like I said in the bug just a moment ago, it’s all a bit
opaque to me. I fixed the DLL in 11.3 and I assumed that was the likely
reason that it was working. But if 11.2 is also working for you now,
¯_(ツ)_/¯

Sigh.

Be seeing you,
norm

--
Norm Tovey-Walsh
Saxonica

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Martin Honnen about 2 years ago

On a closer check I think I stumbled into that project using the custom SaxonCS 11.2 you provided me around the 18 March. A project trying to use SaxonCS 11.2 from NuGet indeed, with .NET 6, gives the bad image exception.

So yes, it looks like the fix you applied for the DLL in 11.3 allows me to run SaxonCS on an ARM Mac with .NET 6 Arm64 while doing no harm to the other platforms like Windows Amd64.

RE: SaxonCS on MacOs ARM with .NET 6? - Added by Norm Tovey-Walsh about 2 years ago

So yes, it looks like the fix you applied for the DLL in 11.3 allows
me to run SaxonCS on an ARM Mac with .NET 6 Arm64 while doing no harm
to the other platforms like Windows Amd64.

Great! Now if I could just get the dotnet folks to sort out the code
signing issue[1] I could declare victory.

Be seeing you,
norm

[1] https://github.com/dotnet/sdk/issues/24337

--
Norman Tovey-Walsh
https://nwalsh.com/

There are three difficulties in authorship: to write anything worth the
publishing—to find honest men to publish it—and to get sensible men to
read it.--C. C. Colton

    (1-16/16)

    Please register to reply