Skip to content

Commit f519d3d

Browse files
committed
Auto merge of #4447 - lucaskolstad:proc-macro-crates-host-deps-linkage, r=alexcrichton
Add host dependency path via -L for cargo_test. Proc-macro crates' dependencies in the host dependency directory cannot be found when running `cargo test` with the `--target {target}` flag set as reported in #4224. This adds the host dependency directory to the search path to find those missing dependencies with -L when building tests and adds a test case that fails before and passes after this patch. A new function `find_host_deps(..)` is added to accomplish this which can determine the path of the host dependencies directory from within `run_doc_tests(..)` where the missing library search path is needed. Fixes #4224 Fixes #4254 Modeled after a similar patch: a298346 **Concerns** The test case seems to require a non-local crate from crates.io to example the failure before this patch. Couldn't make it fail with simply another local subcrate, but if others think it's possible that'd be great. This means that during tests for the cargo project itself that this test case actually downloads and compiles a crate, which I don't think any other tests do and is obviously not ideal and is perhaps even unacceptable. I've used the base64 crate pretty arbitrarily, but which crate it is really doesn't matter to test the case's content. So if anyone knows a tiny or empty crate on crates.io to use instead that'd speed this up and if someone can figure out how to make it fail before this patch is applied without downloading an external crate that would help as well. Also, I'm not 100% confident about the `find_host_deps(..)` style and whether it's the best way to find the host dependencies directory with just the `TestOptions` and `Compilation` structs available since I'm new to this project. Any comments are appreciated.
2 parents 62f800f + 95c6e68 commit f519d3d

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ pub struct Compilation<'cfg> {
2828
/// Root output directory (for the local package's artifacts)
2929
pub root_output: PathBuf,
3030

31-
/// Output directory for rust dependencies
31+
/// Output directory for rust dependencies.
32+
/// May be for the host or for a specific target.
3233
pub deps_output: PathBuf,
3334

34-
/// Library search path for compiler plugins and build scripts
35-
/// which have dynamic dependencies.
36-
pub plugins_dylib_path: PathBuf,
35+
/// Output directory for the rust host dependencies.
36+
pub host_deps_output: PathBuf,
3737

3838
/// The path to rustc's own libstd
3939
pub host_dylib_path: Option<PathBuf>,
@@ -64,7 +64,7 @@ impl<'cfg> Compilation<'cfg> {
6464
native_dirs: HashSet::new(), // TODO: deprecated, remove
6565
root_output: PathBuf::from("/"),
6666
deps_output: PathBuf::from("/"),
67-
plugins_dylib_path: PathBuf::from("/"),
67+
host_deps_output: PathBuf::from("/"),
6868
host_dylib_path: None,
6969
target_dylib_path: None,
7070
tests: Vec::new(),
@@ -124,7 +124,7 @@ impl<'cfg> Compilation<'cfg> {
124124
-> CargoResult<ProcessBuilder> {
125125

126126
let mut search_path = if is_host {
127-
let mut search_path = vec![self.plugins_dylib_path.clone()];
127+
let mut search_path = vec![self.host_deps_output.clone()];
128128
search_path.extend(self.host_dylib_path.clone());
129129
search_path
130130
} else {

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
175175
None => {}
176176
}
177177

178-
self.compilation.plugins_dylib_path = self.host.deps().to_path_buf();
178+
self.compilation.host_deps_output = self.host.deps().to_path_buf();
179179

180180
let layout = self.target.as_ref().unwrap_or(&self.host);
181181
self.compilation.root_output = layout.dest().to_path_buf();

src/cargo/ops/cargo_test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,17 @@ fn run_doc_tests(options: &TestOptions,
149149
arg.push(rust_dep);
150150
p.arg("-L").arg(arg);
151151
}
152+
152153
for native_dep in compilation.native_dirs.iter() {
153154
p.arg("-L").arg(native_dep);
154155
}
155156

157+
for &host_rust_dep in &[&compilation.host_deps_output] {
158+
let mut arg = OsString::from("dependency=");
159+
arg.push(host_rust_dep);
160+
p.arg("-L").arg(arg);
161+
}
162+
156163
for arg in test_args {
157164
p.arg("--test-args").arg(arg);
158165
}

tests/test.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fs::File;
66
use std::io::prelude::*;
77
use std::str;
88

9-
use cargotest::{sleep_ms, is_nightly};
9+
use cargotest::{sleep_ms, is_nightly, rustc_host};
1010
use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest, cargo_exe};
1111
use cargotest::support::paths::CargoPathExt;
1212
use cargotest::support::registry::Package;
@@ -2782,3 +2782,58 @@ fn publish_a_crate_without_tests() {
27822782
assert_that(p.cargo("test").arg("--package").arg("testless"),
27832783
execs().with_status(0));
27842784
}
2785+
2786+
#[test]
2787+
fn find_dependency_of_proc_macro_dependency_with_target() {
2788+
let workspace = project("workspace")
2789+
.file("Cargo.toml", r#"
2790+
[workspace]
2791+
members = ["root", "proc_macro_dep"]
2792+
"#)
2793+
.file("root/Cargo.toml", r#"
2794+
[project]
2795+
name = "root"
2796+
version = "0.1.0"
2797+
authors = []
2798+
2799+
[dependencies]
2800+
proc_macro_dep = { path = "../proc_macro_dep" }
2801+
"#)
2802+
.file("root/src/lib.rs", r#"
2803+
#[macro_use]
2804+
extern crate proc_macro_dep;
2805+
2806+
#[derive(Noop)]
2807+
pub struct X;
2808+
"#)
2809+
.file("proc_macro_dep/Cargo.toml", r#"
2810+
[project]
2811+
name = "proc_macro_dep"
2812+
version = "0.1.0"
2813+
authors = []
2814+
2815+
[lib]
2816+
proc-macro = true
2817+
2818+
[dependencies]
2819+
bar = "^0.1"
2820+
"#)
2821+
.file("proc_macro_dep/src/lib.rs", r#"
2822+
extern crate bar;
2823+
extern crate proc_macro;
2824+
use proc_macro::TokenStream;
2825+
2826+
#[proc_macro_derive(Noop)]
2827+
pub fn noop(_input: TokenStream) -> TokenStream {
2828+
"".parse().unwrap()
2829+
}
2830+
"#);
2831+
Package::new("foo", "0.1.0").publish();
2832+
Package::new("bar", "0.1.0")
2833+
.dep("foo", "0.1")
2834+
.file("src/lib.rs", "extern crate foo;")
2835+
.publish();
2836+
workspace.build();
2837+
assert_that(workspace.cargo("test").arg("--all").arg("--target").arg(rustc_host()),
2838+
execs().with_status(0));
2839+
}

0 commit comments

Comments
 (0)