CSC : error CS7038: Failed to emit module
Asked Answered
O

20

24

After installing Visual Studio 2015 and building my project I receive the error

"CSC : error CS7038: Failed to emit module".

However my solution is building fine in Visual Studio 2013.

It is an ASP.NET webforms project .NET 4.0

Anyone?

UPDATE: it looks like the problem has to do with Red Gate Smart Assembly in combination with method parameters with default values.

UPDATE: Smart Assembly 6.9 fixes the error for me.

Omora answered 29/7, 2015 at 6:25 Comment(3)
Is there really no earlier error messages relating to this? If not, can you try to create a MCVE? We don't even know (at this point) what type of project you're trying to build.Geraldo
Sounds like a bug in Roslyn to me. If it's not a bug in compiling your project, than it's a bug in emiting a useful error message. Maybe try github.com/dotnet/roslyn/issuesCesura
It is an ASP.NET webforms project .NET 4.0Omora
E
7

Got the same error (fresh installation of the VS2015 Enterprise, ASP.NET webforms project .NET 4.0).

After some investigation I've found that there are two DLLs in references which causes this. Both are .Net 2.0 assemblies and both of them obfuscated by Red Gate Smart Assembly 6.5. And the real reason is... obfuscation.

Luckily, these assemblies are mine, so I've tried to build them without using of Smart Assembly - error is gone.

Interesting is that no any errors or warnings shown by Visual Studio before trying to build a project.

Good luck!

EDIT: Updating Smart Assembly to version 6.9 fixed an issue.

Emotionality answered 2/8, 2015 at 17:17 Comment(2)
any Ideas if im not using smart assembly, but im getting this errror?Rose
@Rose check out my answer below, maybe it will help you find the cause (if still relevant).Carmellacarmelle
A
14

Original Snippet:

    private void radButton1_Click(object sender, EventArgs e)
    {
        string perp = radTextBox1.Text;

        int i = 0;
        DataRow arp = ale.Rows[i];
        while (i <= ale.Rows.Count)
        {
            if (ale.Rows[i].Field<>("FullName") = perp)
            {
                arp = ale.Rows[i];
                ale.Rows.Remove(arp);
            }

        }

        i = ale.Rows.Count;
        radLabel1.Text = i.ToString();
    }

Changed this:

    if (ale.Rows[i].Field<>("FullName") = perp)

To This:

    if (ale.Rows[i].Field<String>("FullName") == perp)
Aleishaalejandra answered 4/7, 2016 at 16:10 Comment(1)
Yep! this is exactly how I first came across this. Field is strongly-typed, not specifying it doesn't show an error instead we see the glorious "Failed to emit blah blah" which doesn't help anything. This could be a tough piece to debug specially when you've made a lot of change after your last check-in or push. Took me about half an hour before I could get down to the root cause.Charlottcharlotta
E
7

Got the same error (fresh installation of the VS2015 Enterprise, ASP.NET webforms project .NET 4.0).

After some investigation I've found that there are two DLLs in references which causes this. Both are .Net 2.0 assemblies and both of them obfuscated by Red Gate Smart Assembly 6.5. And the real reason is... obfuscation.

Luckily, these assemblies are mine, so I've tried to build them without using of Smart Assembly - error is gone.

Interesting is that no any errors or warnings shown by Visual Studio before trying to build a project.

Good luck!

EDIT: Updating Smart Assembly to version 6.9 fixed an issue.

Emotionality answered 2/8, 2015 at 17:17 Comment(2)
any Ideas if im not using smart assembly, but im getting this errror?Rose
@Rose check out my answer below, maybe it will help you find the cause (if still relevant).Carmellacarmelle
P
5

As @Andrey reported this does appear to be an issue with obfuscated assemblies that is causing some difficulty for Roslyn. Today I was able to get a live repro of this error and the root cause appears to be the obfuscator is invalidating / corrupting how default parameter values are stored in metadata. When run through ildasm the default values will be displayed as:

.param [3] /* Invalid default value for 0800001F: */

The previous version of the compiler handled this scenario by treating the invalid value as null or default(T). We will be fixing Roslyn to have the same behavior.

Pedanticism answered 5/8, 2015 at 18:10 Comment(2)
How does one go about getting the updated Roslyn? I have a large project which I cannot run or build with no indication of where the error or default parameter is. Do I have any options?Rose
I have the same problem, updated rosylyn (via Microsoft Compiler using Nuget) and still seeing the CSC "Failed to emit module" error. Have downgraded all projects in sln to 4.5.2 as well, no changeCullan
C
5

I also had this exception being thrown in VB.NET (Visual Studio 2015 - Pro), and isolated a single line that was causing the error.

In the line of code below, if you define model as an integer, as in:

Dim model as Integer = 2

and then use:

Const N As Integer = model

you will throw the exception.

However, when I modified this to:

Dim N As Integer = model

the exception was not thrown. The Const syntax was legacy code from another program that I rapidly added model to, and constants can't be set to pre-defined integer types.

Crellen answered 3/2, 2016 at 22:11 Comment(2)
Similar issue -- had Const Sql as String = "SQL STATEMENT" & nonConstantString & "END SQL STATEMENT". It can't be a Const due to the concatenation of the non const string. Instead of an error message, I got Failed to emit module.Seigneury
Same sort of situation here. Took me a while to figure out how to find the cause: eventually I had to run a Find on the project for Const and search through these to eliminate 'calculated' constsUnstrained
E
3

I just had "Failed to emit module". I mistakenly put empty brackets in a call to a generic extension method, only when inside a ternary operator, like this:

var a = b == 6 ? 8 : x.Foo<>();

(Outside of a ternary op, I just get regular error CS7003: Unexpected use of an unbound generic name.)

Engelhart answered 23/10, 2017 at 13:9 Comment(1)
<> empty brackets was the cause for a "Failed to emit module" when developing with Xamarin.Forms with FindByName<>Suitor
M
2

Extending on the answer from @jony-adamit:

I also had this compilation error being thrown in C# (Visual Studio 2015). It came down to the differences in compiler output from .NET 4.5 and Roslyn the new compiler in VS2015. You can take the below code and run it yourself or use dotnetfiddle (https://dotnetfiddle.net/fa2rMs) and switch between compilers.

In .NET 4.5 compiler (C# compiler version 12.0 or earlier) you get this message:

Compilation error (line 16, col 7): The type or namespace name 'Select' does not exist in the namespace 'Tools.Weapons' (are you missing an assembly reference?)

In the Roslyn 1.0.0 and 1.1.0 compiler versions you get this message:

Compilation error (line 1, col 1): Failed to emit module 'MyAssembly'.


Code to reproduce the error (notice the namespace):

public class Program
{   
    public static void Main()
    {
        Tools.Delegater.Work();
    }
}

namespace Tools 
{
    public static class Delegater
    {   
        public static System.Action Work = () => 
        {
            var query = from x in Weapons
                        select x;
        };
    }
}

namespace Tools.Weapons
{
    public class Cannon { }
}

As you can tell from the compiler messages, Roslyn's message leaves you guessing as to the what and the where about the compiler error. And depending on the size of the application, the lack of details could take days or weeks to discover the root cause. Whereas the message in the previous compiler pointed you to the exact spot to start reviewing your code.

Another big difference is the previous compiler will show you a syntax error in Visual Studio for this scenario, but unfortunately Roslyn (atm) does not. However, if you knew where to look and you hovered your mouse over the 'x' variable in the linq to sql statement, then you would see that Rosyln doesn't understand how to interpret it.

Micropyle answered 15/7, 2016 at 21:22 Comment(0)
U
2

I was getting a similar error: "BC36970. Failed to emit module 'Something.dll'." I realize the error code is not the same but it is also a "failed to emit" type issue so I thought I'd share my answer.

Problem: My problem was that I had a string that was a constant but I was trying to append another string to it, like so (using VB code):

Dim MyString1 As String = "Test1"
Const MyString2 As String = "Test2" & MyString1

Solution: I simply had to convert the second string from Const to Dim and the error disappeared:

Dim MyString1 As String = "Test1"
Dim MyString2 As String = "Test2" & MyString1
Unwept answered 20/9, 2016 at 18:4 Comment(1)
Thanks. Helped me look back at my code. For a change, this wasn't a visual studio issue!Cytotaxonomy
P
2

I got this error when using a generic method but failing to include the type.

This code gave the error:

example.GetValue<>(foo);

Correction simply was to specify the type:

example.GetValue<string>(foo);

I'm not sure if the erroring example is valid syntax or not: I would have expected a syntax error if it is not rather than this error, and Intellisense accepted the syntax.

Premeditation answered 2/11, 2017 at 18:21 Comment(0)
C
1

There's another bug that can cause this exact error:

Happened to me after changing a property name without refactoring, which caused some linq code to call the namespace itself!

To see the bug in action all you have to do is run this piece of code:

public static int Test()
{
    var x = from c in ConsoleApplication5
        select 3;

    return x.First();
}

Where ConsoleApplication5 is the namespace!
This only happens in VS2015.

Update:
There's an issue for this now on GitHub if anyone's interested.

Carmellacarmelle answered 23/12, 2015 at 8:8 Comment(1)
@JaredPar, I've sent a feedback through VS, but maybe you can speed things from within :-) This happens in the latest VS2015 Update 1.Carmellacarmelle
C
1

I got this error when I was working with data table and did not provide a data type where it was required:

bool temp = dtTab.AsEnumerable().Any(row => row.Field<>("active") == 1);

Giving it a proper data type got rid of the error. I also had to convert it to string to be able to compare it correctly-

bool temp = dtTab.AsEnumerable().Any(row => row.Field<Boolean>("active").toString() == "1");
Cytotaxonomy answered 7/11, 2016 at 20:20 Comment(0)
L
1

I got this error too, seems like there is some major reasons that cause this error.

I posted this answer to wrap up mentioned solutions and help other guys so they can solve this error quickly

Please read all answers here on this page carefully, I'm sure you can solve the issue using one of mentioned solutions, good luck :)

Legalism answered 31/1, 2017 at 8:20 Comment(0)
S
1

This answer is similar to that posted by @wrtsvkrfm

  'Dim NEWLINE As String = "<BR/>"
   Dim NEWLINE As String = vbCrLf

 Const HT_QTC_VENDOR As String = "<B>some stuff {0}</B>" & NEWLINE

This will cause the DLL to be not emitted,

Change the top 2 lines to

  'Const NEWLINE As String = "<BR/>"
   Const NEWLINE As String = vbCrLf

and the error goes away. This should be a compilation error. A CONST clearly cant depend on a variable, which may..., well..., vary. :-)

Studding answered 16/8, 2017 at 21:41 Comment(0)
L
0

I get the same "Failed to emit module" error with this code but changing the "Const" to "Dim" resolves the problem:

Dim userStatus1 As String = convAttrSet("user_status")
Dim userStatus2 As String = convAttrSet("user_status")
Const PENDING As Boolean = (userStatus1 = userStatus2)
Lesko answered 22/9, 2016 at 3:41 Comment(0)
C
0

I got this error when using linq to entities and my linq query had a syntax error in the join clause

Coo answered 7/11, 2017 at 22:39 Comment(0)
T
0

For me the issue occured after adding a postbuild step that changes the assemblyinfo.cs (NetRevisionTool.exe). Take care to not change the AssemblyInfo.cs between building and Edit and continue.

Temptress answered 5/12, 2017 at 16:57 Comment(0)
M
0

I also had this exception being thrown in VB.NET (Visual Studio 2015 - Pro), and isolated a single line that was causing the error

     Faulty....
    Dim i As Short
    Dim j As Short = BancoCombienElement(LaChaine)
    Const a  = (22 / 60) * j
    Dim TiTxt As String = ""
    Dim S_8_10_12_14 As String = ""

    Work good!!!!!!!!
    Dim i As Short
    Dim j As Short = BancoCombienElement(LaChaine)
    Dim a As Short = (22 / 60) * j
    Dim TiTxt As String = ""
    Dim S_8_10_12_14 As String = ""
Mallarme answered 8/4, 2018 at 21:1 Comment(1)
The source code seems to be off topic, Only the exception is similar. It would be nice to narrow down the answer to the specific line from 5 lines to 1.Lallation
B
0

Reason: This error seems to be introduced when we have something weird in our code. For me I made a mistake while getting the current user from identity.

Error Code: int currentUserId = User.Identity.GetUserId<>();

Fixed Code: Please specify the data type after GetUserID<---------->();In this case I have specified int.

Note Please try to undo your code step by step so that you can find this issue. But otherwise it was very difficult for me track that error.

Thanks

Byssinosis answered 27/8, 2018 at 13:14 Comment(0)
S
0

This can happen for any error that the compiler did not catch. Probably

  1. either the compiler doesn't know what IL to emit, or
  2. it knows but in the process of actually doing it, something unexpected happens or
  3. the IL that it emits is invalid and some safety net at a lower level than the compiler itself produces an error that the compiler is not built to handle.

In any case, this CS7038 occurs because the C# is valid and there is a good chance the compiler encountered an error in such an unexpected place that it is no longer able to trace it to any source code line that you wrote. Hence the seemingly random errors that people report here to cause this.

I've used C# for decades and I arrive here now because this is the first time ever I get an error like this.

In my case the following code generates the error. Use <LangVersion>Preview</LangVersion> in the project file and use the C# 8 compiler in Visual Studio 2019 preview.

void FailToEmitDll(ReadOnlySpan<char> span) {
  ReadOnlySpan<char> temp;
  temp = 1 switch
  {
    _ => span
  };
}

Odd enough, if you change the declaration and the assignment to read like var temp instead, all works well.

Shrift answered 3/5, 2019 at 6:56 Comment(0)
R
0

Adding to the list of possible causes, in VS2019 with C# 8, I got the "Failed to emit module" message when doing something like:

_ = someBool ? SomeVoid() : default;

I filed a bug report about it here: https://github.com/dotnet/roslyn/issues/41741

Rosemarierosemary answered 17/2, 2020 at 12:51 Comment(0)
M
0

I got this error when I run a complex expression in Mock object, I fixed this by store the expression result to local variable.

Mannequin answered 23/2, 2023 at 10:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.