Skip to content

Commit

Permalink
Fix build script outputs ending up in the wrong place
Browse files Browse the repository at this point in the history
  • Loading branch information
JWorthe committed Jan 6, 2021
1 parent e0fbf9c commit f4305ca
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/context/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
/// Note that some units may share the same directory, so care should be
/// taken in those cases!
fn pkg_dir(&self, unit: &Unit) -> String {
let name = unit.pkg.package_id().name();
let name = unit.pkg.package_id().file_safe_name();
match self.metas[unit] {
Some(ref meta) => format!("{}-{}", name, meta),
None => format!("{}-{}", name, self.target_short_hash(unit)),
Expand Down
8 changes: 2 additions & 6 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use serde::Serialize;

use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
use crate::core::manifest::{SUBCRATE_DELIMETER, SUBCRATE_DELIMETER_FILENAME_REPLACEMENT};
use crate::core::manifest::SUBCRATE_DELIMETER;
use crate::core::resolver::features::ForceAllTargets;
use crate::core::resolver::{HasDevUnits, Resolve};
use crate::core::source::MaybePackage;
Expand Down Expand Up @@ -142,11 +142,7 @@ impl Package {
}

pub fn file_safe_name(&self) -> InternedString {
let i_str = self
.package_id()
.name()
.replace(SUBCRATE_DELIMETER, SUBCRATE_DELIMETER_FILENAME_REPLACEMENT);
i_str.into()
self.package_id().file_safe_name()
}

/// Gets the name of the package that can be used in URLs and filenames
Expand Down
8 changes: 8 additions & 0 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::core::source::SourceId;
use crate::util::interning::InternedString;
use crate::util::{CargoResult, ToSemver};

use super::manifest::{SUBCRATE_DELIMETER, SUBCRATE_DELIMETER_FILENAME_REPLACEMENT};

lazy_static::lazy_static! {
static ref PACKAGE_ID_CACHE: Mutex<HashSet<&'static PackageIdInner>> =
Mutex::new(HashSet::new());
Expand Down Expand Up @@ -139,6 +141,12 @@ impl PackageId {
pub fn name(self) -> InternedString {
self.inner.name
}
pub fn file_safe_name(self) -> InternedString {
let i_str = self
.name()
.replace(SUBCRATE_DELIMETER, SUBCRATE_DELIMETER_FILENAME_REPLACEMENT);
i_str.into()
}
pub fn version(self) -> &'static semver::Version {
&self.inner.version
}
Expand Down
33 changes: 33 additions & 0 deletions tests/testsuite/build_subcrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,36 @@ To learn more, run the command again with --verbose.\n",
.run();
assert!(p.root().join("Cargo.lock").is_file());
}

#[cargo_test]
fn cargo_compile_with_build_script_puts_output_in_correct_folder() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo/bar"
authors = []
version = "0.0.0"
"#,
)
.file("src/lib.rs", "pub fn bar() {}")
.file("build.rs", "pub fn main() { println!(\"Hello world\"); }")
.build();
p.cargo("build").run();

let build_script_dir = p.target_debug_dir().join("build");
let matches: Vec<std::fs::DirEntry> = build_script_dir
.read_dir()
.expect("build dir did not exist")
.map(|dir| dir.unwrap())
.filter(|dir| dir.file_name().to_string_lossy().starts_with("foo_bar"))
.filter(|dir| dir.path().join("output").exists())
.collect();

assert!(matches.len() >= 1);

let output_contents = std::fs::read_to_string(matches[0].path().join("output"))
.unwrap_or_else(|e| panic!("could not read file {}: {}", matches[0].path().display(), e));
assert_eq!(output_contents, "Hello world\n");
}

0 comments on commit f4305ca

Please sign in to comment.