Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Predefined type 'System.*' is not defined or imported #6615

Closed
edgardozoppi opened this issue Nov 6, 2015 · 8 comments
Closed

Predefined type 'System.*' is not defined or imported #6615

edgardozoppi opened this issue Nov 6, 2015 · 8 comments
Assignees
Milestone

Comments

@edgardozoppi
Copy link
Member

Hi,

I am trying to compile a solution using Roslyn. I have installed in my machine VS 2013 and Build Tools 2015. But I am getting a lot of CS0518 errors from Roslyn like the following one:

error CS0518: Predefined type 'System.Object' is not defined or imported

Is not only for System.Object, but also for System.String, System.Void, etc. (all kind of standard types defined in mscorlib or other standard .net libraries).

The solution I want to compile with Roslyn compiles just fine in VS 2013. Also, after installing VS 2015 those erros are not longer given by Roslyn when compiling the same solution. Of course, the solution is also compiling fine in VS 2015.

I already verified the target frameworks of all the projects in the solution and they all target 4.5 that I have installed in my machine (otherwise it shouldn't compile with VS and it does).

I am loading the solution like this:

        var props = new Dictionary<string, string>()
        {
            { "CheckForSystemRuntimeDependency", "true" }
        };

        var ws = MSBuildWorkspace.Create(props);
        var solution = ws.OpenSolutionAsync(solutionPath);

I searched for a solution to this problem but I only found the following link. According to that link, this problem seems to be related with non-portable projects referencing portable assemblies but I am not sure if this is the case here.

http://stackoverflow.com/questions/29523473/roslyn-compilation-doesnt-resolve-mscorlib-references

Do you know how can I solve this problem without having to install VS 2015?

Thanks a lot,
Edgardo.

@Pilchie Pilchie added this to the 1.2 milestone Nov 6, 2015
@Pilchie Pilchie self-assigned this Nov 6, 2015
@bigbaldy1128
Copy link

I also encountered this problem,I found project.MetadataReferences.Count==0,I have a lot of samples that can reproduce this problem,if you need,I can copy them to you,I do hope this problem can be fixed

@mattwar
Copy link
Contributor

mattwar commented Jan 11, 2016

If you are experiencing problems loading projects/solutions with MSBuildWorkspace you'll want to try adding these redirects for the msbuild API to your application's .config file. These are the same redirects the msbuild.exe and devenv.exe (visual studio) use.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="14.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Engine" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="14.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="14.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Conversion.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="14.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Tasks.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="14.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Utilities.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="14.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

@bigbaldy1128
Copy link

I have added these redirects,but not work,I testing project is https://github.com/opensim/opensim.git,after the download,run runprebuild.bat and load OpenSim.sln,almost all projects MetadataReferences is 0

@mattwar
Copy link
Contributor

mattwar commented Jan 13, 2016

I've taken a look at the opensim projects.

The opensim project's are failing to build on the command line (msbuild.exe), but not inside VS. When using the msbuild API they fail silently (without exception message.)

The project files (.csproj) that are generated by the pre-build tool do not have conditional nodes that handle the default case when no configuration or platform properties are set. When the are not set, the default behavior will be used. But in this case, since there is no handling of the missing properites, that means a build failure.

You can assign them when you create the MsBuildWorkspace and they will be passed along to the msbuild API which is used to interpret the project files.

 var ws = MSBuildWorkspace.Create(
      new Dictionary<string, string>() { { "Configuration", "Debug" }, { "Platform", "AnyCPU" } });

If you do this, then the MSBuildWorkspace will successfully open the projects.

@bigbaldy1128
Copy link

Thank you very much,my problem is solved

@mattwar mattwar closed this as completed Jan 13, 2016
@ziriax
Copy link

ziriax commented Apr 18, 2017

I'm experiencing this problem with just a single project in my solution, while I do pass the Configuration and Platform parameters to the MSBuildWorkspace. I have no clue what is different between this project and the others, all projects target .NET 4.6.2, and the same configuration and platform (x86).

Do you know other things that could cause this behavior?

@GrahamTheCoder
Copy link
Contributor

GrahamTheCoder commented Jul 25, 2020

Google leads here for this issue so I'll add some extra info for people

I've seen this issue (0 metadata references) occur in VS 2019 16.6 when calling Roslyn APIs on a project in the Visual Studio workspace. I've seen the same issue with MSBuildWorkspace (with the platform and configuration props defined)
The solution had a mix of dot net core, standard and framework (non-sdk style) projects (in VB and C#) and I suspect that mix of old and new is the root cause of the issue. Unfortunately, that's a state a lot of people are in, so my extension and related dot net tool need to deal with it somehow.

Calling dotnet restore on the solution doesn't fix it.
Building the project in Visual Studio first does fix it, so there must be some other bit of "restoring" needed that only the Visual Studio build process knows how to do. So I'm going to call Visual Studio build from within the VS extension. I have no workaround for the dotnet tool using an MSBuildWorkspace for now.

@GrahamTheCoder
Copy link
Contributor

GrahamTheCoder commented Jul 25, 2020

Partial repro for context. Save this as a vbproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <RootNamespace>ClassLibrary4</RootNamespace>
    <TargetFramework>netstandard2.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="FluentAssertions" Version="5.10.3" />
  </ItemGroup>
</Project>
  1. Open in Visual Studio (debugged from another instance)
  2. Do a solution build
  3. Double click the project file and change the TargetFramework to netstandard2.0
  4. Save the file
  5. Break process execution

If you inspect the workspace's projects you'll notice that all the 113 .net metadata references are missing. If you try something like compilation.GetTypeByMetadataName("System.Int32") it returns null.

If you run a dotnet restore on the project, then break process execution and look again: All is fine.
Is there a programmatic API for doing this that works for a solution with a combination of netstandard and netframework?

Other related:

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

6 participants