Skip to content

Commit 3c77e5d

Browse files
committed
Add a directive for ignoring imports
1 parent 7d43a12 commit 3c77e5d

11 files changed

Lines changed: 63 additions & 1 deletion

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# gazelle:rust_ignore_import false_positive
2+
3+
load("@rules_rust//rust:defs.bzl", "rust_library")
4+
5+
rust_library(
6+
name = "lib",
7+
srcs = ["lib.rs"],
8+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# gazelle:rust_ignore_import false_positive
2+
3+
load("@rules_rust//rust:defs.bzl", "rust_library")
4+
5+
rust_library(
6+
name = "lib",
7+
srcs = ["lib.rs"],
8+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The `rust_ignore_import` directive allows ignoring specific imports when resolving dependencies, helping to deal with false positives in the dependency detection logic.
2+
3+
This test verifies:
4+
1. The directive successfully ignores the specified import (`false_positive`) in the BUILD file where it's defined
5+
2. The directive does NOT propagate to subdirectories - `subpkg/` will report an error for `false_positive` since it doesn't have its own directive

generation_tests/ignore_import/WORKSPACE

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gazelle: //subpkg: no match for false_positive
2+
gazelle: //:lib: no match for real_dep
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use real_dep::Something;
2+
use false_positive::FalsePositive;
3+
4+
fn main() {
5+
Something::new();
6+
FalsePositive::new();
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_library")
2+
3+
rust_library(
4+
name = "subpkg",
5+
srcs = ["lib.rs"],
6+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_library")
2+
3+
rust_library(
4+
name = "subpkg",
5+
srcs = ["lib.rs"],
6+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use false_positive::FalsePositive;
2+
3+
fn helper() {
4+
FalsePositive::new();
5+
}

rust_language/lang.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ var (
6161
// and rust_proc_macro targets. rust_binary and cargo_build_script targets
6262
// continue to list files explicitly.
6363
srcsGlobDirective string = "rust_srcs_glob"
64+
65+
// Ignore a specific import when resolving dependencies.
66+
// usage: # gazelle:rust_ignore_import <import name>
67+
ignoreImportDirective string = "rust_ignore_import"
6468
)
6569

6670
type rustConfig struct {
@@ -73,6 +77,7 @@ type rustConfig struct {
7377
DefaultFeatures bool
7478
DefaultEdition string
7579
SrcsGlob bool
80+
IgnoredImports map[string]bool
7681
}
7782

7883
func (cfg *rustConfig) Clone() *rustConfig {
@@ -88,6 +93,8 @@ func (cfg *rustConfig) Clone() *rustConfig {
8893
for k, v := range cfg.KindMapInverse {
8994
copy.KindMapInverse[k] = v
9095
}
96+
// Don't copy IgnoredImports - make rust_ignore_import local to each BUILD file
97+
copy.IgnoredImports = make(map[string]bool)
9198
return &copy
9299
}
93100

@@ -194,7 +201,7 @@ func (*rustLang) KnownDirectives() []string {
194201
return []string{modeDirective, lockfileDirective, cargoLockfileDirective,
195202
cratesPrefixDirective, procMacroOverrideDirective, allowUnusedCrateDirective,
196203
rustFeatureDirective, defaultFeaturesDirective, defaultEditionDirective,
197-
srcsGlobDirective}
204+
srcsGlobDirective, ignoreImportDirective}
198205
}
199206

200207
func (l *rustLang) GetConfig(c *config.Config) *rustConfig {
@@ -230,6 +237,7 @@ func (l *rustLang) Configure(c *config.Config, rel string, from *rule.File) {
230237
DefaultFeatures: true, // enable default features by default
231238
DefaultEdition: "",
232239
SrcsGlob: false,
240+
IgnoredImports: make(map[string]bool),
233241
}
234242
} else {
235243
// NOTE(will): important to clone so that we don't leak state across directories
@@ -296,6 +304,8 @@ func (l *rustLang) Configure(c *config.Config, rel string, from *rule.File) {
296304
directive.Key, directive.Key)
297305
}
298306
cfg.SrcsGlob = value
307+
} else if directive.Key == ignoreImportDirective {
308+
cfg.IgnoredImports[directive.Value] = true
299309
}
300310
}
301311
}

0 commit comments

Comments
 (0)