@@ -749,27 +749,32 @@ export const shouldOpenInExistingInstance = async (args: UserProvidedArgs): Prom
749
749
const paths = getResolvedPathsFromArgs ( args )
750
750
const client = new EditorSessionManagerClient ( DEFAULT_SOCKET_PATH )
751
751
752
- // If we can't connect to the socket then there's no existing instance.
753
- if ( ! ( await client . canConnect ( ) ) ) {
754
- return undefined
755
- }
756
-
757
752
// If these flags are set then assume the user is trying to open in an
758
- // existing instance since these flags have no effect otherwise.
753
+ // existing instance since these flags have no effect otherwise. That means
754
+ // if there is no existing instance we should error rather than falling back
755
+ // to spawning code-server normally.
759
756
const openInFlagCount = [ "reuse-window" , "new-window" ] . reduce ( ( prev , cur ) => {
760
757
return args [ cur as keyof UserProvidedArgs ] ? prev + 1 : prev
761
758
} , 0 )
762
759
if ( openInFlagCount > 0 ) {
763
760
logger . debug ( "Found --reuse-window or --new-window" )
764
- return await client . getConnectedSocketPath ( paths [ 0 ] )
761
+ const socketPath = await client . getConnectedSocketPath ( paths [ 0 ] )
762
+ if ( ! socketPath ) {
763
+ throw new Error ( `No opened code-server instances found to handle ${ paths [ 0 ] } ` )
764
+ }
765
+ return socketPath
765
766
}
766
767
767
768
// It's possible the user is trying to spawn another instance of code-server.
768
769
// 1. Check if any unrelated flags are set (this should only run when
769
770
// code-server is invoked exactly like this: `code-server my-file`).
770
771
// 2. That a file or directory was passed.
771
772
// 3. That the socket is active.
773
+ // 4. That an instance exists to handle the path (implied by #3).
772
774
if ( Object . keys ( args ) . length === 1 && typeof args . _ !== "undefined" && args . _ . length > 0 ) {
775
+ if ( ! ( await client . canConnect ( ) ) ) {
776
+ return undefined
777
+ }
773
778
const socketPath = await client . getConnectedSocketPath ( paths [ 0 ] )
774
779
if ( socketPath ) {
775
780
logger . debug ( "Found existing code-server socket" )
0 commit comments