Skip to content

Commit

Permalink
Merge pull request #2502 from dibarbet/report_old_style_csproj
Browse files Browse the repository at this point in the history
Report to the client if the project being loaded is sdk style
  • Loading branch information
filipw authored Feb 3, 2023
2 parents d6ef83d + 9cf0ffb commit 7299d08
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static void ProjectInformation(this IEventEmitter emitter,
HashedString sdkVersion,
IEnumerable<HashedString> references,
IEnumerable<HashedString> fileExtensions,
IEnumerable<int> fileCounts)
IEnumerable<int> fileCounts,
bool sdkStyleProject)
{
var projectConfiguration = new ProjectConfigurationMessage()
{
Expand All @@ -66,7 +67,8 @@ public static void ProjectInformation(this IEventEmitter emitter,
SessionId = sessionId.Value,
References = references.Select(hashed => hashed.Value),
FileExtensions = fileExtensions.Select(hashed => hashed.Value),
FileCounts = fileCounts
FileCounts = fileCounts,
SdkStyleProject = sdkStyleProject
};

emitter.Emit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public class ProjectConfigurationMessage
public IEnumerable<string> References { get; set; }
public IEnumerable<string> FileExtensions { get; set; }
public IEnumerable<int> FileCounts { get; set; }
public bool SdkStyleProject { get; set; }
}
}
16 changes: 15 additions & 1 deletion src/OmniSharp.MSBuild/ProjectLoadListener.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Build.Execution;
Expand Down Expand Up @@ -51,14 +52,27 @@ public void ProjectLoaded(ProjectLoadedEventArgs args)
var hashedReferences = GetHashedReferences(args);
var (hashedFileExtensions, fileCounts) = GetUniqueHashedFileExtensionsAndCounts(args);

_eventEmitter.ProjectInformation(projectId, sessionId, (int)outputKind, projectCapabilities, targetFrameworks, sdkVersion, hashedReferences, hashedFileExtensions, fileCounts);
var sdkStyleProject = IsSdkStyleProject(args);
_eventEmitter.ProjectInformation(projectId, sessionId, (int)outputKind, projectCapabilities, targetFrameworks, sdkVersion, hashedReferences, hashedFileExtensions, fileCounts, sdkStyleProject);
}
catch (Exception ex)
{
_logger.LogError("Unexpected exception got thrown from project load listener: " + ex);
}
}

private static bool IsSdkStyleProject(ProjectLoadedEventArgs args)
{
// To see if a project is an SDK style project we check for either of two things
// 1. If it has a TargetFramework / TargetFrameworks property. This isn't fully complete
// as this property could come from a different props file
// 2. If it imports an SDK. This can be defined multiple ways in the project file, but
// we can look at the resolved imports after evaluation to see if any are SDK based.
bool hasTargetFrameworkProperty = args.Project.Properties.Any(property => property.Name is "TargetFramework" or "TargetFrameworks");
bool importsSdk = args.Project.Imports.Any(import => import.SdkResult != null);
return hasTargetFrameworkProperty || importsSdk;
}

private static (IEnumerable<HashedString> Extensions, IEnumerable<int> Counts) GetUniqueHashedFileExtensionsAndCounts(ProjectLoadedEventArgs args)
{
var contentFiles = args.ProjectInstance
Expand Down

0 comments on commit 7299d08

Please sign in to comment.