aboutsummaryrefslogtreecommitdiff
path: root/kotlin/forbidden_deps.bzl
blob: 67a04fb3414684b3c37bd7be47922f6800632c74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright 2022 Google LLC. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""kt_traverse_exports visitor for identifying forbidden deps of Kotlin rules.

Currently this system recognizes:
  - nano protos
  - targets in forbidden packages
  - targets exporting other forbidden targets
"""

load("@bazel_skylib//lib:sets.bzl", "sets")
load("//bazel:stubs.bzl", "EXEMPT_DEPS", "FORBIDDEN_DEP_PACKAGES")

def _error(target, msg):
    return (str(target.label), msg)

def _is_exempt(target):
    return sets.contains(EXEMPT_DEPS, str(target.label))

def _check_forbidden(target, ctx_rule):
    if _is_exempt(target):
        return []

    if sets.contains(FORBIDDEN_DEP_PACKAGES, target.label.package):
        return [_error(target, "Forbidden package")]

    # Identify nano protos using tag (b/122083175)
    for tag in ctx_rule.attr.tags:
        if "nano_proto_library" == tag:
            return [_error(target, "nano_proto_library")]

    return []

def _if_not_checked(target):
    return [] if _is_exempt(target) else [_error(target, "Not checked")]

def _validate_deps(error_set):
    if not error_set:
        return

    error_lines = [
        "  " + name + " : " + msg
        for (name, msg) in error_set.to_list()
    ]
    fail("Forbidden deps, see go/kotlin/build-rules#restrictions:\n" + "\n".join(error_lines))

kt_forbidden_deps_visitor = struct(
    name = "forbidden_deps",
    visit_target = _check_forbidden,
    filter_edge = None,
    process_unvisited_target = _if_not_checked,
    finish_expansion = _validate_deps,
)