Skip to content

Commit

Permalink
rustpkg: Search for crates in the current directory
Browse files Browse the repository at this point in the history
As per #8520, find crates in the current working directory even
if it's not a workspace.

Closes #8520
  • Loading branch information
catamorphism committed Oct 12, 2013
1 parent 399a425 commit 1ab2d51
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 17 deletions.
4 changes: 0 additions & 4 deletions src/librustpkg/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ condition! {
pub no_rust_path: (~str) -> Path;
}

condition! {
pub not_a_workspace: (~str) -> Path;
}

condition! {
pub failed_to_create_temp_dir: (~str) -> Path;
}
Expand Down
22 changes: 19 additions & 3 deletions src/librustpkg/package_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::Crate;
use messages::*;
use source_control::{safe_git_clone, git_clone_url, DirToUse, CheckedOutSources};
use source_control::make_read_only;
use path_util::{find_dir_using_rust_path_hack, make_dir_rwx_recursive};
use path_util::{target_build_dir, versionize};
use path_util::{find_dir_using_rust_path_hack, make_dir_rwx_recursive, default_workspace};
use path_util::{target_build_dir, versionize, dir_has_crate_file};
use util::compile_crate;
use workcache_support;
use workcache_support::crate_tag;
Expand Down Expand Up @@ -189,7 +189,23 @@ impl PkgSrc {
match ok_d {
Some(d) => d,
None => {
if use_rust_path_hack {
// See if the sources are in $CWD
let cwd = os::getcwd();
if dir_has_crate_file(&cwd) {
return PkgSrc {
// In this case, source_workspace isn't really a workspace.
// This data structure needs yet more refactoring.
source_workspace: cwd.clone(),
destination_workspace: default_workspace(),
build_in_destination: true,
start_dir: cwd,
id: id,
libs: ~[],
mains: ~[],
benchs: ~[],
tests: ~[]
}
} else if use_rust_path_hack {
match find_dir_using_rust_path_hack(&id) {
Some(d) => d,
None => {
Expand Down
8 changes: 6 additions & 2 deletions src/librustpkg/path_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ pub fn uninstall_package_from(workspace: &Path, pkgid: &PkgId) {

}

pub fn dir_has_crate_file(dir: &Path) -> bool {
dir_has_file(dir, "lib.rs") || dir_has_file(dir, "main.rs")
|| dir_has_file(dir, "test.rs") || dir_has_file(dir, "bench.rs")
}

fn dir_has_file(dir: &Path, file: &str) -> bool {
assert!(dir.is_absolute());
os::path_exists(&dir.push(file))
Expand All @@ -427,8 +432,7 @@ pub fn find_dir_using_rust_path_hack(p: &PkgId) -> Option<Path> {
// has a name that's a single component
if dir.is_parent_of(&p.path) || dir.is_parent_of(&versionize(&p.path, &p.version)) {
debug2!("In find_dir_using_rust_path_hack: checking dir {}", dir.to_str());
if dir_has_file(dir, "lib.rs") || dir_has_file(dir, "main.rs")
|| dir_has_file(dir, "test.rs") || dir_has_file(dir, "bench.rs") {
if dir_has_crate_file(dir) {
debug2!("Did find id {} in dir {}", p.to_str(), dir.to_str());
return Some(dir.clone());
}
Expand Down
13 changes: 6 additions & 7 deletions src/librustpkg/rustpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use messages::{error, warn, note};
use path_util::{build_pkg_id_in_workspace, built_test_in_workspace};
use path_util::{U_RWX, in_rust_path};
use path_util::{built_executable_in_workspace, built_library_in_workspace, default_workspace};
use path_util::{target_executable_in_workspace, target_library_in_workspace};
use path_util::{target_executable_in_workspace, target_library_in_workspace, dir_has_crate_file};
use source_control::{CheckedOutSources, is_git_dir, make_read_only};
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, cwd_to_workspace};
use workspace::determine_destination;
Expand All @@ -48,7 +48,6 @@ use context::{Context, BuildContext,
use package_id::PkgId;
use package_source::PkgSrc;
use target::{WhatToBuild, Everything, is_lib, is_main, is_test, is_bench, Tests};
// use workcache_support::{discover_outputs, digest_only_date};
use workcache_support::digest_only_date;
use exit_codes::{COPY_FAILED_CODE, BAD_FLAG_CODE};

Expand Down Expand Up @@ -204,10 +203,11 @@ pub trait CtxMethods {

impl CtxMethods for BuildContext {
fn build_args(&self, args: ~[~str], what: &WhatToBuild) -> Option<(PkgId, Path)> {
let cwd = os::getcwd();

if args.len() < 1 {
match cwd_to_workspace() {
None if self.context.use_rust_path_hack => {
let cwd = os::getcwd();
None if dir_has_crate_file(&cwd) => {
let pkgid = PkgId::new(cwd.components[cwd.components.len() - 1]);
let mut pkg_src = PkgSrc::new(cwd, default_workspace(), true, pkgid);
self.build(&mut pkg_src, what);
Expand Down Expand Up @@ -253,6 +253,7 @@ impl CtxMethods for BuildContext {
}
}
fn run(&self, cmd: &str, args: ~[~str]) {
let cwd = os::getcwd();
match cmd {
"build" => {
self.build_args(args, &Everything);
Expand All @@ -271,7 +272,6 @@ impl CtxMethods for BuildContext {
// The package id is presumed to be the first command-line
// argument
let pkgid = PkgId::new(args[0].clone());
let cwd = os::getcwd();
self.clean(&cwd, &pkgid); // tjc: should use workspace, not cwd
}
}
Expand All @@ -288,8 +288,7 @@ impl CtxMethods for BuildContext {
"install" => {
if args.len() < 1 {
match cwd_to_workspace() {
None if self.context.use_rust_path_hack => {
let cwd = os::getcwd();
None if dir_has_crate_file(&cwd) => {
let inferred_pkgid =
PkgId::new(cwd.components[cwd.components.len() - 1]);
self.install(PkgSrc::new(cwd, default_workspace(),
Expand Down
57 changes: 56 additions & 1 deletion src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,11 @@ fn test_make_dir_rwx() {
assert!(os::remove_dir_recursive(&dir));
}

// The following two tests call into the rustpkg library directly, and that's not
// really thread-safe. They should still work, but they fail mysteriously on
// rust-linux2. Ignored.
#[test]
#[ignore]
fn test_install_valid() {
use path_util::installed_library_in_workspace;

Expand Down Expand Up @@ -593,6 +597,7 @@ fn test_install_valid() {
}

#[test]
#[ignore]
fn test_install_invalid() {
let sysroot = test_sysroot();
let pkgid = fake_pkg();
Expand All @@ -613,7 +618,43 @@ fn test_install_invalid() {
assert!(result == Err(()));
}

// Tests above should (maybe) be converted to shell out to rustpkg, too
#[test]
fn test_install_valid_external() {
let temp_pkg_id = PkgId::new("foo");
let (tempdir, _) = mk_temp_workspace(&temp_pkg_id.path,
&temp_pkg_id.version);
let temp_workspace = tempdir.path();
command_line_test([~"install", ~"foo"], temp_workspace);

// Check that all files exist
let exec = target_executable_in_workspace(&temp_pkg_id, temp_workspace);
debug2!("exec = {}", exec.to_str());
assert!(os::path_exists(&exec));
assert!(is_rwx(&exec));

let lib = installed_library_in_workspace(&temp_pkg_id.path, temp_workspace);
debug2!("lib = {:?}", lib);
assert!(lib.as_ref().map_default(false, |l| os::path_exists(l)));
assert!(lib.as_ref().map_default(false, |l| is_rwx(l)));

// And that the test and bench executables aren't installed
assert!(!os::path_exists(&target_test_in_workspace(&temp_pkg_id, temp_workspace)));
let bench = target_bench_in_workspace(&temp_pkg_id, temp_workspace);
debug2!("bench = {}", bench.to_str());
assert!(!os::path_exists(&bench));

}

#[test]
fn test_install_invalid_external() {
let cwd = os::getcwd();
command_line_test_expect_fail([~"install", ~"foo"],
&cwd,
None,
// FIXME #3408: Should be NONEXISTENT_PACKAGE_CODE
COPY_FAILED_CODE);
}

#[test]
fn test_install_git() {
let temp_pkg_id = git_repo_pkg();
Expand Down Expand Up @@ -1309,6 +1350,8 @@ fn rust_path_hack_test(hack_flag: bool) {
assert!(!built_executable_exists(workspace, "foo"));
}

// Notice that this is the only test case where the --rust-path-hack
// flag is actually needed
#[test]
fn test_rust_path_can_contain_package_dirs_with_flag() {
/*
Expand Down Expand Up @@ -2084,6 +2127,18 @@ fn test_compile_error() {
}
}

#[test]
fn find_sources_in_cwd() {
let temp_dir = TempDir::new("sources").expect("find_sources_in_cwd failed");
let temp_dir = temp_dir.path();
let source_dir = temp_dir.push("foo");
os::mkdir_recursive(&source_dir, U_RWX);
writeFile(&source_dir.push("main.rs"),
"fn main() { let _x = (); }");
command_line_test([~"install", ~"foo"], &source_dir);
assert_executable_exists(&source_dir.push(".rust"), "foo");
}

/// Returns true if p exists and is executable
fn is_executable(p: &Path) -> bool {
use std::libc::consts::os::posix88::{S_IXUSR};
Expand Down

8 comments on commit 1ab2d51

@bors
Copy link
Contributor

@bors bors commented on 1ab2d51 Oct 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from cmr
at catamorphism@1ab2d51

@bors
Copy link
Contributor

@bors bors commented on 1ab2d51 Oct 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging catamorphism/rust/rustpkg-issue-8520 = 1ab2d51 into auto

@bors
Copy link
Contributor

@bors bors commented on 1ab2d51 Oct 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catamorphism/rust/rustpkg-issue-8520 = 1ab2d51 merged ok, testing candidate = 5dba7c33

@bors
Copy link
Contributor

@bors bors commented on 1ab2d51 Oct 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from cmr
at catamorphism@1ab2d51

@bors
Copy link
Contributor

@bors bors commented on 1ab2d51 Oct 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging catamorphism/rust/rustpkg-issue-8520 = 1ab2d51 into auto

@bors
Copy link
Contributor

@bors bors commented on 1ab2d51 Oct 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catamorphism/rust/rustpkg-issue-8520 = 1ab2d51 merged ok, testing candidate = 4bc34ac0

Please sign in to comment.