Skip to content

Commit 3fb8c49

Browse files
committed
fix the actual bug
- only skip `--target` for proc-macros - don't try build non-host targets for proc-macros, since they're not supported anyway.
1 parent 8772e1a commit 3fb8c49

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

crates/metadata/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,26 @@ impl Metadata {
197197
/// If `include_default_targets` is `true` and `targets` is unset, this also includes
198198
/// [`DEFAULT_TARGETS`]. Otherwise, if `include_default_targets` is `false` and `targets`
199199
/// is unset, `other_targets` will be empty.
200+
///
201+
/// All of the above is ignored for proc-macros, which are always only compiled for the host.
200202
pub fn targets(&self, include_default_targets: bool) -> BuildTargets<'_> {
203+
// Proc macros can only be compiled for the host, so just completely ignore any configured targets.
204+
// It would be nice to warn about this somehow ...
205+
if self.proc_macro {
206+
return BuildTargets {
207+
default_target: HOST_TARGET,
208+
other_targets: HashSet::default(),
209+
};
210+
}
211+
201212
let default_target = self
202213
.default_target
203214
.as_deref()
204215
// Use the first element of `targets` if `default_target` is unset and `targets` is non-empty
205216
.or_else(|| {
206217
self.targets
207218
.as_ref()
208-
.and_then(|targets| targets.iter().next().map(String::as_str))
219+
.and_then(|targets| targets.first().map(String::as_str))
209220
})
210221
.unwrap_or(HOST_TARGET);
211222

src/docbuilder/rustwide_builder.rs

+72-1
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,16 @@ impl RustwideBuilder {
656656
if let Some(cpu_limit) = self.config.build_cpu_limit {
657657
cargo_args.push(format!("-j{}", cpu_limit));
658658
}
659-
if target != HOST_TARGET {
659+
// Cargo has a series of frightening bugs around cross-compiling proc-macros:
660+
// - Passing `--target` causes RUSTDOCFLAGS to fail to be passed 🤦
661+
// - Passing `--target` will *create* `target/{target-name}/doc` but will put the docs in `target/doc` anyway
662+
// As a result, it's not possible for us to support cross-compiling proc-macros.
663+
// However, all these caveats unfortunately still apply when `{target-name}` is the host.
664+
// So, only pass `--target` for crates that aren't proc-macros.
665+
//
666+
// Originally, this had a simpler check `target != HOST_TARGET`, but *that* was buggy when `HOST_TARGET` wasn't the same as the default target.
667+
// Rather than trying to keep track of it all, only special case proc-macros, which are what we actually care about.
668+
if !metadata.proc_macro {
660669
cargo_args.push("--target".into());
661670
cargo_args.push(target.into());
662671
};
@@ -935,4 +944,66 @@ mod tests {
935944
Ok(())
936945
})
937946
}
947+
948+
#[test]
949+
#[ignore]
950+
fn test_proc_macro() {
951+
wrapper(|env| {
952+
let crate_ = "thiserror-impl";
953+
let version = "1.0.26";
954+
let mut builder = RustwideBuilder::init(env).unwrap();
955+
assert!(builder.build_package(crate_, version, PackageKind::CratesIo)?);
956+
957+
let storage = env.storage();
958+
959+
// doc archive exists
960+
let doc_archive = rustdoc_archive_path(crate_, version);
961+
assert!(storage.exists(&doc_archive)?);
962+
963+
// source archive exists
964+
let source_archive = source_archive_path(crate_, version);
965+
assert!(storage.exists(&source_archive)?);
966+
967+
Ok(())
968+
});
969+
}
970+
971+
#[test]
972+
#[ignore]
973+
fn test_cross_compile_non_host_default() {
974+
wrapper(|env| {
975+
let crate_ = "xingapi";
976+
let version = "0.3.3";
977+
let mut builder = RustwideBuilder::init(env).unwrap();
978+
assert!(builder.build_package(crate_, version, PackageKind::CratesIo)?);
979+
980+
let storage = env.storage();
981+
982+
// doc archive exists
983+
let doc_archive = rustdoc_archive_path(crate_, version);
984+
assert!(storage.exists(&doc_archive)?);
985+
986+
// source archive exists
987+
let source_archive = source_archive_path(crate_, version);
988+
assert!(storage.exists(&source_archive)?);
989+
990+
let target = "x86_64-unknown-linux-gnu";
991+
let crate_path = crate_.replace("-", "_");
992+
let target_docs_present = storage.exists_in_archive(
993+
&doc_archive,
994+
&format!("{}/{}/index.html", target, crate_path),
995+
)?;
996+
997+
let web = env.frontend();
998+
let target_url = format!(
999+
"/{}/{}/{}/{}/index.html",
1000+
crate_, version, target, crate_path
1001+
);
1002+
1003+
assert!(target_docs_present);
1004+
assert_success(&target_url, web)?;
1005+
1006+
Ok(())
1007+
});
1008+
}
9381009
}

0 commit comments

Comments
 (0)