Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Targeting IDE projects to .NET 6 #66655

Merged
merged 11 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.ConvertNamespace
Expand All @@ -24,7 +21,7 @@ public static (string title, string equivalenceKey) GetInfo(NamespaceDeclaration
_ => throw ExceptionUtilities.UnexpectedValue(preference),
};

public static bool CanOfferUseBlockScoped(CodeStyleOption2<NamespaceDeclarationPreference> option, BaseNamespaceDeclarationSyntax declaration, bool forAnalyzer)
public static bool CanOfferUseBlockScoped(CodeStyleOption2<NamespaceDeclarationPreference> option, BaseNamespaceDeclarationSyntax? declaration, bool forAnalyzer)
genlu marked this conversation as resolved.
Show resolved Hide resolved
{
if (declaration is not FileScopedNamespaceDeclarationSyntax)
return false;
Expand All @@ -40,13 +37,13 @@ public static bool CanOfferUseBlockScoped(CodeStyleOption2<NamespaceDeclarationP
return canOffer;
}

internal static bool CanOfferUseFileScoped(CodeStyleOption2<NamespaceDeclarationPreference> option, CompilationUnitSyntax root, BaseNamespaceDeclarationSyntax declaration, bool forAnalyzer)
internal static bool CanOfferUseFileScoped(CodeStyleOption2<NamespaceDeclarationPreference> option, CompilationUnitSyntax root, BaseNamespaceDeclarationSyntax? declaration, bool forAnalyzer)
=> CanOfferUseFileScoped(option, root, declaration, forAnalyzer, root.SyntaxTree.Options.LanguageVersion());

internal static bool CanOfferUseFileScoped(
CodeStyleOption2<NamespaceDeclarationPreference> option,
CompilationUnitSyntax root,
BaseNamespaceDeclarationSyntax declaration,
BaseNamespaceDeclarationSyntax? declaration,
bool forAnalyzer,
LanguageVersion version)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private InfoCache(INamedTypeSymbol rangeType, INamedTypeSymbol stringType, IName
.OfType<IMethodSymbol>()
.FirstOrDefault(m => IsTwoArgumentSliceLikeMethod(m));

_methodToMemberInfo[substringMethod] = ComputeMemberInfo(substringMethod, requireRangeMember: false);
if (substringMethod is not null)
_methodToMemberInfo[substringMethod] = ComputeMemberInfo(substringMethod, requireRangeMember: false);
}
}

Expand All @@ -65,7 +66,7 @@ public static bool TryCreate(Compilation compilation, [NotNullWhen(true)] out In
return true;
}

private static IMethodSymbol GetSliceLikeMethod(INamedTypeSymbol namedType)
private static IMethodSymbol? GetSliceLikeMethod(INamedTypeSymbol namedType)
=> namedType.GetMembers()
.OfType<IMethodSymbol>()
.Where(m => IsTwoArgumentSliceLikeMethod(m))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ internal static class Helpers
/// <summary>
/// Find an `int MyType.Count` or `int MyType.Length` property.
/// </summary>
public static IPropertySymbol TryGetLengthOrCountProperty(ITypeSymbol namedType)
public static IPropertySymbol? TryGetLengthOrCountProperty(ITypeSymbol namedType)
=> TryGetNoArgInt32Property(namedType, nameof(string.Length)) ??
TryGetNoArgInt32Property(namedType, nameof(ICollection.Count));

/// <summary>
/// Tried to find a public, non-static, int-returning property in the given type with the
/// specified <paramref name="name"/>.
/// </summary>
public static IPropertySymbol TryGetNoArgInt32Property(ITypeSymbol type, string name)
public static IPropertySymbol? TryGetNoArgInt32Property(ITypeSymbol type, string name)
=> type.GetMembers(name)
.OfType<IPropertySymbol>()
.Where(p => IsPublicInstance(p) &&
Expand Down Expand Up @@ -124,7 +124,7 @@ private static bool IsSliceSecondParameter(IParameterSymbol parameter)
/// provided <paramref name="parameterType"/> and must return the provided <paramref
/// name="returnType"/>.
/// </summary>
public static IPropertySymbol GetIndexer(ITypeSymbol type, ITypeSymbol parameterType, ITypeSymbol returnType)
public static IPropertySymbol? GetIndexer(ITypeSymbol type, ITypeSymbol parameterType, ITypeSymbol returnType)
=> type.GetMembers(WellKnownMemberNames.Indexer)
.OfType<IPropertySymbol>()
.Where(p => p.IsIndexer &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var serializedNamingStyle = diagnostic.Properties[nameof(NamingStyle)];
Contract.ThrowIfNull(serializedNamingStyle);

var style = NamingStyle.FromXElement(XElement.Parse(serializedNamingStyle));

var document = context.Document;
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/Core/MSBuildTask/RCWForCurrentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ private void CleanupComObject()
_shouldReleaseRCW &&
Marshal.IsComObject(_rcwForCurrentCtx))
{
#if NETCOREAPP
Debug.Assert(OperatingSystem.IsWindows());
#endif
Marshal.ReleaseComObject(_rcwForCurrentCtx);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copied from:
// https://github.com/dotnet/runtime/blob/fdd104ec5e1d0d2aa24a6723995a98d0124f724b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CompilerFeatureRequiredAttribute.cs

#if !NET6_0_OR_GREATER
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would #if NETSTANDARD be better, so that it is not updated every time we bump version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems this type is not in .net 6 either, only in net 7

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the runtime PR addign those types
dotnet/runtime#64287

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to change this directive (now or in the future). net7.0 falls under NET6_0_OR_GREATER.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem is we are compiled for .NET6, which would exclude this from code, but the type only available in .NET7

#if !NET7_0_OR_GREATER

namespace System.Runtime.CompilerServices
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.IO;
using System.Runtime.Versioning;

namespace Roslyn.Utilities
{
Expand All @@ -14,7 +15,11 @@ namespace Roslyn.Utilities
/// </summary>
internal static class PlatformInformation
{
#if NET5_0_OR_GREATER
[SupportedOSPlatformGuard("windows")]
#endif
public static bool IsWindows => Path.DirectorySeparatorChar == '\\';

public static bool IsUnix => Path.DirectorySeparatorChar == '/';
public static bool IsRunningOnMono
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copied from:
// https://github.com/dotnet/runtime/blob/fdd104ec5e1d0d2aa24a6723995a98d0124f724b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RequiredMemberAttribute.cs

#if !NET6_0_OR_GREATER
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would #if NETSTANDARD be better, so that it is not updated every time we bump version?

#if !NET7_0_OR_GREATER

namespace System.Runtime.CompilerServices
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copied from:
// https://github.com/dotnet/runtime/blob/fdd104ec5e1d0d2aa24a6723995a98d0124f724b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/SetsRequiredMembersAttribute.cs

#if !NET6_0_OR_GREATER
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would #if NETSTANDARD be better, so that it is not updated every time we bump version?

#if !NET7_0_OR_GREATER

namespace System.Diagnostics.CodeAnalysis
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ internal sealed class GacFileResolver : IEquatable<GacFileResolver>
/// Returns true if GAC is available on the current platform.
/// </summary>
public static bool IsAvailable
// Since mscorlib may not be loaded from the GAC on Mono, also check if the platform is Mono which supports a GAC.
=> typeof(object).Assembly.GlobalAssemblyCache || PlatformInformation.IsRunningOnMono;
{
get
{
// Since mscorlib may not be loaded from the GAC on Mono, also check if the platform is Mono which supports a GAC.
return
#if !NETCOREAPP
typeof(object).Assembly.GlobalAssemblyCache ||
#endif
PlatformInformation.IsRunningOnMono;
}
}

/// <summary>
/// Architecture filter used when resolving assembly references.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.Editor.CSharp</RootNamespace>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>
<!-- NuGet -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Updater;
using Microsoft.CodeAnalysis.EditorConfig;
using Microsoft.CodeAnalysis.Shared.Extensions;
using RoslynEnumerableExtensions = Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Extensions.EnumerableExtensions;

namespace Microsoft.CodeAnalysis.Editor.EditorConfigSettings.DataProvider.Analyzer
{
Expand All @@ -28,7 +29,7 @@ public AnalyzerSettingsProvider(string fileName, AnalyzerSettingsUpdater setting

protected override void UpdateOptions(TieredAnalyzerConfigOptions options, ImmutableArray<Project> projectsInScope)
{
var analyzerReferences = projectsInScope.SelectMany(p => p.AnalyzerReferences).DistinctBy(a => a.Id).ToImmutableArray();
var analyzerReferences = RoslynEnumerableExtensions.DistinctBy(projectsInScope.SelectMany(p => p.AnalyzerReferences), a => a.Id).ToImmutableArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need the extension method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can unify them to BCL implementation, but I'd keep the original logic in this PR

foreach (var analyzerReference in analyzerReferences)
{
var configSettings = GetSettings(analyzerReference, options.EditorConfigOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Extensions;
using Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Updater;
using Microsoft.CodeAnalysis.Options;
using RoslynEnumerableExtensions = Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Extensions.EnumerableExtensions;

namespace Microsoft.CodeAnalysis.Editor.EditorConfigSettings.DataProvider.NamingStyles
{
Expand All @@ -29,7 +30,7 @@ protected override void UpdateOptions(TieredAnalyzerConfigOptions options, Immut

var fileName = (location.LocationKind != LocationKind.VisualStudio) ? options.EditorConfigFileName : null;
var namingRules = namingPreferences.NamingRules.Select(r => r.GetRule(namingPreferences));
var allStyles = namingPreferences.NamingStyles.DistinctBy(s => s.Name).ToArray();
var allStyles = RoslynEnumerableExtensions.DistinctBy(namingPreferences.NamingStyles, s => s.Name).ToArray();
var namingStyles = namingRules.Select(namingRule => new NamingStyleSetting(namingRule, allStyles, SettingsUpdater, fileName));

AddRange(namingStyles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ protected SettingsProviderBase(string fileName, TOptionsUpdater settingsUpdater,
protected void Update()
{
var givenFolder = new DirectoryInfo(FileName).Parent;
if (givenFolder is null)
{
return;
}

var solution = Workspace.CurrentSolution;
var projects = solution.GetProjectsUnderEditorConfigFile(FileName);
var project = projects.FirstOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using static Microsoft.CodeAnalysis.EditorConfig.Parsing.NamingStyles.EditorConfigNamingStylesParser;
using RoslynEnumerableExtensions = Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Extensions.EnumerableExtensions;

namespace Microsoft.CodeAnalysis.Editor.EditorConfigSettings.Updater
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public NamingStyleSettingsUpdater(Workspace workspace, IGlobalOptionService glob
{
var allCurrentStyles = result.Rules.Select(x => x.NamingScheme).Distinct().Select(x => (x, style: x.AsNamingStyle()));
var styleParseResult = TryGetStyleParseResult(prevStyle, allCurrentStyles);
var allDistinctStyles = allCurrentStyles.Select(x => x.style).DistinctBy(x => x.Name).ToArray();
var allDistinctStyles = RoslynEnumerableExtensions.DistinctBy(allCurrentStyles.Select(x => x.style), x => x.Name).ToArray();
if (styleParseResult is (NamingScheme namingScheme, NamingStyle style))
{
var newLine = $"dotnet_naming_rule.{parseResult.RuleName.Value}.style = {namingScheme.OptionName.Value}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async Task QueueUpdateAsync()
}
}

public async Task<SourceText?> GetChangedEditorConfigAsync(AnalyzerConfigDocument analyzerConfigDocument, CancellationToken token)
public async Task<SourceText?> GetChangedEditorConfigAsync(AnalyzerConfigDocument? analyzerConfigDocument, CancellationToken token)
{
if (analyzerConfigDocument is null)
return null;
Expand Down Expand Up @@ -81,7 +81,7 @@ async Task QueueUpdateAsync()
return null;
}

var originalText = await analyzerConfigDocument.GetTextAsync(token).ConfigureAwait(false);
var originalText = await analyzerConfigDocument!.GetTextAsync(token).ConfigureAwait(false);
return newText.GetTextChanges(originalText);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ServiceWrapper(IVSTypeScriptGoToDefinitionService service)
public async Task<IEnumerable<INavigableItem>?> FindDefinitionsAsync(Document document, int position, CancellationToken cancellationToken)
{
var items = await _service.FindDefinitionsAsync(document, position, cancellationToken).ConfigureAwait(false);
return items.Select(item => new VSTypeScriptNavigableItemWrapper(item));
return items?.Select(item => new VSTypeScriptNavigableItemWrapper(item));
}

public bool TryGoToDefinition(Document document, int position, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis</RootNamespace>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);EDITOR_FEATURES</DefineConstants>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.VisualStudio.Text.Classification;
using Roslyn.Utilities;
using ReferenceEqualityComparer = Roslyn.Utilities.ReferenceEqualityComparer;

namespace Microsoft.CodeAnalysis.Editor.Shared.Utilities
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.Text</RootNamespace>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>

<!-- NuGet -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<RootNamespace></RootNamespace>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>
<!-- NuGet -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -15,8 +14,6 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
Expand Down Expand Up @@ -63,7 +60,7 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
}

private static bool CanOfferRefactoring(
BaseNamespaceDeclarationSyntax namespaceDecl,
BaseNamespaceDeclarationSyntax? namespaceDecl,
genlu marked this conversation as resolved.
Show resolved Hide resolved
CompilationUnitSyntax root,
CSharpCodeFixOptionsProvider options,
[NotNullWhen(true)] out (string title, string equivalenceKey)? info)
Expand All @@ -72,6 +69,7 @@ private static bool CanOfferRefactoring(
CanOfferUseBlockScoped(options.NamespaceDeclarations, namespaceDecl, forAnalyzer: false) ? GetInfo(NamespaceDeclarationPreference.BlockScoped) :
CanOfferUseFileScoped(options.NamespaceDeclarations, root, namespaceDecl, forAnalyzer: false) ? GetInfo(NamespaceDeclarationPreference.FileScoped) :
null;

return info != null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.CSharp</RootNamespace>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>

<!-- NuGet -->
Expand Down
Loading