RicoSuter / DNT

DNT (DotNetTools): Command line tools to manage .NET projects and solutions.
MIT License
416 stars 63 forks source link

Switcher configuration with a combination of single project and multiple project are flattened after switch-to-projects #111

Open matt-psaltis opened 2 years ago

matt-psaltis commented 2 years ago
{
  "solution": "All.sln",
  "mappings": {
    "Package1": ["Path1/Package1.csproj"],
    "Package2": [
      "Path2/Module1/Package2.csproj",
      "Path2/Module2/Package2.csproj",
    ]
  }
}

After executing a switch-to-projects the json is modified to:

{
  "solution": "All.sln",
  "mappings": {
    "Package1": "Path1/Package1.csproj",  <- The array is removed here when the file is updated with the package version information.
    "Package2": [
      "Path2/Module1/Package2.csproj",
      "Path2/Module2/Package2.csproj",
    ]
  }
}
Kobus-Smit commented 2 years ago

And then after the array is removed, running dnt switch-to-packages thows this exception:

System.InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JToken.
   at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
   at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable`1 value)
   at Newtonsoft.Json.Linq.Extensions.Value[U](IEnumerable`1 value)
   at Dnt.Commands.Packages.Switcher.SingleOrArrayConverter.<>c.<ReadJson>b__1_1(JProperty val) in C:\projects\dnt\src\Dnt.Commands\Packages\Switcher\SingleOrArrayConvertor.cs:line 25
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Dnt.Commands.Packages.Switcher.SingleOrArrayConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) in C:\projects\dnt\src\Dnt.Commands\Packages\Switcher\SingleOrArrayConvertor.cs:line 25
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
   at Dnt.Commands.Packages.Switcher.ReferenceSwitcherConfiguration.Load(String fileName, IConsoleHost host) in C:\projects\dnt\src\Dnt.Commands\Packages\Switcher\ReferenceSwitcherConfiguration.cs:line 43
   at Dnt.Commands.Packages.SwitchProjectsToPackagesCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\dnt\src\Dnt.Commands\Packages\SwitchProjectsToPackagesCommand.cs:line 22
   at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
   at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
   at NConsole.CommandLineProcessor.Process(String[] args, Object input)
   at Dnt.Program.Main(String[] args) in C:\projects\dnt\src\Dnt\Program.cs:line 33

Current workaround is to manually modify switcher.json again and add the arrays.

vince-wingate-talogy commented 2 years ago

A workaround seems to be to make sure you have no single-item arrays by duplicating single entries. It does then get added to the .csproj twice but Visual Studio handles it.

{
  "solution": "All.sln",
  "mappings": {
    "Package1": [
      "Path1/Package1.csproj",
      "Path1/Package1.csproj" <- duplicate
    ],
    "Package2": [
      "Path2/Module1/Package2.csproj",
      "Path2/Module2/Package2.csproj",
    ]
  }
}