Skip to content

Commit 2f657df

Browse files
committed
Use 'rust-toolchain' instead of '.rust-version'
Also interleave the directory override and rust-toolchain directory walk.
1 parent 107d8e5 commit 2f657df

File tree

4 files changed

+133
-93
lines changed

4 files changed

+133
-93
lines changed

README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ And it runs on all platforms Rust supports, including Windows.
1818
* [Keeping Rust up to date](#keeping-rust-up-to-date)
1919
* [Working with nightly Rust](#working-with-nightly-rust)
2020
* [Toolchain specification](#toolchain-specification)
21-
* [Directory overrides](#directory-overrides)
22-
* [The version file](#the-version-file)
2321
* [Toolchain override shorthand](#toolchain-override-shorthand)
22+
* [Directory overrides](#directory-overrides)
23+
* [The toolchain file](#the-toolchain-file)
24+
* [Override precedence](#override-precedence)
2425
* [Cross-compilation](#cross-compilation)
2526
* [Working with Rust on Windows](#working-with-rust-on-windows)
2627
* [Working with custom toolchains](#working-with-custom-toolchains-and-local-builds)
@@ -293,13 +294,12 @@ cargo +beta test
293294

294295
## Directory overrides
295296

296-
Directories can be assigned their own Rust toolchain with
297-
`rustup override`. When a directory has an override then
298-
any time `rustc` or `cargo` is run inside that directory,
299-
or one of its child directories, the override toolchain
300-
will be invoked.
297+
Directories can be assigned their own Rust toolchain with `rustup
298+
override`. When a directory has an override then any time `rustc` or
299+
`cargo` is run inside that directory, or one of its child directories,
300+
the override toolchain will be invoked.
301301

302-
To pin to a specific nightly:
302+
To use to a specific nightly for a directory:
303303

304304
```
305305
rustup override set nightly-2014-12-18
@@ -314,7 +314,7 @@ rustup override set 1.0.0
314314
To see the active toolchain use `rustup show`. To remove the override
315315
and use the default toolchain again, `rustup override unset`.
316316

317-
## The version file
317+
## The toolchain file
318318

319319
`rustup` directory overrides are a local configuration, stored in
320320
`$RUSTUP_HOME`. Some projects though find themselves 'pinned' to a
@@ -323,7 +323,7 @@ source repository. This is most often the case for nightly-only
323323
software that pins to a revision from the release archives.
324324

325325
In these cases the toolchain can be named in the project's directory
326-
in a file called `.rust-version`, the content of which is the name of
326+
in a file called `rust-toolchain`, the content of which is the name of
327327
a single `rustup` toolchain, and which is suitable to check in to
328328
source control.
329329

@@ -334,8 +334,27 @@ numbers, like '1.0.0', and optionally an archive date, like
334334
'nightly-2017-01-01'. They may not name custom toolchains, nor
335335
host-specific toolchains.
336336

337-
The version file has lower precedence than all other methods of
338-
specifying the toolchain.
337+
## Override precedence
338+
339+
There are several ways to specify which toolchain `rustup` should
340+
execute:
341+
342+
* An explicit toolchain, e.g. `cargo +beta`,
343+
* The `RUSTUP_TOOLCHAIN` environment variable,
344+
* A directory override, ala `rustup override set beta`,
345+
* The `rust-toolchain` file,
346+
* The default toolchain,
347+
348+
and they are prefered by rustup in that order, with the explicit
349+
toolchain having highest precedence, and the default toolchain having
350+
the lowest. There is one exception though: directory overrides and the
351+
`rust-toolchain` file are also preferred by their proximity to the
352+
current directory. That is, these two override methods are discovered
353+
by walking up the directory tree toward the filesystem root, and
354+
`rust-toolchain` file that is closer to the current directory will be
355+
prefered over a directory override that is further away.
356+
357+
To verify which toolchain is active use `rustup show`.
339358

340359
## Cross-compilation
341360

src/rustup/config.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ use rustup_dist::{temp, dist};
1212
use rustup_utils::utils;
1313
use toolchain::{Toolchain, UpdateStatus};
1414
use telemetry_analysis::*;
15-
use settings::{TelemetryMode, SettingsFile, DEFAULT_METADATA_VERSION};
15+
use settings::{TelemetryMode, SettingsFile, Settings, DEFAULT_METADATA_VERSION};
1616

1717
#[derive(Debug)]
1818
pub enum OverrideReason {
1919
Environment,
2020
OverrideDB(PathBuf),
21-
VersionFile(PathBuf),
21+
ToolchainFile(PathBuf),
2222
}
2323

2424
impl 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)

src/rustup/settings.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,9 @@ impl Settings {
162162
self.overrides.insert(key, toolchain);
163163
}
164164

165-
pub fn find_override(&self, dir_unresolved: &Path, notify_handler: &Fn(Notification))
166-
-> Option<(String, PathBuf)> {
167-
let dir = utils::canonicalize_path(dir_unresolved, &|n| notify_handler(n.into()));
168-
let mut maybe_path = Some(&*dir);
169-
while let Some(path) = maybe_path {
170-
let key = Self::path_to_key(path, notify_handler);
171-
if let Some(toolchain) = self.overrides.get(&key) {
172-
return Some((toolchain.to_owned(), path.to_owned()));
173-
}
174-
maybe_path = path.parent();
175-
}
176-
None
165+
pub fn dir_override(&self, dir: &Path, notify_handler: &Fn(Notification)) -> Option<String> {
166+
let key = Self::path_to_key(dir, notify_handler);
167+
self.overrides.get(&key).map(|s| s.clone())
177168
}
178169

179170
pub fn parse(data: &str) -> Result<Self> {

0 commit comments

Comments
 (0)