13

Every now and then when I build a specific solution, I'll get a random amount of "An expression is too long or complex to compile" in the Error List window. However, the only item the error points to is the specific project, not a file within the project or a specific LOC.

When I encounter this, I 'Clean' and then I restart VS and that seems to fix it. Any ideas on what is causing this?

This particular solution has 50 projects in it.

Justin Self
  • 6,137
  • 3
  • 33
  • 48
  • Do you have any addons/extensions enabled that might be interfering with compilation? – drharris Nov 16 '11 at 20:05
  • I currently have "Go To Definition" and "Productivity Power Tools" installed, enabled, and updated. – Justin Self Nov 16 '11 at 20:06
  • I can't stand dealing with more than about 6 projects/solution ... I feel many pains for you. –  Nov 16 '11 at 20:08
  • I have both of those enabled with an 88-project solution, some of which contain switch statements over 5000 lines long (don't ask), and haven't gotten it, so it must be something else. – drharris Nov 16 '11 at 20:08
  • @drharris, wow. You have my sympathy. – Jeff LaFay Nov 16 '11 at 20:14
  • @jlafay No worries; I enjoy refactoring, so I'm making it my personal goal to eliminate as much code as possible. :) – drharris Nov 16 '11 at 20:17
  • I had this problem once. I closed and reopened Visual Studio and it went away. – Dan Stevens Jan 03 '13 at 14:45
  • I has the same error. and when I removed the following codes It fixed (js): _var x = <% #X %>_ – inon Jul 08 '14 at 09:16

11 Answers11

16

FYI, that error is characteristic of the compiler running out of stack space. Typically that happens when you throw a "deep recursion" problem at the compiler, like say,

int x = (1 + (1 + (1 + (1 + ......... + 1 ) + 1 ) + 1 ) + 1);

say, several thousand deep. The syntactic and semantic analyzers are both recursive-descent analyzers and therefore prone to running out of stack space in extreme scenarios.

I have no idea why shutting down and starting over would affect that, though. That is really strange.

If you get a solid repro, I'd love to see it. Either post it here, or enter a bug on Connect and we'll have a look at it. Without a solid repro though it is very hard to say what is going on here.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Why can't the stack overflow error at least point to the right area? (I'm sure there's a perfectly good reason for that) – configurator Nov 17 '11 at 00:24
  • @configurator: Precisely which call stack would you like to use to call the error analysis procedure? We used to run into this problem all the time when I was working on scripting; we'd run out of stack and call the browser to tell it to display the "out of stack" error, which would then of course *run out of stack again*. Windows does not take kindly to programs with threads that run out of stack *twice*, I assure you. In the C# compiler when we run out of stack, we just panic and unwind all the way to a top-level handler; by the time we're unwound, "where we were" is lost. – Eric Lippert Nov 17 '11 at 14:06
  • @Eric I assume that simply having a heaf referenced object per stack holding the current file (and line, but likely a bit too expensive) being processed and updating that whilst going along would suffice so that the error message could be a tad more specific. Or are you recursing into multiple different 'objects' representing potentially different files as you do this. – ShuggyCoUk Nov 17 '11 at 14:44
  • @Eric obviously there's a cost to implementing that feature, and possibly to it running. But I would have thought that it would be useful for you (as in internal developers of the tool chain) for tracking down the more unpleasant bugs inherent in dog fooding – ShuggyCoUk Nov 17 '11 at 14:45
  • @ShuggyCoUk: Indeed, we could do something like that. We already embed hints into the call stack that allow us to determine when a crash occurred in, say, parsing vs semantic analysis vs emit. We likely will build some sort of better diagnostic system into Roslyn, though, no promises there. It's still in flux. – Eric Lippert Nov 17 '11 at 15:58
  • Somewhere in the compiler there's must be a non-recursive method, maybe one that compiles an entire file or an entire class, that can catch the stack overflow. Methods are also non-recursive, so the method that handles them could catch the stack overflow. That said, I am aware of how hard handling stack overflows is. – configurator Nov 17 '11 at 16:00
  • +1 Not an answer, per se, but I'll update if I can get a dependable repro. – Justin Self Nov 17 '11 at 20:47
4

I got this error in one project when I switched from Visual Studio 2012 to Visual Studio Community 2013. In my case it was giant file (25k lines, not written by me) with had List<string[]> initialized by collection initializer.

Something like this:

public class Class
{

    public List<string[]> BigList
    {
        get
        {
            return new List<string[]>()
            {
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                .
                .
                .
                .
                .
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                new string[]{"foo","bar"}
            }
        }
    }
}

I changed it to string[][] and the project started to compile

public class Class
{

    public string[][] BigList
    {
        get
        {
            return new string[][]
            {
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                .
                .
                .
                .
                .
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                new string[]{"foo","bar"},
                new string[]{"foo","bar"}
            }
        }
    }
}
Mariusz Pawelski
  • 25,983
  • 11
  • 67
  • 80
  • This fixed my issue too. I had a List with over 40,000+ words in it and was getting that error and changing it to a string[] resolved the error!!! – MattyMerrix Jun 15 '15 at 15:55
2

When building you can see the the build output the last folder it checks before it fails. I removed the files in that folder and brought them back one by one. Finally found the issue. I dont know exactly what it is but it was a .aspx page with lots of HTML. It wasnt used often so I just removed it from the project and now it compiles.

enter image description here

enter image description here

Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
2

I had the same problem in a 64-bit machine (VS 2012).

I used @MikeFlynn's answer to locate the folder causing the error.

Finally I discovered I had a Help.aspx page with no code behind - just HTML but it had multiple icon images embedded as base 64

<img src="data:image/png;base64 ... />

I converted it to static HTML and it compiled.

P.S. The same project was compiling O.K. in a 32-bit VS2012 machine. Both machines were running Windows 7.

Alkis Giamalis
  • 300
  • 7
  • 14
2

I got this problem today in visual studio 2019. I got a really long string

<img height="445" src="data:image/png;base64,iVBORw...

in my file.aspx file. Even long images in vs2019 that can cause this problem.

Francesco
  • 47
  • 1
  • 4
1

I got this problem today. Somehow I got a really long string in my index.cshtml file. So check for long strings that can cause this problem.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Johan Byrén
  • 890
  • 2
  • 13
  • 28
1

If cleaning and rebuilding works, it's obviously not a problem with your code. You should report this to Microsoft, seems like a VS bug.

zmbq
  • 38,013
  • 14
  • 101
  • 171
0

i got this error cause of very big svg file. and after google it and some personal experiment i found that the solution for big svg file is:

@Html.Raw(File.ReadAllText(Server.MapPath("~/image.svg")))

in razor file there is another approach with html partial but unfortunately this trick is not working with big svg file.

hope this help..

Setmax
  • 946
  • 6
  • 10
0

I can share our experience with this error. Typical Microsoft masterpiece (or just a piece...) Our problem was actually due to too many DLLs for IIS to compile. Diagnosing it was a pain (as diagnostics is nonexistent). Removing some unnecessary DLLs (like unit test DLLs) from the main BIN folder has gotten rid of the problem. Go figure...

0

While running from IDE, the application was working fine, on hosting into IIS, the above error was coming, after trying out many steps given base on images and base64, etc. Following setting on the publish web.config worked for me, earlier debug = "true" was set, made it false, and it worked.

<compilation debug="false" defaultLanguage="c#" maxBatchGeneratedFileSize="1000" maxBatchSize="1000" targetFramework="4.5">
Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
0

I have never seen this in the wild.

However, from googling around it may well be from an excess of assembly references, one particular quote:

If I reduce number of referenced assemblies to 5500 it is compiled and working

Now, surely, you would have noticed a dependency list that large, could you check whether you have an overly large number of assemblies referenced?

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 3
    How could anyone reference 5 thousand assemblies? – svick Nov 16 '11 at 20:32
  • Just checked; it doesn't appear to be overly large. Is that person referring to 5500 individual assemblies? Or just adding up the number of assemblies reference in each project and counting the duplicates like System, System.XML etc? – Justin Self Nov 16 '11 at 20:35
  • I am getting this error, I believe for this same reason and I'm only referencing 63 assemblies. – matthew_360 Oct 17 '14 at 19:04