sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Adding project to solution results in InvalidOperationException

Open dlidstrom opened this issue 3 years ago • 1 comments

Describe the bug

I'm trying to add a project to the Umbraco solution. It gives an exception:

~/programming/Umbraco-CMS> dotnet sln add src/Umbraco.Cms.Persistence.PostgreSQL/Umbraco.Cms.Persistence.PostgreSQL.csproj
Unhandled exception: System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Microsoft.DotNet.Tools.Common.SlnFileExtensions.GetSolutionFolderPaths(SlnFile slnFile, SlnPropertySet nestedProjects)
   at Microsoft.DotNet.Tools.Common.SlnFileExtensions.AddSolutionFolders(SlnFile slnFile, SlnProject slnProject, IList`1 solutionFolders)
   at Microsoft.DotNet.Tools.Common.SlnFileExtensions.AddProject(SlnFile slnFile, String fullProjectPath, IList`1 solutionFolders)
   at Microsoft.DotNet.Tools.Sln.Add.AddProjectToSolutionCommand.Execute()
   at Microsoft.DotNet.Cli.SlnAddParser.<>c.<ConstructCommand>b__5_0(ParseResult parseResult)
   at Microsoft.DotNet.Cli.ParseResultCommandHandler.Invoke(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass12_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__18_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.CommandLineBuilderExtensions.<>c__DisplayClass8_0.<<UseExceptionHandler>b__0>d.MoveNext()

The solution file is in the current folder:

~/programming/Umbraco-CMS> ls -la umbraco.sln src/Umbraco.Cms.Persistence.Postgresql/Umbraco.Cms.Persistence.PostgreSQL.csproj
-rw-r--r--  1 daniel  staff    533 Sep 25 07:46 src/Umbraco.Cms.Persistence.Postgresql/Umbraco.Cms.Persistence.PostgreSQL.csproj
-rw-r--r--  1 daniel  staff  23073 Sep 25 07:40 umbraco.sln

Specifying the solution file in the command does not help (same error).

To Reproduce

Exceptions (if any)

Further technical details

  • Include the output of dotnet --info
~/programming/Umbraco-CMS> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.401
 Commit:    0906eae6f8

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

global.json file:
  Not found

Host:
  Version:      6.0.9
  Architecture: x64
  Commit:       163a63591c

.NET SDKs installed:
  2.1.104 [/usr/local/share/dotnet/sdk]
  2.1.401 [/usr/local/share/dotnet/sdk]
  2.2.106 [/usr/local/share/dotnet/sdk]
  3.0.100 [/usr/local/share/dotnet/sdk]
  3.1.102 [/usr/local/share/dotnet/sdk]
  3.1.202 [/usr/local/share/dotnet/sdk]
  3.1.301 [/usr/local/share/dotnet/sdk]
  3.1.406 [/usr/local/share/dotnet/sdk]
  3.1.407 [/usr/local/share/dotnet/sdk]
  5.0.100-rc.2.20479.15 [/usr/local/share/dotnet/sdk]
  5.0.100 [/usr/local/share/dotnet/sdk]
  5.0.103 [/usr/local/share/dotnet/sdk]
  5.0.201 [/usr/local/share/dotnet/sdk]
  6.0.100-preview.7.21379.14 [/usr/local/share/dotnet/sdk]
  6.0.100 [/usr/local/share/dotnet/sdk]
  6.0.202 [/usr/local/share/dotnet/sdk]
  6.0.401 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.12 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.13 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-rc.2.20475.17 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-preview.7.21378.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.12 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-rc.2.20475.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Download .NET:
  https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
  https://aka.ms/dotnet/runtimes-sdk-info

dlidstrom avatar Sep 25 '22 08:09 dlidstrom

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

I'll try to repro this later; don't have the Mac atm and avoiding wildfire smoke.

But in the meantime, @dlidstrom may you please link to the specific reproduceable branch and repo where that solution file and project file is so we can test it? I'm curious if the solution and project file is 100% valid. Please be cautious of any secrets and do remove any personal data beforehand. Thanks for raising the issue with us.

nagilson avatar Oct 12 '22 17:10 nagilson

Hi Noah, I just performed these steps:

$ git clone --depth 1 --branch persistence-postgres [email protected]:dlidstrom/Umbraco-CMS.git persistence-postgres
$ cd persistence-postgres
$ echo `git log -n 1 --oneline` # show latest commit (if I work on this in the meantime)
7cb2374da9 prepare for tests
persistence-postgres $ dotnet sln add src/Umbraco.Cms.Persistence.Postgresql/Umbraco.Cms.Persistence.PostgreSQL.csproj

and saw the same output as reported.

dlidstrom avatar Oct 12 '22 20:10 dlidstrom

@dlidstrom Thank you so much for the consistent repro and your patience. Was able to repro the issue on both OS. We'll discuss the next steps here. Not sure I have a workaround yet, but you could probably change one of the project IDs in the umbraco.sln or project file to match.


The solution project ID of their "Templating" project is "{D4C3395A-BA9D-4032-9ED3-09F1FC032CBC}", which the global solution aliases (?) to "{5FBDD50D-7A86-4F4D-BEB9-7967FBA4ED2C}".

umbraco.sln, line 298 preSolution NestedProjects  Configuration:
({D4C3395A-BA9D-4032-9ED3-09F1FC032CBC} = {2849E9D4-3B4E-40A3-A309-F3CB4F0E125F}

The id, or slnProject.Id is "{D4C3395A-BA9D-4032-9ED3-09F1FC032CBC}". Meanwhile, the GetProjectsByType returns the ID "{5FBDD50D-7A86-4F4D-BEB9-7967FBA4ED2C}". This causes the exception / failure as we don't account for the aliased ID.

nagilson avatar Oct 26 '22 17:10 nagilson

Hi @nagilson, thanks for the follow-up.

DanielLidstromTicket avatar Oct 27 '22 10:10 DanielLidstromTicket

Of course, we're discussing this with some folks internally about writing a fix for this (they aren't on GH) as I'm not super knowledgeable about how SLN files work.

nagilson avatar Oct 27 '22 16:10 nagilson

@dlidstrom You can fix the problem in that commit by adding EndProject after Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Umbraco.New.Cms.Web.Common", "src\Umbraco.New.Cms.Web.Common\Umbraco.New.Cms.Web.Common.csproj", "{5ED13EC6-399E-49D5-9D26-86501729B08D}". But it looks like Umbraco already fixed it, so just merging with their main would solve the problem.

It turns out the solution file was invalid. https://github.com/dlidstrom/Umbraco-CMS/blob/7cb2374da921536699d07a28f08a032b8e718174/umbraco.sln#L110 If you look at this line, Umbraco.New.Cms.Web.Common doesn't have an EndProject section. So we skip over the Docs project which causes the ID failure, as our Solution code and Mono's solution code didn't load the Docs project. Visual Studio's code that handles solution files has logic to load a new project even if EndProject isn't used. If you added that, it works.

I'll try to create an error to handle this, thanks!

nagilson avatar Oct 27 '22 23:10 nagilson

@nagilson just as you initially suggested then. Thanks for the detailed report Noah 🙏

dlidstrom avatar Oct 28 '22 05:10 dlidstrom