@@ -29,6 +29,7 @@ use foldhash::fast::FoldHasher;
2929use foldhash:: { HashMap , SharedSeed } ;
3030use numeric_str_cmp:: { NumInfo , NumInfoParseSettings , human_numeric_str_cmp, numeric_str_cmp} ;
3131use rand:: { RngExt as _, rng} ;
32+ #[ cfg( not( target_os = "wasi" ) ) ]
3233use rayon:: prelude:: * ;
3334use std:: cmp:: Ordering ;
3435use std:: env;
@@ -2127,9 +2128,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
21272128 Ok ( 0 ) | Err ( _) => std:: thread:: available_parallelism ( ) . map_or ( 1 , NonZero :: get) ,
21282129 Ok ( n) => n,
21292130 } ;
2130- let _ = rayon:: ThreadPoolBuilder :: new ( )
2131- . num_threads ( num_threads)
2132- . build_global ( ) ;
2131+ #[ cfg( not( target_os = "wasi" ) ) ]
2132+ {
2133+ let _ = rayon:: ThreadPoolBuilder :: new ( )
2134+ . num_threads ( num_threads)
2135+ . build_global ( ) ;
2136+ }
21332137 }
21342138
21352139 if let Some ( size_str) = matches. get_one :: < String > ( options:: BUF_SIZE ) {
@@ -2602,10 +2606,19 @@ fn exec(
26022606}
26032607
26042608fn sort_by < ' a > ( unsorted : & mut Vec < Line < ' a > > , settings : & GlobalSettings , line_data : & LineData < ' a > ) {
2609+ let cmp = |a : & Line < ' a > , b : & Line < ' a > | compare_by ( a, b, settings, line_data, line_data) ;
2610+ // WASI does not support threads, so use non-parallel sort to avoid
2611+ // rayon's thread pool which triggers an unreachable trap.
26052612 if settings. stable || settings. unique {
2606- unsorted. par_sort_by ( |a, b| compare_by ( a, b, settings, line_data, line_data) ) ;
2613+ #[ cfg( not( target_os = "wasi" ) ) ]
2614+ unsorted. par_sort_by ( cmp) ;
2615+ #[ cfg( target_os = "wasi" ) ]
2616+ unsorted. sort_by ( cmp) ;
26072617 } else {
2608- unsorted. par_sort_unstable_by ( |a, b| compare_by ( a, b, settings, line_data, line_data) ) ;
2618+ #[ cfg( not( target_os = "wasi" ) ) ]
2619+ unsorted. par_sort_unstable_by ( cmp) ;
2620+ #[ cfg( target_os = "wasi" ) ]
2621+ unsorted. sort_unstable_by ( cmp) ;
26092622 }
26102623}
26112624
0 commit comments