Skip to content

RunInTerminal Request strategy #1

Description

@GustavEikaas

Hi there,

I randomly stumbled upon this repository while checking out some netcoredbg stuff. It looks really cool! I'm not sure if this will be helpful, but I am currently building C# "IDE tooling" for Neovim.

I recently had some users asking about runInTerminal support because they wanted to use methods like System.Console.ReadLine(), which I ended up building on top of netcoredbg. I figured if you are considering implementing runInTerminal for this project, you might run into the exact same issue I did: How the heck do you pause the external terminal process long enough to properly attach the debugger?

I ended up using a .NET Startup Hook to solve this and just wanted to share the idea. Here is the full PR if you are interested in seeing how it works.

I am definitely better at C# than C++, but if I can help out in any way, please don't hesitate to ask. Currently, I am bundling netcoredbg with my server, but perhaps I can bundle this project in the future!

internal static class StartupHook
{
  public static void Initialize()
  {
    var pipeName = ReadAndDiscardHookPipe();
    RemoveStartupHookEnvironment();

    if (string.IsNullOrEmpty(pipeName)) return;

    using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut);
    client.Connect(5000);

    var pid = Environment.ProcessId;
    var pidBytes = BitConverter.GetBytes(pid);
    client.Write(pidBytes, 0, pidBytes.Length);
    client.Flush();
    client.ReadByte();
  }

  private static string? ReadAndDiscardHookPipe()
  {
    const string envVarName = "EASY_DOTNET_HOOK_PIPE";
    var pipeName = Environment.GetEnvironmentVariable(envVarName);
    Environment.SetEnvironmentVariable(envVarName, null);
    return pipeName;
  }

  private static void RemoveStartupHookEnvironment()
  {
    var currentHooks = Environment.GetEnvironmentVariable("DOTNET_STARTUP_HOOKS");
    if (string.IsNullOrEmpty(currentHooks)) return;

    var hooksList = currentHooks.Split(Path.PathSeparator);

    var remainingHooks = string.Join(Path.PathSeparator.ToString(), hooksList.Skip(1));

    Environment.SetEnvironmentVariable("DOTNET_STARTUP_HOOKS", string.IsNullOrEmpty(remainingHooks) ? null : remainingHooks);
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions