@@ -74,10 +74,31 @@ impl Autobuilder {
7474 cmd. arg ( "--working-dir=." ) ;
7575 cmd. arg ( & self . database ) ;
7676
77- for line in env:: var ( "LGTM_INDEX_FILTERS" )
78- . unwrap_or_default ( )
79- . split ( '\n' )
80- {
77+ // LGTM_INDEX_FILTERS is a prioritized list of include/exclude filters, where
78+ // later filters take priority over earlier ones.
79+ // 1) If we only see includes, we should ignore everything else, which is
80+ // achieved by using `--also-match={filter}`.
81+ // 2) if we see both includes and excludes, we process them in order by using
82+ // `--also-match={filter}` for includes and `--also-match=!{filter}` for
83+ // excludes.
84+ // 3) If we only see excludes, we should accept everything else. Naive solution
85+ // of just using `--also-match=!{filter}` is not good enough, since nothing
86+ // will make the `--also-match`` pass for any file. In that case, we add a dummy
87+ // initial `--also-match=**/*``to get the desired behavior.
88+ let tmp = env:: var ( "LGTM_INDEX_FILTERS" ) . unwrap_or_default ( ) ;
89+ let lgtm_index_filters = tmp. split ( '\n' ) ;
90+ let lgtm_index_filters_has_include = lgtm_index_filters
91+ . clone ( )
92+ . any ( |s| s. starts_with ( "include:" ) ) ;
93+ let lgtm_index_filters_has_exclude = lgtm_index_filters
94+ . clone ( )
95+ . any ( |s| s. starts_with ( "exclude:" ) ) ;
96+
97+ if !lgtm_index_filters_has_include && lgtm_index_filters_has_exclude {
98+ cmd. arg ( "--also-match=**/*" ) ;
99+ }
100+
101+ for line in lgtm_index_filters {
81102 if let Some ( stripped) = line. strip_prefix ( "include:" ) {
82103 cmd. arg ( "--also-match=" . to_owned ( ) + stripped) ;
83104 } else if let Some ( stripped) = line. strip_prefix ( "exclude:" ) {
0 commit comments