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

Fall back to VS looking for port. Add way to disable csharpier server. #1257

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions Src/CSharpier.Cli/Server/ServerFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ public static int FindFreePort()
var ipEndPoint = ipGlobalProperties.GetActiveTcpListeners();

var usedPorts = ipEndPoint
.Where(o => o.Port >= startPort)
.Select(o => o.Port)
.Concat(tcpConnInfoArray.Select(o => o.LocalEndPoint.Port))
.ToList();
.Concat(
tcpConnInfoArray
.Where(o => o.LocalEndPoint.Port >= startPort)
.Select(o => o.LocalEndPoint.Port)
)
.ToHashSet();

for (var i = startPort; i < endPort; i++)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.7.2" Language="en-US" Publisher="CSharpier" />
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.7.3" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.7.2" Language="en-US" Publisher="CSharpier" />
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.7.3" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier 2019</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ public class CSharpierOptions
)]
public string? CustomPath { get; set; }

[Category("CSharpier - Developer")]
[DisplayName("Disable CSharpier Server")]
[Description(
"Disable CSharpier Server - Use the legacy version of piping stdin to csharpier for formatting files."
)]
public bool DisableCSharpierServer { get; set; }

protected void LoadFrom(CSharpierOptions newInstance)
{
this.SolutionRunOnSave = newInstance.SolutionRunOnSave;
this.GlobalRunOnSave = newInstance.GlobalRunOnSave;
this.GlobalLogDebugMessages = newInstance.GlobalLogDebugMessages;
this.CustomPath = newInstance.CustomPath;
this.DisableCSharpierServer = newInstance.DisableCSharpierServer;
}

private static readonly AsyncLazy<CSharpierOptions> liveModel =
Expand Down Expand Up @@ -118,6 +126,7 @@ await LoadOptionsFromFile(
newInstance.GlobalRunOnSave = o.RunOnSave;
newInstance.GlobalLogDebugMessages = o.LogDebugMessages;
newInstance.CustomPath = o.CustomPath;
newInstance.DisableCSharpierServer = o.DisableCSharpierServer;
}
);

Expand Down Expand Up @@ -172,6 +181,7 @@ await SaveOptions(
RunOnSave = this.GlobalRunOnSave,
LogDebugMessages = this.GlobalLogDebugMessages,
CustomPath = this.CustomPath,
DisableCSharpierServer = this.DisableCSharpierServer,
}
);
}
Expand Down Expand Up @@ -206,6 +216,7 @@ private class OptionsDto
public bool? RunOnSave { get; set; }
public bool LogDebugMessages { get; set; }
public string? CustomPath { get; set; }
public bool DisableCSharpierServer { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,25 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version
var serverVersion = new Version("0.28.0");
ICSharpierProcess cSharpierProcess;

if (installedVersion.CompareTo(serverVersion) >= 0)
if (
installedVersion.CompareTo(serverVersion) >= 0
&& !CSharpierOptions.Instance.DisableCSharpierServer
)
{
cSharpierProcess = new CSharpierProcessServer(customPath, version, this.logger);
}
else if (installedVersion.CompareTo(pipeFilesVersion) >= 0)
{
if (
installedVersion.CompareTo(serverVersion) >= 0
&& CSharpierOptions.Instance.DisableCSharpierServer
)
{
this.logger.Debug(
"CSharpier server is disabled, falling back to piping via stdin"
);
}

cSharpierProcess = new CSharpierProcessPipeMultipleFiles(
customPath,
version,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.CodeDom;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using CSharpier.VisualStudio;
Expand Down Expand Up @@ -32,10 +35,32 @@ public CSharpierProcessServer(string csharpierPath, string version, Logger logge
}

private void StartProcess()
{
if (this.ActuallyStartProcess())
{
return;
}

var portToUse = this.FindFreePort();
if (!this.ActuallyStartProcess(portToUse))
{
this.ProcessFailedToStart = true;
}
}

private bool ActuallyStartProcess(int portToUse = -1)
{
try
{
var processStartInfo = new ProcessStartInfo(this.csharpierPath, "--server")
var arguments = "--server";
if (portToUse > 0)
{
arguments += " --server-port " + portToUse;
}

this.logger.Debug("Running " + this.csharpierPath + " " + arguments);

var processStartInfo = new ProcessStartInfo(this.csharpierPath, arguments)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
Expand All @@ -54,14 +79,13 @@ private void StartProcess()

if (!task.Wait(TimeSpan.FromSeconds(2)))
{
this.process!.Kill();
this.logger.Warn(
"Spawning the csharpier server timed out. Formatting cannot occur."
"Spawning the csharpier server timed out."
+ Environment.NewLine
+ output
+ this.process!.StandardError.ReadToEnd()
);
this.process!.Kill();
this.ProcessFailedToStart = true;
return;
return false;
}

if (this.process!.HasExited)
Expand All @@ -70,20 +94,54 @@ private void StartProcess()
"Spawning the csharpier server failed because it exited. "
+ this.process!.StandardError.ReadToEnd()
);
this.ProcessFailedToStart = true;
return;
return false;
}

var portString = output.Replace("Started on ", "");
this.port = int.Parse(portString);

this.logger.Debug("Connecting via port " + portString);
return true;
}
catch (Exception e)
{
this.logger.Warn("Failed to spawn the needed csharpier server." + e);
this.ProcessFailedToStart = true;
return false;
}
}

private int FindFreePort()
{
this.logger.Debug("Trying to find free port in extension");
const int startPort = 49152;
const int endPort = 65535;
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
var tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
var ipEndPoint = ipGlobalProperties.GetActiveTcpListeners();

var usedPorts = ipEndPoint
.Where(o => o.Port >= startPort)
.Select(o => o.Port)
.Concat(
tcpConnInfoArray
.Where(o => o.LocalEndPoint.Port >= startPort)
.Select(o => o.LocalEndPoint.Port)
)
.ToHashSet();

this.logger.Debug($"Found {usedPorts.Count} used ports that could conflict");

for (var i = startPort; i < endPort; i++)
{
if (!usedPorts.Contains(i))
{
return i;
}
}

throw new InvalidOperationException(
$"Could not find any free TCP port between ports {startPort}-{endPort}"
);
}

public string FormatFile(string content, string filePath)
Expand Down
6 changes: 5 additions & 1 deletion Src/CSharpier.VisualStudio/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## [1.7.2]
## [1.7.3]
- If CSharpier doesn't respond when trying to find a port, then try to find a port in extension
- Add option to bypass csharpier server.

## [1.7.2]
- Fix bad code path when csharpier server failed to start

## [1.7.1]
Expand Down
Loading