Building vSphere DLLs fails with CS8078: An expression is too long or complex to compile

1.2k views Asked by At

I am following the documentation here Setting Up for Microsoft C# Development and at this step Building the C# vSphere DLLs I get the following in Developer Command Prompt:

C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin>build.bat
        1 file(s) copied.
Fixing HttpNfcLeaseInfo type, adding missing leaseState property
Generating VimService.cs
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.6.1055.0]
Copyright (c) Microsoft Corporation.  All rights reserved.

Generating files...
C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin\VimService.cs
Compiling original VimService.dll
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.6.1055.0]
Copyright (c) Microsoft Corporation.  All rights reserved.

Generating XML serializers...
C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin\VimServiceSerializers.cs
        1 file(s) copied.
Optimizing VimService.cs by stripping serializer hint attributes.
Compiling optimized VimService.dll
FAILED

Looking at build.bat it looks like it is failing on this line:

echo Compiling optimized VimService.dll
csc /t:library /out:Vim25Service.dll VimService.cs VimServiceSerializers.cs >nul || goto ERROR

If I run csc /t:library /out:Vim25Service.dll VimService.cs VimServiceSerializers.cs manually i get the following:

C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin>csc /t:library /out:Vim25Service.dll VimService.cs VimServiceSerializers.cs
Microsoft (R) Visual C# Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.

VimServiceSerializers.cs(32548,98): error CS8078: An expression is too long or complex to compile

I also tried with VS2017:

C:\Users\user\Downloads\VMware-vSphereSDK-6.5.0-4571253\SDK\vsphere-ws\dotnet\bin>csc /t:library /out:Vim25Service.dll VimService.cs VimServiceSerializers.cs
Microsoft (R) Visual C# Compiler version 2.0.0.61213
Copyright (C) Microsoft Corporation. All rights reserved.

VimServiceSerializers.cs(31372,109): error CS8078: An expression is too long or complex to compile

A behavior to note, on VimServiceSerializers.cs(#####,##) the line and column are different every time.

Googling error CS8078, found it is an issue with the compiler running out of stack space. https://stackoverflow.com/a/8160109/6656422

How do I successfully compile VmWare's code?

2

There are 2 answers

1
Asad Saeeduddin On BEST ANSWER

I figured it out. The serializer CS file has long uninterrupted stretches of if ... else if ... else if ... clauses. The compiler has to deal with the whole if/else expression at once, which causes it to run out of stack space.

Fortunately, each branch in these else ifs terminates with a return statement. This makes all the else ifs functionally equivalent to just independent if statements, which are parsed independently.

After making this substitution in a number of places, the file compiles. Here's my modified VimServiceSerializers.cs: https://1drv.ms/u/s!Al6mzY0CpY7EnHqBRDyg-z0ctrjk

0
user496934 On

The answer given by splitting the if ...else into individual if statements is one solution. The other option is to check the version of the C# compiler being used to compile the code. I have seen that the csc.exe bundled with .NET 4.5, 4.6 can compile such code without generating any errors. But the Roslyn .NET compiler fails to compile such code and generates CS8078 errors. So if you don't want to modify the code the other option is to change the C# compiler. For example the below csc.exe can compile such code -

 C:\Windows\Microsoft.NET\Framework\v4.0.30319>csc.exe /version
Microsoft (R) Visual C# Compiler version 4.6.1055.0
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.