Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

System.BadImageException on NCrunch loading inside Rider if NCrunch Console Tool is installed
talbrecht
#1 Posted : Wednesday, July 3, 2024 9:25:14 AM(UTC)
Rank: Member

Groups: Registered
Joined: 5/10/2019(UTC)
Posts: 20
Location: Germany

Thanks: 8 times
Was thanked: 3 time(s) in 3 post(s)
NCrunch Console Tool gets installed as x86 application including GAC registration. As soon as it is available in GAC NCrunch cannot be loaded in Rider anymore. So both cannot be installed on the same machine at the same time.
Quote:
Details: System.Exception: An exception was thrown in the remote environment: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)
at nCrunch.Core.Gac.a(a& a, Int32 b)
at nCrunch.Core.Gac.()
at nCrunch.Core.Gac.(String )
at nCrunch.Core.Gac.IsAssemblyLoadedInGac(String shortOrFullAssemblyName)
at nCrunch.Core.BuildManagement.GacReferencedBuildEngine.CheckIsAvailable()
at nCrunch.Core.BuildManagement.MSBuildEngineRegistry.AddRegisteredBuildEngine(IRegisteredBuildEngine engine)
at nCrunch.Core.BuildManagement.MSBuildEngineRegistry.AddAllBuildEngines()
at nCrunch.Client.EngineServiceInitialiser.InitialiseServiceLocator(Nullable`1 ideVersion, FilePath solutionFilePath, Boolean isHostedInSatelliteProcess, Boolean readOnlyConfig, UIState uiState, ISupplementaryEngineInitialiser initialiser, FilePath[] assembliesResidentInIDE)
at nCrunch.RiderIntegration.RiderServiceInitialiser.InitialiseServiceLocatorForEngine(FilePath solutionPath, ISupplementaryEngineInitialiser initialiser)
at nCrunch.RiderIntegration.RiderRemoteBootstrap.StartEngine(FilePath solutionPath)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at nCrunch.TaskRunner.Ipc.IpcMessageProcessor.(CallMethodMessage )
at nCrunch.TaskRunner.Ipc.IpcMessageProcessor.ProcessMessageReturningResult(Byte[] data)
at nCrunch.TaskRunner.Ipc.RemoteProxyCommunicator.(Byte[] )
at nCrunch.TaskRunner.Ipc.RemoteProxyCommunicator.Invoke(String methodName, Object[] args)
at nCrunch.RiderIntegration.RiderRemoteBootstrapProxy.StartEngine(FilePath solutionPath)
at nCrunch.RiderIntegration.RiderEngineBootstrap.StartRemoteEngine(FilePath solutionPath, Action`1 logErrorDuringInitialisation, Action processHasTerminated)

java.lang.Throwable: NCrunch encountered an error during the initialisation
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:410)
at com.remco.software.ncrunch.ErrorService$Companion.notifyUserOfError(ErrorService.kt:36)
at com.remco.software.ncrunch.ErrorService.errorEventUpdate(ErrorService.kt:27)
at com.remco.software.ncrunch.ErrorService.access$errorEventUpdate(ErrorService.kt:13)
at com.remco.software.ncrunch.ErrorService$1.invoke(ErrorService.kt:17)
at com.remco.software.ncrunch.ErrorService$1.invoke(ErrorService.kt:17)
at com.jetbrains.rd.util.reactive.Signal.fire(Signal.kt:32)
at com.jetbrains.rd.framework.impl.RdSignal$onWireReceived$2.invoke(RdSignal.kt:33)
at com.jetbrains.rd.framework.impl.RdSignal$onWireReceived$2.invoke(RdSignal.kt:31)
at com.jetbrains.rd.framework.MessageBroker$RdWireableDispatchHelper$doDispatch$2.invoke(MessageBroker.kt:122)
at com.jetbrains.rd.framework.MessageBroker$RdWireableDispatchHelper$doDispatch$2.invoke(MessageBroker.kt:115)
at com.jetbrains.rdclient.protocol.RdDispatcher.flushAll(RdDispatcher.kt:112)
at com.jetbrains.rdclient.protocol.RdDispatcher.flushAllRunnable$lambda$0(RdDispatcher.kt:33)
at com.intellij.openapi.application.TransactionGuardImpl.runWithWritingAllowed(TransactionGuardImpl.java:209)
at com.intellij.openapi.application.TransactionGuardImpl.access$100(TransactionGuardImpl.java:22)
at com.intellij.openapi.application.TransactionGuardImpl$1.run(TransactionGuardImpl.java:191)
at com.intellij.openapi.application.impl.RwLockHolder.runIntendedWriteActionOnCurrentThread(RwLockHolder.kt:204)
at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:830)
at com.intellij.openapi.application.impl.ApplicationImpl$2.run(ApplicationImpl.java:419)
at com.intellij.openapi.application.impl.RwLockHolder.runWithEnabledImplicitRead(RwLockHolder.kt:138)
at com.intellij.openapi.application.impl.RwLockHolder.runWithImplicitRead(RwLockHolder.kt:129)
at com.intellij.openapi.application.impl.ApplicationImpl.runWithImplicitRead(ApplicationImpl.java:1152)
at com.intellij.openapi.application.impl.FlushQueue.doRun(FlushQueue.java:81)
at com.intellij.openapi.application.impl.FlushQueue.runNextEvent(FlushQueue.java:123)
at com.intellij.openapi.application.impl.FlushQueue.flushNow(FlushQueue.java:43)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:792)
at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:739)
at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:733)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:761)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.kt:699)
at com.intellij.ide.IdeEventQueue._dispatchEvent$lambda$12(IdeEventQueue.kt:593)
at com.intellij.openapi.application.impl.RwLockHolder.runWithoutImplicitRead(RwLockHolder.kt:105)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.kt:593)
at com.intellij.ide.IdeEventQueue.access$_dispatchEvent(IdeEventQueue.kt:77)
at com.intellij.ide.IdeEventQueue$dispatchEvent$processEventRunnable$1$1$1.compute(IdeEventQueue.kt:362)
at com.intellij.ide.IdeEventQueue$dispatchEvent$processEventRunnable$1$1$1.compute(IdeEventQueue.kt:361)
at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:843)
at com.intellij.ide.IdeEventQueue$dispatchEvent$processEventRunnable$1$1.invoke(IdeEventQueue.kt:361)
at com.intellij.ide.IdeEventQueue$dispatchEvent$processEventRunnable$1$1.invoke(IdeEventQueue.kt:356)
at com.intellij.ide.IdeEventQueueKt.performActivity$lambda$1(IdeEventQueue.kt:1022)
at com.intellij.openapi.application.TransactionGuardImpl.performActivity(TransactionGuardImpl.java:106)
at com.intellij.ide.IdeEventQueueKt.performActivity(IdeEventQueue.kt:1022)
at com.intellij.ide.IdeEventQueue.dispatchEvent$lambda$7(IdeEventQueue.kt:356)
at com.intellij.openapi.application.impl.RwLockHolder.runIntendedWriteActionOnCurrentThread(RwLockHolder.kt:209)
at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:830)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.kt:398)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:207)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:92)

Please provide a x64 version of the console tool, too, or find a way that Rider does not try to mix 32- and 64-bit NCrunch assemblies. Thanks in advance.
Remco
#2 Posted : Wednesday, July 3, 2024 12:46:59 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,110

Thanks: 955 times
Was thanked: 1283 time(s) in 1190 post(s)
Hi, thanks for sharing this issue.

Although this looks like one of those x86/x64 issues, I don't think this is actually the case here. This exception is thrown by the platform when a component in the engine is trying to make a pinvoke call to CreateAssemblyCache in fusion.dll. This is being done when the engine is trying to establish which msbuild engines are installed on the system. Although this is done using the GAC, the problem actually isn't related to the contents of the GAC.

It's not clear for me why this call would fail. I haven't seen this happen before. My first though is that perhaps something is wrong with the installation of fusion.dll on your system.

Did you install anything else with the console tool? Was the Rider installation working before that? I suspect something else is involved here.
talbrecht
#3 Posted : Wednesday, July 3, 2024 3:00:01 PM(UTC)
Rank: Member

Groups: Registered
Joined: 5/10/2019(UTC)
Posts: 20
Location: Germany

Thanks: 8 times
Was thanked: 3 time(s) in 3 post(s)
Hi, Remco.

While I prefer Rider (and CLI) my team uses VS2022 w/NCrunch (and without any hassle). Beside Rider I have both VS2019 and VS2022 installed. Do you think that side-by-side installation is the root cause? I've loaded a VS2019 solution while fusion logging was enabled and I got such nCrunch related entries like below (but also successful ones)
Quote:
*** Assembly Binder Log Entry (03.07.2024 @ 16:11:11) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable c:\program files (x86)\microsoft visual studio\2019\professional\common7\ide\extensions\remco software\ncrunch for visual studio 2019\nCrunch.TestHost48.x64.exe
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: DisplayName = nCrunch.Core, Version=5.8.0.2, Culture=neutral, PublicKeyToken=01d101bf6f3e0aea
(Fully-specified)
LOG: Appbase = file:///D:/Projekte/NCrunch/17720/16/bin/Debug
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = nCrunch.TestRunner_086b53b9ed9a4f369e0d55ea7e4a914c
Calling assembly : nCrunch.Module.XUnit2, Version=5.8.0.2, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: D:\Projekte\NCrunch\17720\16\bin\Debug\Stratec.LiaisonXS.UserInterface.Tests.dll.config.ncrunchconfig
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: nCrunch.Core, Version=5.8.0.2, Culture=neutral, PublicKeyToken=01d101bf6f3e0aea
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///D:/Projekte/NCrunch/17720/16/bin/Debug/nCrunch.Core.DLL.
LOG: Attempting download of new URL file:///D:/Projekte/NCrunch/17720/16/bin/Debug/nCrunch.Core/nCrunch.Core.DLL.
LOG: Attempting download of new URL file:///D:/Projekte/NCrunch/17720/16/bin/Debug/nCrunch.Core.EXE.
LOG: Attempting download of new URL file:///D:/Projekte/NCrunch/17720/16/bin/Debug/nCrunch.Core/nCrunch.Core.EXE.
LOG: Attempting download of new URL file:///c:/program files (x86)/microsoft visual studio/2019/professional/common7/ide/extensions/remco software/ncrunch for visual studio 2019/Modules/XUnit2/nCrunch.Core.DLL.
LOG: Attempting download of new URL file:///c:/program files (x86)/microsoft visual studio/2019/professional/common7/ide/extensions/remco software/ncrunch for visual studio 2019/Modules/XUnit2/nCrunch.Core/nCrunch.Core.DLL.
LOG: Attempting download of new URL file:///c:/program files (x86)/microsoft visual studio/2019/professional/common7/ide/extensions/remco software/ncrunch for visual studio 2019/Modules/XUnit2/nCrunch.Core.EXE.
LOG: Attempting download of new URL file:///c:/program files (x86)/microsoft visual studio/2019/professional/common7/ide/extensions/remco software/ncrunch for visual studio 2019/Modules/XUnit2/nCrunch.Core/nCrunch.Core.EXE.
LOG: All probing URLs attempted and failed.

Nevertheless the tests were executed and reported as passed.

Can you give me a hint where to look next?

Best regards,
Thomas
Remco
#4 Posted : Wednesday, July 3, 2024 10:14:55 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,110

Thanks: 955 times
Was thanked: 1283 time(s) in 1190 post(s)
Hi Thomas,

I wonder if this could be an install state issue. Could you try the removal and reinstall fix for both VS instances of NCrunch that you have installed, and try reinstalling the Rider plugin through the Rider plugin configuration?
talbrecht
#5 Posted : Thursday, July 4, 2024 12:27:31 PM(UTC)
Rank: Member

Groups: Registered
Joined: 5/10/2019(UTC)
Posts: 20
Location: Germany

Thanks: 8 times
Was thanked: 3 time(s) in 3 post(s)
Hi Remco.

I uninstalled all NCrunch installations (Console, VS2019, VS2022, Rider) and have reinstalled NCrunch in Rider only... and it still fails. So there is something wrong with my system/GAC.

I remember me that I've had multiple VSes side-by-side installed (VS2017, VS2019 and VS2022). We switched completely to VS2022 last year and I uninstalled VS2017 and VS2019. Unfortunately I got some tasks for an old product which still needs the x86 in-proc Windows Forms designer, so I've needed to install VS2019 again. Maybe this wrecked my GAC somehow.

I'm going to reinstall all VSes in the "correct" order now, check NCrunch in Rider again (without any other installation) and then proceed with the console tool, VS2019 and VS2022 versions.

Best regards,
Thomas
Remco
#6 Posted : Thursday, July 4, 2024 11:05:59 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,110

Thanks: 955 times
Was thanked: 1283 time(s) in 1190 post(s)
talbrecht;17472 wrote:

I uninstalled all NCrunch installations (Console, VS2019, VS2022, Rider) and have reinstalled NCrunch in Rider only... and it still fails. So there is something wrong with my system/GAC.


Sorry to hear that. We have a number of dev/test environments with many versions of VS working side-by-side, and I've never seen this issue. I think it may be caused by something deeper in the system than VS.

Our behaviour here is a bit different under Rider, as it uses the GAC query to identify all installed versions of MSBuild (something that isn't necessarily done under VS). You'll likely find the same behaviour in the console tool and grid node. It makes no sense to me why that API call would fail.

I think the fusion errors you've identified are a red herring that are unrelated to the core issue. I hope the reinstall of VSs solves the issue, otherwise you might want to try a system restore if you have an earlier working snapshot.
talbrecht
#7 Posted : Monday, July 8, 2024 9:40:13 AM(UTC)
Rank: Member

Groups: Registered
Joined: 5/10/2019(UTC)
Posts: 20
Location: Germany

Thanks: 8 times
Was thanked: 3 time(s) in 3 post(s)
No success. Even after reinstalling Visual Studio 2019 and 2022 the issue with NCrunch in Rider still exists.

It seems that I can use NCrunch in VS and console only - like in the good old days -, sigh.

Best regards,
Thomas

PS: I simply do not understand why Rider's NCrunch tries to load an assembly with the wrong architecture. I've created two empty solutions, one referencing .NET 4.8 and one .NET 8 - both trigger this error out-of-the-box, just while loading NCrunch into Rider. And it's a pity that the exception does not show which assembly fails.
Remco
#8 Posted : Monday, July 8, 2024 12:17:49 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,110

Thanks: 955 times
Was thanked: 1283 time(s) in 1190 post(s)
Here is the code that is failing:

Code:

private static IAssemblyCache createAssemblyCache()
{
IAssemblyCache assemblyCache;

int hr = CreateAssemblyCache(out assemblyCache, 0);
if (hr != 0)
Marshal.ThrowExceptionForHR(hr);
return assemblyCache;
}

[DllImport("fusion.dll")]
private static extern int CreateAssemblyCache(out IAssemblyCache ppAsmCache, int reserved);


The precise point of failure is in CreateAssemblyCache, which is a pinvoke call used to obtain a reference to the system's assembly cache. This works in all of our test and development environments. For some reason, the call is failing on your machine - which shouldn't happen.

It would seem that something is not right in your O/S. Check that your version of Windows is up to date. If you're running any virus scanners, try switching them off as they may be interfering with the system. Try running as Administrator to see if there's something in the O/S blocking the call.
1 user thanked Remco for this useful post.
talbrecht on 7/8/2024(UTC)
talbrecht
#9 Posted : Monday, July 8, 2024 2:12:42 PM(UTC)
Rank: Member

Groups: Registered
Joined: 5/10/2019(UTC)
Posts: 20
Location: Germany

Thanks: 8 times
Was thanked: 3 time(s) in 3 post(s)
Hi Remco!

I've solved it. My GAC is not corrupt, just the GAC handling in .NET Core needs extra care.

Quote:
using System.Runtime.InteropServices;

// THIS IS NEEDED FOR .NET CORE
string fusionFullPath = Environment.Is64BitProcess
? @"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\fusion.dll"
: @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\fusion.dll";
NativeLibrary.Load(fusionFullPath);
// WITHOUT PRELOADING THE CORRECT FUSION.DLL THE x86 VERSION IS USED - EVEN IN A x64 PROCESS -> System.BadImageFormatException

IAssemblyCache cache = GacUtility.CreateAssemblyCache();

Console.WriteLine("[OK] Assembly cache created");
Console.WriteLine();
Console.Write("Press any key to continue!");
Console.ReadKey();

[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
[ComImport]
internal interface IAssemblyCache
{
int UninstallAssembly();
[PreserveSig]
int QueryAssemblyInfo(int flags, [MarshalAs(UnmanagedType.LPWStr)] string assemblyName, ref AssemblyInfo assemblyInfo);
int CreateAssemblyCacheItem();
int CreateAssemblyScavenger();
int InstallAssembly();
}

[StructLayout(LayoutKind.Sequential)]
internal struct AssemblyInfo
{
public int cbAssemblyInfo;
public int assemblyFlags;
public long assemblySizeInKB;
[MarshalAs(UnmanagedType.LPWStr)]
public string currentAssemblyPath;
public int cchBuffer;
}

file static class GacUtility
{
internal static IAssemblyCache CreateAssemblyCache()
{
int hr = Interop.CreateAssemblyCache(out IAssemblyCache assemblyCache, 0);
if (hr != 0)
{
Marshal.ThrowExceptionForHR(hr);
}

return assemblyCache;
}
}

file static class Interop
{
[DllImport("fusion.dll")]
public static extern int CreateAssemblyCache(out IAssemblyCache ppAsmCache, int reserved);
}


Maybe you can add that fix to the next NCrunch version. I appreciate your help. Thx.

Best regards,
Thomas
talbrecht
#10 Posted : Monday, July 8, 2024 2:29:35 PM(UTC)
Rank: Member

Groups: Registered
Joined: 5/10/2019(UTC)
Posts: 20
Location: Germany

Thanks: 8 times
Was thanked: 3 time(s) in 3 post(s)
I've tested this with .NET Full Framework, too. Here the pre-loading is not necessary and the correct Fusion.dll is chosen for the executed architecture (tested with x86/x64/AnyCPU/AnyCPU+Prefer32bit).
Remco
#11 Posted : Monday, July 8, 2024 11:15:15 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,110

Thanks: 955 times
Was thanked: 1283 time(s) in 1190 post(s)
Wow, good find!

Terribly useful thing, having developers as users :)

It's interesting that this doesn't seem to be failing in other environments. Maybe there are different install states when it comes to fusion.dll. I'll see if I can get you a build with this fix included.
talbrecht
#12 Posted : Tuesday, July 9, 2024 7:42:12 AM(UTC)
Rank: Member

Groups: Registered
Joined: 5/10/2019(UTC)
Posts: 20
Location: Germany

Thanks: 8 times
Was thanked: 3 time(s) in 3 post(s)
Hi Remco.

No hurry. Please proceed with your normal schedule! I can wait for the next official release. Because the team uses VS anyway I'm used to run the tests via CLI prior push/PR. I'm just happy that I can continue using Rider.

Have a nice day!
Thomas

Remco
#13 Posted : Wednesday, July 10, 2024 11:19:27 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,110

Thanks: 955 times
Was thanked: 1283 time(s) in 1190 post(s)
Fix is now out with v5.9.

I'll add that this is speculative. I couldn't get it to fail on my side, but I've implemented the pre-loading of Fusion.dll and all of our own tests are staying green, so I'm fairly confident that it won't be any worse than it has been :)
1 user thanked Remco for this useful post.
talbrecht on 7/29/2024(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.135 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download