Skip to content

Commit cf8f31b

Browse files
Switch to named pipes (#1327)
* Switch to named pipes * no need for string interpolation
1 parent 34bafe7 commit cf8f31b

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

src/debugAdapter.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import fs = require("fs");
66
import net = require("net");
7+
import os = require("os");
78
import path = require("path");
89
import { Logger } from "./logging";
910
import utils = require("./utils");
@@ -39,10 +40,10 @@ function startDebugging() {
3940
utils.deleteSessionFile(debugSessionFilePath);
4041

4142
// Establish connection before setting up the session
42-
debugAdapterLogWriter.write("Connecting to port: " + sessionDetails.debugServicePort + "\r\n");
43+
debugAdapterLogWriter.write("Connecting to pipe: " + sessionDetails.debugServicePipeName + "\r\n");
4344

4445
let isConnected = false;
45-
const debugServiceSocket = net.connect(sessionDetails.debugServicePort, "127.0.0.1");
46+
const debugServiceSocket = net.connect(utils.getPipePath(sessionDetails.debugServicePipeName));
4647

4748
// Write any errors to the log file
4849
debugServiceSocket.on(
@@ -73,7 +74,7 @@ function startDebugging() {
7374

7475
// Resume the stdin stream
7576
process.stdin.resume();
76-
});
77+
});
7778

7879
// When the socket closes, end the session
7980
debugServiceSocket.on(

src/session.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*--------------------------------------------------------*/
44

55
import cp = require("child_process");
6+
import crypto = require("crypto");
67
import fs = require("fs");
78
import net = require("net");
89
import os = require("os");
@@ -165,19 +166,23 @@ export class SessionManager implements Middleware {
165166
}
166167
}
167168

169+
// Generate a random id for the named pipes in case they have multiple instances of PSES running
170+
const id = crypto.randomBytes(10).toString("hex");
168171
this.editorServicesArgs =
169-
"-HostName 'Visual Studio Code Host' " +
170-
"-HostProfileId 'Microsoft.VSCode' " +
171-
"-HostVersion '" + this.hostVersion + "' " +
172-
"-AdditionalModules @('PowerShellEditorServices.VSCode') " +
173-
"-BundledModulesPath '" + this.bundledModulesPath + "' " +
174-
"-EnableConsoleRepl ";
172+
`-HostName 'Visual Studio Code Host' ` +
173+
`-HostProfileId 'Microsoft.VSCode' ` +
174+
`-HostVersion '${this.hostVersion}'` +
175+
`-AdditionalModules @('PowerShellEditorServices.VSCode') ` +
176+
`-BundledModulesPath '${this.bundledModulesPath}'` +
177+
`-EnableConsoleRepl ` +
178+
`-LanguageServicePipeName LanguageService_${id}.pipe ` +
179+
`-DebugServicePipeName DebugService_${id}.pipe `;
175180

176181
if (this.sessionSettings.developer.editorServicesWaitForDebugger) {
177182
this.editorServicesArgs += "-WaitForDebugger ";
178183
}
179184
if (this.sessionSettings.developer.editorServicesLogLevel) {
180-
this.editorServicesArgs += "-LogLevel '" + this.sessionSettings.developer.editorServicesLogLevel + "' ";
185+
this.editorServicesArgs += `-LogLevel '${this.sessionSettings.developer.editorServicesLogLevel}' `;
181186
}
182187

183188
this.startPowerShell();
@@ -531,18 +536,18 @@ export class SessionManager implements Middleware {
531536

532537
private startLanguageClient(sessionDetails: utils.IEditorServicesSessionDetails) {
533538

534-
const port = sessionDetails.languageServicePort;
539+
const pipeName = sessionDetails.languageServicePipeName;
535540

536541
// Log the session details object
537542
this.log.write(JSON.stringify(sessionDetails));
538543

539544
try {
540-
this.log.write("Connecting to language service on port " + port + "...");
545+
this.log.write("Connecting to language service on pipe " + pipeName + "...");
541546

542547
const connectFunc = () => {
543548
return new Promise<StreamInfo>(
544549
(resolve, reject) => {
545-
const socket = net.connect(port);
550+
const socket = net.connect(utils.getPipePath(pipeName));
546551
socket.on(
547552
"connect",
548553
() => {

src/utils.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ export function getPipePath(pipeName: string) {
2626
if (os.platform() === "win32") {
2727
return "\\\\.\\pipe\\" + pipeName;
2828
} else {
29-
// On UNIX platforms the pipe will live under the temp path
30-
// For details on how this path is computed, see the corefx
31-
// source for System.IO.Pipes.PipeStream:
32-
// tslint:disable-next-line:max-line-length
33-
// https://github.com/dotnet/corefx/blob/d0dc5fc099946adc1035b34a8b1f6042eddb0c75/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs#L340
34-
return path.resolve(
35-
os.tmpdir(),
36-
".dotnet", "corefx", "pipe",
37-
pipeName);
29+
// Windows uses NamedPipes where non-Windows platforms use Unix Domain Sockets.
30+
// This requires connecting to the pipe file in different locations on Windows vs non-Windows.
31+
return path.join(os.tmpdir(), `CoreFxPipe_${pipeName}`);
3832
}
3933
}
4034

@@ -46,6 +40,8 @@ export interface IEditorServicesSessionDetails {
4640
channel: string;
4741
languageServicePort: number;
4842
debugServicePort: number;
43+
languageServicePipeName: string;
44+
debugServicePipeName: string;
4945
}
5046

5147
export type IReadSessionFileCallback = (details: IEditorServicesSessionDetails) => void;

0 commit comments

Comments
 (0)