1
1
#![ feature( test) ] // compiletest_rs requires this attribute
2
2
#![ feature( once_cell) ]
3
- #![ feature( try_blocks) ]
4
3
5
4
use compiletest_rs as compiletest;
6
5
use compiletest_rs:: common:: Mode as TestMode ;
7
6
7
+ use std:: collections:: HashMap ;
8
8
use std:: env:: { self , remove_var, set_var, var_os} ;
9
9
use std:: ffi:: { OsStr , OsString } ;
10
10
use std:: fs;
@@ -16,6 +16,28 @@ mod cargo;
16
16
// whether to run internal tests or not
17
17
const RUN_INTERNAL_TESTS : bool = cfg ! ( feature = "internal-lints" ) ;
18
18
19
+ /// All crates used in UI tests are listed here
20
+ static TEST_DEPENDENCIES : & [ & str ] = & [
21
+ "clippy_utils" ,
22
+ "derive_new" ,
23
+ "if_chain" ,
24
+ "itertools" ,
25
+ "quote" ,
26
+ "regex" ,
27
+ "serde" ,
28
+ "serde_derive" ,
29
+ "syn" ,
30
+ ] ;
31
+
32
+ // Test dependencies may need an `extern crate` here to ensure that they show up
33
+ // in the depinfo file (otherwise cargo thinks they are unused)
34
+ extern crate clippy_utils;
35
+ extern crate derive_new;
36
+ extern crate if_chain;
37
+ extern crate itertools;
38
+ extern crate quote;
39
+ extern crate syn;
40
+
19
41
fn host_lib ( ) -> PathBuf {
20
42
option_env ! ( "HOST_LIBS" ) . map_or ( cargo:: CARGO_TARGET_DIR . join ( env ! ( "PROFILE" ) ) , PathBuf :: from)
21
43
}
@@ -24,72 +46,58 @@ fn clippy_driver_path() -> PathBuf {
24
46
option_env ! ( "CLIPPY_DRIVER_PATH" ) . map_or ( cargo:: TARGET_LIB . join ( "clippy-driver" ) , PathBuf :: from)
25
47
}
26
48
27
- // When we'll want to use `extern crate ..` for a dependency that is used
28
- // both by the crate and the compiler itself, we can't simply pass -L flags
29
- // as we'll get a duplicate matching versions. Instead, disambiguate with
30
- // `--extern dep=path`.
31
- // See https://github.com/rust-lang/rust-clippy/issues/4015.
32
- //
33
- // FIXME: We cannot use `cargo build --message-format=json` to resolve to dependency files.
34
- // Because it would force-rebuild if the options passed to `build` command is not the same
35
- // as what we manually pass to `cargo` invocation
36
- fn third_party_crates ( ) -> String {
37
- use std:: collections:: HashMap ;
38
- static CRATES : & [ & str ] = & [
39
- "clippy_lints" ,
40
- "clippy_utils" ,
41
- "if_chain" ,
42
- "itertools" ,
43
- "quote" ,
44
- "regex" ,
45
- "serde" ,
46
- "serde_derive" ,
47
- "syn" ,
48
- ] ;
49
- let dep_dir = cargo:: TARGET_LIB . join ( "deps" ) ;
50
- let mut crates: HashMap < & str , Vec < PathBuf > > = HashMap :: with_capacity ( CRATES . len ( ) ) ;
51
- let mut flags = String :: new ( ) ;
52
- for entry in fs:: read_dir ( dep_dir) . unwrap ( ) . flatten ( ) {
53
- let path = entry. path ( ) ;
54
- if let Some ( name) = try {
55
- let name = path. file_name ( ) ?. to_str ( ) ?;
56
- let ( name, _) = name. strip_suffix ( ".rlib" ) ?. strip_prefix ( "lib" ) ?. split_once ( '-' ) ?;
57
- CRATES . iter ( ) . copied ( ) . find ( |& c| c == name) ?
58
- } {
59
- flags += & format ! ( " --extern {}={}" , name, path. display( ) ) ;
60
- crates. entry ( name) . or_default ( ) . push ( path. clone ( ) ) ;
49
+ /// Produces a string with an `--extern` flag for all UI test crate
50
+ /// dependencies.
51
+ ///
52
+ /// The dependency files are located by parsing the depinfo file for this test
53
+ /// module. This assumes the `-Z binary-dep-depinfo` flag is enabled. All test
54
+ /// dependencies must be added to Cargo.toml at the project root. Test
55
+ /// dependencies that are not *directly* used by this test module require an
56
+ /// `extern crate` declaration.
57
+ fn extern_flags ( ) -> String {
58
+ let current_exe_depinfo = {
59
+ let mut path = env:: current_exe ( ) . unwrap ( ) ;
60
+ path. set_extension ( "d" ) ;
61
+ std:: fs:: read_to_string ( path) . unwrap ( )
62
+ } ;
63
+ let mut crates: HashMap < & str , & str > = HashMap :: with_capacity ( TEST_DEPENDENCIES . len ( ) ) ;
64
+ for line in current_exe_depinfo. lines ( ) {
65
+ // each dependency is expected to have a Makefile rule like `/path/to/crate-hash.rlib:`
66
+ let parse_name_path = || {
67
+ if line. starts_with ( char:: is_whitespace) {
68
+ return None ;
69
+ }
70
+ let path_str = line. strip_suffix ( ':' ) ?;
71
+ let path = Path :: new ( path_str) ;
72
+ if !matches ! ( path. extension( ) ?. to_str( ) ?, "rlib" | "so" | "dylib" | "dll" ) {
73
+ return None ;
74
+ }
75
+ let ( name, _hash) = path. file_stem ( ) ?. to_str ( ) ?. rsplit_once ( '-' ) ?;
76
+ // the "lib" prefix is not present for dll files
77
+ let name = name. strip_prefix ( "lib" ) . unwrap_or ( name) ;
78
+ Some ( ( name, path_str) )
79
+ } ;
80
+ if let Some ( ( name, path) ) = parse_name_path ( ) {
81
+ if TEST_DEPENDENCIES . contains ( & name) {
82
+ // A dependency may be listed twice if it is available in sysroot,
83
+ // and the sysroot dependencies are listed first. As of the writing,
84
+ // this only seems to apply to if_chain.
85
+ crates. insert ( name, path) ;
86
+ }
61
87
}
62
88
}
63
- crates. retain ( |_, paths| paths. len ( ) > 1 ) ;
64
- if !crates. is_empty ( ) {
65
- let crate_names = crates. keys ( ) . map ( |s| format ! ( "`{}`" , s) ) . collect :: < Vec < _ > > ( ) . join ( ", " ) ;
66
- // add backslashes for an easy copy-paste `rm` command
67
- let paths = crates
68
- . into_values ( )
69
- . flatten ( )
70
- . map ( |p| strip_current_dir ( & p) . display ( ) . to_string ( ) )
71
- . collect :: < Vec < _ > > ( )
72
- . join ( " \\ \n " ) ;
73
- // Check which action should be done in order to remove compiled deps.
74
- // If pre-installed version of compiler is used, `cargo clean` will do.
75
- // Otherwise (for bootstrapped compiler), the dependencies directory
76
- // must be removed manually.
77
- let suggested_action = if std:: env:: var_os ( "RUSTC_BOOTSTRAP" ) . is_some ( ) {
78
- "removing the stageN-tools directory"
79
- } else {
80
- "running `cargo clean`"
81
- } ;
82
-
83
- panic ! (
84
- "\n ----------------------------------------------------------------------\n \
85
- ERROR: Found multiple rlibs for crates: {}\n \
86
- Try {} or remove the following files:\n \n {}\n \n \
87
- For details on this error see https://github.com/rust-lang/rust-clippy/issues/7343\n \
88
- ----------------------------------------------------------------------\n ",
89
- crate_names, suggested_action, paths
90
- ) ;
89
+ let not_found: Vec < & str > = TEST_DEPENDENCIES
90
+ . iter ( )
91
+ . copied ( )
92
+ . filter ( |n| !crates. contains_key ( n) )
93
+ . collect ( ) ;
94
+ if !not_found. is_empty ( ) {
95
+ panic ! ( "dependencies not found in depinfo: {:?}" , not_found) ;
91
96
}
92
- flags
97
+ crates
98
+ . into_iter ( )
99
+ . map ( |( name, path) | format ! ( "--extern {}={} " , name, path) )
100
+ . collect ( )
93
101
}
94
102
95
103
fn default_config ( ) -> compiletest:: Config {
@@ -105,11 +113,14 @@ fn default_config() -> compiletest::Config {
105
113
config. compile_lib_path = path;
106
114
}
107
115
116
+ // Using `-L dependency={}` enforces that external dependencies are added with `--extern`.
117
+ // This is valuable because a) it allows us to monitor what external dependencies are used
118
+ // and b) it ensures that conflicting rlibs are resolved properly.
108
119
config. target_rustcflags = Some ( format ! (
109
- "--emit=metadata -L {0 } -L {1 } -Dwarnings -Zui-testing {2 }" ,
120
+ "--emit=metadata -L dependency={ } -L dependency={ } -Dwarnings -Zui-testing {}" ,
110
121
host_lib( ) . join( "deps" ) . display( ) ,
111
122
cargo:: TARGET_LIB . join( "deps" ) . display( ) ,
112
- third_party_crates ( ) ,
123
+ extern_flags ( ) ,
113
124
) ) ;
114
125
115
126
config. build_base = host_lib ( ) . join ( "test_build_base" ) ;
@@ -316,12 +327,3 @@ impl Drop for VarGuard {
316
327
}
317
328
}
318
329
}
319
-
320
- fn strip_current_dir ( path : & Path ) -> & Path {
321
- if let Ok ( curr) = env:: current_dir ( ) {
322
- if let Ok ( stripped) = path. strip_prefix ( curr) {
323
- return stripped;
324
- }
325
- }
326
- path
327
- }
0 commit comments