Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload sources even for binaries and failed builds #928

Merged
merged 2 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/db/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(crate) fn add_package_into_database(
source_dir: &Path,
res: &BuildResult,
default_target: &str,
source_files: Option<Value>,
source_files: Value,
doc_targets: Vec<String>,
registry_data: &ReleaseData,
has_docs: bool,
Expand Down
19 changes: 9 additions & 10 deletions src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,32 +343,24 @@ impl RustwideBuilder {
.run(|build| {
use crate::docbuilder::metadata::BuildTargets;

let mut files_list = None;
let mut has_docs = false;
let mut algs = CompressionAlgorithms::default();
let mut successful_targets = Vec::new();
let metadata = Metadata::from_source_dir(&build.host_source_dir())?;
let BuildTargets {
default_target,
other_targets,
} = metadata.targets();

// Do an initial build and then copy the sources in the database
// Perform an initial build
let res = self.execute_build(default_target, true, &build, &limits, &metadata)?;
if res.result.successful {
debug!("adding sources into database");
let prefix = format!("sources/{}/{}", name, version);
let (files, new_algs) =
add_path_into_database(&self.storage, &prefix, build.host_source_dir())?;
files_list = Some(files);
algs.extend(new_algs);

if let Some(name) = res.cargo_metadata.root().library_name() {
let host_target = build.host_target_dir();
has_docs = host_target.join("doc").join(name).is_dir();
}
}

let mut algs = HashSet::new();
if has_docs {
debug!("adding documentation for the default target to the database");
self.copy_docs(&build.host_target_dir(), local_storage.path(), "", true)?;
Expand All @@ -392,6 +384,13 @@ impl RustwideBuilder {
algs.extend(new_algs);
};

// Store the sources even if the build fails
debug!("adding sources into database");
let prefix = format!("sources/{}/{}", name, version);
let (files_list, new_algs) =
add_path_into_database(&self.storage, &prefix, build.host_source_dir())?;
algs.extend(new_algs);

let has_examples = build.host_source_dir().join("examples").is_dir();
if res.result.successful {
crate::web::metrics::SUCCESSFUL_BUILDS.inc();
Expand Down
83 changes: 40 additions & 43 deletions src/test/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,67 +174,64 @@ impl<'a> FakeRelease<'a> {

/// Returns the release_id
pub(crate) fn create(self) -> Result<i32, Error> {
use std::collections::HashSet;
use std::fs;
use std::path::Path;

let tempdir = tempfile::Builder::new().prefix("docs.rs-fake").tempdir()?;
let package = self.package;
let db = self.db;
let mut rustdoc_files = self.rustdoc_files;
let storage = self.storage;

let mut source_meta = None;
let mut algs = HashSet::new();
if self.build_result.successful {
let storage = self.storage.clone();
let upload_files = |prefix: &str, files: &[(&str, &[u8])], target: Option<&str>| {
let mut path_prefix = tempdir.path().join(prefix);
if let Some(target) = target {
path_prefix.push(target);
}
fs::create_dir(&path_prefix)?;
// Upload all source files as rustdoc files
// In real life, these would be highlighted HTML, but for testing we just use the files themselves.
for (source_path, data) in &self.source_files {
if source_path.starts_with("src/") {
let updated = ["src", &package.name, &source_path[4..]].join("/");
rustdoc_files.push((Box::leak(Box::new(updated)), data));
}
}

for (path, data) in files {
// allow `src/main.rs`
if let Some(parent) = Path::new(path).parent() {
fs::create_dir_all(path_prefix.join(parent))?;
}
let file = path_prefix.join(&path);
log::debug!("writing file {}", file.display());
fs::write(file, data)?;
let upload_files = |prefix: &str, files: &[(&str, &[u8])], target: Option<&str>| {
let mut path_prefix = tempdir.path().join(prefix);
if let Some(target) = target {
path_prefix.push(target);
}
fs::create_dir(&path_prefix)?;

for (path, data) in files {
// allow `src/main.rs`
if let Some(parent) = Path::new(path).parent() {
fs::create_dir_all(path_prefix.join(parent))?;
}
let file = path_prefix.join(&path);
log::debug!("writing file {}", file.display());
fs::write(file, data)?;
}

let prefix = format!(
"{}/{}/{}/{}",
prefix,
package.name,
package.version,
target.unwrap_or("")
);
log::debug!("adding directory {} from {}", prefix, path_prefix.display());
crate::db::add_path_into_database(&storage, &prefix, path_prefix)
};
let prefix = format!(
"{}/{}/{}/{}",
prefix,
package.name,
package.version,
target.unwrap_or("")
);
log::debug!("adding directory {} from {}", prefix, path_prefix.display());
crate::db::add_path_into_database(&storage, &prefix, path_prefix)
};

let (source_meta, mut algs) = upload_files("source", &self.source_files, None)?;
log::debug!("added source files {}", source_meta);

if self.build_result.successful {
let index = [&package.name, "index.html"].join("/");
let mut rustdoc_files = self.rustdoc_files;
if package.is_library() && !rustdoc_files.iter().any(|(path, _)| path == &index) {
rustdoc_files.push((&index, b"default index content"));
}
for (source_path, data) in &self.source_files {
if source_path.starts_with("src/") {
let updated = ["src", &package.name, &source_path[4..]].join("/");
rustdoc_files.push((Box::leak(Box::new(updated)), data));
}
}

let (rustdoc_meta, new_algs) = upload_files("rustdoc", &rustdoc_files, None)?;
algs.extend(new_algs);
log::debug!("added rustdoc files {}", rustdoc_meta);
match upload_files("source", &self.source_files, None)? {
(json, new_algs) => {
source_meta = Some(json);
algs.extend(new_algs);
}
}
log::debug!("added source files {}", source_meta.as_ref().unwrap());

for target in &package.targets[1..] {
let platform = target.src_path.as_ref().unwrap();
Expand Down