dotnet / dotnet/msbuild

Error diagnostic present for .net core project opened using MSBuild API

Open
#5,002 3 comments 0 reactions 1 assignee Claimed by @rainersigwald View on GitHub
triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 13h
Merged PRs (30d)
133

Description

### Description
Hi,

I'm trying to open a .net core project using msbuild api from a .net framework cli program (to later use Roslyn). The project is a simple Hello World created by VS with 1 unit testing project added.

The code that opens it basically does following (I'm attaching actual working programs below):
```
MSBuildLocator.RegisterMSBuildPath(vs2019Instance.MSBuildPath)
var workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync(path);
var compilation = await project.GetCompilationAsync();
var diagnostics = compilation.GetDiagnostics();
```

**When I compile this program with VS and run it at once from cli, the diagnostics contains errors**
For example:

```
C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\UnitTest1.cs(1,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\UnitTest1.cs(2,7): error CS0246: The type or namespace name 'Xunit' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\pkruk\.nuget\packages\microsoft.net.test.sdk\16.2.0\build\netcoreapp1.0\Microsoft.NET.Test.Sdk.Program.cs(2,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\UnitTest1.cs(6,18): error CS0518: Predefined type 'System.Object' is not defined or imported
```

**But if I debug this program in VS, the diagnostics has no errors!**

### Steps to reproduce

* Unpack [ConsoleApp8.zip](https://github.com/microsoft/msbuild/files/3995133/ConsoleApp8.zip) (solution with .net core application project and .net core unit test project)
* Unpack [[RoslynOpenProjectV2.zip](https://github.com/microsoft/msbuild/files/4019286/RoslynOpenProjectV2.zip) (solution with .net framework application that uses MSBuild api to open a project provided as argument)
* Compile both but **do not start debugging**
* Remove microsoft.build.dll and microsoft.framework.dll assemblies from build output (leave only microsoft.build.locator.dll)
* Run RoslynOpenProject with XUnitTestProject1 project, for example:
```
C:\Users\pkruk\Source\repro\RoslynOpenProject\RoslynOpenProject\bin\Debug>RoslynOpenProject.exe C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\XUnitTestProject1.csproj
```
* Notice there are error diagnostics in the output
* In VS run debug on this program (Debug>Start Debugging)
* Run RoslynOpenProject in command line again
* Notice there are NO error diagnostics in the output

### Expected behavior

```
C:\Users\pkruk\Source\repro\RoslynOpenProject\RoslynOpenProject\bin\Debug>RoslynOpenProject.exe C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\XUnitTestProject1.csproj
Project C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\XUnitTestProject1.csproj

6 not errors diagnostics exist.
0 error diagnostics:
```

(The not error diagnostics are warning that some "usings" are not needed)

### Actual behavior

```
C:\Users\pkruk\Source\repro\RoslynOpenProject\RoslynOpenProject\bin\Debug>RoslynOpenProject.exe C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\XUnitTestProject1.csproj
Project C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\XUnitTestProject1.csproj

3 not errors diagnostics exist.
15 error diagnostics:
C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\UnitTest1.cs(1,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\pkruk\Source\repro\ConsoleApp8\XUnitTestProject1\UnitTest1.cs(2,7): error CS0246: The type or namespace name 'Xunit' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\pkruk\.nuget\packages\microsoft.net.test.sdk\16.2.0\build\netcoreapp1.0\Microsoft.NET.Test.Sdk.Program.cs(2,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
[...]
```

(The not error diagnostics are warning that some "usings" are not needed)

### Environment data

For compilation I'm using Visual Studio 2019 16.4.0.

MSBuildLocator.QueryVisualStudioInstances finds following vs instances:
```
Name: Visual Studio Build Tools 2017
Version: 15.9.28307.960
MSBuildPath: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin
VisualStudioRootPath: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools

Name: Visual Studio Professional 2019
Version: 16.4.29519.181
MSBuildPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin
VisualStudioRootPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional

Name: Visual Studio Professional 2017
Version: 15.9.28307.718
MSBuildPath: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin
VisualStudioRootPath: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional
```

dotnet 3.1.100
Windows 10

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.