@@ -332,6 +332,86 @@ impl RustEnvironmentProvider {
332332 Ok ( ( ) )
333333 }
334334
335+ fn merge_rust_std ( & self , install_path : & Path ) -> Result < ( ) , String > {
336+ use std:: fs;
337+
338+ info ! ( "合并 Rust 标准库到 rustc 目录" ) ;
339+
340+ // 查找所有 rust-std-* 目录
341+ let entries = fs:: read_dir ( install_path)
342+ . map_err ( |e| format ! ( "读取安装目录失败: {}" , e) ) ?;
343+
344+ for entry in entries. flatten ( ) {
345+ let path = entry. path ( ) ;
346+ if path. is_dir ( ) {
347+ if let Some ( name) = path. file_name ( ) . and_then ( |n| n. to_str ( ) ) {
348+ if name. starts_with ( "rust-std-" ) {
349+ info ! ( "找到标准库组件: {}" , name) ;
350+
351+ // 源路径: install_path/rust-std-xxx/lib/rustlib/
352+ let std_rustlib_src = path. join ( "lib" ) . join ( "rustlib" ) ;
353+
354+ // 目标路径: install_path/rustc/lib/rustlib/
355+ let rustc_rustlib_dst = install_path. join ( "rustc" ) . join ( "lib" ) . join ( "rustlib" ) ;
356+
357+ if std_rustlib_src. exists ( ) && rustc_rustlib_dst. exists ( ) {
358+ // 遍历标准库中的所有目标平台
359+ if let Ok ( std_entries) = fs:: read_dir ( & std_rustlib_src) {
360+ for std_entry in std_entries. flatten ( ) {
361+ let std_target_path = std_entry. path ( ) ;
362+ if std_target_path. is_dir ( ) {
363+ if let Some ( target_name) = std_target_path. file_name ( ) {
364+ let dst_target_path = rustc_rustlib_dst. join ( target_name) ;
365+
366+ // 如果目标路径不存在,创建它
367+ if !dst_target_path. exists ( ) {
368+ fs:: create_dir_all ( & dst_target_path)
369+ . map_err ( |e| format ! ( "创建目标目录失败: {}" , e) ) ?;
370+ }
371+
372+ // 复制 lib 目录
373+ let std_lib_src = std_target_path. join ( "lib" ) ;
374+ let dst_lib = dst_target_path. join ( "lib" ) ;
375+
376+ if std_lib_src. exists ( ) {
377+ info ! ( "复制标准库: {} -> {}" , std_lib_src. display( ) , dst_lib. display( ) ) ;
378+ Self :: copy_dir_all ( & std_lib_src, & dst_lib) ?;
379+ }
380+ }
381+ }
382+ }
383+ }
384+ }
385+ }
386+ }
387+ }
388+ }
389+
390+ info ! ( "标准库合并完成" ) ;
391+ Ok ( ( ) )
392+ }
393+
394+ fn copy_dir_all ( src : & Path , dst : & Path ) -> Result < ( ) , String > {
395+ use std:: fs;
396+
397+ fs:: create_dir_all ( dst) . map_err ( |e| format ! ( "创建目录失败: {}" , e) ) ?;
398+
399+ for entry in fs:: read_dir ( src) . map_err ( |e| format ! ( "读取目录失败: {}" , e) ) ? {
400+ let entry = entry. map_err ( |e| format ! ( "读取条目失败: {}" , e) ) ?;
401+ let ty = entry. file_type ( ) . map_err ( |e| format ! ( "获取文件类型失败: {}" , e) ) ?;
402+ let src_path = entry. path ( ) ;
403+ let dst_path = dst. join ( entry. file_name ( ) ) ;
404+
405+ if ty. is_dir ( ) {
406+ Self :: copy_dir_all ( & src_path, & dst_path) ?;
407+ } else {
408+ fs:: copy ( & src_path, & dst_path) . map_err ( |e| format ! ( "复制文件失败: {}" , e) ) ?;
409+ }
410+ }
411+
412+ Ok ( ( ) )
413+ }
414+
335415 async fn update_plugin_config (
336416 & self ,
337417 version : & str ,
@@ -350,6 +430,15 @@ impl RustEnvironmentProvider {
350430 let rustc_bin = install_path. join ( "rustc" ) . join ( "bin" ) ;
351431 let has_rustc_dir = rustc_bin. exists ( ) ;
352432
433+ // 使用相对路径,工作目录会被设置为 execute_home
434+ #[ cfg( target_os = "windows" ) ]
435+ let run_command = if has_rustc_dir {
436+ "rustc\\ bin\\ rustc $filename -o main.exe && main.exe" . to_string ( )
437+ } else {
438+ "bin\\ rustc $filename -o main.exe && main.exe" . to_string ( )
439+ } ;
440+
441+ #[ cfg( not( target_os = "windows" ) ) ]
353442 let run_command = if has_rustc_dir {
354443 "rustc/bin/rustc $filename -o /tmp/main && /tmp/main" . to_string ( )
355444 } else {
@@ -523,6 +612,9 @@ impl EnvironmentProvider for RustEnvironmentProvider {
523612 std:: fs:: remove_dir_all ( & temp_extract_dir) . ok ( ) ;
524613 std:: fs:: remove_file ( & temp_file) . ok ( ) ;
525614
615+ // 合并标准库到 rustc 目录
616+ self . merge_rust_std ( & install_path) ?;
617+
526618 self . update_plugin_config ( version, app_handle. clone ( ) )
527619 . await ?;
528620
@@ -543,6 +635,10 @@ impl EnvironmentProvider for RustEnvironmentProvider {
543635 return Err ( format ! ( "版本 {} 未安装" , version) ) ;
544636 }
545637
638+ // 切换版本时也需要确保标准库已合并
639+ let install_path = self . get_version_install_path ( version) ;
640+ self . merge_rust_std ( & install_path) ?;
641+
546642 self . update_plugin_config ( version, app_handle) . await ?;
547643 info ! ( "已切换到 Rust {}" , version) ;
548644 Ok ( ( ) )
0 commit comments