Skip to content

Commit

Permalink
Remove ServiceHub execution restriction from Semantic Search
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Apr 23, 2024
1 parent 6dd5071 commit 1cec1a5
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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.

#if NET6_0_OR_GREATER

using System;
using System.Collections.Generic;
using System.Composition;
Expand Down Expand Up @@ -48,5 +46,3 @@ internal sealed class CSharpSemanticSearchService() : AbstractSemanticSearchServ
CSharpSemanticSearchUtilities.CompilationOptions);
}
}

#endif
3 changes: 0 additions & 3 deletions src/Features/Core/Portable/FeaturesResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3240,9 +3240,6 @@ Zero-width positive lookbehind assertions are typically used at the beginning of
<data name="Query" xml:space="preserve">
<value>Query</value>
</data>
<data name="Semantic_search_only_supported_on_net_core" xml:space="preserve">
<value>Semantic search is only supported when code analysis runs in a separate process on the latest .NET (see Tools > Options > Text Editor > C# > Advanced).</value>
</data>
<data name="Semantic_search_query_terminated_with_exception" xml:space="preserve">
<value>Semantic search query terminated with exception</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#if NET6_0_OR_GREATER

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.FindUsages;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Tags;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

#if NET6_0_OR_GREATER
using System.Runtime.Loader;
#endif

namespace Microsoft.CodeAnalysis.SemanticSearch;

internal abstract partial class AbstractSemanticSearchService : ISemanticSearchService
{
#if NET6_0_OR_GREATER
internal sealed class LoadContext() : AssemblyLoadContext("SemanticSearchLoadContext", isCollectible: true)
{
private readonly AssemblyLoadContext _current = GetLoadContext(typeof(LoadContext).Assembly)!;
Expand All @@ -47,6 +43,22 @@ internal sealed class LoadContext() : AssemblyLoadContext("SemanticSearchLoadCon
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
=> IntPtr.Zero;
}
#else
internal sealed class LoadContext
{
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Signature needs to match AssemblyLoadContext")]
public Assembly LoadFromStream(Stream assembly, Stream? assemblySymbols)
{
return Assembly.Load(assembly.ReadAllBytes(), assemblySymbols?.ReadAllBytes());
}

[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Signature needs to match AssemblyLoadContext")]
public void Unload()
{
// No action to take
}
}
#endif

private static readonly FindReferencesSearchOptions s_findReferencesSearchOptions = new()
{
Expand All @@ -63,7 +75,7 @@ protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
string referenceAssembliesDir,
ISemanticSearchResultsObserver observer,
OptionsProvider<ClassificationOptions> classificationOptions,
TraceSource traceSource,
TraceSource? traceSource,
CancellationToken cancellationToken)
{
try
Expand Down Expand Up @@ -108,7 +120,7 @@ protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
if (diagnostic.Severity == DiagnosticSeverity.Error)
{
traceSource.TraceInformation($"Semantic search query compilation failed: {diagnostic}");
traceSource?.TraceInformation($"Semantic search query compilation failed: {diagnostic}");
}
}

Expand Down Expand Up @@ -136,7 +148,7 @@ protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)

if (!TryGetFindMethod(queryAssembly, out var findMethod, out var errorMessage, out var errorMessageArgs))
{
traceSource.TraceInformation($"Semantic search failed: {errorMessage}");
traceSource?.TraceInformation($"Semantic search failed: {errorMessage}");
return CreateResult(errorMessage, errorMessageArgs);
}

Expand Down Expand Up @@ -208,7 +220,7 @@ protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)

await observer.OnUserCodeExceptionAsync(new UserCodeExceptionInfo(projectDisplay, e.Message, exceptionNameTaggedText, stackTraceTaggedText, span), cancellationToken).ConfigureAwait(false);

traceSource.TraceInformation($"Semantic query execution failed due to user code exception: {e}");
traceSource?.TraceInformation($"Semantic query execution failed due to user code exception: {e}");
return CreateResult(FeaturesResources.Semantic_search_query_terminated_with_exception);
}

Expand Down Expand Up @@ -289,7 +301,7 @@ private static void FormatStackTrace(Exception e, Assembly queryAssembly, out Li

// display last StackDisplayDepthLimit frames preceding the host frame:
skippedFrameCount = Math.Max(0, displayFramesEnd - StackDisplayDepthLimit);
displayFrames = frames[skippedFrameCount..displayFramesEnd];
displayFrames = frames.AsSpan()[skippedFrameCount..displayFramesEnd].ToArray();
}
catch
{
Expand Down Expand Up @@ -433,4 +445,3 @@ private static bool TryGetFindMethod(Assembly queryAssembly, [NotNullWhen(true)]
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.FindUsages;
using Microsoft.CodeAnalysis.Host.Mef;
Expand Down Expand Up @@ -131,19 +130,28 @@ public async ValueTask<ClassificationOptions> GetClassificationOptionsAsync(stri
public static async ValueTask<ExecuteQueryResult> ExecuteQueryAsync(Solution solution, string language, string query, string referenceAssembliesDir, ISemanticSearchResultsObserver results, OptionsProvider<ClassificationOptions> classificationOptions, CancellationToken cancellationToken)
{
var client = await RemoteHostClient.TryGetClientAsync(solution.Services, cancellationToken).ConfigureAwait(false);
if (client == null)
if (client != null)
{
return new ExecuteQueryResult(FeaturesResources.Semantic_search_only_supported_on_net_core);
}
var serverCallback = new ServerCallback(solution, results, classificationOptions);

var serverCallback = new ServerCallback(solution, results, classificationOptions);
var result = await client.TryInvokeAsync<IRemoteSemanticSearchService, ExecuteQueryResult>(
solution,
(service, solutionInfo, callbackId, cancellationToken) => service.ExecuteQueryAsync(solutionInfo, callbackId, language, query, referenceAssembliesDir, cancellationToken),
callbackTarget: serverCallback,
cancellationToken).ConfigureAwait(false);

var result = await client.TryInvokeAsync<IRemoteSemanticSearchService, ExecuteQueryResult>(
solution,
(service, solutionInfo, callbackId, cancellationToken) => service.ExecuteQueryAsync(solutionInfo, callbackId, language, query, referenceAssembliesDir, cancellationToken),
callbackTarget: serverCallback,
cancellationToken).ConfigureAwait(false);
if (result.HasValue)
{
return result.Value;
}
}

return result.Value;
return await ExecuteQueryInCurrentProcessAsync(solution, language, query, referenceAssembliesDir, results, classificationOptions, cancellationToken).ConfigureAwait(false);
}

public static async ValueTask<ExecuteQueryResult> ExecuteQueryInCurrentProcessAsync(Solution solution, string language, string query, string referenceAssembliesDir, ISemanticSearchResultsObserver results, OptionsProvider<ClassificationOptions> classificationOptions, CancellationToken cancellationToken)
{
var service = solution.Services.GetLanguageServices(language).GetRequiredService<ISemanticSearchService>();
return await service.ExecuteQueryAsync(solution, query, referenceAssembliesDir, results, classificationOptions, traceSource: null, cancellationToken).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
Expand All @@ -29,6 +28,6 @@ internal interface ISemanticSearchService : ILanguageService
string referenceAssembliesDir,
ISemanticSearchResultsObserver observer,
OptionsProvider<ClassificationOptions> classificationOptions,
TraceSource traceSource,
TraceSource? traceSource,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Shared.Extensions;
using System.Linq;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.SemanticSearch;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.SemanticSearch;
Expand Down
5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/Features/Core/Portable/xlf/FeaturesResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1cec1a5

Please sign in to comment.