@@ -3,7 +3,9 @@ import { promises as fs } from "fs"
3
3
import yaml from "js-yaml"
4
4
import * as os from "os"
5
5
import * as path from "path"
6
- import { canConnect , generateCertificate , generatePassword , humanPath , paths } from "./util"
6
+ import { canConnect , generateCertificate , generatePassword , humanPath , paths , isNodeJSErrnoException } from "./util"
7
+
8
+ const DEFAULT_SOCKET_PATH = path . join ( os . tmpdir ( ) , "vscode-ipc" )
7
9
8
10
export enum Feature {
9
11
// No current experimental features!
@@ -667,6 +669,26 @@ export const shouldRunVsCodeCli = (args: Args): boolean => {
667
669
return extensionRelatedArgs . some ( ( arg ) => argKeys . includes ( arg ) )
668
670
}
669
671
672
+ /**
673
+ * Reads the socketPath which defaults to a temporary directory
674
+ * with another directory called vscode-ipc.
675
+ *
676
+ * If it can't read the path, it throws an error and returns undefined.
677
+ */
678
+ export async function readSocketPath ( path : string ) : Promise < string | undefined > {
679
+ try {
680
+ return await fs . readFile ( path , "utf8" )
681
+ } catch ( error ) {
682
+ // If it doesn't exist, we don't care.
683
+ // But if it fails for some reason, we should throw.
684
+ // We want to surface that to the user.
685
+ if ( ! isNodeJSErrnoException ( error ) || error . code !== "ENOENT" ) {
686
+ throw error
687
+ }
688
+ }
689
+ return undefined
690
+ }
691
+
670
692
/**
671
693
* Determine if it looks like the user is trying to open a file or folder in an
672
694
* existing instance. The arguments here should be the arguments the user
@@ -678,32 +700,21 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise<string |
678
700
return process . env . VSCODE_IPC_HOOK_CLI
679
701
}
680
702
681
- const readSocketPath = async ( ) : Promise < string | undefined > => {
682
- try {
683
- return await fs . readFile ( path . join ( os . tmpdir ( ) , "vscode-ipc" ) , "utf8" )
684
- } catch ( error : any ) {
685
- if ( error . code !== "ENOENT" ) {
686
- throw error
687
- }
688
- }
689
- return undefined
690
- }
691
-
692
703
// If these flags are set then assume the user is trying to open in an
693
704
// existing instance since these flags have no effect otherwise.
694
705
const openInFlagCount = [ "reuse-window" , "new-window" ] . reduce ( ( prev , cur ) => {
695
706
return args [ cur as keyof Args ] ? prev + 1 : prev
696
707
} , 0 )
697
708
if ( openInFlagCount > 0 ) {
698
- return readSocketPath ( )
709
+ return readSocketPath ( DEFAULT_SOCKET_PATH )
699
710
}
700
711
701
712
// It's possible the user is trying to spawn another instance of code-server.
702
713
// Check if any unrelated flags are set (check against one because `_` always
703
714
// exists), that a file or directory was passed, and that the socket is
704
715
// active.
705
716
if ( Object . keys ( args ) . length === 1 && args . _ . length > 0 ) {
706
- const socketPath = await readSocketPath ( )
717
+ const socketPath = await readSocketPath ( DEFAULT_SOCKET_PATH )
707
718
if ( socketPath && ( await canConnect ( socketPath ) ) ) {
708
719
return socketPath
709
720
}
0 commit comments