dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.86k stars 672 forks source link

"random" failure to load project Microsoft.Build.Exceptions.InvalidProjectFileException: The SDK 'Microsoft.NET.Sdk' specified could not be found #2330

Open rshillington opened 6 years ago

rshillington commented 6 years ago

Environment data

.NET Command Line Tools (2.1.105)

Product Information:
 Version:            2.1.105
 Commit SHA-1 hash:  141cc8d976

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.13-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.105/

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.7
  Build    : 2d61d0b043915bc948ebf98836fefe9ba942be11

VS Code version: 1.23.1

C# Extension version: 1.15.2

Steps to reproduce

Open a folder with a valid .csproj file and source code

Expected behavior

OmniSharp.MSBuild.ProjectManager should load without problem.

I tend to open VS Code a number of different ways, but the method that seems to produce this error is from terminal code .

Actual behavior

       Failed to load project file '/Users/ralphshillington/[.....].csproj'.
/Users/ralphshillington/[....].csproj(1,1)
Microsoft.Build.Exceptions.InvalidProjectFileException: The SDK 'Microsoft.NET.Sdk' specified could not be found.  /Users/ralphshillington/[......].csproj
  at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject (System.String errorSubCategoryResourceName, Microsoft.Build.Shared.IElementLocation elementLocation, System.String resourceName, System.Object[] args) [0x00042] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Shared.ProjectErrorUtilities.VerifyThrowInvalidProject[T1] (System.Boolean condition, System.String errorSubCategoryResourceName, Microsoft.Build.Shared.IElementLocation elementLocation, System.String resourceName, T1 arg0) [0x00003] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject[T1] (Microsoft.Build.Shared.IElementLocation elementLocation, System.String resourceName, T1 arg0) [0x00000] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Evaluator`4[P,I,M,D].ExpandAndLoadImportsFromUnescapedImportExpressionConditioned (System.String directoryOfImportingFile, Microsoft.Build.Construction.ProjectImportElement importElement, System.Collections.Generic.List`1[Microsoft.Build.Construction.ProjectRootElement]& projects, System.Boolean throwOnFileNotExistsError) [0x0019e] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Evaluator`4[P,I,M,D].ExpandAndLoadImports (System.String directoryOfImportingFile, Microsoft.Build.Construction.ProjectImportElement importElement) [0x00024] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Evaluator`4[P,I,M,D].EvaluateImportElement (System.String directoryOfImportingFile, Microsoft.Build.Construction.ProjectImportElement importElement) [0x0000d] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Evaluator`4[P,I,M,D].PerformDepthFirstPass (Microsoft.Build.Construction.ProjectRootElement currentProjectOrImport) [0x000e6] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Evaluator`4[P,I,M,D].Evaluate (Microsoft.Build.BackEnd.Logging.ILoggingService loggingService, Microsoft.Build.Framework.BuildEventContext buildEventContext) [0x000f8] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Evaluator`4[P,I,M,D].Evaluate (Microsoft.Build.Evaluation.IEvaluatorData`4[P,I,M,D] data, Microsoft.Build.Construction.ProjectRootElement root, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings, System.Int32 maxNodeCount, Microsoft.Build.Collections.PropertyDictionary`1[T] environmentProperties, Microsoft.Build.BackEnd.Logging.ILoggingService loggingService, Microsoft.Build.Evaluation.IItemFactory`2[S,T] itemFactory, Microsoft.Build.Evaluation.IToolsetProvider toolsetProvider, Microsoft.Build.Evaluation.ProjectRootElementCache projectRootElementCache, Microsoft.Build.Framework.BuildEventContext buildEventContext, Microsoft.Build.Execution.ProjectInstance projectInstanceIfAnyForDebuggerOnly, Microsoft.Build.BackEnd.SdkResolution.ISdkResolverService sdkResolverService, System.Int32 submissionId) [0x00018] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Project.Reevaluate (Microsoft.Build.BackEnd.Logging.ILoggingService loggingServiceForEvaluation, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings) [0x00046] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Project.ReevaluateIfNecessary (Microsoft.Build.BackEnd.Logging.ILoggingService loggingServiceForEvaluation, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings) [0x00034] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Project.ReevaluateIfNecessary (Microsoft.Build.BackEnd.Logging.ILoggingService loggingServiceForEvaluation) [0x00000] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Project.ReevaluateIfNecessary () [0x00007] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Project.Initialize (System.Collections.Generic.IDictionary`2[TKey,TValue] globalProperties, System.String toolsVersion, System.String subToolsetVersion, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings) [0x00126] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Project..ctor (System.String projectFile, System.Collections.Generic.IDictionary`2[TKey,TValue] globalProperties, System.String toolsVersion, System.String subToolsetVersion, Microsoft.Build.Evaluation.ProjectCollection projectCollection, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings) [0x0009c] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Project..ctor (System.String projectFile, System.Collections.Generic.IDictionary`2[TKey,TValue] globalProperties, System.String toolsVersion, Microsoft.Build.Evaluation.ProjectCollection projectCollection, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings) [0x00000] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.Project..ctor (System.String projectFile, System.Collections.Generic.IDictionary`2[TKey,TValue] globalProperties, System.String toolsVersion, Microsoft.Build.Evaluation.ProjectCollection projectCollection) [0x00000] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.ProjectCollection.LoadProject (System.String fileName, System.Collections.Generic.IDictionary`2[TKey,TValue] globalProperties, System.String toolsVersion) [0x000f5] in <61115f75067146fab35b10183e6ee379>:0 
  at Microsoft.Build.Evaluation.ProjectCollection.LoadProject (System.String fileName, System.String toolsVersion) [0x00000] in <61115f75067146fab35b10183e6ee379>:0 
  at OmniSharp.MSBuild.ProjectLoader.EvaluateProjectFileCore (System.String filePath) [0x0003e] in <64f2a38974d54ecaaa93e3697628b738>:0 
  at OmniSharp.MSBuild.ProjectLoader.BuildProject (System.String filePath) [0x0000d] in <64f2a38974d54ecaaa93e3697628b738>:0 
  at OmniSharp.MSBuild.ProjectFile.ProjectFileInfo.Load (System.String filePath, OmniSharp.MSBuild.ProjectLoader loader) [0x00014] in <64f2a38974d54ecaaa93e3697628b738>:0 
  at OmniSharp.MSBuild.ProjectManager+<>c__DisplayClass23_0.<LoadProject>b__0 () [0x00000] in <64f2a38974d54ecaaa93e3697628b738>:0 
  at (wrapper delegate-invoke) System.Func`1[System.ValueTuple`2[OmniSharp.MSBuild.ProjectFile.ProjectFileInfo,System.Collections.Immutable.ImmutableArray`1[OmniSharp.MSBuild.Logging.MSBuildDiagnostic]]].invoke_TResult()
  at OmniSharp.MSBuild.ProjectManager.LoadOrReloadProject (System.String projectFilePath, System.Func`1[TResult] loadFunc) [0x0001b] in <64f2a38974d54ecaaa93e3697628b738>:0 
DustinCampbell commented 6 years ago

@rshillington: Could you provide your full OmniSharp Log? Seeing the information at the top is helpful for seeing how OmniSharp was initialized.

rshillington commented 6 years ago

Attached is I believe what you're looking for? Perhaps there's a way to get OmniSharp to be more verbose in its logging, which I'm happy to turn on.

omnisharp.log

DustinCampbell commented 6 years ago

Thanks! That was what indeed I was looking for. Seeing the top of the log where the information about how MSBuild is set up is very helpful.

I have a couple of potential options to try that might address the problem for you:

  1. Try setting the "omnisharp.path" VS Code setting to "latest". This will cause C# for VS Code to download and install the latest build of OmniSharp, which includes some recent fixes to the MSBuild project system. One of these fixes is around SDK resolution, which might positively impact your setup.

  2. If that doesn't work, try downloading and installing the latest stable Mono MDK from http://www.mono-project.com/download/stable/. When C# for VS Code detects a valid version of Mono on the machine, it will launch OmniSharp on it. And, in that case, it will use the MSBuild bits that ship with Mono. Note: If you choose to install via Homebrew, use brew cask install mono-mdk rather than brew install mono. The latter does not include a recent enough version of Mono.

Please let me know if either of these solutions work for you.

tomvonclef commented 6 years ago

Hi @DustinCampbell ,

I've had the same issue after trying your steps above. I just installed .NET Core on MacOS and tried to get the dotnet Angular project template working. My error message is

[warn]: OmniSharp.MSBuild.ProjectManager
        Failed to load project file '/Users/tvc/dotnet-angular/dotnet-angular.csproj'.
/Users/tvc/dotnet-angular/dotnet-angular.csproj(1,1)
Microsoft.Build.Exceptions.InvalidProjectFileException: The SDK 'Microsoft.NET.Sdk.Web' specified could not be found.

(The file dotnet-angular.csproj begins with <Project Sdk="Microsoft.NET.Sdk.Web">).

My dotnet --info:

.NET Command Line Tools (2.1.200)

Product Information:
 Version:            2.1.200
 Commit SHA-1 hash:  2edba8d7f1

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.12
 OS Platform: Darwin
 RID:         osx.10.12-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.200/

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.7
  Build    : 2d61d0b043915bc948ebf98836fefe9ba942be11

I am using VS Code Version 1.23.1.

I followed your advice and set "omnisharp.path" to "latest" and ran the command brew cask install mono-mdk, but I'm still having the issue.

Here is the OmniSharp Log with loggingLevel set to "trace": vscode_sdk_error_tracelog.txt

FYI the directory /Users/tvc/.vscode/extensions/ms-vscode.csharp-1.15.2/.omnisharp/1.30.1-beta.63/omnisharp/msbuild/15.0/Bin/Sdks does not exist. Is it possible to override MSBuildSDKsPath? I can't seem to change it via an environmental variable.

DustinCampbell commented 6 years ago

@tomvonclef : The log that you provided leads me to believe that that something is different about your machine configuration because it didn't even detect that Mono had been installed. When you run mono --version in a terminal window, do you get any output? Have you set your Path environment variable differently in some way?

tomvonclef commented 6 years ago

@DustinCampbell mono --version outputs:

Mono JIT compiler version 5.12.0.226 (2018-02/9824e260f56 Mon Apr 30 18:45:52 EDT 2018)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           normal
        SIGSEGV:       altstack
        Notification:  kqueue
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        Interpreter:   yes
        LLVM:          yes(3.6.0svn-mono-master/8b1520c8aae)
        GC:            sgen (concurrent by default)

mono is /Library/Frameworks/Mono.framework/Versions/Current/Commands/mono

My PATH is: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/Library/Frameworks/Mono.framework/Versions/Current/Commands

Is there an issue with just using ~/.vscode/extensions/ms-vscode.csharp-1.15.2/.omnisharp/1.30.1-beta.63/bin/mono.osx?

Also, I'm not sure my situation is different from @rshillington. The log lines below show up for me because my logging level includes debug messages. I don't understand why a failure to locate Mono is a debug message that never shows up in the VS Code UI.

[dbug]: OmniSharp.MSBuild.Discovery.Providers.MonoInstanceProvider
        Could not retrieve Mono runtime path
DustinCampbell commented 6 years ago

Yes, I agree that the behavior seems similar between the both you and @rshillington. Do you also see different behavior depending on if you launch VS Code by typing code . at a terminal window vs. launch VS Code from the OSX launch bar? Are you able to launch dotnet --info and mono --version from within VS Code's integrated terminal?

tomvonclef commented 6 years ago

OK, things work if I launch via typing code . in the terminal!

FYI, things are still broken if I launch from the dock. dotnet --info and mono --version return the same correct results from VS Code's integrated terminal whether I launch from the dock or the terminal.

Here is the log created when I launch from the terminal and things work: omnisharp-vscode-tracelog-started-from-terminal.txt

Launching with code . causes the version of Mono installed with brew to be discovered:

[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 2 MSBuild instance(s)
            1: Mono 15.0 - "/Library/Frameworks/Mono.framework/Versions/5.12.0/lib/mono/msbuild/15.0/bin"
            2: StandAlone 15.0 - "/Users/tvc/.vscode/extensions/ms-vscode.csharp-1.15.2/.omnisharp/1.30.1-beta.63/omnisharp/msbuild/15.0/Bin"

Also, MSBuildSDKsPath gets set to /Library/Frameworks/Mono.framework/Versions/5.12.0/lib/mono/msbuild/15.0/bin/Sdks, which contains the correct files. When I launch from the Dock MSBuildSDKsPath is set to /Users/tvc/.vscode/extensions/ms-vscode.csharp-1.15.2/.omnisharp/1.30.1-beta.63/omnisharp/msbuild/15.0/Bin/Sdks, which does not exist.

DustinCampbell commented 6 years ago

This is progress! :smile:

I have a suspicion that your PATH is different somehow when launching VS Code from the macOS LaunchPad, though I don't know why that would be. I'm basing this on a couple of insights from the logs provide by yourself and @rshillington.

  1. When C# for VS Code launches OmniSharp. It first tries launching mono --version to check the version of Mono that's installed. Then, if it finds a valid version, it launches OmniSharp on that Mono. So, if the log shows that it isn't launching on the installed Mono, that's an indicator that it didn't find it.
  2. The latest version of OmniSharp allows MSBuild to handle SDK resolution. The MSBuild SDK resolver handles this by searching for dotnet on the PATH. And, the MSBuild project errors that you're getting would indicate that it isn't finding dotnet.

Searching around on the Internet, I've found a number of issues where users have had troubles with the environment variables being different when launching an application via LaunchPad. I don't know why it would be that way on your machine, but I hope that gives you a lead to look into.

AndrewTriesToCode commented 6 years ago

I ran into this after I installed something via Homebrew which also installed Mono 5.12.x

I found that rolling back to Mono 5.4.x solved the issue for now.

TheAifam5 commented 5 years ago

Not needed to rollback.. Just:

/etc/profile.d/dotnet.csh

setenv DOTNET_ROOT="/opt/dotnet"
setenv MSBuildSDKsPath="${DOTNET_ROOT}/sdk/$(${DOTNET_ROOT}/dotnet --version)/Sdks"
setenv PATH="${PATH}:${DOTNET_ROOT}"

/etc/profile.d/dotnet.sh

export DOTNET_ROOT=/opt/dotnet
export MSBuildSDKsPath=$DOTNET_ROOT/sdk/$(${DOTNET_ROOT}/dotnet --version)/Sdks
export PATH=${PATH}:${DOTNET_ROOT}

The MSBuildSDKsPath env is required to get this ... working again -.-

JacobHenner commented 5 years ago

For fish:

export DOTNET_ROOT="/opt/dotnet"
export MSBuildSDKsPath="$DOTNET_ROOT/sdk/"(eval $DOTNET_ROOT/dotnet --version)"/Sdks"
davidlfox commented 5 years ago

Not needed to rollback.. Just:

/etc/profile.d/dotnet.csh

setenv DOTNET_ROOT="/opt/dotnet"
setenv MSBuildSDKsPath="${DOTNET_ROOT}/sdk/$(${DOTNET_ROOT}/dotnet --version)/Sdks"
setenv PATH="${PATH}:${DOTNET_ROOT}"

/etc/profile.d/dotnet.sh

export DOTNET_ROOT=/opt/dotnet
export MSBuildSDKsPath=$DOTNET_ROOT/sdk/$(${DOTNET_ROOT}/dotnet --version)/Sdks
export PATH=${PATH}:${DOTNET_ROOT}

The MSBuildSDKsPath env is required to get this ... working again -.-

I don't have a path /etc/profile.d. My issue is actually the opposite: works from launch pad, doesnt work from shell code .. What sets up /etc/profile.d? My dotnet runs from /usr/local/share/dotnet/dotnet

TheAifam5 commented 5 years ago

@davidfowl Which OS you use?