Skip to content

Commit 09b5848

Browse files
committed
Add env flags RUSTC_COLOR and RUSTC_ERROR_FORMAT
1 parent 5165ee9 commit 09b5848

File tree

6 files changed

+111
-24
lines changed

6 files changed

+111
-24
lines changed

src/bootstrap/bin/rustc.rs

-9
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,6 @@ fn main() {
269269
cmd.arg("--cfg").arg("parallel_queries");
270270
}
271271

272-
let color = match env::var("RUSTC_COLOR") {
273-
Ok(s) => usize::from_str(&s).expect("RUSTC_COLOR should be an integer"),
274-
Err(_) => 0,
275-
};
276-
277-
if color != 0 {
278-
cmd.arg("--color=always");
279-
}
280-
281272
if verbose > 1 {
282273
eprintln!("rustc command: {:?}", cmd);
283274
}

src/bootstrap/compile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ fn run_cargo(build: &Build, cargo: &mut Command, stamp: &Path) {
799799
// since we pass message-format=json to cargo, we need to tell the rustc
800800
// wrapper to give us colored output if necessary. This is because we
801801
// only want Cargo's JSON output, not rustcs.
802-
cargo.env("RUSTC_COLOR", "1");
802+
cargo.env("RUSTC_COLOR", "always");
803803
}
804804

805805
build.verbose(&format!("running: {:?}", cargo));

src/librustc/session/config.rs

+38-12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use syntax::parse;
3333
use syntax::symbol::Symbol;
3434
use syntax::feature_gate::UnstableFeatures;
3535

36-
use errors::{ColorConfig, FatalError, Handler};
36+
use errors::{self, ColorConfig, FatalError, Handler, ErrorFormat};
3737

3838
use getopts;
3939
use std::collections::{BTreeMap, BTreeSet};
@@ -1540,14 +1540,15 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
15401540
}).collect::<ast::CrateConfig>()
15411541
}
15421542

1543-
pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
1543+
pub fn build_session_options_and_crate_config(matches: &getopts::Matches,
1544+
defaults: errors::EnvDefaults)
15441545
-> (Options, ast::CrateConfig) {
15451546
let color = match matches.opt_str("color").as_ref().map(|s| &s[..]) {
15461547
Some("auto") => ColorConfig::Auto,
15471548
Some("always") => ColorConfig::Always,
15481549
Some("never") => ColorConfig::Never,
15491550

1550-
None => ColorConfig::Auto,
1551+
None => defaults.color,
15511552

15521553
Some(arg) => {
15531554
early_error(ErrorOutputType::default(), &format!("argument for --color must be auto, \
@@ -1574,8 +1575,10 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
15741575
enable the short error message option"));
15751576
}
15761577
}
1577-
None => ErrorOutputType::HumanReadable(color),
1578-
1578+
None => match (defaults.error_format, nightly_options::is_nightly_build()) {
1579+
(ErrorFormat::Short, true) => ErrorOutputType::Short(color),
1580+
_ => ErrorOutputType::HumanReadable(color),
1581+
},
15791582
Some(arg) => {
15801583
early_error(ErrorOutputType::HumanReadable(color),
15811584
&format!("argument for --error-format must be `human`, `json` or \
@@ -1584,7 +1587,10 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
15841587
}
15851588
}
15861589
} else {
1587-
ErrorOutputType::HumanReadable(color)
1590+
match (defaults.error_format, nightly_options::is_nightly_build()) {
1591+
(ErrorFormat::Short, true) => ErrorOutputType::Short(color),
1592+
_ => ErrorOutputType::HumanReadable(color),
1593+
}
15881594
};
15891595

15901596
let unparsed_crate_types = matches.opt_strs("crate-type");
@@ -2156,9 +2162,9 @@ mod dep_tracking {
21562162

21572163
#[cfg(test)]
21582164
mod tests {
2159-
use errors;
21602165
use getopts;
21612166
use lint;
2167+
use errors::{self, EnvDefaults, ColorConfig, ErrorFormat};
21622168
use middle::cstore;
21632169
use session::config::{build_configuration, build_session_options_and_crate_config};
21642170
use session::build_session;
@@ -2194,7 +2200,11 @@ mod tests {
21942200
Err(f) => panic!("test_switch_implies_cfg_test: {}", f)
21952201
};
21962202
let registry = errors::registry::Registry::new(&[]);
2197-
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
2203+
let defaults = EnvDefaults {
2204+
error_format: ErrorFormat::Regular,
2205+
color: ColorConfig::Auto,
2206+
};
2207+
let (sessopts, cfg) = build_session_options_and_crate_config(&matches, defaults);
21982208
let sess = build_session(sessopts, None, registry);
21992209
let cfg = build_configuration(&sess, cfg);
22002210
assert!(cfg.contains(&(Symbol::intern("test"), None)));
@@ -2212,7 +2222,11 @@ mod tests {
22122222
}
22132223
};
22142224
let registry = errors::registry::Registry::new(&[]);
2215-
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
2225+
let defaults = EnvDefaults {
2226+
error_format: ErrorFormat::Regular,
2227+
color: ColorConfig::Auto,
2228+
};
2229+
let (sessopts, cfg) = build_session_options_and_crate_config(&matches, defaults);
22162230
let sess = build_session(sessopts, None, registry);
22172231
let cfg = build_configuration(&sess, cfg);
22182232
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
@@ -2227,7 +2241,11 @@ mod tests {
22272241
"-Awarnings".to_string()
22282242
]).unwrap();
22292243
let registry = errors::registry::Registry::new(&[]);
2230-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2244+
let defaults = EnvDefaults {
2245+
error_format: ErrorFormat::Regular,
2246+
color: ColorConfig::Auto,
2247+
};
2248+
let (sessopts, _) = build_session_options_and_crate_config(&matches, defaults);
22312249
let sess = build_session(sessopts, None, registry);
22322250
assert!(!sess.diagnostic().flags.can_emit_warnings);
22332251
}
@@ -2238,7 +2256,11 @@ mod tests {
22382256
"-Dwarnings".to_string()
22392257
]).unwrap();
22402258
let registry = errors::registry::Registry::new(&[]);
2241-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2259+
let defaults = EnvDefaults {
2260+
error_format: ErrorFormat::Regular,
2261+
color: ColorConfig::Auto,
2262+
};
2263+
let (sessopts, _) = build_session_options_and_crate_config(&matches, defaults);
22422264
let sess = build_session(sessopts, None, registry);
22432265
assert!(sess.diagnostic().flags.can_emit_warnings);
22442266
}
@@ -2248,7 +2270,11 @@ mod tests {
22482270
"-Adead_code".to_string()
22492271
]).unwrap();
22502272
let registry = errors::registry::Registry::new(&[]);
2251-
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2273+
let defaults = EnvDefaults {
2274+
error_format: ErrorFormat::Regular,
2275+
color: ColorConfig::Auto,
2276+
};
2277+
let (sessopts, _) = build_session_options_and_crate_config(&matches, defaults);
22522278
let sess = build_session(sessopts, None, registry);
22532279
assert!(sess.diagnostic().flags.can_emit_warnings);
22542280
}

src/librustc_driver/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ use syntax::codemap::{CodeMap, FileLoader, RealFileLoader};
9999
use syntax::feature_gate::{GatedCfg, UnstableFeatures};
100100
use syntax::parse::{self, PResult};
101101
use syntax_pos::{DUMMY_SP, MultiSpan, FileName};
102+
use errors::{EnvDefaults, FromEnv};
102103

103104
#[cfg(test)]
104105
mod test;
@@ -203,7 +204,8 @@ pub fn run_compiler<'a>(args: &[String],
203204
None => return (Ok(()), None),
204205
};
205206

206-
let (sopts, cfg) = config::build_session_options_and_crate_config(&matches);
207+
let defaults = EnvDefaults::from_env();
208+
let (sopts, cfg) = config::build_session_options_and_crate_config(&matches, defaults);
207209

208210
if sopts.debugging_opts.debug_llvm {
209211
rustc_trans::enable_llvm_debug();

src/librustc_errors/emitter.rs

+68
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ use snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledStrin
1717
use styled_buffer::StyledBuffer;
1818

1919
use std::borrow::Cow;
20+
use std::env;
2021
use std::io::prelude::*;
2122
use std::io;
2223
use std::rc::Rc;
2324
use term;
2425
use std::collections::HashMap;
2526
use std::cmp::min;
27+
use std::str::FromStr;
2628
use unicode_width;
2729

2830
/// Emitter trait for emitting errors.
@@ -85,6 +87,51 @@ pub const MAX_HIGHLIGHT_LINES: usize = 6;
8587
/// Arbitrary, but taken from trait import suggestion limit
8688
pub const MAX_SUGGESTIONS: usize = 4;
8789

90+
#[derive(Debug)]
91+
pub struct EnvDefaults {
92+
pub error_format: ErrorFormat,
93+
pub color: ColorConfig,
94+
}
95+
96+
pub trait FromEnv {
97+
fn from_env() -> Self;
98+
}
99+
100+
impl FromEnv for EnvDefaults {
101+
fn from_env() -> EnvDefaults {
102+
EnvDefaults {
103+
error_format: ErrorFormat::from_env(),
104+
color: ColorConfig::from_env(),
105+
}
106+
}
107+
}
108+
109+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
110+
pub enum ErrorFormat {
111+
Regular,
112+
Short,
113+
}
114+
115+
impl FromEnv for ErrorFormat {
116+
fn from_env() -> ErrorFormat {
117+
env::var("RUSTC_ERROR_FORMAT")
118+
.map(|s| ErrorFormat::from_str(&s).expect(
119+
"invalid error format in environment variable `RUSTC_ERROR_FORMAT`"))
120+
.unwrap_or(ErrorFormat::Regular)
121+
}
122+
}
123+
124+
impl FromStr for ErrorFormat {
125+
type Err = String;
126+
fn from_str(s: &str) -> Result<Self, Self::Err> {
127+
match s {
128+
"" | "regular" => Ok(ErrorFormat::Regular),
129+
"short" => Ok(ErrorFormat::Short),
130+
_ => Err(s.into()),
131+
}
132+
}
133+
}
134+
88135
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89136
pub enum ColorConfig {
90137
Auto,
@@ -102,6 +149,27 @@ impl ColorConfig {
102149
}
103150
}
104151

152+
impl FromEnv for ColorConfig {
153+
fn from_env() -> ColorConfig {
154+
env::var("RUSTC_COLOR")
155+
.map(|s| ColorConfig::from_str(&s).expect(
156+
"invalid color configuration in environment variable `RUSTC_COLOR`"))
157+
.unwrap_or(ColorConfig::Auto)
158+
}
159+
}
160+
161+
impl FromStr for ColorConfig {
162+
type Err = String;
163+
fn from_str(s: &str) -> Result<Self, Self::Err> {
164+
match s {
165+
"" | "auto" => Ok(ColorConfig::Auto),
166+
"1" | "always" => Ok(ColorConfig::Always),
167+
"never" => Ok(ColorConfig::Never),
168+
_ => Err(s.into()),
169+
}
170+
}
171+
}
172+
105173
pub struct EmitterWriter {
106174
dst: Destination,
107175
cm: Option<Rc<CodeMapper>>,

src/librustc_errors/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern crate serialize as rustc_serialize;
2828
extern crate syntax_pos;
2929
extern crate unicode_width;
3030

31-
pub use emitter::ColorConfig;
31+
pub use emitter::{ColorConfig, ErrorFormat, EnvDefaults, FromEnv};
3232

3333
use self::Level::*;
3434

0 commit comments

Comments
 (0)