@@ -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!
@@ -657,6 +659,26 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
657
659
return addr
658
660
}
659
661
662
+ /**
663
+ * Reads the socketPath which defaults to a temporary directory
664
+ * with another directory called vscode-ipc.
665
+ *
666
+ * If it can't read the path, it throws an error and returns undefined.
667
+ */
668
+ export async function readSocketPath ( path : string ) : Promise < string | undefined > {
669
+ try {
670
+ return await fs . readFile ( path , "utf8" )
671
+ } catch ( error ) {
672
+ // If it doesn't exist, we don't care.
673
+ // But if it fails for some reason, we should throw.
674
+ // We want to surface that to the user.
675
+ if ( ! isNodeJSErrnoException ( error ) || error . code !== "ENOENT" ) {
676
+ throw error
677
+ }
678
+ }
679
+ return undefined
680
+ }
681
+
660
682
/**
661
683
* Determine if it looks like the user is trying to open a file or folder in an
662
684
* existing instance. The arguments here should be the arguments the user
@@ -668,32 +690,21 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise<string |
668
690
return process . env . VSCODE_IPC_HOOK_CLI
669
691
}
670
692
671
- const readSocketPath = async ( ) : Promise < string | undefined > => {
672
- try {
673
- return await fs . readFile ( path . join ( os . tmpdir ( ) , "vscode-ipc" ) , "utf8" )
674
- } catch ( error : any ) {
675
- if ( error . code !== "ENOENT" ) {
676
- throw error
677
- }
678
- }
679
- return undefined
680
- }
681
-
682
693
// If these flags are set then assume the user is trying to open in an
683
694
// existing instance since these flags have no effect otherwise.
684
695
const openInFlagCount = [ "reuse-window" , "new-window" ] . reduce ( ( prev , cur ) => {
685
696
return args [ cur as keyof Args ] ? prev + 1 : prev
686
697
} , 0 )
687
698
if ( openInFlagCount > 0 ) {
688
- return readSocketPath ( )
699
+ return readSocketPath ( DEFAULT_SOCKET_PATH )
689
700
}
690
701
691
702
// It's possible the user is trying to spawn another instance of code-server.
692
703
// Check if any unrelated flags are set (check against one because `_` always
693
704
// exists), that a file or directory was passed, and that the socket is
694
705
// active.
695
706
if ( Object . keys ( args ) . length === 1 && args . _ . length > 0 ) {
696
- const socketPath = await readSocketPath ( )
707
+ const socketPath = await readSocketPath ( DEFAULT_SOCKET_PATH )
697
708
if ( socketPath && ( await canConnect ( socketPath ) ) ) {
698
709
return socketPath
699
710
}
0 commit comments