Skip to content

Commit e8912bf

Browse files
committed
auto merge of rust-lang#83 : huonw/cargo/manifest-search, r=alexcrichton,alexcrichton
2 parents 52f050a + c0865e8 commit e8912bf

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

src/bin/cargo-build.rs

100644100755
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use cargo::{execute_main_without_stdin};
1616
use cargo::ops;
1717
use cargo::core::MultiShell;
1818
use cargo::util::{CliResult, CliError};
19-
use cargo::util::important_paths::find_project;
19+
use cargo::util::important_paths::find_project_manifest;
2020

2121
#[deriving(PartialEq,Clone,Decodable,Encodable)]
2222
pub struct Options {
@@ -37,8 +37,7 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
3737

3838
let root = match options.manifest_path {
3939
Some(path) => Path::new(path),
40-
None => try!(find_project(os::getcwd(), "Cargo.toml")
41-
.map(|path| path.join("Cargo.toml"))
40+
None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml")
4241
.map_err(|_| {
4342
CliError::new("Could not find Cargo.toml in this \
4443
directory or any parent directory",

src/bin/cargo-test.rs

100644100755
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use cargo::{execute_main_without_stdin};
1616
use cargo::core::{MultiShell};
1717
use cargo::util;
1818
use cargo::util::{CliResult, CliError};
19-
use cargo::util::important_paths::find_project;
19+
use cargo::util::important_paths::find_project_manifest;
2020

2121
#[deriving(PartialEq,Clone,Decodable)]
2222
struct Options {
@@ -33,8 +33,7 @@ fn main() {
3333
fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
3434
let root = match options.manifest_path {
3535
Some(path) => Path::new(path),
36-
None => try!(find_project(os::getcwd(), "Cargo.toml")
37-
.map(|path| path.join("Cargo.toml"))
36+
None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml")
3837
.map_err(|_| {
3938
CliError::new("Could not find Cargo.toml in this \
4039
directory or any parent directory",

src/bin/cargo.rs

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn config_list(args: ConfigListFlags, _: &mut MultiShell) -> CliResult<Option<Co
142142
}
143143

144144
fn locate_project(_: NoFlags, _: &mut MultiShell) -> CliResult<Option<ProjectLocation>> {
145-
let root = try!(find_project(os::getcwd(), "Cargo.toml").map_err(|e| {
145+
let root = try!(find_project(&os::getcwd(), "Cargo.toml").map_err(|e| {
146146
CliError::from_boxed(e, 1)
147147
}));
148148

src/cargo/ops/cargo_read_manifest.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io::File;
22
use util;
33
use core::{Package,Manifest,SourceId};
4-
use util::{CargoResult, human};
4+
use util::{CargoResult, human, important_paths};
55

66
pub fn read_manifest(contents: &[u8], source_id: &SourceId)
77
-> CargoResult<(Manifest, Vec<Path>)>
@@ -24,7 +24,8 @@ pub fn read_package(path: &Path, source_id: &SourceId)
2424
pub fn read_packages(path: &Path, source_id: &SourceId)
2525
-> CargoResult<Vec<Package>>
2626
{
27-
let (pkg, nested) = try!(read_package(&path.join("Cargo.toml"), source_id));
27+
let manifest = try!(important_paths::find_project_manifest_exact(path, "Cargo.toml"));
28+
let (pkg, nested) = try!(read_package(&manifest, source_id));
2829
let mut ret = vec!(pkg);
2930

3031
for p in nested.iter() {

src/cargo/util/important_paths.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
use util::{CargoResult, human};
22

3-
pub fn find_project(pwd: Path, file: &str) -> CargoResult<Path> {
3+
/// Iteratively search for `file` in `pwd` and its parents, returning
4+
/// the path of the directory.
5+
pub fn find_project(pwd: &Path, file: &str) -> CargoResult<Path> {
6+
find_project_manifest(pwd, file)
7+
.map(|mut p| {
8+
// remove the file, leaving just the directory
9+
p.pop();
10+
p
11+
})
12+
}
13+
14+
/// Iteratively search for `file` in `pwd` and its parents, returning
15+
/// the path to the file.
16+
pub fn find_project_manifest(pwd: &Path, file: &str) -> CargoResult<Path> {
417
let mut current = pwd.clone();
518

619
loop {
7-
if current.join(file.clone()).exists() {
8-
return Ok(current)
20+
let manifest = current.join(file);
21+
if manifest.exists() {
22+
return Ok(manifest)
923
}
1024

1125
if !current.pop() { break; }
1226
}
1327

14-
Err(human(format!("no manifest found in `{}`", pwd.display())))
28+
Err(human(format!("Could not find `{}` in `{}` or any parent directory",
29+
file, pwd.display())))
30+
}
31+
32+
/// Return the path to the `file` in `pwd`, if it exists.
33+
pub fn find_project_manifest_exact(pwd: &Path, file: &str) -> CargoResult<Path> {
34+
let manifest = pwd.join(file);
35+
36+
if manifest.exists() {
37+
Ok(manifest)
38+
} else {
39+
Err(human(format!("Could not find `{}` in `{}`",
40+
file, pwd.display())))
41+
}
1542
}

tests/test_cargo_compile_path_deps.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,26 @@ test!(nested_deps_recompile {
354354
FRESH, bar.display(),
355355
COMPILING, p.root().display())));
356356
})
357+
358+
test!(error_message_for_missing_manifest {
359+
let p = project("foo")
360+
.file("Cargo.toml", r#"
361+
[project]
362+
363+
name = "foo"
364+
version = "0.5.0"
365+
authors = ["[email protected]"]
366+
367+
[dependencies.bar]
368+
369+
path = "src/bar"
370+
"#)
371+
.file("src/bar/not-a-manifest", "");
372+
373+
assert_that(p.cargo_process("cargo-build"),
374+
execs()
375+
.with_status(101)
376+
.with_stderr(format!("Could not find `Cargo.toml` in `{}`\n",
377+
p.root().join_many(&["src", "bar"]).display())));
378+
379+
})

0 commit comments

Comments
 (0)