Skip to content

Commit

Permalink
Fix issue with csharpier server not using dotnet root (#1239)
Browse files Browse the repository at this point in the history
closes #1238
  • Loading branch information
belav authored Apr 26, 2024
1 parent 5d898a3 commit 488ddab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Src/CSharpier.VSCode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.7.1]
- Fix issue with csharpier server not supporting dotnet root

## [1.7.0]
- Use CSharpier Http Server for 0.28.0+
- Log version of CSharpier used to format a given file
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.VSCode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "csharpier-vscode",
"displayName": "CSharpier - Code formatter",
"description": "Code formatter using csharpier",
"version": "1.7.0",
"version": "1.7.1",
"publisher": "csharpier",
"author": "CSharpier",
"homepage": "https://marketplace.visualstudio.com/items?itemName=csharpier.csharpier-vscode",
Expand Down
20 changes: 16 additions & 4 deletions Src/CSharpier.VSCode/src/CSharpierProcessServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { Logger } from "./Logger";
import { FormatFileParameter, FormatFileResult, ICSharpierProcess2 } from "./ICSharpierProcess";
import fetch from "node-fetch";
import { getDotNetRoot } from "./DotNetProvider";

export class CSharpierProcessServer implements ICSharpierProcess2 {
private csharpierPath: string;
Expand All @@ -19,8 +20,9 @@ export class CSharpierProcessServer implements ICSharpierProcess2 {

this.logger.debug("Warm CSharpier with initial format");
// warm by formatting a file twice, the 3rd time is when it gets really fast
this.formatFile("public class ClassName { }", "/Temp/Test.cs").then(() => {
this.formatFile("public class ClassName { }", "/Temp/Test.cs");
// make sure we give a path that should not exist to avoid any errors when trying to find config files etc.
this.formatFile("public class ClassName { }", `/${Date.now()}/Test.cs`).then(() => {
this.formatFile("public class ClassName { }", `/${Date.now()}/Test.cs`);
});
}

Expand All @@ -32,7 +34,7 @@ export class CSharpierProcessServer implements ICSharpierProcess2 {
const csharpierProcess = spawn(csharpierPath, ["--server"], {
stdio: "pipe",
cwd: workingDirectory,
env: { ...process.env, DOTNET_NOLOGO: "1" },
env: { ...process.env, DOTNET_NOLOGO: "1", DOTNET_ROOT: getDotNetRoot() },
});

csharpierProcess.on("error", data => {
Expand All @@ -43,6 +45,16 @@ export class CSharpierProcessServer implements ICSharpierProcess2 {
this.processFailedToStart = true;
});

csharpierProcess.on("exit", () => {
if (csharpierProcess.exitCode !== null && csharpierProcess.exitCode > 0) {
this.processFailedToStart = true;
}
});

csharpierProcess.stderr.on("data", data => {
this.logger.warn("CSharpier process return the following error: ", data.toString());
});

let output = "";
const regex = /^Started on (\d+)/;

Expand Down Expand Up @@ -72,7 +84,7 @@ export class CSharpierProcessServer implements ICSharpierProcess2 {
}

if (typeof this.process === "undefined") {
await new Promise(r => setTimeout(r, 2000));
await new Promise(r => setTimeout(r, 1000));
}

if (this.processFailedToStart || typeof this.process === "undefined") {
Expand Down

0 comments on commit 488ddab

Please sign in to comment.