Skip to content

Commit 64c106b

Browse files
authored
sln-add: Get project DefaultProjectTypeGuid (#47661)
Fixes #47638, #522 Description and impact When adding a new project that is not known for the SDK or MSBuild, it should read the DefaultProjectTypeGuid property from the project file. Regression Yes - this worked before 9.0.2xx Risk Low – Currently it errors when adding unknown project types, unless the ProjectTypeGuid is specified on the project itself, or on the solution file. Testing Unit tests on this were ambiguous, so they were renamed to better match their cases. I was also able to reproduce the bug...
1 parent 993c073 commit 64c106b

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

src/Cli/dotnet/commands/dotnet-sln/add/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,16 @@ private void AddProject(SolutionModel solution, string solutionRelativeProjectPa
136136
{
137137
// Open project instance to see if it is a valid project
138138
ProjectRootElement projectRootElement = ProjectRootElement.Open(fullPath);
139+
ProjectInstance projectInstance = new ProjectInstance(projectRootElement);
139140
SolutionProjectModel project;
140141
try
141142
{
142143
project = solution.AddProject(solutionRelativeProjectPath, null, solutionFolder);
143144
}
144-
catch (SolutionArgumentException ex) when (ex.ParamName == "projectTypeName")
145+
catch (SolutionArgumentException ex) when (ex.Type == SolutionErrorType.InvalidProjectTypeReference)
145146
{
146147
// If guid is not identified by vs-solutionpersistence, check in project element itself
147-
var guid = projectRootElement.GetProjectTypeGuid();
148+
var guid = projectRootElement.GetProjectTypeGuid() ?? projectInstance.GetDefaultProjectTypeGuid();
148149
if (string.IsNullOrEmpty(guid))
149150
{
150151
Reporter.Error.WriteLine(CommonLocalizableStrings.UnsupportedProjectType, fullPath);
@@ -153,7 +154,6 @@ private void AddProject(SolutionModel solution, string solutionRelativeProjectPa
153154
project = solution.AddProject(solutionRelativeProjectPath, guid, solutionFolder);
154155
}
155156
// Add settings based on existing project instance
156-
ProjectInstance projectInstance = new ProjectInstance(projectRootElement);
157157
string projectInstanceId = projectInstance.GetProjectId();
158158
if (!string.IsNullOrEmpty(projectInstanceId) && serializer is ISolutionSerializer<SlnV12SerializerSettings>)
159159
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<Solution />
1+
<Solution />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26006.2
5+
MinimumVisualStudioVersion = 10.0.40219.1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<Solution />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<DefaultProjectTypeGuid>{2F08BC15-189B-4804-B644-653F34C968A8}</DefaultProjectTypeGuid>
5+
</PropertyGroup>
6+
7+
</Project>

test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class ProjectTypeGuids
1818
public const string VBProjectTypeGuid = "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}";
1919
public const string SolutionFolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}";
2020
public const string SharedProjectGuid = "{D954291E-2A0B-460D-934E-DC6B0785DB48}";
21+
public const string DefaultProjectGuid = "{130159A9-F047-44B3-88CF-0CF7F02ED50F}";
2122
}
2223

2324
public class GivenDotnetSlnAdd : SdkTest
@@ -749,13 +750,13 @@ public void WhenPassedAnUnknownProjectTypeItFails(string solutionCommand)
749750
[InlineData("sln", "SlnFileWithNoProjectReferencesAndCSharpProject", "CSharpProject", "CSharpProject.csproj", ProjectTypeGuids.CSharpProjectTypeGuid)]
750751
[InlineData("sln", "SlnFileWithNoProjectReferencesAndFSharpProject", "FSharpProject", "FSharpProject.fsproj", ProjectTypeGuids.FSharpProjectTypeGuid)]
751752
[InlineData("sln", "SlnFileWithNoProjectReferencesAndVBProject", "VBProject", "VBProject.vbproj", ProjectTypeGuids.VBProjectTypeGuid)]
752-
[InlineData("sln", "SlnFileWithNoProjectReferencesAndUnknownProjectWithSingleProjectTypeGuid", "UnknownProject", "UnknownProject.unknownproj", "{130159A9-F047-44B3-88CF-0CF7F02ED50F}")]
753-
[InlineData("sln", "SlnFileWithNoProjectReferencesAndUnknownProjectWithMultipleProjectTypeGuids", "UnknownProject", "UnknownProject.unknownproj", "{130159A9-F047-44B3-88CF-0CF7F02ED50F}")]
753+
[InlineData("sln", "SlnFileWithNoProjectReferencesAndUnknownProjectWithSingleProjectTypeGuid", "UnknownProject", "UnknownProject.unknownproj", ProjectTypeGuids.DefaultProjectGuid)]
754+
[InlineData("sln", "SlnFileWithNoProjectReferencesAndUnknownProjectWithMultipleProjectTypeGuids", "UnknownProject", "UnknownProject.unknownproj", ProjectTypeGuids.DefaultProjectGuid)]
754755
[InlineData("solution", "SlnFileWithNoProjectReferencesAndCSharpProject", "CSharpProject", "CSharpProject.csproj", ProjectTypeGuids.CSharpProjectTypeGuid)]
755756
[InlineData("solution", "SlnFileWithNoProjectReferencesAndFSharpProject", "FSharpProject", "FSharpProject.fsproj", ProjectTypeGuids.FSharpProjectTypeGuid)]
756757
[InlineData("solution", "SlnFileWithNoProjectReferencesAndVBProject", "VBProject", "VBProject.vbproj", ProjectTypeGuids.VBProjectTypeGuid)]
757-
[InlineData("solution", "SlnFileWithNoProjectReferencesAndUnknownProjectWithSingleProjectTypeGuid", "UnknownProject", "UnknownProject.unknownproj", "{130159A9-F047-44B3-88CF-0CF7F02ED50F}")]
758-
[InlineData("solution", "SlnFileWithNoProjectReferencesAndUnknownProjectWithMultipleProjectTypeGuids", "UnknownProject", "UnknownProject.unknownproj", "{130159A9-F047-44B3-88CF-0CF7F02ED50F}")]
758+
[InlineData("solution", "SlnFileWithNoProjectReferencesAndUnknownProjectWithSingleProjectTypeGuid", "UnknownProject", "UnknownProject.unknownproj", ProjectTypeGuids.DefaultProjectGuid)]
759+
[InlineData("solution", "SlnFileWithNoProjectReferencesAndUnknownProjectWithMultipleProjectTypeGuids", "UnknownProject", "UnknownProject.unknownproj", ProjectTypeGuids.DefaultProjectGuid)]
759760
public async Task WhenPassedAProjectItAddsCorrectProjectTypeGuid(
760761
string solutionCommand,
761762
string testAsset,
@@ -788,7 +789,7 @@ public async Task WhenPassedAProjectItAddsCorrectProjectTypeGuid(
788789
[InlineData("solution", ".sln")]
789790
[InlineData("sln", ".slnx")]
790791
[InlineData("solution", ".slnx")]
791-
public void WhenPassedAProjectWithoutATypeGuidItErrors(string solutionCommand, string solutionExtension)
792+
public void WhenPassedAProjectWithoutATypeGuidNorDefaultTypeGuidItErrors(string solutionCommand, string solutionExtension)
792793
{
793794
var solutionDirectory = _testAssetsManager
794795
.CopyTestAsset("SlnFileWithNoProjectReferencesAndUnknownProjectType", identifier: $"GivenDotnetSlnAdd-{solutionCommand}{solutionExtension}")
@@ -814,6 +815,26 @@ public void WhenPassedAProjectWithoutATypeGuidItErrors(string solutionCommand, s
814815
.BeVisuallyEquivalentTo(contentBefore);
815816
}
816817

818+
[Theory]
819+
[InlineData("sln", ".sln")]
820+
[InlineData("solution", ".sln")]
821+
[InlineData("sln", ".slnx")]
822+
[InlineData("solution", ".slnx")]
823+
public void WhenPassedAProjectWithDefaultProjectGuidItPasses(string solutionCommand, string solutionExtension)
824+
{
825+
var solutionDirectory = _testAssetsManager
826+
.CopyTestAsset("TestAppWithSlnAndDefaultProjectType", identifier: $"GivenDotnetSlnAdd-{solutionCommand}{solutionExtension}")
827+
.WithSource()
828+
.Path;
829+
830+
var cmd = new DotnetCommand(Log)
831+
.WithWorkingDirectory(solutionDirectory)
832+
.Execute(solutionCommand, $"App{solutionExtension}", "add", "Unknown.unknownproj");
833+
834+
cmd.Should().Pass();
835+
cmd.StdErr.Should().BeEmpty();
836+
}
837+
817838
[Theory]
818839
[InlineData("sln", ".sln")]
819840
[InlineData("solution", ".sln")]

0 commit comments

Comments
 (0)