Skip to content

Commit

Permalink
Fantomas & Type Providers: use trace scenarios for logger (JetBrains#561
Browse files Browse the repository at this point in the history
)
  • Loading branch information
DedSec256 authored Sep 6, 2023
1 parent baae79b commit e278a70
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Linq;
using JetBrains.Collections.Viewable;
using JetBrains.Core;
using JetBrains.Diagnostics;
using JetBrains.Lifetimes;
using JetBrains.Platform.RdFramework.ExternalProcess;
using JetBrains.Platform.RdFramework.ExternalProcess.Util;
using JetBrains.Rd.Impl;
using JetBrains.Util;
using JetBrains.Rd.Tasks;
Expand All @@ -23,8 +21,9 @@ public FantomasEndPoint() : base(FantomasProtocolConstants.PARENT_PROCESS_PID_EN

protected override RdSimpleDispatcher InitDispatcher(Lifetime lifetime, ILogger logger) => new(lifetime, logger);

protected override void InitLogger(Lifetime lifetime, string path) =>
ProtocolEndPointUtil.InitLogger(path, lifetime, LoggingLevel.TRACE);
protected override void InitLogger(Lifetime lifetime, string path)
{
}

protected override RdFantomasModel InitModel(Lifetime lifetime, JetBrains.Rd.Impl.Protocol protocol)
{
Expand Down
10 changes: 10 additions & 0 deletions ReSharper.FSharp/src/FSharp.Fantomas.Protocol/src/ZoneMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using JetBrains.Application.BuildScript.Application.Zones;
using JetBrains.ProjectModel;

namespace JetBrains.ReSharper.Plugins.FSharp.Fantomas.Protocol
{
[ZoneMarker]
public class ZoneMarker : IRequire<IProjectModelZone>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class
TypeProvidersEndPoint : ProtocolEndPoint<RdFSharpTypeProvidersModel, RdSimpleDispatcher>
{
private RdSimpleDispatcher myDispatcher;
private LifetimeDefinition myLoggerLifetime = Lifetime.Define(Lifetime.Terminated);
private string myLoggingPath;

protected override string ProtocolName => "Out-of-Process Type Providers Host";

Expand All @@ -33,21 +35,31 @@ protected override RdSimpleDispatcher InitDispatcher(Lifetime lifetime, ILogger

protected override void InitLogger(Lifetime lifetime, string path)
{
ProtocolEndPointUtil.InitLogger(path, lifetime, LoggingLevel.TRACE);
myLoggingPath = path;

if (Environment.GetEnvironmentVariable("RESHARPER_INTERNAL_MODE") is { } env &&
bool.TryParse(env, out var isInternalMode) && isInternalMode)
{
Trace.Listeners.Clear();
Trace.Listeners.Add(new WriteToLogTraceListener(Logger));
}
}

private void ConfigureTracing(Lifetime lifetime, bool enable)
{
if (enable && myLoggerLifetime.Lifetime.IsNotAlive)
{
myLoggerLifetime = Lifetime.Define(lifetime);
ProtocolEndPointUtil.InitLogger(myLoggingPath, myLoggerLifetime.Lifetime, LoggingLevel.TRACE);
}

Logger.Log(LoggingLevel.INFO, $"Process Runtime: {RuntimeInformation.FrameworkDescription}");
else myLoggerLifetime.Terminate();
}

protected override RdFSharpTypeProvidersModel InitModel(Lifetime lifetime, Rd.Impl.Protocol protocol)
{
var model = new RdFSharpTypeProvidersModel(lifetime, protocol);
model.EnableTracing.Advise(lifetime, enabled => ConfigureTracing(lifetime, enabled));
var typeProvidersContext = new TypeProvidersContext(Logger, myDispatcher.AsTaskScheduler());

new TypeProvidersHost(typeProvidersContext).Initialize(model.RdTypeProviderProcessModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
using JetBrains.Diagnostics;
using JetBrains.Lifetimes;
using JetBrains.Platform.RdFramework.ExternalProcess;
using JetBrains.Platform.RdFramework.Util;
using JetBrains.ProjectModel.BuildTools;
using JetBrains.Rd;
using JetBrains.Rider.FSharp.TypeProviders.Protocol.Client;
using JetBrains.Rider.Model.Loggers;
using JetBrains.Util;

namespace JetBrains.ReSharper.Plugins.FSharp.TypeProviders.Protocol
Expand All @@ -22,15 +24,27 @@ public class
private readonly JetProcessRuntimeRequest myRequest;
private readonly DotNetCoreToolset myToolset;
private readonly bool myIsInternalMode;
private readonly LoggerModel myLoggerModel;
private Lifetime myLifetime;

protected override string Name => "Out-of-Process TypeProviders";

private static readonly FileSystemPath TypeProvidersDirectory =
typeof(TypeProvidersExternalProcess).Assembly.GetPath().Directory.Parent / "typeProviders";

protected override RdFSharpTypeProvidersModel CreateModel(Lifetime lifetime, IProtocol protocol) =>
new(lifetime, protocol);
protected override RdFSharpTypeProvidersModel CreateModel(Lifetime lifetime, IProtocol protocol)
{
var model = new RdFSharpTypeProvidersModel(lifetime, protocol);

if (myLoggerModel.TryGetProto() is { } loggerModelProtocol)
loggerModelProtocol.Scheduler.Queue(() =>
myLoggerModel.TraceCategories.Advise(lifetime, categories =>
protocol.Scheduler.Queue(() =>
model.EnableTracing.Value = categories.Contains(TypeProvidersProtocolConstants.TraceScenario))));

else Logger.Info("Unable to subscribe to LoggerModel.TraceCategories because its protocol is null");
return model;
}

protected override TypeProvidersConnection CreateConnection(Lifetime lifetime,
RdFSharpTypeProvidersModel model, IProtocol protocol, StartupOutputWriter outputWriter, int processId,
Expand Down Expand Up @@ -116,18 +130,20 @@ protected override IDictionary<string, string> GetAdditionalProcessEnvVars()

protected override bool Shutdown(RdFSharpTypeProvidersModel model)
{
model.TryGetProto().NotNull().Scheduler.Queue(() => model.RdTypeProviderProcessModel.Kill.Start(myLifetime, Unit.Instance));
model.TryGetProto().NotNull().Scheduler
.Queue(() => model.RdTypeProviderProcessModel.Kill.Start(myLifetime, Unit.Instance));
return true;
}

public TypeProvidersExternalProcess(Lifetime lifetime, ILogger logger, IShellLocks locks,
IProcessStartInfoPatcher processInfoPatcher, JetProcessRuntimeRequest request, DotNetCoreToolset toolset,
bool isInternalMode)
bool isInternalMode, LoggerModel loggerModel)
: base(lifetime, logger, locks, processInfoPatcher, request, InteractionContext.SolutionContext)
{
myRequest = request;
myToolset = toolset;
myIsInternalMode = isInternalMode;
myLoggerModel = loggerModel;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using JetBrains.Application.Threading;
using JetBrains.Lifetimes;
using JetBrains.Platform.MsBuildHost;
using JetBrains.Platform.RdFramework.Util;
using JetBrains.ProjectModel;
using JetBrains.ProjectModel.Build;
using JetBrains.ProjectModel.Properties;
using JetBrains.Rider.Model.Loggers;
using JetBrains.Util;

namespace JetBrains.ReSharper.Plugins.FSharp.TypeProviders.Protocol
Expand All @@ -19,21 +21,24 @@ public class TypeProvidersExternalProcessFactory
[NotNull] private readonly ISolutionToolset myToolset;
[NotNull] private readonly OutputAssemblies myOutputAssemblies;
[NotNull] private readonly IShellLocks myLocks;
[NotNull] private readonly LoggerModel myLoggerModel;

public TypeProvidersExternalProcessFactory(
[NotNull] ISolutionProcessStartInfoPatcher solutionProcessStartInfoPatcher,
[NotNull] ILogger logger,
[NotNull] IShellLocks shellLocks,
[NotNull] ISolutionToolset toolset,
[NotNull] OutputAssemblies outputAssemblies,
[NotNull] IShellLocks locks)
[NotNull] IShellLocks locks,
[NotNull] LoggerModel loggerModel)
{
mySolutionProcessStartInfoPatcher = solutionProcessStartInfoPatcher;
myLogger = logger;
myShellLocks = shellLocks;
myToolset = toolset;
myOutputAssemblies = outputAssemblies;
myLocks = locks;
myLoggerModel = loggerModel;
}

[CanBeNull]
Expand Down Expand Up @@ -82,7 +87,8 @@ public TypeProvidersExternalProcess Create(Lifetime lifetime,
mySolutionProcessStartInfoPatcher,
runtimeRequest,
toolset,
isInternalMode);
isInternalMode,
myLoggerModel);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using JetBrains.Util;
using JetBrains.Util;
using JetBrains.Util.Logging;

namespace JetBrains.ReSharper.Plugins.FSharp.TypeProviders.Protocol
{
public static class TypeProvidersProtocolConstants
{
public const string TypeProvidersHostPid = "TypeProvidersHost";
public const string TraceScenario = "JetBrains.ReSharper.Plugins.FSharp.TypeProviders.Host";

public static string TypeProvidersHostFrameworkFilename =>
"JetBrains.ReSharper.Plugins.FSharp.TypeProviders.Host.exe";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using JetBrains.Application.BuildScript.Application.Zones;
using JetBrains.ProjectModel;
using JetBrains.ProjectModel.NuGet;
using JetBrains.Rider.Model;

namespace JetBrains.ReSharper.Plugins.FSharp.TypeProviders.Protocol
{
[ZoneMarker]
public class ZoneMarker : IRequire<IProjectModelZone>, IRequire<INuGetZone>
public class ZoneMarker : IRequire<INuGetZone>, IRequire<IRiderModelZone>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,7 @@ object RdFSharpTypeProvidersModel : Root() {
field("RdProvidedConstructorInfoProcessModel", RdProvidedConstructorInfoProcessModel)
field("RdProvidedAssemblyProcessModel", RdProvidedAssemblyProcessModel)
field("RdTestHost", RdTestHost)

property("EnableTracing", bool)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ class FSharpLogTraceScenarios {
"JetBrains.ReSharper.Plugins.FSharp.Shim.FileSystem.FSharpSourceCache",
"JetBrains.ReSharper.Plugins.FSharp.Shim.FileSystem.AssemblyInfoShim"
)

object TypeProviders : LogTraceScenario(
"JetBrains.ReSharper.Plugins.FSharp.TypeProviders.Host"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jetbrains.rider.plugins.fsharp.test.cases.typeProviders

import com.jetbrains.rd.platform.diagnostics.LogTraceScenario
import com.jetbrains.rider.plugins.fsharp.logs.FSharpLogTraceScenarios
import com.jetbrains.rider.plugins.fsharp.rdFSharpModel
import com.jetbrains.rider.projectView.solution
import com.jetbrains.rider.test.base.BaseTestWithSolution

abstract class BaseTypeProvidersTest : BaseTestWithSolution() {
override val restoreNuGetPackages = true
override val traceScenarios: Set<LogTraceScenario>
get() = super.traceScenarios + FSharpLogTraceScenarios.TypeProviders

protected val rdFcsHost get() = project.solution.rdFSharpModel.fsharpTestHost
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.jetbrains.rider.projectView.solutionDirectoryPath
import com.jetbrains.rider.test.annotations.TestEnvironment
import com.jetbrains.rider.test.asserts.shouldBeFalse
import com.jetbrains.rider.test.asserts.shouldBeTrue
import com.jetbrains.rider.test.base.BaseTestWithSolution
import com.jetbrains.rider.test.env.enums.BuildTool
import com.jetbrains.rider.test.env.enums.SdkVersion
import com.jetbrains.rider.test.framework.executeWithGold
Expand All @@ -18,7 +17,7 @@ import java.time.Duration

@Test
@TestEnvironment(sdkVersion = SdkVersion.DOT_NET_CORE_3_1, buildTool = BuildTool.FULL)
class GenerativeTypeProvidersTest : BaseTestWithSolution() {
class GenerativeTypeProvidersTest : BaseTypeProvidersTest() {
override fun getSolutionDirectoryName() = "TypeProviderLibrary"

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.jetbrains.rider.plugins.fsharp.test.cases.typeProviders
import com.jetbrains.rider.daemon.util.hasErrors
import com.jetbrains.rider.test.annotations.TestEnvironment
import com.jetbrains.rider.test.asserts.shouldBeFalse
import com.jetbrains.rider.test.base.BaseTestWithSolution
import com.jetbrains.rider.test.env.enums.SdkVersion
import com.jetbrains.rider.test.framework.executeWithGold
import com.jetbrains.rider.test.scriptingApi.*
Expand All @@ -13,9 +12,8 @@ import org.testng.annotations.Test
import java.io.File

@Test
class TypeProvidersCSharpTest : BaseTestWithSolution() {
class TypeProvidersCSharpTest : BaseTypeProvidersTest() {
override fun getSolutionDirectoryName() = "YamlProviderCSharp"
override val restoreNuGetPackages = true

@Test
fun resolveTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.jetbrains.rider.plugins.fsharp.test.cases.typeProviders
import com.jetbrains.rider.plugins.fsharp.test.dumpTypeProviders
import com.jetbrains.rider.test.annotations.TestEnvironment
import com.jetbrains.rider.test.asserts.shouldBeTrue
import com.jetbrains.rider.test.base.BaseTestWithSolution
import com.jetbrains.rider.test.env.enums.BuildTool
import com.jetbrains.rider.test.env.enums.SdkVersion
import com.jetbrains.rider.test.framework.executeWithGold
Expand All @@ -17,9 +16,8 @@ import java.io.File

@Test
@TestEnvironment(sdkVersion = SdkVersion.DOT_NET_CORE_3_1, buildTool = BuildTool.FULL)
class TypeProvidersCacheTest : BaseTestWithSolution() {
class TypeProvidersCacheTest : BaseTypeProvidersTest() {
override fun getSolutionDirectoryName() = "TypeProviderLibrary"
override val restoreNuGetPackages = true
private val defaultSourceFile = "TypeProviderLibrary/Caches.fs"

private fun checkTypeProviders(testGoldFile: File, sourceFile: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.jetbrains.rider.plugins.fsharp.test.cases.typeProviders

import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.IdeActions
import com.jetbrains.rd.platform.diagnostics.LogTraceScenario
import com.jetbrains.rdclient.testFramework.executeWithGold
import com.jetbrains.rdclient.testFramework.waitForDaemon
import com.jetbrains.rider.plugins.fsharp.logs.FSharpLogTraceScenarios
import com.jetbrains.rider.test.annotations.TestEnvironment
import com.jetbrains.rider.test.base.EditorTestBase
import com.jetbrains.rider.test.env.enums.SdkVersion
Expand All @@ -16,6 +18,8 @@ import java.time.Duration
class TypeProvidersFeaturesTest : EditorTestBase() {
override fun getSolutionDirectoryName() = "SwaggerProviderCSharp"
override val restoreNuGetPackages = true
override val traceScenarios: Set<LogTraceScenario>
get() = super.traceScenarios + FSharpLogTraceScenarios.TypeProviders

@Test
fun `signature file navigation`() = doNavigationTestWithMultipleDeclarations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import com.jetbrains.rider.test.annotations.TestEnvironment
import com.jetbrains.rider.test.asserts.shouldBeFalse
import com.jetbrains.rider.test.asserts.shouldBeTrue
import com.jetbrains.rider.test.asserts.shouldNotBeNull
import com.jetbrains.rider.test.base.BaseTestWithSolution
import com.jetbrains.rider.test.env.enums.BuildTool
import com.jetbrains.rider.test.env.enums.SdkVersion
import com.jetbrains.rider.test.scriptingApi.markupAdapter
import com.jetbrains.rider.test.scriptingApi.withOpenedEditor
import org.testng.annotations.Test

@Test
class TypeProvidersRuntimeTest : BaseTestWithSolution() {
class TypeProvidersRuntimeTest : BaseTypeProvidersTest() {
override fun getSolutionDirectoryName() = "CoreTypeProviderLibrary"
override val restoreNuGetPackages = true

@Test
@TestEnvironment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package com.jetbrains.rider.plugins.fsharp.test.cases.typeProviders

import com.jetbrains.rdclient.testFramework.waitForDaemon
import com.jetbrains.rider.daemon.util.hasErrors
import com.jetbrains.rider.plugins.fsharp.rdFSharpModel
import com.jetbrains.rider.plugins.fsharp.test.withDisabledOutOfProcessTypeProviders
import com.jetbrains.rider.projectView.solution
import com.jetbrains.rider.test.annotations.TestEnvironment
import com.jetbrains.rider.test.asserts.shouldBeFalse
import com.jetbrains.rider.test.asserts.shouldBeNull
import com.jetbrains.rider.test.base.BaseTestWithSolution
import com.jetbrains.rider.test.env.enums.BuildTool
import com.jetbrains.rider.test.env.enums.SdkVersion
import com.jetbrains.rider.test.scriptingApi.markupAdapter
Expand All @@ -17,10 +14,8 @@ import org.testng.annotations.Test

@Test
@TestEnvironment(sdkVersion = SdkVersion.DOT_NET_CORE_3_1, buildTool = BuildTool.FULL)
class TypeProvidersSettingTest : BaseTestWithSolution() {
class TypeProvidersSettingTest : BaseTypeProvidersTest() {
override fun getSolutionDirectoryName() = "TypeProviderLibrary"
override val restoreNuGetPackages = true
private val rdFcsHost get() = project.solution.rdFSharpModel.fsharpTestHost

@Test
fun disabledTypeProvidersSetting() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.jetbrains.rider.plugins.fsharp.test.cases.typeProviders
import com.jetbrains.rdclient.testFramework.executeWithGold
import com.jetbrains.rdclient.testFramework.waitForDaemon
import com.jetbrains.rider.test.annotations.TestEnvironment
import com.jetbrains.rider.test.base.BaseTestWithSolution
import com.jetbrains.rider.test.env.enums.BuildTool
import com.jetbrains.rider.test.env.enums.SdkVersion
import com.jetbrains.rider.test.scriptingApi.dumpSevereHighlighters
Expand All @@ -12,9 +11,8 @@ import org.testng.annotations.Test

@Test
@TestEnvironment(sdkVersion = SdkVersion.DOT_NET_CORE_3_1, buildTool = BuildTool.FULL)
class TypeProvidersTest : BaseTestWithSolution() {
class TypeProvidersTest : BaseTypeProvidersTest() {
override fun getSolutionDirectoryName() = "TypeProviderLibrary"
override val restoreNuGetPackages = true

@Test
fun swaggerProvider() = doTest("SwaggerProvider")
Expand Down

0 comments on commit e278a70

Please sign in to comment.