microsoft / microsoft/vs-solutionpersistence

Adding the first project gets solution model into an incorrect state with no build types or platforms

Open
#132 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
213
Forks
14
PR merge metrics
No merged PRs in 30d

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 - https://github.com/microsoft/vs-solutionpersistence/blob/a5c8b172f89d74a447648518b16e02978c79c8a3/src/Microsoft.VisualStudio.SolutionPersistence/Serializer/Xml/XmlDecorators/XmlSolution.cs#L139-L151

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproduction and inspect XmlSolution.ToModel in src/Microsoft.VisualStudio.SolutionPersistence/Serializer/Xml/XmlDecorators/XmlSolution.cs, then trace SolutionModel.AddProject. The issue is done when adding the first project leaves BuildTypes and Platforms populated with the defaults without requiring serialization and reopening.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.