@@ -37,7 +37,7 @@ async fn capture_unix(
3737 use crate :: command:: new_std_command;
3838
3939 let shell_kind = ShellKind :: new ( shell_path, false ) ;
40- let zed_path = super :: get_shell_safe_zed_path ( shell_kind) ?;
40+ let quoted_zed_path = super :: get_shell_safe_zed_path ( shell_kind) ?;
4141
4242 let mut command_string = String :: new ( ) ;
4343 let mut command = new_std_command ( shell_path) ;
@@ -82,12 +82,23 @@ async fn capture_unix(
8282 _ => command. args ( [ "-i" , "-c" ] ) ,
8383 } ;
8484
85+ // Prefix with "./" if the path starts with "-" to prevent cd from interpreting it as a flag
86+ let dir_str = directory. to_string_lossy ( ) ;
87+ let dir_str = if dir_str. starts_with ( '-' ) {
88+ format ! ( "./{dir_str}" ) . into ( )
89+ } else {
90+ dir_str
91+ } ;
92+ let quoted_dir = shell_kind
93+ . try_quote ( & dir_str)
94+ . context ( "unexpected null in directory name" ) ?;
95+
8596 // cd into the directory, triggering directory specific side-effects (asdf, direnv, etc)
86- command_string. push_str ( & format ! ( "cd '{}' ;" , directory . display ( ) ) ) ;
97+ command_string. push_str ( & format ! ( "cd {} ;" , quoted_dir ) ) ;
8798 if let Some ( prefix) = shell_kind. command_prefix ( ) {
8899 command_string. push ( prefix) ;
89100 }
90- command_string. push_str ( & format ! ( "{} --printenv {}" , zed_path , redir) ) ;
101+ command_string. push_str ( & format ! ( "{} --printenv {}" , quoted_zed_path , redir) ) ;
91102
92103 if let ShellKind :: Nushell = shell_kind {
93104 command_string. push_str ( "; exit" ) ;
@@ -155,56 +166,50 @@ async fn capture_windows(
155166 std:: env:: current_exe ( ) . context ( "Failed to determine current zed executable path." ) ?;
156167
157168 let shell_kind = ShellKind :: new ( shell_path, true ) ;
169+ // Prefix with "./" if the path starts with "-" to prevent cd from interpreting it as a flag
158170 let directory_string = directory. display ( ) . to_string ( ) ;
171+ let directory_string = if directory_string. starts_with ( '-' ) {
172+ format ! ( "./{directory_string}" )
173+ } else {
174+ directory_string
175+ } ;
159176 let zed_path_string = zed_path. display ( ) . to_string ( ) ;
160177 let quote_for_shell = |value : & str | {
161178 shell_kind
162179 . try_quote ( value)
163180 . map ( |quoted| quoted. into_owned ( ) )
164- . unwrap_or_else ( || value . to_owned ( ) )
181+ . context ( "unexpected null in directory name" )
165182 } ;
166183 let mut cmd = crate :: command:: new_command ( shell_path) ;
167184 cmd. args ( args) ;
185+ let quoted_directory = quote_for_shell ( & directory_string) ?;
186+ let quoted_zed_path = quote_for_shell ( & zed_path_string) ?;
168187 let cmd = match shell_kind {
169188 ShellKind :: Csh
170189 | ShellKind :: Tcsh
171190 | ShellKind :: Rc
172191 | ShellKind :: Fish
173192 | ShellKind :: Xonsh
174- | ShellKind :: Posix => {
175- let quoted_directory = quote_for_shell ( & directory_string) ;
176- let quoted_zed_path = quote_for_shell ( & zed_path_string) ;
177- cmd. args ( [
178- "-l" ,
179- "-i" ,
180- "-c" ,
181- & format ! ( "cd {}; {} --printenv" , quoted_directory, quoted_zed_path) ,
182- ] )
183- }
184- ShellKind :: PowerShell | ShellKind :: Pwsh => {
185- let quoted_directory = ShellKind :: quote_pwsh ( & directory_string) ;
186- let quoted_zed_path = ShellKind :: quote_pwsh ( & zed_path_string) ;
187- cmd. args ( [
188- "-NonInteractive" ,
189- "-NoProfile" ,
190- "-Command" ,
191- & format ! (
192- "Set-Location {}; & {} --printenv" ,
193- quoted_directory, quoted_zed_path
194- ) ,
195- ] )
196- }
197- ShellKind :: Elvish => {
198- let quoted_directory = quote_for_shell ( & directory_string) ;
199- let quoted_zed_path = quote_for_shell ( & zed_path_string) ;
200- cmd. args ( [
201- "-c" ,
202- & format ! ( "cd {}; {} --printenv" , quoted_directory, quoted_zed_path) ,
203- ] )
204- }
193+ | ShellKind :: Posix => cmd. args ( [
194+ "-l" ,
195+ "-i" ,
196+ "-c" ,
197+ & format ! ( "cd {}; {} --printenv" , quoted_directory, quoted_zed_path) ,
198+ ] ) ,
199+ ShellKind :: PowerShell | ShellKind :: Pwsh => cmd. args ( [
200+ "-NonInteractive" ,
201+ "-NoProfile" ,
202+ "-Command" ,
203+ & format ! (
204+ "Set-Location {}; & {} --printenv" ,
205+ quoted_directory, quoted_zed_path
206+ ) ,
207+ ] ) ,
208+ ShellKind :: Elvish => cmd. args ( [
209+ "-c" ,
210+ & format ! ( "cd {}; {} --printenv" , quoted_directory, quoted_zed_path) ,
211+ ] ) ,
205212 ShellKind :: Nushell => {
206- let quoted_directory = quote_for_shell ( & directory_string) ;
207- let quoted_zed_path = quote_for_shell ( & zed_path_string) ;
208213 let zed_command = shell_kind
209214 . prepend_command_prefix ( & quoted_zed_path)
210215 . into_owned ( ) ;
0 commit comments