4

I'm trying to load an existing C# project and analyze one of its source files using Roslyn. However, when compiling the source file, I get compilation errors which make no sense and are not present when compiling with Visual Studio. (the code also runs fine)

Can anyone help me to diagnose this problem?

Parts of the code used to compile the file:

var oriSln = workspace.OpenSolutionAsync(@"absolutepath.sln").Result;
var oriProject = oriSln.Projects.First(p => p.Name == "projectname");
var doc = oriProject.GetDocument(docId);
var model = doc.GetSemanticModelAsync().Result;
var diag = model.GetDiagnostics(); // has strange errors

The errors:

thefile.cs(36,68): error CS0518: Predefined type 'System.Void' is not defined or imported
thefile.cs(40,74): error CS0518: Predefined type 'System.Void' is not defined or imported
thefile.cs(40,50): error CS1503: Argument 1: cannot convert from 'float' to 'int'
thefile.cs(41,50): error CS1503: Argument 1: cannot convert from 'float' to 'int'
thefile.cs(51,50): error CS1503: Argument 2: cannot convert from 'float' to 'int'
thefile.cs(58,58): error CS1503: Argument 2: cannot convert from 'float' to 'int'
thefile.cs(130,40): error CS0518: Predefined type 'System.ValueType' is not defined or imported
thefile.cs(168,55): error CS0246: The type or namespace name 'Vector3' could not be found (are you missing a using directive or an assembly reference?)
thefile.cs(158,38): warning CS0168: The variable 'ex' is declared but never used
thefile.cs(7,1): hidden CS8019: Unnecessary using directive.
thefile.cs(6,1): hidden CS8019: Unnecessary using directive.

The solution consists of about 30 projects, the target project references some of these, and also some third parties, one of which contains the Vector3.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
MHGameWork
  • 616
  • 5
  • 14

1 Answers1

2

I found a solution to this problem. One of the other projects i was referencing from my solution did not have any MetadataReferences loaded, so it was missing mscrolib. I think this causes the dependent project to throw these errors.

As of why it was loading no MetadataReferences, I'm not entirely sure why it didn't load any. I found that the csproj file had as a default configuration Debug|AnyCPU, which did not exist for this project, so i changed it to Debug|x86. It then loaded all references correctly and the compile errors i mentioned previously are gone now.

MHGameWork
  • 616
  • 5
  • 14
  • 1
    If it appeared that the configuration issue was the problem, feel free to file a bug on https://github.com/dotnet/roslyn if you haven't already. – Jason Malinowski Sep 21 '15 at 19:21