From a4a2396c20a345c208d58a8652814a46e39b4800 Mon Sep 17 00:00:00 2001 From: Snorri Beck Gislason Date: Wed, 9 Oct 2024 22:48:27 +0000 Subject: [PATCH] Always honor ProjectGuid, if provided (#615) * Always honor ProjectGuid, if provided * Upgrade System.Text.Json (#616) Co-authored-by: Snorri Gislason * Always honor ProjectGuid, if provided * Don't require ProjectGuid for legacy projects --------- Co-authored-by: Snorri Gislason --- .../SlnProject.cs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.VisualStudio.SlnGen/SlnProject.cs b/src/Microsoft.VisualStudio.SlnGen/SlnProject.cs index b29675b..a13e439 100644 --- a/src/Microsoft.VisualStudio.SlnGen/SlnProject.cs +++ b/src/Microsoft.VisualStudio.SlnGen/SlnProject.cs @@ -293,25 +293,28 @@ internal static bool GetIsDeployable(Project project, string projectFileExtensio /// The GUID of the specified project. internal static Guid GetProjectGuid(Project project, bool isUsingMicrosoftNETSdk) { - Guid projectGuid = Guid.NewGuid(); + Guid projectGuid; - if (!isUsingMicrosoftNETSdk && !Guid.TryParse(project.GetPropertyValueOrDefault(MSBuildPropertyNames.ProjectGuid, projectGuid.ToString()), out projectGuid)) - { - string projectGuidValue = project.GetPropertyValueOrDefault(MSBuildPropertyNames.ProjectGuid, projectGuid.ToString()); + string projectGuidValue = project.GetPropertyValue(MSBuildPropertyNames.ProjectGuid); + bool projectGuidIsEmpty = string.IsNullOrEmpty(projectGuidValue); - if (!Guid.TryParse(projectGuidValue, out projectGuid)) - { - throw new InvalidProjectFileException( - projectFile: project.FullPath, - lineNumber: 0, - columnNumber: 0, - endLineNumber: 0, - endColumnNumber: 0, - message: $"The {MSBuildPropertyNames.ProjectGuid} property value \"{projectGuidValue}\" is not a valid GUID.", - errorSubcategory: null, - errorCode: null, - helpKeyword: null); - } + // If a ProjectGuid is provided it should be honored (regardless of project style) and must be valid + if (projectGuidIsEmpty) + { + projectGuid = Guid.NewGuid(); + } + else if (!Guid.TryParse(projectGuidValue, out projectGuid)) + { + throw new InvalidProjectFileException( + projectFile: project.FullPath, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: $"The {MSBuildPropertyNames.ProjectGuid} property value \"{projectGuidValue}\" is not a valid GUID.", + errorSubcategory: null, + errorCode: null, + helpKeyword: null); } return projectGuid;