...

Text file src/github.com/bazelbuild/bazel-gazelle/internal/go_repository_config.bzl

Documentation: github.com/bazelbuild/bazel-gazelle/internal

     1# Copyright 2019 The Bazel Authors. All rights reserved.
     2#
     3# Licensed under the Apache License, Version 2.0 (the "License");
     4# you may not use this file except in compliance with the License.
     5# You may obtain a copy of the License at
     6#
     7#    http://www.apache.org/licenses/LICENSE-2.0
     8#
     9# Unless required by applicable law or agreed to in writing, software
    10# distributed under the License is distributed on an "AS IS" BASIS,
    11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12# See the License for the specific language governing permissions and
    13# limitations under the License.
    14
    15load("//internal:common.bzl", "env_execute", "executable_extension")
    16load("//internal:go_repository_cache.bzl", "read_cache_env")
    17
    18def _go_repository_config_impl(ctx):
    19    # Locate and resolve configuration files. Gazelle reads directives and
    20    # known repositories from these files. Resolving them here forces the
    21    # go_repository_config rule to be invalidated when they change. Gazelle's cache
    22    # should NOT be invalidated, so we shouldn't need to download these again.
    23    config_path = None
    24    if ctx.attr.config:
    25        config_path = ctx.path(ctx.attr.config)
    26
    27    if config_path:
    28        env = read_cache_env(ctx, str(ctx.path(Label("@bazel_gazelle_go_repository_cache//:go.env"))))
    29        generate_repo_config = str(ctx.path(Label("@bazel_gazelle_go_repository_tools//:bin/generate_repo_config{}".format(executable_extension(ctx)))))
    30        list_repos_args = [
    31            "-config_source=" + str(config_path),
    32            "-config_dest=" + str(ctx.path("WORKSPACE")),
    33        ]
    34        result = env_execute(
    35            ctx,
    36            [generate_repo_config] + list_repos_args,
    37            environment = env,
    38        )
    39        if result.return_code:
    40            fail("generate_repo_config: " + result.stderr)
    41        if result.stdout:
    42            for f in result.stdout.splitlines():
    43                f = f.lstrip()
    44                if len(f) > 0:
    45                    macro_label_str = "@" + ctx.attr.config.workspace_name + "//:" + f
    46                    if "~" in ctx.attr.config.workspace_name:
    47                        # The workspace name is a Bzlmod canonical repository
    48                        # name that we don't have visibility into directly.
    49                        # Instead, use a canonical label literal (starting with
    50                        # "@@") to bypass visibility checks.
    51                        macro_label_str = "@" + macro_label_str
    52                    macro_label = Label(macro_label_str)
    53                    ctx.path(macro_label)
    54
    55    else:
    56        ctx.file(
    57            "WORKSPACE",
    58            "",
    59            False,
    60        )
    61
    62    # add an empty build file so Bazel recognizes the config
    63    ctx.file(
    64        "BUILD.bazel",
    65        "exports_files([\"WORKSPACE\"])",
    66        False,
    67    )
    68
    69go_repository_config = repository_rule(
    70    implementation = _go_repository_config_impl,
    71    attrs = {
    72        "config": attr.label(),
    73    },
    74)

View as plain text