@@ -26,18 +26,28 @@ use super::multi_target_metadata::MultiTargetMetadata;
2626/// This prevents pruning features that are used for conditional compilation.
2727fn scan_source_for_cfg_features ( crate_dir : & Path ) -> HashSet < String > {
2828 let mut features = HashSet :: new ( ) ;
29- let src_dir = crate_dir. join ( "src" ) ;
29+ // Feature gates can be used in multiple crate targets, not only src/.
30+ // Scan the common Rust target roots to avoid pruning test-only features.
31+ for dir_name in [ "src" , "tests" , "benches" , "examples" ] {
32+ let dir = crate_dir. join ( dir_name) ;
33+ if !dir. exists ( ) {
34+ continue ;
35+ }
3036
31- if !src_dir. exists ( ) {
32- return features;
37+ if let Ok ( entries) = glob:: glob ( & format ! ( "{}/**/*.rs" , dir. display( ) ) ) {
38+ for entry in entries. flatten ( ) {
39+ if let Ok ( content) = fs:: read_to_string ( & entry) {
40+ extract_cfg_features ( & content, & mut features) ;
41+ }
42+ }
43+ }
3344 }
3445
35- // Walk the src directory for .rs files
36- if let Ok ( entries) = glob:: glob ( & format ! ( "{}/**/*.rs" , src_dir. display( ) ) ) {
37- for entry in entries. flatten ( ) {
38- if let Ok ( content) = fs:: read_to_string ( & entry) {
39- extract_cfg_features ( & content, & mut features) ;
40- }
46+ // build.rs may also use feature cfgs.
47+ let build_script = crate_dir. join ( "build.rs" ) ;
48+ if build_script. exists ( ) {
49+ if let Ok ( content) = fs:: read_to_string ( build_script) {
50+ extract_cfg_features ( & content, & mut features) ;
4151 }
4252 }
4353
@@ -532,4 +542,25 @@ mod tests {
532542 assert ! ( !is_valid_feature_name( "foo/bar" ) ) ;
533543 assert ! ( !is_valid_feature_name( "foo:bar" ) ) ;
534544 }
545+
546+ #[ test]
547+ fn test_scan_source_for_cfg_features_includes_tests_dir ( ) {
548+ let temp = tempfile:: TempDir :: new ( ) . expect ( "tempdir" ) ;
549+ let crate_dir = temp. path ( ) ;
550+
551+ fs:: create_dir_all ( crate_dir. join ( "src" ) ) . expect ( "mkdir src" ) ;
552+ fs:: create_dir_all ( crate_dir. join ( "tests" ) ) . expect ( "mkdir tests" ) ;
553+ fs:: write ( crate_dir. join ( "src/lib.rs" ) , "pub fn ping() {}" ) . expect ( "write lib.rs" ) ;
554+ fs:: write (
555+ crate_dir. join ( "tests/integration.rs" ) ,
556+ r#"#[cfg(feature = "test-ollama")] fn run() {}"# ,
557+ )
558+ . expect ( "write integration test" ) ;
559+
560+ let features = scan_source_for_cfg_features ( crate_dir) ;
561+ assert ! (
562+ features. contains( "test-ollama" ) ,
563+ "should detect feature cfg used from tests/"
564+ ) ;
565+ }
535566}
0 commit comments