@@ -250,7 +250,14 @@ impl Verification {
250250pub enum LaunchError {
251251 /// The stream ended before the shell answered, usually because the
252252 /// connection or authentication failed.
253- NoShell ,
253+ NoShell {
254+ /// The lines the stream carried before it ended, each with its
255+ /// newline (a last line without one gains it), at most the probe's
256+ /// bound on output before the server; empty when it ended after the
257+ /// probe. `wsl.exe` prints its own failures there, not on standard
258+ /// error.
259+ output : Vec < u8 > ,
260+ } ,
254261 /// The caller has no artifact for the reported platform.
255262 NoArtifact ( Box < ProbeReport > ) ,
256263 /// An install directory on the host is not private to the remote user,
@@ -273,7 +280,7 @@ pub enum LaunchError {
273280impl fmt:: Display for LaunchError {
274281 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
275282 match self {
276- Self :: NoShell => formatter. write_str ( "the remote shell did not start" ) ,
283+ Self :: NoShell { .. } => formatter. write_str ( "the remote shell did not start" ) ,
277284 Self :: NoArtifact ( report) => write ! (
278285 formatter,
279286 "no remote server is available for {} {}" ,
@@ -312,7 +319,7 @@ impl LaunchError {
312319 /// Stable category for logs. It never contains remote output or paths.
313320 pub fn failure_category ( & self ) -> String {
314321 match self {
315- Self :: NoShell => "no-shell" . into ( ) ,
322+ Self :: NoShell { .. } => "no-shell" . into ( ) ,
316323 Self :: NoArtifact ( _) => "no-artifact" . into ( ) ,
317324 Self :: UnsafeInstall { reason, .. } => format ! ( "unsafe-{}" , reason. as_str( ) ) ,
318325 Self :: InstallFailed ( step) => format ! ( "install-{}" , step. as_str( ) ) ,
@@ -373,8 +380,13 @@ fn probe(
373380) -> Result < ProbeReport , LaunchError > {
374381 let mut probe_output = Vec :: new ( ) ;
375382 loop {
376- let line = read_line ( output, NOISE_LIMIT . saturating_sub ( probe_output. len ( ) ) ) ?
377- . ok_or ( LaunchError :: NoShell ) ?;
383+ // `read_line` drops only the newline, so the lines and their
384+ // newlines are the bytes as they arrived, UTF-16 included.
385+ let Some ( line) = read_line ( output, NOISE_LIMIT . saturating_sub ( probe_output. len ( ) ) ) ? else {
386+ return Err ( LaunchError :: NoShell {
387+ output : probe_output,
388+ } ) ;
389+ } ;
378390 if probe_output. is_empty ( ) {
379391 stage ( Stage :: Authenticated ) ;
380392 }
@@ -384,7 +396,7 @@ fn probe(
384396 break ;
385397 }
386398 }
387- parse_probe ( & probe_output) . ok_or ( LaunchError :: NoShell )
399+ parse_probe ( & probe_output) . ok_or ( LaunchError :: NoShell { output : Vec :: new ( ) } )
388400}
389401
390402/// Checks the version, the only field the caller chose freely, and returns
@@ -453,7 +465,8 @@ fn deploy(
453465
454466/// Reads one answer line of the script.
455467fn answer ( output : & mut impl Read ) -> Result < String , LaunchError > {
456- let line = read_line ( output, NOISE_LIMIT ) ?. ok_or ( LaunchError :: NoShell ) ?;
468+ let line =
469+ read_line ( output, NOISE_LIMIT ) ?. ok_or ( LaunchError :: NoShell { output : Vec :: new ( ) } ) ?;
457470 Ok ( String :: from_utf8_lossy ( & line) . trim_end ( ) . to_owned ( ) )
458471}
459472
@@ -585,6 +598,30 @@ mod tests {
585598 }
586599 }
587600
601+ // `wsl.exe` prints why it gave up on the stream the probe reads, in
602+ // UTF-16 unless `WSL_UTF8` is set; the failure keeps those lines so the
603+ // caller can read them, and its text shows none. Splitting UTF-16 at its
604+ // newline bytes loses nothing: only the last line gains a newline byte,
605+ // which completes no code unit.
606+ #[ test]
607+ fn a_stream_that_ends_before_the_probe_keeps_what_it_carried ( ) {
608+ let text =
609+ "不存在具有所提供名称的分发。\r \n 错误代码: Wsl/Service/WSL_E_DISTRO_NOT_FOUND\r \n " ;
610+ let message: Vec < u8 > = text. encode_utf16 ( ) . flat_map ( u16:: to_le_bytes) . collect ( ) ;
611+ let error = probe ( & mut message. as_slice ( ) , & mut |_| { } ) . unwrap_err ( ) ;
612+ let LaunchError :: NoShell { output } = & error else {
613+ panic ! ( "{error:?}" ) ;
614+ } ;
615+ assert_eq ! ( output[ ..message. len( ) ] , message[ ..] ) ;
616+ assert_eq ! ( crate :: client:: wsl:: output_text( output) , text) ;
617+ let error = probe ( & mut text. as_bytes ( ) , & mut |_| { } ) . unwrap_err ( ) ;
618+ assert ! ( matches!( & error, LaunchError :: NoShell { output } if output == text. as_bytes( ) ) ) ;
619+ assert_eq ! ( error. to_string( ) , "the remote shell did not start" ) ;
620+ assert_eq ! ( error. failure_category( ) , "no-shell" ) ;
621+ let error = probe ( & mut io:: empty ( ) , & mut |_| { } ) . unwrap_err ( ) ;
622+ assert ! ( matches!( error, LaunchError :: NoShell { output } if output. is_empty( ) ) ) ;
623+ }
624+
588625 // Categories reach the log, so none carries remote output or a path.
589626 #[ test]
590627 fn failure_categories_carry_no_remote_text ( ) {
0 commit comments