@@ -49,6 +49,8 @@ _deps_aspect = aspect(
49
49
)
50
50
51
51
def _find_deps_with_disallowed_prefixes (current_pkg , dep_pkgs , prefixes ):
52
+ if prefixes == None :
53
+ return []
52
54
return [dp for dp_list in [
53
55
[(d , p ) for p in prefixes if d .startswith (p ) and d != current_pkg ]
54
56
for d in dep_pkgs
@@ -61,11 +63,13 @@ def _deps_rule_impl(ctx):
61
63
prefixes = ctx .attr .disallowed_prefixes ,
62
64
)
63
65
deps = {k : None for k in ctx .attr .src [_DepsInfo ].deps .to_list ()}
64
- if ctx .attr .allowlist :
66
+ if ctx .attr .has_allowlist :
65
67
failed = [p for p in deps if p not in ctx .attr .allowlist and
66
68
p .label != ctx .attr .src .label ]
67
- else :
69
+ elif ctx . attr . has_disallowed_list :
68
70
failed = [p for p in ctx .attr .disallowed_list if p in deps ]
71
+ else :
72
+ failed = []
69
73
failures = []
70
74
if failed_prefixes :
71
75
failures .extend ([
@@ -108,10 +112,14 @@ _deps_rule = rule(
108
112
"disallow_cdeps" : attr .bool (mandatory = False , default = False ),
109
113
"disallowed_list" : attr .label_list (providers = [GoInfo ]),
110
114
"disallowed_prefixes" : attr .string_list (mandatory = False , allow_empty = True ),
115
+ "has_allowlist" : attr .bool (default = False ),
116
+ "has_disallowed_list" : attr .bool (default = False ),
111
117
},
112
118
)
113
119
114
120
def _validate_disallowed_prefixes (prefixes ):
121
+ if prefixes == None :
122
+ return []
115
123
validated = []
116
124
repo_prefix = "github.com/cockroachdb/cockroach/"
117
125
short_prefix = "pkg/"
@@ -130,11 +138,15 @@ def _validate_disallowed_prefixes(prefixes):
130
138
131
139
def disallowed_imports_test (
132
140
src ,
133
- disallowed_list = [] ,
134
- disallowed_prefixes = [] ,
141
+ disallowed_list = None ,
142
+ disallowed_prefixes = None ,
135
143
disallow_cdeps = False ,
136
- allowlist = []):
137
- if (disallowed_list and allowlist ) or (disallowed_prefixes and allowlist ):
144
+ allowlist = None ):
145
+
146
+ if allowlist == None and disallowed_list == None and disallowed_prefixes == None :
147
+ fail ("Either allowlist, disallowed_list or disallowed_prefixes should be passed, to block " +
148
+ "all imports you must explicitly pass allowlist = []" )
149
+ if (allowlist != None and disallowed_list != None ) or (allowlist != None and disallowed_prefixes != None ):
138
150
fail ("allowlist or (disallowed_list or disallowed_prefixes) can be " +
139
151
"provided, but not both" )
140
152
disallowed_prefixes = _validate_disallowed_prefixes (disallowed_prefixes )
@@ -147,6 +159,8 @@ def disallowed_imports_test(
147
159
disallowed_list = disallowed_list ,
148
160
disallowed_prefixes = disallowed_prefixes ,
149
161
disallow_cdeps = disallow_cdeps ,
162
+ has_allowlist = allowlist != None ,
163
+ has_disallowed_list = disallowed_list != None ,
150
164
)
151
165
native .sh_test (
152
166
name = src .strip (":" ) + "_disallowed_imports_test" ,
0 commit comments