dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.83k stars 654 forks source link

If I open a second C# project, Omnisharp crashes with InvalidProjectFileException #4380

Open massimilianokraus opened 3 years ago

massimilianokraus commented 3 years ago

Issue Description

I can open a VS Code window with whatever C# project, and it works fine: it asks me to build the .NET assets if missing, the code analysis starts, I see the errors on the code, I can start/debug the app and execute dotnet tasks. If I open a second project in a separate window, the extension breaks: it can't find the SDK, so none of the C# features works (the first project keeps working fine).

Steps to Reproduce

Open a first C# project with VS Code (ProjectInvalidTest1). Open a second C# project with VS Code (ProjectInvalidTest2).

Expected Behavior

The second project should work fine as the first.

Actual Behavior

Omnisharp breaks, I can't see the errors highlighted in code, the IntelliSense doesn't work, I can't execute tasks based on the dotnet command. If I close VS Code, and I reopen it, it reopens all the windows/projects of the last session, and since the two projects are loaded simultaneously, now they both work correctly. This behavior does not change if I invert the order of opening, or the framework version, or the application type (console, web, etc.).

Logs

OmniSharp log

The Output console shows this (I replaced the actual path with '...'): OmniSharp.MSBuild.ProjectManager Loading project: /Users/.../ProjectInvalidTest2/ProjectInvalidTest2.csproj It was not possible to find any installed .NET SDKs. Install a .NET SDK from: https://aka.ms/dotnet-download

    Failed to load project file '/Users/.../ProjectInvalidTest2/ProjectInvalidTest2.csproj'.

/Users/.../ProjectInvalidTest2/ProjectInvalidTest2.csproj(0,0) Microsoft.Build.Exceptions.InvalidProjectFileException: The SDK 'Microsoft.NET.Sdk' specified could not be found. /Users/.../ProjectInvalidTest2/ProjectInvalidTest2.csproj at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject (System.String errorSubCategoryResourceName, Microsoft.Build.Shared.IElementLocation elementLocation, System.String resourceName, System.Object[] args) [0x00040] in :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 :0 at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject[T1] (Microsoft.Build.Shared.IElementLocation elementLocation, System.String resourceName, T1 arg0) [0x00000] in :0 at Microsoft.Build.Evaluation.Evaluator4[P,I,M,D].ExpandAndLoadImportsFromUnescapedImportExpressionConditioned (System.String directoryOfImportingFile, Microsoft.Build.Construction.ProjectImportElement importElement, System.Collections.Generic.List1[Microsoft.Build.Construction.ProjectRootElement]& projects, Microsoft.Build.BackEnd.SdkResolution.SdkResult& sdkResult, System.Boolean throwOnFileNotExistsError) [0x00309] in :0 at Microsoft.Build.Evaluation.Evaluator4[P,I,M,D].ExpandAndLoadImports (System.String directoryOfImportingFile, Microsoft.Build.Construction.ProjectImportElement importElement, Microsoft.Build.BackEnd.SdkResolution.SdkResult& sdkResult) [0x00027] in <f7274894ce154696afba1f0ffcd06434>:0 at Microsoft.Build.Evaluation.Evaluator4[P,I,M,D].EvaluateImportElement (System.String directoryOfImportingFile, Microsoft.Build.Construction.ProjectImportElement importElement) [0x0000d] in :0 at Microsoft.Build.Evaluation.Evaluator4[P,I,M,D].PerformDepthFirstPass (Microsoft.Build.Construction.ProjectRootElement currentProjectOrImport) [0x000e6] in <f7274894ce154696afba1f0ffcd06434>:0 at Microsoft.Build.Evaluation.Evaluator4[P,I,M,D].Evaluate () [0x00139] in :0 at Microsoft.Build.Evaluation.Evaluator4[P,I,M,D].Evaluate (Microsoft.Build.Evaluation.IEvaluatorData4[P,I,M,D] data, Microsoft.Build.Construction.ProjectRootElement root, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings, System.Int32 maxNodeCount, Microsoft.Build.Collections.PropertyDictionary1[T] environmentProperties, Microsoft.Build.BackEnd.Logging.ILoggingService loggingService, Microsoft.Build.Evaluation.IItemFactory2[S,T] itemFactory, Microsoft.Build.Evaluation.IToolsetProvider toolsetProvider, Microsoft.Build.Evaluation.ProjectRootElementCacheBase projectRootElementCache, Microsoft.Build.Framework.BuildEventContext buildEventContext, Microsoft.Build.BackEnd.SdkResolution.ISdkResolverService sdkResolverService, System.Int32 submissionId, Microsoft.Build.Evaluation.Context.EvaluationContext evaluationContext, System.Boolean interactive) [0x00047] in :0 at Microsoft.Build.Evaluation.Project+ProjectImpl.Reevaluate (Microsoft.Build.BackEnd.Logging.ILoggingService loggingServiceForEvaluation, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings, Microsoft.Build.Evaluation.Context.EvaluationContext evaluationContext) [0x00063] in :0 at Microsoft.Build.Evaluation.Project+ProjectImpl.ReevaluateIfNecessary (Microsoft.Build.BackEnd.Logging.ILoggingService loggingServiceForEvaluation, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings, Microsoft.Build.Evaluation.Context.EvaluationContext evaluationContext) [0x00035] in :0 at Microsoft.Build.Evaluation.Project+ProjectImpl.ReevaluateIfNecessary (Microsoft.Build.BackEnd.Logging.ILoggingService loggingServiceForEvaluation, Microsoft.Build.Evaluation.Context.EvaluationContext evaluationContext) [0x00000] in :0 at Microsoft.Build.Evaluation.Project+ProjectImpl.ReevaluateIfNecessary (Microsoft.Build.Evaluation.Context.EvaluationContext evaluationContext) [0x00007] in :0 at Microsoft.Build.Evaluation.Project+ProjectImpl.Initialize (System.Collections.Generic.IDictionary2[TKey,TValue] globalProperties, System.String toolsVersion, System.String subToolsetVersion, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings, Microsoft.Build.Evaluation.Context.EvaluationContext evaluationContext) [0x00127] in <f7274894ce154696afba1f0ffcd06434>:0 at Microsoft.Build.Evaluation.Project..ctor (System.String projectFile, System.Collections.Generic.IDictionary2[TKey,TValue] globalProperties, System.String toolsVersion, System.String subToolsetVersion, Microsoft.Build.Evaluation.ProjectCollection projectCollection, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings, Microsoft.Build.Evaluation.Context.EvaluationContext evaluationContext) [0x00073] in :0 at Microsoft.Build.Evaluation.Project..ctor (System.String projectFile, System.Collections.Generic.IDictionary2[TKey,TValue] globalProperties, System.String toolsVersion, System.String subToolsetVersion, Microsoft.Build.Evaluation.ProjectCollection projectCollection, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings) [0x00000] in <f7274894ce154696afba1f0ffcd06434>:0 at Microsoft.Build.Evaluation.Project..ctor (System.String projectFile, System.Collections.Generic.IDictionary2[TKey,TValue] globalProperties, System.String toolsVersion, Microsoft.Build.Evaluation.ProjectCollection projectCollection, Microsoft.Build.Evaluation.ProjectLoadSettings loadSettings) [0x00000] in :0 at Microsoft.Build.Evaluation.Project..ctor (System.String projectFile, System.Collections.Generic.IDictionary2[TKey,TValue] globalProperties, System.String toolsVersion, Microsoft.Build.Evaluation.ProjectCollection projectCollection) [0x00000] in <f7274894ce154696afba1f0ffcd06434>:0 at Microsoft.Build.Evaluation.ProjectCollection.LoadProject (System.String fileName, System.Collections.Generic.IDictionary2[TKey,TValue] globalProperties, System.String toolsVersion) [0x000f7] in :0 at Microsoft.Build.Evaluation.ProjectCollection.LoadProject (System.String fileName, System.String toolsVersion) [0x00000] in :0 at OmniSharp.MSBuild.ProjectLoader.EvaluateProjectFileCore (System.String filePath, System.Collections.Generic.IReadOnlyDictionary2[TKey,TValue] projectConfigurationsInSolution) [0x00160] in <7b181b7eb4a044e1be529668c275a7d5>:0 at OmniSharp.MSBuild.ProjectLoader.BuildProject (System.String filePath, System.Collections.Generic.IReadOnlyDictionary2[TKey,TValue] configurationsInSolution) [0x0000d] in <7b181b7eb4a044e1be529668c275a7d5>:0 at OmniSharp.MSBuild.ProjectFile.ProjectFileInfo.Load (System.String filePath, OmniSharp.MSBuild.ProjectIdInfo projectIdInfo, OmniSharp.MSBuild.ProjectLoader loader, System.Guid sessionId, OmniSharp.Services.DotNetInfo dotNetInfo) [0x00023] in <7b181b7eb4a044e1be529668c275a7d5>:0 at OmniSharp.MSBuild.ProjectManager+<>c__DisplayClass31_0.b__0 () [0x00000] in <7b181b7eb4a044e1be529668c275a7d5>:0 at (wrapper delegate-invoke) System.Func1[System.ValueTuple3[OmniSharp.MSBuild.ProjectFile.ProjectFileInfo,System.Collections.Immutable.ImmutableArray1[OmniSharp.MSBuild.Logging.MSBuildDiagnostic],OmniSharp.MSBuild.Notification.ProjectLoadedEventArgs]].invoke_TResult() at OmniSharp.MSBuild.ProjectManager.LoadOrReloadProject (System.String projectFilePath, System.Func1[TResult] loader) [0x0001b] in <7b181b7eb4a044e1be529668c275a7d5>:0

    Attempted to update project that is not loaded: /Users/.../ProjectInvalidTest2/ProjectInvalidTest2.csproj

C# log

Empty

Environment information

VSCode version: 1.53.0 C# Extension: 1.23.9

Mono Information OmniSharp using built-in mono
Dotnet Information A valid dotnet installation could not be found.
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |auto-close-tag|formulahendry|0.5.10| |auto-rename-tag|formulahendry|0.1.6| |Bookmarks|alefragnani|12.1.4| |code-settings-sync|Shan|3.4.3| |cpptools|ms-vscode|1.2.0| |csharp|ms-dotnettools|1.23.9| |csharpsortusings|jongrant|0.0.3| |debugger-for-chrome|msjsdiag|4.12.12| |dotenv|mikestead|1.0.1| |EditorConfig|EditorConfig|0.16.4| |eml|leighlondon|0.4.0| |git-project-manager|felipecaputo|1.7.1| |githistory|donjayamanne|0.6.14| |gitlens|eamodio|11.2.1| |html-snippets|abusaidm|0.2.1| |htmltagwrap|bradgashler|0.0.7| |import-sorter|mike-co|3.3.1| |JavaScriptSnippets|xabikos|1.8.0| |jinja|wholroyd|0.0.8| |js-debug-nightly|ms-vscode|2021.2.117| |latex-workshop|James-Yu|8.15.0| |MagicPython|magicstack|1.1.0| |markdown-shortcuts|mdickin|0.12.0| |mssql|ms-mssql|1.10.1| |npm-intellisense|christian-kohler|1.3.1| |oracle-plsql|mycelo|1.0.3| |output-colorizer|IBM|0.1.2| |overtype|adammaras|0.2.0| |path-intellisense|christian-kohler|2.3.0| |powershell|ms-vscode|2020.6.0| |propertylist|zhouronghui|0.0.2| |python|ms-python|2021.1.502429796| |python-extension-pack|donjayamanne|1.6.0| |sass-indented|robinbentley|1.5.1| |sqltools|mtxr|0.23.0| |sqltools-driver-pg|mtxr|0.2.0| |vscode-attrs-sorter|mrmlnc|2.1.0| |vscode-css-formatter|aeschli|1.0.1| |vscode-django|batisteo|1.3.0| |vscode-docker|ms-azuretools|1.9.1| |vscode-eslint|dbaeumer|2.1.14| |vscode-icons|vscode-icons-team|11.1.0| |vscode-npm-script|eg2|0.3.13| |vscode-nuget-package-manager|jmrog|1.1.6| |vscode-open-multiple-files|fabiospampinato|1.3.0| |vscode-react-native|msjsdiag|1.3.0| |vscode-search-open-all-results|fabiospampinato|1.0.3| |vscode-sort|henriiik|0.2.5| |vscode-sort-json|richie5um2|1.19.2| |vscode-test-explorer|hbenl|2.19.5| |vscode-xml|redhat|0.15.0| |vscode-yaml|redhat|0.14.0| |vscode-yarn|gamunu|1.7.1| |vscodeintellicode|VisualStudioExptTeam|1.2.11|;
massimilianokraus commented 2 years ago

I discovered this happens only when I open the two instances of Code through this automation script: https://www.jimbobbennett.io/open-anything-in-vs-code-using-a-macos-quick-action/

If I open the windows through the terminal with "code .", I don't have any issue, I can open how many projects I want and Omnisharp works perfectly.

Any idea why open -n -b "com.microsoft.VSCode" --args "$*" does not work with the second window (and subsequent ones)? Is there any environment variable I have to set in the script?

JoeRobich commented 2 years ago

@massimilianokraus My first thought is that opening VS this way does not inherit the environment variables from the shell. But I would expect it to be an issue every time, not just the second time.