-
Notifications
You must be signed in to change notification settings - Fork 12
Description
Run the following program:
using System.Text;
using Microsoft.VisualStudio.SolutionPersistence.Serializer;
using var memoryStream = new MemoryStream();
var encoding = new UTF8Encoding(false);
memoryStream.Write(encoding.GetBytes("<Solution/>"));
memoryStream.Position = 0;
var solutionModel = await SolutionSerializers.SlnXml.OpenAsync(memoryStream, CancellationToken.None);
solutionModel.AddProject("ConsoleApp1.csproj");
Console.WriteLine("After adding a project:");
Console.WriteLine("BuildTypes: " + string.Join(", ", solutionModel.BuildTypes));
Console.WriteLine("Platforms: " + string.Join(", ", solutionModel.Platforms));
memoryStream.SetLength(0);
memoryStream.Position = 0;
await SolutionSerializers.SlnXml.SaveAsync(memoryStream, solutionModel, CancellationToken.None);
memoryStream.Position = 0;
solutionModel = await SolutionSerializers.SlnXml.OpenAsync(memoryStream, CancellationToken.None);
Console.WriteLine("After saving and re-opening the solution:");
Console.WriteLine("BuildTypes: " + string.Join(", ", solutionModel.BuildTypes));
Console.WriteLine("Platforms: " + string.Join(", ", solutionModel.Platforms));Actual result:
After adding a project:
BuildTypes:
Platforms:
After saving and re-opening the solution:
BuildTypes: Debug, Release
Platforms: Any CPU
Note that after adding a project the solution model gets into an invalid state where it doesn't have any build types or platforms. If you serialize and deserialize back this model the default build types and platforms are added.
This happens because XmlSolution.ToModel adds default build types and platforms only if the solution already contains at least one project -
Lines 139 to 151 in a5c8b17
| // Create default configurations if they weren't provided by the Configurations section. | |
| // Add default build types (Debug/Release) if not specified. | |
| if (solutionModel.BuildTypes.IsNullOrEmpty() && solutionModel.SolutionProjects.Count > 0) | |
| { | |
| solutionModel.AddBuildType(BuildTypeNames.Debug); | |
| solutionModel.AddBuildType(BuildTypeNames.Release); | |
| } | |
| // Add default platform (Any CPU) if not specified. | |
| if (solutionModel.Platforms.IsNullOrEmpty() && solutionModel.SolutionProjects.Count > 0) | |
| { | |
| solutionModel.AddPlatform(PlatformNames.AnySpaceCPU); | |
| } |
However, adding the first project to the SolutionModel doesn't add the default build types and platforms. You need to either add them manually upon adding the first project, or serialize and deserialize the model after adding the first project. Both options are either brittle or cumbersome.
I'd expect either the model to add default build types and platforms automatically upon adding the first project, or adding them by default on deserialization even if there are no projects yet.