From 1759cf074a57dc3917de86c4dbd2bab7c4422c51 Mon Sep 17 00:00:00 2001 From: derwolfe Date: Sun, 23 Mar 2014 19:11:18 -0500 Subject: [PATCH] Provide errors with faulty -o or --out-dir usage It is possible a user might enter faulty path information with either the -o or --out-dir parameters for rustc. There are two conditions for `-o` that this commit targets. 1) if the provided path contains a non-existent directory, return an error message and the specified path. 2) if the specified path is a directory and not a file, return the path and an error message. There are also two conditions on which `--out-dir` can fail: 1) if the specified path doesn't exist, return the path and an error message 2) if the provided path isn't a directory, return the specified path and an error message. Tests have been added as well. A new makefile variabe RUSTCNOARGS was created in tools.mk to facilitate running the compiler without any options. This is necessary because the main RUSTC variable contains the `--out-dir` parameter; this parameter needs to be specified or avoided in all of the new tests. --- src/librustc/lib.rs | 33 +++++++++++++++++++ .../run-make/rustc-outfile-error/Makefile | 5 +++ src/test/run-make/rustc-outfile-error/foo.rs | 11 +++++++ .../run-make/rustc-output-dir-error/Makefile | 5 +++ .../run-make/rustc-output-dir-error/foo.rs | 11 +++++++ src/test/run-make/tools.mk | 1 + 6 files changed, 66 insertions(+) create mode 100644 src/test/run-make/rustc-outfile-error/Makefile create mode 100644 src/test/run-make/rustc-outfile-error/foo.rs create mode 100644 src/test/run-make/rustc-output-dir-error/Makefile create mode 100644 src/test/run-make/rustc-output-dir-error/foo.rs diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 6aff15301048d..83e0cae93179e 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -286,8 +286,41 @@ pub fn run_compiler(args: &[~str]) { let sopts = d::build_session_options(matches); let sess = d::build_session(sopts, input_file_path); + // get the output dir, if there is one + // make sure that the directory structure + // exists, e.g. if -out-dir is ./foo/bar/baz, each needs to exist + // otherwise error out early let odir = matches.opt_str("out-dir").map(|o| Path::new(o)); + match odir { + Some(ref odir) => { + if !odir.exists() { + d::early_error(format!("output directory {} does not exist", + odir.display())) + } + // if it exists, is it a file? + if !odir.is_dir() { + d::early_error(format!("specified output directory {} is not a directory", + odir.display())) + } + } + None => {/* continue */ } + } let ofile = matches.opt_str("o").map(|o| Path::new(o)); + match ofile { + Some(ref ofile) => { + // does the directory for -o exist? + if !ofile.dir_path().exists() { + d::early_error(format!("specified output location's directory {} does not exist", + ofile.display())) + } + // does a file exist at -o and is it a directory? + if ofile.is_dir() { + d::early_error(format!("specified output location {} is a directory", + ofile.display())) + } + } + None => {/* continue */ } + } let cfg = d::build_configuration(&sess); let pretty = matches.opt_default("pretty", "normal").map(|a| { d::parse_pretty(&sess, a) diff --git a/src/test/run-make/rustc-outfile-error/Makefile b/src/test/run-make/rustc-outfile-error/Makefile new file mode 100644 index 0000000000000..4e620ae06f9b1 --- /dev/null +++ b/src/test/run-make/rustc-outfile-error/Makefile @@ -0,0 +1,5 @@ +-include ../tools.mk + +all: + $(RUSTCNOARGS) foo.rs -o ./baz/bar.out 2>&1 | grep "error: specified output location's directory baz/bar.out does not exist" + $(RUSTCNOARGS) foo.rs -o . 2>&1| grep "error: specified output location . is a directory" diff --git a/src/test/run-make/rustc-outfile-error/foo.rs b/src/test/run-make/rustc-outfile-error/foo.rs new file mode 100644 index 0000000000000..2661b1f4eb49b --- /dev/null +++ b/src/test/run-make/rustc-outfile-error/foo.rs @@ -0,0 +1,11 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub fn foo() {} diff --git a/src/test/run-make/rustc-output-dir-error/Makefile b/src/test/run-make/rustc-output-dir-error/Makefile new file mode 100644 index 0000000000000..0f069ccda3e88 --- /dev/null +++ b/src/test/run-make/rustc-output-dir-error/Makefile @@ -0,0 +1,5 @@ +-include ../tools.mk + +all: + $(RUSTCNOARGS) foo.rs --out-dir ./baz/bar 2>&1 | grep "error: output directory baz/bar does not exist" + $(RUSTCNOARGS) foo.rs --out-dir ./foo.rs 2>&1 | grep "error: specified output directory foo.rs is not a directory" diff --git a/src/test/run-make/rustc-output-dir-error/foo.rs b/src/test/run-make/rustc-output-dir-error/foo.rs new file mode 100644 index 0000000000000..2661b1f4eb49b --- /dev/null +++ b/src/test/run-make/rustc-output-dir-error/foo.rs @@ -0,0 +1,11 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub fn foo() {} diff --git a/src/test/run-make/tools.mk b/src/test/run-make/tools.mk index 26e6e06c2ed8a..3d96c400444f0 100644 --- a/src/test/run-make/tools.mk +++ b/src/test/run-make/tools.mk @@ -1,6 +1,7 @@ export LD_LIBRARY_PATH:=$(TMPDIR):$(LD_LIBRARY_PATH) export DYLD_LIBRARY_PATH:=$(TMPDIR):$(DYLD_LIBRARY_PATH) +RUSTCNOARGS := $(RUSTC) RUSTC := $(RUSTC) --out-dir $(TMPDIR) -L $(TMPDIR) CC := $(CC) -L $(TMPDIR)