.Net Core - CS0012 'Object' is defined in an assembly that is not referenced
<p>I'm brand new to .Net Core and I'm trying to build a build system based on it. As part of this project, I've created an abstract class that spells out what a build task should implement, and I've stuffed this into a shared library.</p>

<p>The executable project references this library and scans project directories for a specially named directory, then checks to see if there are any <code>.cs</code> files in there. These scripts are loaded and then compilation is attempted using the tools provided through <code>Microsoft.CodeAnalysis</code> and friends.</p>

<p>With that background, here's an odd issue I'm hitting at the compilation stage:</p>

<p>If I attempt to supply the shared library containing the abstract class to the compilation process as a reference, I get the following error:</p>

<blockquote>
  <p>Failed CS0012: The type 'Object' is defined in an assembly that is not
  referenced. You must add a reference to assembly 'mscorlib,
  Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.</p>
</blockquote>

<p>Followed by a bunch complaints about predefined types:</p>

<blockquote>
  <p>CS0518: Predefined type 'System.Boolean' is not defined or imported
  CS0518: Predefined type 'System.Boolean' is not defined or imported
  CS0518: Predefined type 'System.Void' is not defined or imported ...
  etc etc.</p>
</blockquote>

<p>However, if I omit the reference and instead parse each of the shared library's source files into syntax trees and pass them to the compilation process, the whole process succeeds and I get a returned in-memory assembly I can pull types out of and instantiate.</p>

<p>I've read everything on this error that Google has to offer, and I'm at a loss. Can somebody enlighten me as to why this is happening and bonus internet points for how I can accomplish my original goal of simply linking in a common shared library?</p>

<p><strong>Relevant code</strong>  </p>

<pre><code>    CSharpParseOptions parseOptions = CSharpParseOptions.Default;

    SyntaxTree jobSyntaxTree = CSharpSyntaxTree.ParseText(scriptContents, parseOptions);

    string generatedAssemblyName = Path.GetRandomFileName();
    var referencedAssemblies = Assembly.GetEntryAssembly().GetReferencedAssemblies();

    foreach(var referencedAssembly in referencedAssemblies)
    {
        var rfas = Assembly.Load(referencedAssembly);
        references.Add(MetadataReference.CreateFromFile(rfas.Location)); 
    }

    var op = new  CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

    CSharpCompilation compilation = CSharpCompilation.Create(
       generatedAssemblyName,
       syntaxTrees: new[] { jobSyntaxTree },
       references: references,
       options: op);

    var ms = new MemoryStream();

    EmitOptions emitOptions = new EmitOptions();

    EmitResult result = compilation.Emit(ms);

    if(result.Success)
    {
       // Yay
    }
    else
    {
       // Boo-hoo
    }
</code></pre>

<p><strong>Project.json file for the shared library</strong> </p>

<pre><code>{
    "title": "BuildBotCore",
    "version": "1.0.0-*",
    "buildOptions": {
        "emitEntryPoint": false,
        "preserveCompilationContext": true
    },
    "dependencies": {
        "Microsoft.NETCore.App": {
            "type": "platform",
            "version": "1.0.0"
        },
        "System.Runtime": "4.1.0",
        "System.Runtime.Loader": "4.0.0",
        "Microsoft.NETCore.Portable.Compatibility": "1.0.1"
    },
    "frameworks": {
        "netcoreapp1.0": {
            "imports": "netcore50",
            "buildOptions": {
                "preserveCompilationContext": true
            }
        }
    },
    "configurations": {
        "Debug": {
            "buildOptions": {
                "define": [
                    "DEBUG",
                    "TRACE"
                ],
                "optimize": false,
                "preserveCompilationContext": true
            }
        },
        "Release": {
            "buildOptions": {
                "define": [
                    "RELEASE",
                    "TRACE"
                ],
                "optimize": true,
                "preserveCompilationContext": true
            }
        }
    }
}
</code></pre>

<p><strong>Project.json for main executable</strong> </p>

<pre><code>{
    "version": "1.0.0-*",
    "buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true
    },
    "dependencies": {
        "Microsoft.NETCore.App": {
            "type": "platform",
            "version": "1.0.0"
        },
        "System.Runtime": "4.1.0",
        "Microsoft.CodeAnalysis.CSharp": "1.3.2",
        "System.Runtime.Loader": "4.0.0",
        "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
        "BuildBotCore": "1.0.0-*"
    },
    "frameworks": {
        "netcoreapp1.0": {
            "imports": "netcore50",
            "buildOptions": {
                "preserveCompilationContext": true
            }
        }
    },
    "configurations": {
        "Debug": {
            "buildOptions": {
                "define": [
                    "DEBUG",
                    "TRACE"
                ],
                "optimize": false,
                "preserveCompilationContext": true
            }
        },
        "Release": {
            "buildOptions": {
                "define": [
                    "RELEASE",
                    "TRACE"
                ],
                "optimize": true,
                "preserveCompilationContext": true
            }
        }
    }
}
</code></pre>