@@ -13,6 +13,8 @@ use std::fs::File;
1313use std:: io:: Write ;
1414use std:: path:: Path ;
1515use std:: process:: ExitCode ;
16+ #[ cfg( windows) ]
17+ use windows_registry:: LOCAL_MACHINE ;
1618
1719#[ derive( Parser , Debug ) ]
1820#[ command( version) ]
@@ -117,6 +119,34 @@ fn main() -> ExitCode {
117119 ) ;
118120 }
119121
122+ #[ cfg( windows) ]
123+ if !config. skip_longpaths_check
124+ && let Err ( e) = windows_set_longpaths ( )
125+ {
126+ warn ! (
127+ "Error while checking or enabling LongPaths for Windows: {}" ,
128+ e
129+ ) ;
130+ eprintln ! (
131+ "Continuing without LongPaths support can have unwanted effects and is not recommended."
132+ ) ;
133+ eprint ! ( "Continue anyway? [y/N]: " ) ;
134+ let mut input = String :: new ( ) ;
135+ if let Err ( _) = std:: io:: stdin ( ) . read_line ( & mut input) {
136+ error ! ( "Failed to get user input" ) ;
137+ }
138+ match input. trim ( ) . as_ref ( ) {
139+ "y" | "Y" | "yes" => info ! (
140+ "You can add the line 'skip_longpaths_check: true' to {} to skip this prompt in the future" ,
141+ home. join( ".csmrc" ) . display( )
142+ ) ,
143+ _ => {
144+ error ! ( "Exiting." ) ;
145+ return ExitCode :: FAILURE ;
146+ }
147+ }
148+ }
149+
120150 match cli. command {
121151 Command :: Env ( sub) => env:: run ( config, sub) . finish ( ) ,
122152 Command :: Robot ( sub) => robot:: run ( config, sub) . finish ( ) ,
@@ -150,3 +180,25 @@ fn create_mambarc(config: &Config, home: &Path) -> std::io::Result<()> {
150180 }
151181 Ok ( ( ) )
152182}
183+
184+ /// On windows, check if LongPaths are enabled in the registry, and enable them
185+ /// if they are not.
186+ #[ cfg( windows) ]
187+ fn windows_set_longpaths ( ) -> windows_registry:: Result < ( ) > {
188+ let reg_path = r"SYSTEM\CurrentControlSet\Control\FileSystem" ;
189+ let ro_key = LOCAL_MACHINE . open ( reg_path) ?;
190+ match ro_key. get_u32 ( "LongPathsEnabled" ) {
191+ Ok ( 1 ) => {
192+ debug ! ( "Windows LongPathsEnabled setting is already enabled" ) ;
193+ Ok ( ( ) )
194+ }
195+ Ok ( _) => {
196+ info ! (
197+ "Enabling LongPaths in the registry, this may require administrative permissions"
198+ ) ;
199+ let rw_key = LOCAL_MACHINE . create ( reg_path) ?;
200+ rw_key. set_u32 ( "LongPathsEnabled" , 1 )
201+ }
202+ e @ Err ( _) => e. map ( |_| ( ) ) ,
203+ }
204+ }
0 commit comments