@@ -12,13 +12,13 @@ use rustup_dist::{temp, dist};
1212use rustup_utils:: utils;
1313use toolchain:: { Toolchain , UpdateStatus } ;
1414use telemetry_analysis:: * ;
15- use settings:: { TelemetryMode , SettingsFile , DEFAULT_METADATA_VERSION } ;
15+ use settings:: { TelemetryMode , SettingsFile , Settings , DEFAULT_METADATA_VERSION } ;
1616
1717#[ derive( Debug ) ]
1818pub enum OverrideReason {
1919 Environment ,
2020 OverrideDB ( PathBuf ) ,
21- VersionFile ( PathBuf ) ,
21+ ToolchainFile ( PathBuf ) ,
2222}
2323
2424impl Display for OverrideReason {
@@ -28,7 +28,7 @@ impl Display for OverrideReason {
2828 OverrideReason :: OverrideDB ( ref path) => {
2929 write ! ( f, "directory override for '{}'" , path. display( ) )
3030 }
31- OverrideReason :: VersionFile ( ref path) => {
31+ OverrideReason :: ToolchainFile ( ref path) => {
3232 write ! ( f, "overridden by '{}'" , path. display( ) )
3333 }
3434 }
@@ -232,20 +232,14 @@ impl Cfg {
232232 override_ = Some ( ( name. to_string ( ) , OverrideReason :: Environment ) ) ;
233233 }
234234
235- // Then look in the override database
235+ // Then walk up the directory tree from 'path' looking for either the
236+ // directory in override database, or a `rust-toolchain` file.
236237 if override_. is_none ( ) {
237- try!( self . settings_file . with ( |s| {
238- let name = s. find_override ( path, self . notify_handler . as_ref ( ) ) ;
239- override_ = name. map ( |( name, reason_path) | ( name, OverrideReason :: OverrideDB ( reason_path) ) ) ;
238+ self . settings_file . with ( |s| {
239+ override_ = self . find_override_from_dir_walk ( path, s) ?;
240240
241241 Ok ( ( ) )
242- } ) ) ;
243- }
244-
245- // Then check the explicit version file
246- if override_. is_none ( ) {
247- let name_path = self . find_override_version_file ( path) ?;
248- override_ = name_path. map ( |( name, path) | ( name, OverrideReason :: VersionFile ( path) ) ) ;
242+ } ) ?;
249243 }
250244
251245 if let Some ( ( name, reason) ) = override_ {
@@ -260,8 +254,8 @@ impl Cfg {
260254 OverrideReason :: OverrideDB ( ref path) => {
261255 format ! ( "the directory override for '{}' specifies an uninstalled toolchain" , path. display( ) )
262256 }
263- OverrideReason :: VersionFile ( ref path) => {
264- format ! ( "the version file at '{}' specifies an uninstalled toolchain" , path. display( ) )
257+ OverrideReason :: ToolchainFile ( ref path) => {
258+ format ! ( "the toolchain file at '{}' specifies an uninstalled toolchain" , path. display( ) )
265259 }
266260 } ;
267261
@@ -286,25 +280,36 @@ impl Cfg {
286280 }
287281 }
288282
289- /// Starting in path walk up the tree looking for .rust-version
290- fn find_override_version_file ( & self , path : & Path ) -> Result < Option < ( String , PathBuf ) > > {
291- let mut path = Some ( path) ;
283+ fn find_override_from_dir_walk ( & self , dir : & Path , settings : & Settings )
284+ -> Result < Option < ( String , OverrideReason ) > >
285+ {
286+ let notify = self . notify_handler . as_ref ( ) ;
287+ let dir = utils:: canonicalize_path ( dir, & |n| notify ( n. into ( ) ) ) ;
288+ let mut dir = Some ( & * dir) ;
289+
290+ while let Some ( d) = dir {
291+ // First check the override database
292+ if let Some ( name) = settings. dir_override ( d, notify) {
293+ let reason = OverrideReason :: OverrideDB ( d. to_owned ( ) ) ;
294+ return Ok ( Some ( ( name, reason) ) ) ;
295+ }
292296
293- while let Some ( p ) = path {
294- let version_file = p . join ( ". rust-version " ) ;
295- if let Ok ( s) = utils:: read_file ( "version file" , & version_file ) {
297+ // Then look for 'rust-toolchain'
298+ let toolchain_file = d . join ( "rust-toolchain " ) ;
299+ if let Ok ( s) = utils:: read_file ( "toolchain file" , & toolchain_file ) {
296300 if let Some ( s) = s. lines ( ) . next ( ) {
297301 let toolchain_name = s. trim ( ) ;
298302 dist:: validate_channel_name ( & toolchain_name)
299303 . chain_err ( || format ! ( "invalid channel name '{}' in '{}'" ,
300304 toolchain_name,
301- version_file . display( ) ) ) ?;
305+ toolchain_file . display( ) ) ) ?;
302306
303- return Ok ( Some ( ( toolchain_name. to_string ( ) , version_file) ) ) ;
307+ let reason = OverrideReason :: ToolchainFile ( toolchain_file) ;
308+ return Ok ( Some ( ( toolchain_name. to_string ( ) , reason) ) ) ;
304309 }
305310 }
306311
307- path = p . parent ( ) ;
312+ dir = d . parent ( ) ;
308313 }
309314
310315 Ok ( None )
0 commit comments