From c292df3e24f6471a97a38f4f80d435392991c36a Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Mon, 15 Apr 2024 14:01:31 -0600 Subject: [PATCH] [Feature] bzlmod & go.work * adds go_deps.from_file(go_work = "//:go.work") * adds ability to op-out of version conflict failures go_deps.from_file(go_work = "//:go.work, fail_on_version_conflict = False) * parses go.work files to discover used go modules and replace statements * generates repos for all deps specified in go.mod's referenced by the //:go.work * handle replace statements (both at the go.work and the go.mod level) * Errors with actionable error message if duplicate dependencies with differing versions arise (Question: can we do better?) * expand BCR test coverage to include both go.work and go.mod scenarios Caveats: * allows for dependency bleed between different go modules in the workspace - this is sorta by design for this stepping stone, but is ideal * doesn't support replace statements in go.work yet (but that shouldn't be to hard to add in a follow up) --- .bazelci/presubmit.yml | 8 +- .bcr/presubmit.yml | 2 +- .gitignore | 10 +- internal/bzlmod/go_deps.bzl | 100 +++++++- internal/bzlmod/go_mod.bzl | 215 ++++++++++++++---- tests/BUILD.bazel | 1 - tests/bcr/{ => go_mod}/.bazelignore | 0 tests/bcr/{ => go_mod}/.bazelrc | 0 tests/bcr/{ => go_mod}/.bazelversion | 0 tests/bcr/{ => go_mod}/BUILD.bazel | 0 tests/bcr/{ => go_mod}/MODULE.bazel | 4 +- tests/bcr/{ => go_mod}/WORKSPACE | 0 tests/bcr/{ => go_mod}/WORKSPACE.bzlmod | 0 tests/bcr/{ => go_mod}/go.mod | 2 +- tests/bcr/{ => go_mod}/go.sum | 0 tests/bcr/{ => go_mod}/patches/BUILD.bazel | 0 .../bcr/{ => go_mod}/patches/buildtools.patch | 0 tests/bcr/{ => go_mod}/patches/testify.patch | 0 tests/bcr/{ => go_mod}/pkg/BUILD.bazel | 2 +- tests/bcr/{ => go_mod}/pkg/data/BUILD.bazel | 2 +- tests/bcr/{ => go_mod}/pkg/data/data.go | 0 tests/bcr/{ => go_mod}/pkg/pkg_test.go | 2 +- .../bcr/{ => go_mod}/pkg/platform_lib_unix.go | 0 .../{ => go_mod}/pkg/platform_lib_windows.go | 0 tests/bcr/{ => go_mod}/proto/BUILD.bazel | 0 tests/bcr/{ => go_mod}/proto/foo.proto | 0 tests/bcr/{ => go_mod}/proto/foo_test.go | 0 tests/bcr/{ => go_mod}/test_dep/BUILD.bazel | 0 tests/bcr/{ => go_mod}/test_dep/MODULE.bazel | 3 +- tests/bcr/{ => go_mod}/test_dep/WORKSPACE | 0 .../{ => go_mod}/test_dep/patches/BUILD.bazel | 0 .../test_dep/patches/testify.patch | 0 tests/bcr/{ => go_mod}/test_dep/test.go | 0 tests/bcr/{ => go_mod}/tools/BUILD.bazel | 0 tests/bcr/{ => go_mod}/tools/go.mod | 2 +- tests/bcr/{ => go_mod}/tools/go.sum | 0 tests/bcr/{ => go_mod}/tools/tools.go | 0 tests/bcr/go_work/.bazelignore | 1 + tests/bcr/go_work/.bazelrc | 6 + tests/bcr/go_work/.bazelversion | 1 + tests/bcr/go_work/BUILD.bazel | 5 + tests/bcr/go_work/MODULE.bazel | 150 ++++++++++++ tests/bcr/go_work/WORKSPACE | 0 tests/bcr/go_work/WORKSPACE.bzlmod | 0 tests/bcr/go_work/go.work | 7 + tests/bcr/go_work/patches/BUILD.bazel | 0 tests/bcr/go_work/patches/buildtools.patch | 13 ++ tests/bcr/go_work/patches/testify.patch | 13 ++ tests/bcr/go_work/pkg/BUILD.bazel | 65 ++++++ tests/bcr/go_work/pkg/data/BUILD.bazel | 9 + tests/bcr/go_work/pkg/data/data.go | 3 + tests/bcr/go_work/pkg/go.mod | 29 +++ tests/bcr/go_work/pkg/go.sum | 119 ++++++++++ tests/bcr/go_work/pkg/pkg_test.go | 79 +++++++ tests/bcr/go_work/pkg/platform_lib_unix.go | 10 + tests/bcr/go_work/pkg/platform_lib_windows.go | 7 + tests/bcr/go_work/proto/BUILD.bazel | 31 +++ tests/bcr/go_work/proto/foo.proto | 12 + tests/bcr/go_work/proto/foo_test.go | 23 ++ tests/bcr/go_work/proto/go.mod | 2 + tests/bcr/go_work/test_dep/BUILD.bazel | 8 + tests/bcr/go_work/test_dep/MODULE.bazel | 49 ++++ tests/bcr/go_work/test_dep/WORKSPACE | 0 tests/bcr/go_work/test_dep/go.mod | 11 + tests/bcr/go_work/test_dep/go.sum | 10 + .../bcr/go_work/test_dep/patches/BUILD.bazel | 0 .../go_work/test_dep/patches/testify.patch | 13 ++ tests/bcr/go_work/test_dep/test.go | 13 ++ tests/bcr/go_work/tools/BUILD.bazel | 10 + tests/bcr/go_work/tools/go.mod | 26 +++ tests/bcr/go_work/tools/go.sum | 134 +++++++++++ tests/bcr/go_work/tools/tools.go | 12 + tests/bzlmod/go_mod_test.bzl | 55 ++++- tests/bzlmod/utils_test.bzl | 2 +- 74 files changed, 1210 insertions(+), 71 deletions(-) rename tests/bcr/{ => go_mod}/.bazelignore (100%) rename tests/bcr/{ => go_mod}/.bazelrc (100%) rename tests/bcr/{ => go_mod}/.bazelversion (100%) rename tests/bcr/{ => go_mod}/BUILD.bazel (100%) rename tests/bcr/{ => go_mod}/MODULE.bazel (98%) rename tests/bcr/{ => go_mod}/WORKSPACE (100%) rename tests/bcr/{ => go_mod}/WORKSPACE.bzlmod (100%) rename tests/bcr/{ => go_mod}/go.mod (92%) rename tests/bcr/{ => go_mod}/go.sum (100%) rename tests/bcr/{ => go_mod}/patches/BUILD.bazel (100%) rename tests/bcr/{ => go_mod}/patches/buildtools.patch (100%) rename tests/bcr/{ => go_mod}/patches/testify.patch (100%) rename tests/bcr/{ => go_mod}/pkg/BUILD.bazel (99%) rename tests/bcr/{ => go_mod}/pkg/data/BUILD.bazel (93%) rename tests/bcr/{ => go_mod}/pkg/data/data.go (100%) rename tests/bcr/{ => go_mod}/pkg/pkg_test.go (97%) rename tests/bcr/{ => go_mod}/pkg/platform_lib_unix.go (100%) rename tests/bcr/{ => go_mod}/pkg/platform_lib_windows.go (100%) rename tests/bcr/{ => go_mod}/proto/BUILD.bazel (100%) rename tests/bcr/{ => go_mod}/proto/foo.proto (100%) rename tests/bcr/{ => go_mod}/proto/foo_test.go (100%) rename tests/bcr/{ => go_mod}/test_dep/BUILD.bazel (100%) rename tests/bcr/{ => go_mod}/test_dep/MODULE.bazel (99%) rename tests/bcr/{ => go_mod}/test_dep/WORKSPACE (100%) rename tests/bcr/{ => go_mod}/test_dep/patches/BUILD.bazel (100%) rename tests/bcr/{ => go_mod}/test_dep/patches/testify.patch (100%) rename tests/bcr/{ => go_mod}/test_dep/test.go (100%) rename tests/bcr/{ => go_mod}/tools/BUILD.bazel (100%) rename tests/bcr/{ => go_mod}/tools/go.mod (92%) rename tests/bcr/{ => go_mod}/tools/go.sum (100%) rename tests/bcr/{ => go_mod}/tools/tools.go (100%) create mode 100644 tests/bcr/go_work/.bazelignore create mode 100644 tests/bcr/go_work/.bazelrc create mode 100644 tests/bcr/go_work/.bazelversion create mode 100644 tests/bcr/go_work/BUILD.bazel create mode 100644 tests/bcr/go_work/MODULE.bazel create mode 100644 tests/bcr/go_work/WORKSPACE create mode 100644 tests/bcr/go_work/WORKSPACE.bzlmod create mode 100644 tests/bcr/go_work/go.work create mode 100644 tests/bcr/go_work/patches/BUILD.bazel create mode 100644 tests/bcr/go_work/patches/buildtools.patch create mode 100644 tests/bcr/go_work/patches/testify.patch create mode 100644 tests/bcr/go_work/pkg/BUILD.bazel create mode 100644 tests/bcr/go_work/pkg/data/BUILD.bazel create mode 100644 tests/bcr/go_work/pkg/data/data.go create mode 100644 tests/bcr/go_work/pkg/go.mod create mode 100644 tests/bcr/go_work/pkg/go.sum create mode 100644 tests/bcr/go_work/pkg/pkg_test.go create mode 100644 tests/bcr/go_work/pkg/platform_lib_unix.go create mode 100644 tests/bcr/go_work/pkg/platform_lib_windows.go create mode 100644 tests/bcr/go_work/proto/BUILD.bazel create mode 100644 tests/bcr/go_work/proto/foo.proto create mode 100644 tests/bcr/go_work/proto/foo_test.go create mode 100644 tests/bcr/go_work/proto/go.mod create mode 100644 tests/bcr/go_work/test_dep/BUILD.bazel create mode 100644 tests/bcr/go_work/test_dep/MODULE.bazel create mode 100644 tests/bcr/go_work/test_dep/WORKSPACE create mode 100644 tests/bcr/go_work/test_dep/go.mod create mode 100644 tests/bcr/go_work/test_dep/go.sum create mode 100644 tests/bcr/go_work/test_dep/patches/BUILD.bazel create mode 100644 tests/bcr/go_work/test_dep/patches/testify.patch create mode 100644 tests/bcr/go_work/test_dep/test.go create mode 100644 tests/bcr/go_work/tools/BUILD.bazel create mode 100644 tests/bcr/go_work/tools/go.mod create mode 100644 tests/bcr/go_work/tools/go.sum create mode 100644 tests/bcr/go_work/tools/tools.go diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 0714d0280..6e106c0b9 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -4,6 +4,9 @@ matrix: - ubuntu2204 - macos bazel: [6.x, 7.x] + bcr_variant: + - tests/bcr/go_work + - tests/bcr/go_mod tasks: ubuntu2204: name: Ubuntu 22.04 with WORKSPACE @@ -39,11 +42,12 @@ tasks: name: BCR test module platform: ${{ platform }} bazel: ${{ bazel }} - working_directory: tests/bcr + working_directory: ${{ bcr_variant }} shell_commands: # Regenerate the BUILD files for the test module using Gazelle. # Also verify -repo_config are generated correctly in gazelle.bash - rm pkg/BUILD.bazel proto/BUILD.bazel + - touch pkg/BUILD.bazel proto/BUILD.bazel - bazel run //:gazelle -- update pkg proto - bazel run //:gazelle -- pkg proto build_targets: @@ -59,7 +63,7 @@ tasks: name: BCR test on Windows platform: windows bazel: ${{ bazel }} - working_directory: tests/bcr + working_directory: ${{ bcr_variant }} shell_commands: # Regenerate the BUILD file for the test module using Gazelle. # Also verify -repo_config are generated correctly in gazelle.bash diff --git a/.bcr/presubmit.yml b/.bcr/presubmit.yml index 5ebe41093..e08fd1dff 100644 --- a/.bcr/presubmit.yml +++ b/.bcr/presubmit.yml @@ -1,5 +1,5 @@ bcr_test_module: - module_path: tests/bcr + module_path: tests/bcr/go_mod matrix: platform: - debian10 diff --git a/.gitignore b/.gitignore index bea0af6e4..856a26b60 100644 --- a/.gitignore +++ b/.gitignore @@ -2,11 +2,11 @@ /bazel-bin /bazel-out /bazel-testlogs -/tests/bcr/bazel-bcr -/tests/bcr/bazel-bin -/tests/bcr/bazel-out -/tests/bcr/bazel-testlogs -/tests/bcr/MODULE.bazel.lock +/tests/bcr/*/bazel-go_* +/tests/bcr/*/bazel-bin +/tests/bcr/*/bazel-out +/tests/bcr/*/bazel-testlogs +/tests/bcr/*/MODULE.bazel.lock MODULE.bazel.lock .DS_STORE .ijwb/ diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index e0bd42841..8a61d59b1 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -13,7 +13,7 @@ # limitations under the License. load("//internal:go_repository.bzl", "go_repository") -load(":go_mod.bzl", "deps_from_go_mod", "sums_from_go_mod") +load(":go_mod.bzl", "deps_from_go_mod", "parse_go_work", "sums_from_go_mod", "sums_from_go_work") load( ":default_gazelle_overrides.bzl", "DEFAULT_BUILD_EXTRA_ARGS_BY_PATH", @@ -83,6 +83,12 @@ _GAZELLE_ATTRS = { ), } +def go_work_from_label(module_ctx, go_work_label): + """Loads deps from a go.work file""" + go_work_path = module_ctx.path(go_work_label) + go_work_content = module_ctx.read(go_work_path) + return parse_go_work(go_work_content, go_work_label) + def _fail_on_non_root_overrides(module_ctx, module, tag_class): if module.is_root: return @@ -276,6 +282,41 @@ _go_repository_config = repository_rule( }, ) +def check_for_version_conflict(version, previous, module_tag, module_name_to_go_dot_mod_label, conflict_printer): + """ + Check if duplicate modules have different versions, and fail with a useful error message if they do. + + Args: + version: The version of the module. + previous: The previous module object. + module_tag: The module tag. + module_name_to_go_dot_mod_label: A dictionary mapping module paths to go.mod labels. + conflict_printer: a printer function to use for printing the error message, generally either print or fail. + """ + + if not previous or version == previous.version: + # no previous module, so no possible error OR + # version is the same, skip because we won't error + return + + # When using go.work, duplicate dependency versions are possible. + # This can cause issues, so we fail with a hopefully actionable error. + current_label = module_tag._parent_label + + previous_label = previous.module_tag._parent_label + + corrective_measure = """To correct this: + 1. ensure that '{}' in all go.mod files is the same version. + 2. in the folders where you made changes run: bazel run @rules_go//go -- mod tidy + 3. at the workspace root run: bazel run @rules_go//go -- work sync.""".format(module_tag.path) + + message = """Multiple versions of {} found: + - {} contains: {} + - {} contains: {} +{}""".format(module_tag.path, current_label, module_tag.version, previous_label, previous.module_tag.version, corrective_measure) + + conflict_printer(message) + def _noop(_): pass @@ -344,9 +385,40 @@ def _go_deps_impl(module_ctx): ", ".join([str(tag.go_mod) for tag in module.tags.from_file]), ), ) + additional_module_tags = [] + from_file_tags = [] + module_name_to_go_dot_mod_label = {} + for from_file_tag in module.tags.from_file: - module_path, module_tags_from_go_mod, go_mod_replace_map = deps_from_go_mod(module_ctx, from_file_tag.go_mod) + if bool(from_file_tag.go_work) == bool(from_file_tag.go_mod): + fail("go_deps.from_file tag must have either go_work or go_mod attribute, but not both.") + + if from_file_tag.go_mod: + from_file_tags.append(from_file_tag) + elif from_file_tag.go_work: + if module.is_root != True: + fail("go_deps.from_file(go_work = '{}') tag can only be used from a root module but: '{}' is not a root module.".format(from_file_tag.go_work, module.name)) + + go_work = go_work_from_label(module_ctx, from_file_tag.go_work) + + # this ensures go.work replacements are considered + additional_module_tags += [ + with_replaced_or_new_fields(tag, _is_dev_dependency = False) + for tag in go_work.module_tags + ] + + for entry, new_sum in sums_from_go_work(module_ctx, from_file_tag.go_work).items(): + _safe_insert_sum(sums, entry, new_sum) + + replace_map.update(go_work.replace_map) + from_file_tags = from_file_tags + go_work.from_file_tags + else: + fail("Either \"go_mod\" or \"go_work\" must be specified in \"go_deps.from_file\" tags.") + + for from_file_tag in from_file_tags: + module_path, module_tags_from_go_mod, go_mod_replace_map, module_name = deps_from_go_mod(module_ctx, from_file_tag.go_mod) + module_name_to_go_dot_mod_label[module_name] = from_file_tag.go_mod # Collect the relative path of the root module's go.mod file if it lives in the main # repository. @@ -406,12 +478,11 @@ def _go_deps_impl(module_ctx): # transitive dependencies have also been declared - we may end up # resolving them to higher versions, but only compatible ones. paths = {} + for module_tag in module.tags.module + additional_module_tags: - if module_tag.path in paths: - fail("Duplicate Go module path \"{}\" in module \"{}\".".format(module_tag.path, module.name)) if module_tag.path in bazel_deps: continue - paths[module_tag.path] = None + raw_version = _canonicalize_raw_version(module_tag.version) # For modules imported from a go.sum, we know which ones are direct @@ -427,6 +498,14 @@ def _go_deps_impl(module_ctx): root_module_direct_deps[_repo_name(module_tag.path)] = None version = semver.to_comparable(raw_version) + previous = paths.get(module_tag.path) + + fail_on_version_conflict = any([x.fail_on_version_conflict for x in module.tags.from_file]) + + conflict_printer = fail if fail_on_version_conflict else print + check_for_version_conflict(version, previous, module_tag, module_name_to_go_dot_mod_label, conflict_printer) + paths[module_tag.path] = struct(version = version, module_tag = module_tag) + if module_tag.path not in module_resolutions or version > module_resolutions[module_tag.path].version: module_resolutions[module_tag.path] = struct( repo_name = _repo_name(module_tag.path), @@ -580,13 +659,13 @@ def _get_sum_from_module(path, module, sums): entry = (module.replace, module.raw_version) if entry not in sums: - fail("No sum for {}@{} found".format(path, module.raw_version)) + fail("No sum for {}@{} found. You may need to run: bazel run @rules_go//go -- mod tidy".format(path, module.raw_version)) return sums[entry] def _safe_insert_sum(sums, entry, new_sum): if entry in sums and new_sum != sums[entry]: - fail("Multiple mismatching sums for {}@{} found.".format(entry[0], entry[1])) + fail("Multiple mismatching sums for {}@{} found: {} vs {}".format(entry[0], entry[1], new_sum, sums[entry])) sums[entry] = new_sum def _canonicalize_raw_version(raw_version): @@ -607,7 +686,12 @@ _config_tag = tag_class( _from_file_tag = tag_class( attrs = { - "go_mod": attr.label(mandatory = True), + "go_mod": attr.label(mandatory = False), + "go_work": attr.label(mandatory = False), + "fail_on_version_conflict": attr.bool( + default = True, + doc = "Fail if duplicate modules have different versions", + ), }, ) diff --git a/internal/bzlmod/go_mod.bzl b/internal/bzlmod/go_mod.bzl index 59e134670..9f07ff8cf 100644 --- a/internal/bzlmod/go_mod.bzl +++ b/internal/bzlmod/go_mod.bzl @@ -16,6 +16,91 @@ visibility([ "//tests/bzlmod/...", ]) +def _validate_go_version(path, state, tokens, line_no): + if len(tokens) == 1: + fail("{}:{}: expected another token after 'go'".format(path, line_no)) + if state["go"] != None: + fail("{}:{}: unexpected second 'go' directive".format(path, line_no)) + if len(tokens) > 2: + fail("{}:{}: unexpected token '{}' after '{}'".format(path, line_no, tokens[2], tokens[1])) + +def use_spec_to_label(repo_name, use_directive): + if use_directive.startswith("../") or "/../" in use_directive or use_directive.endswith("/.."): + fail("go.work use directive: '{}' contains '..' which is not currently supported.".format(use_directive)) + + if use_directive.startswith("./"): + use_directive = use_directive[2:] + + if use_directive.endswith("/"): + use_directive = use_directive[:-1] + + return Label("@@{}//{}:go.mod".format(repo_name, use_directive)) + +def parse_go_work(content, go_work_label): + # see: https://go.dev/ref/mod#go-work-file + + # Valid directive values understood by this parser never contain tabs or + # carriage returns, so we can simplify the parsing below by canonicalizing + # whitespace upfront. + content = content.replace("\t", " ").replace("\r", " ") + + state = { + "go": None, + "use": [], + "replace": {}, + } + + current_directive = None + for line_no, line in enumerate(content.splitlines(), 1): + tokens, _ = _tokenize_line(line, go_work_label.name, line_no) + + if not tokens: + continue + + if current_directive: + if tokens[0] == ")": + current_directive = None + elif current_directive == "use": + state["use"].append(tokens[0]) + elif current_directive == "replace": + _parse_replace_directive(state, tokens, go_work_label.name, line_no) + else: + fail("{}:{}: unexpected directive '{}'".format(go_work_label.name, line_no, current_directive)) + elif tokens[0] == "go": + _validate_go_version(go_work_label.name, state, tokens, line_no) + go = tokens[1] + elif tokens[0] == "replace": + if tokens[1] == "(": + current_directive = tokens[0] + continue + else: + _parse_replace_directive(state, tokens[1:], go_work_label.name, line_no) + elif tokens[0] == "use": + if len(tokens) != 2: + fail("{}:{}: expected path or block in 'use' directive".format(go_work_label.name, line_no)) + elif tokens[1] == "(": + current_directive = tokens[0] + continue + else: + state["use"].append(tokens[1]) + else: + fail("{}:{}: unexpected directive '{}'".format(go_work_label.name, line_no, tokens[0])) + + major, minor = go.split(".")[:2] + + go_mods = [use_spec_to_label(go_work_label.workspace_name, use) for use in state["use"]] + from_file_tags = [struct(go_mod = go_mod, _is_dev_dependency = False) for go_mod in go_mods] + + module_tags = [struct(version = mod.version, path = mod.to_path, _parent_label = go_work_label, indirect = False) for mod in state["replace"].values()] + + return struct( + go = (int(major), int(minor)), + from_file_tags = from_file_tags, + replace_map = state["replace"], + module_tags = module_tags, + use = state["use"], + ) + def deps_from_go_mod(module_ctx, go_mod_label): """Loads the entries from a go.mod file. @@ -45,9 +130,10 @@ def deps_from_go_mod(module_ctx, go_mod_label): path = require.path, version = require.version, indirect = require.indirect, + _parent_label = go_mod_label, )) - return go_mod.module, deps, go_mod.replace_map + return go_mod.module, deps, go_mod.replace_map, go_mod.module def parse_go_mod(content, path): # See https://go.dev/ref/mod#go-mod-file. @@ -79,13 +165,8 @@ def parse_go_mod(content, path): # The 'go' directive only has a single-line form and is thus parsed # here rather than in _parse_directive. if tokens[0] == "go": - if len(tokens) == 1: - fail("{}:{}: expected another token after 'go'".format(path, line_no)) - if state["go"] != None: - fail("{}:{}: unexpected second 'go' directive".format(path, line_no)) + _validate_go_version(path, state, tokens, line_no) state["go"] = tokens[1] - if len(tokens) > 2: - fail("{}:{}: unexpected token '{}' after '{}'".format(path, line_no, tokens[2], tokens[1])) if tokens[1] == "(": current_directive = tokens[0] @@ -139,37 +220,42 @@ def _parse_directive(state, directive, tokens, comment, path, line_no): indirect = comment == "indirect", )) elif directive == "replace": - # A replace directive might use a local file path beginning with ./ or ../ - # These are not supported with gazelle~go_deps. - if (len(tokens) == 3 and tokens[2][0] == ".") or (len(tokens) > 3 and tokens[3][0] == "."): - fail("{}:{}: local file path not supported in replace directive: '{}'".format(path, line_no, tokens[2])) - - # replacements key off of the from_path - from_path = tokens[0] - - # pattern: replace from_path => to_path to_version - if len(tokens) == 4 and tokens[1] == "=>": - state["replace"][from_path] = struct( - from_version = None, - to_path = tokens[2], - version = _canonicalize_raw_version(tokens[3]), - ) - # pattern: replace from_path from_version => to_path to_version - elif len(tokens) == 5 and tokens[2] == "=>": - state["replace"][from_path] = struct( - from_version = _canonicalize_raw_version(tokens[1]), - to_path = tokens[3], - version = _canonicalize_raw_version(tokens[4]), - ) - else: - fail( - "{}:{}: replace directive must follow pattern: ".format(path, line_no) + - "'replace from_path from_version => to_path to_version' or " + - "'replace from_path => to_path to_version'" - ) + _parse_replace_directive(state, tokens, path, line_no) # TODO: Handle exclude. +def _parse_replace_directive(state, tokens, path, line_no): + # A replace directive might use a local file path beginning with ./ or ../ + # These are not supported with gazelle~go_deps. + if (len(tokens) == 3 and tokens[2][0] == ".") or (len(tokens) > 3 and tokens[3][0] == "."): + fail("{}:{}: local file path not supported in replace directive: '{}'".format(path, line_no, tokens[2])) + + # replacements key off of the from_path + from_path = tokens[0] + + # pattern: replace from_path => to_path to_version + if len(tokens) == 4 and tokens[1] == "=>": + state["replace"][from_path] = struct( + from_version = None, + to_path = tokens[2], + version = _canonicalize_raw_version(tokens[3]), + ) + # pattern: replace from_path from_version => to_path to_version + + elif len(tokens) == 5 and tokens[2] == "=>": + state["replace"][from_path] = struct( + from_version = _canonicalize_raw_version(tokens[1]), + to_path = tokens[3], + version = _canonicalize_raw_version(tokens[4]), + ) + else: + fail( + "{}:{}: replace directive must follow pattern: ".format(path, line_no) + + "'replace from_path from_version => to_path to_version' or " + + "'replace from_path => to_path to_version'", + "but got: '{} {} {}'".format(*tokens), + ) + def _tokenize_line(line, path, line_no): tokens = [] r = line @@ -240,16 +326,55 @@ def sums_from_go_mod(module_ctx, go_mod_label): """ _check_go_mod_name(go_mod_label.name) - # We go through a Label so that the module extension is restarted if go.sum + return parse_sumfile(module_ctx, go_mod_label, "go.sum") + +def sums_from_go_work(module_ctx, go_work_label): + """Loads the entries from a go.work.sum file given a go.work label. + + Args: + module_ctx: a https://bazel.build/rules/lib/module_ctx object + passed from the MODULE.bazel call. + go_work_label: a Label for a `go.work` file. This label is used + to find the associated `go.work.sum` file. + + Returns: + A Dict[(string, string) -> (string)] is returned where each entry + is defined by a Go Module's sum: + (path, version) -> (sum) + """ + _check_go_work_name(go_work_label.name) + + # next we need to test if the go.work.sum file exists, this is a little tricky so we use an indirect approach: + + # 1. convert go_work_label into a path + go_work_path = module_ctx.path(go_work_label) + + # 2. use the go_work_path to create a path for the heisen go.work.sum file + maybe_go_work_sum_path = go_work_path.dirname.get_child("go.work.sum") + + # 3. check for its existence + if maybe_go_work_sum_path.exists: + return parse_sumfile(module_ctx, go_work_label, "go.work.sum") + else: + # 4. if go.work.sum does not exist, we should watch it in case it appears in the future + if hasattr(module_ctx, "watch"): + # module_ctx.watch_tree is only available in bazel >= 7.1 + module_ctx.watch(maybe_go_work_sum_path) + + # 5. return an empty dict as no sum file was found + return {} + +def parse_sumfile(module_ctx, label, sumfile): + # We go through a Label so that the module extension is restarted if the sumfile # changes. We have to use a canonical label as we may not have visibility - # into the module that provides the go.sum. - go_sum_label = Label("@@{}//{}:{}".format( - go_mod_label.workspace_name, - go_mod_label.package, - "go.sum", + # into the module that provides the sumfile + sum_label = Label("@@{}//{}:{}".format( + label.workspace_name, + label.package, + sumfile, )) - go_sum_content = module_ctx.read(go_sum_label) - return parse_go_sum(go_sum_content) + + return parse_go_sum(module_ctx.read(sum_label)) def parse_go_sum(content): hashes = {} @@ -264,6 +389,10 @@ def _check_go_mod_name(name): if name != "go.mod": fail("go_deps.from_file requires a 'go.mod' file, not '{}'".format(name)) +def _check_go_work_name(name): + if name != "go.work": + fail("go_deps.from_file requires a 'go.work' file, not '{}'".format(name)) + def _canonicalize_raw_version(raw_version): if raw_version.startswith("v"): return raw_version[1:] diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index e2ebfeaf6..17f1aa607 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -3,7 +3,6 @@ load( "gazelle_binary", "gazelle_generation_test", ) - load("//tests:tools.bzl", "get_binary") # Exclude this entire directly from having anything gnerated by Gazelle. That diff --git a/tests/bcr/.bazelignore b/tests/bcr/go_mod/.bazelignore similarity index 100% rename from tests/bcr/.bazelignore rename to tests/bcr/go_mod/.bazelignore diff --git a/tests/bcr/.bazelrc b/tests/bcr/go_mod/.bazelrc similarity index 100% rename from tests/bcr/.bazelrc rename to tests/bcr/go_mod/.bazelrc diff --git a/tests/bcr/.bazelversion b/tests/bcr/go_mod/.bazelversion similarity index 100% rename from tests/bcr/.bazelversion rename to tests/bcr/go_mod/.bazelversion diff --git a/tests/bcr/BUILD.bazel b/tests/bcr/go_mod/BUILD.bazel similarity index 100% rename from tests/bcr/BUILD.bazel rename to tests/bcr/go_mod/BUILD.bazel diff --git a/tests/bcr/MODULE.bazel b/tests/bcr/go_mod/MODULE.bazel similarity index 98% rename from tests/bcr/MODULE.bazel rename to tests/bcr/go_mod/MODULE.bazel index 68d51d810..90572eb69 100644 --- a/tests/bcr/MODULE.bazel +++ b/tests/bcr/go_mod/MODULE.bazel @@ -1,11 +1,11 @@ module( - name = "gazelle_bcr_tests", + name = "gazelle_bcr_go_mod_tests", ) bazel_dep(name = "gazelle", version = "") local_path_override( module_name = "gazelle", - path = "../..", + path = "../../..", ) bazel_dep(name = "test_dep", version = "1.0.0") diff --git a/tests/bcr/WORKSPACE b/tests/bcr/go_mod/WORKSPACE similarity index 100% rename from tests/bcr/WORKSPACE rename to tests/bcr/go_mod/WORKSPACE diff --git a/tests/bcr/WORKSPACE.bzlmod b/tests/bcr/go_mod/WORKSPACE.bzlmod similarity index 100% rename from tests/bcr/WORKSPACE.bzlmod rename to tests/bcr/go_mod/WORKSPACE.bzlmod diff --git a/tests/bcr/go.mod b/tests/bcr/go_mod/go.mod similarity index 92% rename from tests/bcr/go.mod rename to tests/bcr/go_mod/go.mod index d4f1957f5..9becf5d60 100644 --- a/tests/bcr/go.mod +++ b/tests/bcr/go_mod/go.mod @@ -1,5 +1,5 @@ // This will stop go mod from descending into this directory. -module github.com/bazelbuild/bazel-gazelle/tests/bcr +module github.com/bazelbuild/bazel-gazelle/tests/bcr/go_mod go 1.19 diff --git a/tests/bcr/go.sum b/tests/bcr/go_mod/go.sum similarity index 100% rename from tests/bcr/go.sum rename to tests/bcr/go_mod/go.sum diff --git a/tests/bcr/patches/BUILD.bazel b/tests/bcr/go_mod/patches/BUILD.bazel similarity index 100% rename from tests/bcr/patches/BUILD.bazel rename to tests/bcr/go_mod/patches/BUILD.bazel diff --git a/tests/bcr/patches/buildtools.patch b/tests/bcr/go_mod/patches/buildtools.patch similarity index 100% rename from tests/bcr/patches/buildtools.patch rename to tests/bcr/go_mod/patches/buildtools.patch diff --git a/tests/bcr/patches/testify.patch b/tests/bcr/go_mod/patches/testify.patch similarity index 100% rename from tests/bcr/patches/testify.patch rename to tests/bcr/go_mod/patches/testify.patch diff --git a/tests/bcr/pkg/BUILD.bazel b/tests/bcr/go_mod/pkg/BUILD.bazel similarity index 99% rename from tests/bcr/pkg/BUILD.bazel rename to tests/bcr/go_mod/pkg/BUILD.bazel index 383211102..7d6c40305 100644 --- a/tests/bcr/pkg/BUILD.bazel +++ b/tests/bcr/go_mod/pkg/BUILD.bazel @@ -6,7 +6,7 @@ go_library( "platform_lib_unix.go", "platform_lib_windows.go", ], - importpath = "github.com/bazelbuild/bazel-gazelle/tests/bcr/pkg", + importpath = "github.com/bazelbuild/bazel-gazelle/tests/bcr/go_mod/pkg", visibility = ["//visibility:public"], deps = select({ "@my_rules_go//go/platform:aix": [ diff --git a/tests/bcr/pkg/data/BUILD.bazel b/tests/bcr/go_mod/pkg/data/BUILD.bazel similarity index 93% rename from tests/bcr/pkg/data/BUILD.bazel rename to tests/bcr/go_mod/pkg/data/BUILD.bazel index ac9a2c939..115252d98 100644 --- a/tests/bcr/pkg/data/BUILD.bazel +++ b/tests/bcr/go_mod/pkg/data/BUILD.bazel @@ -4,6 +4,6 @@ go_library( name = "data", srcs = ["data.go"], data = ["@bazel_gazelle_go_repository_config//:WORKSPACE"], - importpath = "github.com/bazelbuild/bazel-gazelle/tests/bcr/pkg/data", + importpath = "github.com/bazelbuild/bazel-gazelle/tests/bcr/go_mod/pkg/data", visibility = ["//visibility:public"], ) diff --git a/tests/bcr/pkg/data/data.go b/tests/bcr/go_mod/pkg/data/data.go similarity index 100% rename from tests/bcr/pkg/data/data.go rename to tests/bcr/go_mod/pkg/data/data.go diff --git a/tests/bcr/pkg/pkg_test.go b/tests/bcr/go_mod/pkg/pkg_test.go similarity index 97% rename from tests/bcr/pkg/pkg_test.go rename to tests/bcr/go_mod/pkg/pkg_test.go index a1ae8cdad..bc93a2f90 100644 --- a/tests/bcr/pkg/pkg_test.go +++ b/tests/bcr/go_mod/pkg/pkg_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/DataDog/sketches-go/ddsketch" - "github.com/bazelbuild/bazel-gazelle/tests/bcr/pkg/data" + "github.com/bazelbuild/bazel-gazelle/tests/bcr/go_mod/pkg/data" "github.com/bazelbuild/buildtools/labels" "github.com/bazelbuild/rules_go/go/runfiles" "github.com/bmatcuk/doublestar/v4" diff --git a/tests/bcr/pkg/platform_lib_unix.go b/tests/bcr/go_mod/pkg/platform_lib_unix.go similarity index 100% rename from tests/bcr/pkg/platform_lib_unix.go rename to tests/bcr/go_mod/pkg/platform_lib_unix.go diff --git a/tests/bcr/pkg/platform_lib_windows.go b/tests/bcr/go_mod/pkg/platform_lib_windows.go similarity index 100% rename from tests/bcr/pkg/platform_lib_windows.go rename to tests/bcr/go_mod/pkg/platform_lib_windows.go diff --git a/tests/bcr/proto/BUILD.bazel b/tests/bcr/go_mod/proto/BUILD.bazel similarity index 100% rename from tests/bcr/proto/BUILD.bazel rename to tests/bcr/go_mod/proto/BUILD.bazel diff --git a/tests/bcr/proto/foo.proto b/tests/bcr/go_mod/proto/foo.proto similarity index 100% rename from tests/bcr/proto/foo.proto rename to tests/bcr/go_mod/proto/foo.proto diff --git a/tests/bcr/proto/foo_test.go b/tests/bcr/go_mod/proto/foo_test.go similarity index 100% rename from tests/bcr/proto/foo_test.go rename to tests/bcr/go_mod/proto/foo_test.go diff --git a/tests/bcr/test_dep/BUILD.bazel b/tests/bcr/go_mod/test_dep/BUILD.bazel similarity index 100% rename from tests/bcr/test_dep/BUILD.bazel rename to tests/bcr/go_mod/test_dep/BUILD.bazel diff --git a/tests/bcr/test_dep/MODULE.bazel b/tests/bcr/go_mod/test_dep/MODULE.bazel similarity index 99% rename from tests/bcr/test_dep/MODULE.bazel rename to tests/bcr/go_mod/test_dep/MODULE.bazel index c2278edf2..340ade58b 100644 --- a/tests/bcr/test_dep/MODULE.bazel +++ b/tests/bcr/go_mod/test_dep/MODULE.bazel @@ -28,7 +28,6 @@ go_deps.module_override( ], path = "github.com/stretchr/testify", ) - go_deps.module( indirect = True, path = "gopkg.in/yaml.v3", @@ -36,10 +35,10 @@ go_deps.module( version = "v3.0.1", ) go_deps.gazelle_override( - path = "gopkg.in/yaml.v3", directives = [ "gazelle:go_naming_convention import", ], + path = "gopkg.in/yaml.v3", ) go_deps.module( indirect = True, diff --git a/tests/bcr/test_dep/WORKSPACE b/tests/bcr/go_mod/test_dep/WORKSPACE similarity index 100% rename from tests/bcr/test_dep/WORKSPACE rename to tests/bcr/go_mod/test_dep/WORKSPACE diff --git a/tests/bcr/test_dep/patches/BUILD.bazel b/tests/bcr/go_mod/test_dep/patches/BUILD.bazel similarity index 100% rename from tests/bcr/test_dep/patches/BUILD.bazel rename to tests/bcr/go_mod/test_dep/patches/BUILD.bazel diff --git a/tests/bcr/test_dep/patches/testify.patch b/tests/bcr/go_mod/test_dep/patches/testify.patch similarity index 100% rename from tests/bcr/test_dep/patches/testify.patch rename to tests/bcr/go_mod/test_dep/patches/testify.patch diff --git a/tests/bcr/test_dep/test.go b/tests/bcr/go_mod/test_dep/test.go similarity index 100% rename from tests/bcr/test_dep/test.go rename to tests/bcr/go_mod/test_dep/test.go diff --git a/tests/bcr/tools/BUILD.bazel b/tests/bcr/go_mod/tools/BUILD.bazel similarity index 100% rename from tests/bcr/tools/BUILD.bazel rename to tests/bcr/go_mod/tools/BUILD.bazel diff --git a/tests/bcr/tools/go.mod b/tests/bcr/go_mod/tools/go.mod similarity index 92% rename from tests/bcr/tools/go.mod rename to tests/bcr/go_mod/tools/go.mod index 29249d341..30eec8274 100644 --- a/tests/bcr/tools/go.mod +++ b/tests/bcr/go_mod/tools/go.mod @@ -1,4 +1,4 @@ -module github.com/bazelbuild/bazel-gazelle/tests/bcr/tools +module github.com/bazelbuild/bazel-gazelle/tests/bcr/go_mod/tools go 1.21.0 diff --git a/tests/bcr/tools/go.sum b/tests/bcr/go_mod/tools/go.sum similarity index 100% rename from tests/bcr/tools/go.sum rename to tests/bcr/go_mod/tools/go.sum diff --git a/tests/bcr/tools/tools.go b/tests/bcr/go_mod/tools/tools.go similarity index 100% rename from tests/bcr/tools/tools.go rename to tests/bcr/go_mod/tools/tools.go diff --git a/tests/bcr/go_work/.bazelignore b/tests/bcr/go_work/.bazelignore new file mode 100644 index 000000000..777d432da --- /dev/null +++ b/tests/bcr/go_work/.bazelignore @@ -0,0 +1 @@ +test_dep diff --git a/tests/bcr/go_work/.bazelrc b/tests/bcr/go_work/.bazelrc new file mode 100644 index 000000000..7ccbf79fb --- /dev/null +++ b/tests/bcr/go_work/.bazelrc @@ -0,0 +1,6 @@ +common --enable_bzlmod +common --experimental_isolated_extension_usages +common --lockfile_mode=update +common --enable_platform_specific_config +# Required by abseil-cpp on macOS with Bazel 6. +common:macos --host_cxxopt=-std=c++14 diff --git a/tests/bcr/go_work/.bazelversion b/tests/bcr/go_work/.bazelversion new file mode 100644 index 000000000..a8907c025 --- /dev/null +++ b/tests/bcr/go_work/.bazelversion @@ -0,0 +1 @@ +7.0.2 diff --git a/tests/bcr/go_work/BUILD.bazel b/tests/bcr/go_work/BUILD.bazel new file mode 100644 index 000000000..8b3d89b76 --- /dev/null +++ b/tests/bcr/go_work/BUILD.bazel @@ -0,0 +1,5 @@ +load("@gazelle//:def.bzl", "gazelle") + +# gazelle:go_naming_convention import +# gazelle:go_naming_convention_external import +gazelle(name = "gazelle") diff --git a/tests/bcr/go_work/MODULE.bazel b/tests/bcr/go_work/MODULE.bazel new file mode 100644 index 000000000..b13d9b399 --- /dev/null +++ b/tests/bcr/go_work/MODULE.bazel @@ -0,0 +1,150 @@ +module( + name = "gazelle_bcr_go_work_tests", +) + +bazel_dep(name = "gazelle", version = "") +local_path_override( + module_name = "gazelle", + path = "../../..", +) + +bazel_dep(name = "test_dep", version = "1.0.0") +local_path_override( + module_name = "test_dep", + path = "test_dep", +) + +bazel_dep(name = "protobuf", version = "23.1", repo_name = "my_protobuf") +bazel_dep(name = "rules_go", version = "0.44.0", repo_name = "my_rules_go") +bazel_dep(name = "rules_proto", version = "6.0.0-rc2", repo_name = "my_rules_proto") + +go_sdk = use_extension("@my_rules_go//go:extensions.bzl", "go_sdk") + +# This bazel_dep provides the Go dependency github.com/cloudflare/circl, which requires custom +# patches beyond what Gazelle can generate. +bazel_dep(name = "circl", version = "1.3.7") + +go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") +go_deps.config( + go_env = { + "GOPRIVATE": "example.com/*", + }, +) + +# Validate a full go workspace +go_deps.from_file(go_work = "//:go.work") +go_deps.gazelle_default_attributes( + build_file_generation = "on", + directives = [ + "gazelle:proto disable", + ], +) + +# By defining `gazelle_default_attributes`, we also must individually +# specify certain overrides from internal/bzlmod/default_gazelle_overrides.bzl +# +# "build_file_generation" defaults to "on" because we provided a "gazelle_override" +# (which contains either directives or build extra args). +go_deps.gazelle_override( + directives = [ + "gazelle:build_file_name BUILD.bazel", + "gazelle:build_file_proto_mode disable_global", + ], + path = "github.com/google/safetext", +) +go_deps.gazelle_override( + directives = [ + "gazelle:build_file_name BUILD.bazel", + ], + path = "github.com/envoyproxy/protoc-gen-validate", +) + +# Verify that the gazelle:go_naming_convention directive in an override is +# respected. +go_deps.module( + path = "github.com/stretchr/testify", + sum = "h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=", + version = "v1.8.4", +) +go_deps.gazelle_override( + directives = [ + "gazelle:go_naming_convention go_default_library", + ], + path = "github.com/stretchr/testify", +) + +# Apply a patch to test the `module_override` tags. +go_deps.module_override( + patch_strip = 1, + patches = [ + "//patches:testify.patch", + ], + path = "github.com/stretchr/testify", +) + +# Test an archive override from a known archive. +go_deps.gazelle_override( + directives = [ + "gazelle:go_naming_convention go_default_library", + ], + path = "github.com/bazelbuild/buildtools", +) +go_deps.archive_override( + patch_strip = 1, + patches = [ + "//patches:buildtools.patch", + ], + path = "github.com/bazelbuild/buildtools", + sha256 = "05d7c3d2bd3cc0b02d15672fefa0d6be48c7aebe459c1c99dced7ac5e598508f", + strip_prefix = "buildtools-ae8e3206e815d086269eb208b01f300639a4b194", + urls = [ + "https://github.com/bazelbuild/buildtools/archive/ae8e3206e815d086269eb208b01f300639a4b194.tar.gz", + ], +) + +# Transitive dependencies have to be listed here explicitly. +go_deps.module( + indirect = True, + path = "gopkg.in/yaml.v3", + sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=", + version = "v3.0.1", +) +go_deps.gazelle_override( + directives = [ + # Verify that the build naming convention is picked up by Gazelle when it + # emits references to this repo. + "gazelle:go_naming_convention go_default_library", + ], + path = "gopkg.in/yaml.v3", +) +go_deps.module( + indirect = True, + path = "github.com/davecgh/go-spew", + sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=", + version = "v1.1.1", +) +use_repo( + go_deps, + "com_github_99designs_gqlgen", + "com_github_bazelbuild_buildtools", + "com_github_bmatcuk_doublestar_v4", + "com_github_datadog_sketches_go", + "com_github_envoyproxy_protoc_gen_validate", + "com_github_fmeum_dep_on_gazelle", + "com_github_google_safetext", + "com_github_stretchr_testify", + "org_golang_google_protobuf", + "org_golang_x_sys", + # Only used for testing. + "bazel_gazelle_go_repository_config", +) + +# Use an isolated usage to bring in Go tools from the tools package with their own dependency +# closure. +go_tools = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True) +go_tools.from_file(go_mod = "//tools:go.mod") +use_repo( + go_tools, + buildtools = "com_github_bazelbuild_buildtools", + gqlgen = "com_github_99designs_gqlgen", +) diff --git a/tests/bcr/go_work/WORKSPACE b/tests/bcr/go_work/WORKSPACE new file mode 100644 index 000000000..e69de29bb diff --git a/tests/bcr/go_work/WORKSPACE.bzlmod b/tests/bcr/go_work/WORKSPACE.bzlmod new file mode 100644 index 000000000..e69de29bb diff --git a/tests/bcr/go_work/go.work b/tests/bcr/go_work/go.work new file mode 100644 index 000000000..c4a80cf5b --- /dev/null +++ b/tests/bcr/go_work/go.work @@ -0,0 +1,7 @@ +go 1.21.5 + +use ( + ./pkg + ./tools + ./proto +) diff --git a/tests/bcr/go_work/patches/BUILD.bazel b/tests/bcr/go_work/patches/BUILD.bazel new file mode 100644 index 000000000..e69de29bb diff --git a/tests/bcr/go_work/patches/buildtools.patch b/tests/bcr/go_work/patches/buildtools.patch new file mode 100644 index 000000000..1c75d4e39 --- /dev/null +++ b/tests/bcr/go_work/patches/buildtools.patch @@ -0,0 +1,13 @@ +diff --git a/labels/labels.go b/labels/labels.go +index 7564621..aba07c0 100644 +--- a/labels/labels.go ++++ b/labels/labels.go +@@ -23,6 +23,8 @@ import ( + "strings" + ) + ++const Patched = "hello" ++ + // Label represents a Bazel target label. + type Label struct { + Repository string // Repository of the target, can be empty if the target belongs to the current repository diff --git a/tests/bcr/go_work/patches/testify.patch b/tests/bcr/go_work/patches/testify.patch new file mode 100644 index 000000000..cf1075eeb --- /dev/null +++ b/tests/bcr/go_work/patches/testify.patch @@ -0,0 +1,13 @@ +diff --git a/require/require.go b/require/require.go +index a6e1b7c..bc21879 100644 +--- a/require/require.go ++++ b/require/require.go +@@ -12,6 +12,8 @@ import ( + time "time" + ) + ++const Hello = "hello" ++ + // Condition uses a Comparison to assert a complex condition. + func Condition(t TestingT, comp assert.Comparison, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { diff --git a/tests/bcr/go_work/pkg/BUILD.bazel b/tests/bcr/go_work/pkg/BUILD.bazel new file mode 100644 index 000000000..1e75d93df --- /dev/null +++ b/tests/bcr/go_work/pkg/BUILD.bazel @@ -0,0 +1,65 @@ +load("@my_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "pkg", + srcs = [ + "platform_lib_unix.go", + "platform_lib_windows.go", + ], + importpath = "github.com/bazelbuild/bazel-gazelle/tests/bcr/go_work/pkg", + visibility = ["//visibility:public"], + deps = select({ + "@my_rules_go//go/platform:aix": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:android": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:darwin": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:dragonfly": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:freebsd": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:illumos": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:ios": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:linux": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:netbsd": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:openbsd": [ + "@org_golang_x_sys//unix", + ], + "@my_rules_go//go/platform:solaris": [ + "@org_golang_x_sys//unix", + ], + "//conditions:default": [], + }), +) + +go_test( + name = "pkg_test", + srcs = ["pkg_test.go"], + embed = [":pkg"], + deps = [ + "//pkg/data", + "@circl//dh/x25519", + "@com_github_bazelbuild_buildtools//labels:go_default_library", + "@com_github_bmatcuk_doublestar_v4//:doublestar", + "@com_github_datadog_sketches_go//ddsketch", + "@com_github_envoyproxy_protoc_gen_validate//validate", + "@com_github_fmeum_dep_on_gazelle//:dep_on_gazelle", + "@com_github_google_safetext//yamltemplate", + "@com_github_stretchr_testify//require:go_default_library", + "@my_rules_go//go/runfiles", + ], +) diff --git a/tests/bcr/go_work/pkg/data/BUILD.bazel b/tests/bcr/go_work/pkg/data/BUILD.bazel new file mode 100644 index 000000000..2d507569b --- /dev/null +++ b/tests/bcr/go_work/pkg/data/BUILD.bazel @@ -0,0 +1,9 @@ +load("@my_rules_go//go:def.bzl", "go_library") + +go_library( + name = "data", + srcs = ["data.go"], + data = ["@bazel_gazelle_go_repository_config//:WORKSPACE"], + importpath = "github.com/bazelbuild/bazel-gazelle/tests/bcr/go_work/pkg/data", + visibility = ["//visibility:public"], +) diff --git a/tests/bcr/go_work/pkg/data/data.go b/tests/bcr/go_work/pkg/data/data.go new file mode 100644 index 000000000..300ef2783 --- /dev/null +++ b/tests/bcr/go_work/pkg/data/data.go @@ -0,0 +1,3 @@ +package data + +const RepoConfigRlocationPath = "bazel_gazelle_go_repository_config/WORKSPACE" diff --git a/tests/bcr/go_work/pkg/go.mod b/tests/bcr/go_work/pkg/go.mod new file mode 100644 index 000000000..202fdd260 --- /dev/null +++ b/tests/bcr/go_work/pkg/go.mod @@ -0,0 +1,29 @@ +module github.com/bazelbuild/bazel-gazelle/tests/bcr/go_work/pkg + +go 1.21.5 + +require ( + github.com/DataDog/sketches-go v1.4.4 + github.com/bazelbuild/buildtools v0.0.0-20240207142252-03bf520394af + github.com/bazelbuild/rules_go v0.44.0 + github.com/bmatcuk/doublestar/v4 v4.6.1 + github.com/cloudflare/circl v1.3.7 + github.com/envoyproxy/protoc-gen-validate v1.0.4 + github.com/fmeum/dep_on_gazelle v1.0.0 + github.com/google/safetext v0.0.0-20240104143208-7a7d9b3d812f + github.com/stretchr/testify v1.8.4 + golang.org/x/sys v0.17.0 +) + +require ( + github.com/bazelbuild/bazel-gazelle v0.30.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/pborman/uuid v1.2.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + google.golang.org/protobuf v1.32.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +// Validate go.mod replace directives can be properly used: +replace github.com/bmatcuk/doublestar/v4 => github.com/bmatcuk/doublestar v1.3.4 diff --git a/tests/bcr/go_work/pkg/go.sum b/tests/bcr/go_work/pkg/go.sum new file mode 100644 index 000000000..d3b2c1dc6 --- /dev/null +++ b/tests/bcr/go_work/pkg/go.sum @@ -0,0 +1,119 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/DataDog/sketches-go v1.4.4 h1:dF52vzXRFSPOj2IjXSWLvXq3jubL4CI69kwYjJ1w5Z8= +github.com/DataDog/sketches-go v1.4.4/go.mod h1:XR0ns2RtEEF09mDKXiKZiQg+nfZStrq1ZuL1eezeZe0= +github.com/bazelbuild/bazel-gazelle v0.30.0 h1:q9XLWQSCA5NZPJ98WFqicHkq6fxrDPnfvMO1XycQBMg= +github.com/bazelbuild/bazel-gazelle v0.30.0/go.mod h1:6RxhjM1v/lTpD3JlMpKUCcdus4tvdqsqdfbjYi+czYs= +github.com/bazelbuild/buildtools v0.0.0-20240207142252-03bf520394af h1:wycIfuqZJzkloRT+fcazTM3NjvAMyAi1qC2QXmEZP4s= +github.com/bazelbuild/buildtools v0.0.0-20240207142252-03bf520394af/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo= +github.com/bazelbuild/rules_go v0.45.1 h1:kuiC/mK1SKoZ0lyX5gD9uVBGFWOoXT3I4W/ose6Jouo= +github.com/bazelbuild/rules_go v0.45.1/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs= +github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0= +github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= +github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A= +github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew= +github.com/fmeum/dep_on_gazelle v1.0.0 h1:7gEtQ2CoD77tYca+1iUnKjIBUZ4mX7mZwjdWp3uuN/E= +github.com/fmeum/dep_on_gazelle v1.0.0/go.mod h1:VYCjwfsyRHOJL8oenaEjhIzgM7Oj70iTxgJ2RfXbYr0= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/safetext v0.0.0-20240104143208-7a7d9b3d812f h1:o2yGZLlsOj5H5uvtQNEdi6DeA0GbUP3lm0gWW5RvY0s= +github.com/google/safetext v0.0.0-20240104143208-7a7d9b3d812f/go.mod h1:H3K1Iu/utuCfa10JO+GsmKUYSWi7ug57Rk6GaDRHaaQ= +github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw= +github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +go.starlark.net v0.0.0-20210223155950-e043a3d3c984/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/tests/bcr/go_work/pkg/pkg_test.go b/tests/bcr/go_work/pkg/pkg_test.go new file mode 100644 index 000000000..85227abd6 --- /dev/null +++ b/tests/bcr/go_work/pkg/pkg_test.go @@ -0,0 +1,79 @@ +package pkg + +import ( + "crypto/rand" + "os" + "testing" + + "github.com/DataDog/sketches-go/ddsketch" + "github.com/bazelbuild/bazel-gazelle/tests/bcr/go_work/pkg/data" + "github.com/bazelbuild/buildtools/labels" + "github.com/bazelbuild/rules_go/go/runfiles" + "github.com/bmatcuk/doublestar/v4" + "github.com/cloudflare/circl/dh/x25519" + "github.com/fmeum/dep_on_gazelle" + "github.com/google/safetext/yamltemplate" + "github.com/stretchr/testify/require" + + _ "github.com/envoyproxy/protoc-gen-validate/validate" +) + +func TestReplace(t *testing.T) { + // doublestar.StandardOS does NOT exist in doublestar/v4 + // See: https://pkg.go.dev/github.com/bmatcuk/doublestar#OS + // If we are able to initialize this variable, it validates that the dependency is properly + // being replaced with github.com/bmatcuk/doublestar@v1.3.4 + _ = doublestar.StandardOS +} + +func TestPatch(t *testing.T) { + // a patch is used to add this constant. + require.Equal(t, "hello", require.Hello) +} + +func TestBuildFileGeneration(t *testing.T) { + // github.com/google/safetext@v0.0.0-20220905092116-b49f7bc46da2 requires overwriting the BUILD + // files it provides as well as directives. + yamltemplate.HTMLEscapeString("foo") +} + +func TestGeneratedFilesPreferredOverProtos(t *testing.T) { + _, _ = ddsketch.NewDefaultDDSketch(0.01) +} + +func TestPlatformDependentDep(t *testing.T) { + PlatformDependentFunction() +} + +func TestNoGoRepositoryForRulesGoAndGazelle(t *testing.T) { + path, err := runfiles.Rlocation(data.RepoConfigRlocationPath) + require.NoError(t, err) + config, err := os.ReadFile(path) + require.NoError(t, err) + + content := string(config) + require.NotContains(t, content, "com_github_bazelbuild_rules_go") + require.NotContains(t, content, "com_github_bazelbuild_bazel_gazelle") + require.Contains(t, content, "module_name = \"rules_go\"") + require.Contains(t, content, "module_name = \"gazelle\"") +} + +func TestIndirectlyUseGazelle(t *testing.T) { + dep_on_gazelle.MakeLabel("foo", "bar", "baz") +} + +func TestBazelDepUsedAsGoDep(t *testing.T) { + var public, secret x25519.Key + _, err := rand.Read(secret[:]) + require.NoError(t, err) + x25519.KeyGen(&public, &secret) +} + +func TestArchiveOverrideUsed(t *testing.T) { + label := labels.Parse("@com_github_bazelbuild_buildtools//labels:labels") + require.NotEmpty(t, label) +} + +func TestArchiveOverrideWithPatch(t *testing.T) { + require.Equal(t, labels.Patched, "hello") +} diff --git a/tests/bcr/go_work/pkg/platform_lib_unix.go b/tests/bcr/go_work/pkg/platform_lib_unix.go new file mode 100644 index 000000000..e23e36ac2 --- /dev/null +++ b/tests/bcr/go_work/pkg/platform_lib_unix.go @@ -0,0 +1,10 @@ +//go:build unix + +package pkg + +import "golang.org/x/sys/unix" + +func PlatformDependentFunction() string { + home, _ := unix.Getenv("HOME") + return home +} diff --git a/tests/bcr/go_work/pkg/platform_lib_windows.go b/tests/bcr/go_work/pkg/platform_lib_windows.go new file mode 100644 index 000000000..28034acda --- /dev/null +++ b/tests/bcr/go_work/pkg/platform_lib_windows.go @@ -0,0 +1,7 @@ +//go:build windows + +package pkg + +func PlatformDependentFunction() string { + return "C:\\Users\\gopher" +} diff --git a/tests/bcr/go_work/proto/BUILD.bazel b/tests/bcr/go_work/proto/BUILD.bazel new file mode 100644 index 000000000..d1621cc20 --- /dev/null +++ b/tests/bcr/go_work/proto/BUILD.bazel @@ -0,0 +1,31 @@ +load("@my_rules_proto//proto:defs.bzl", "proto_library") +load("@my_rules_go//go:def.bzl", "go_test") +load("@my_rules_go//proto:def.bzl", "go_proto_library") + +proto_library( + name = "foo_proto", + srcs = ["foo.proto"], + visibility = ["//visibility:public"], + deps = [ + "@my_protobuf//:timestamp_proto", + "@my_protobuf//:type_proto", + ], +) + +go_proto_library( + name = "foo_go_proto", + importpath = "github.com/bazelbuild/bazel-gazelle/tests/bcr/proto/foo", + proto = ":foo_proto", + visibility = ["//visibility:public"], +) + +go_test( + name = "proto_test", + srcs = ["foo_test.go"], + deps = [ + ":foo_go_proto", + "@org_golang_google_protobuf//types/known/sourcecontextpb", + "@org_golang_google_protobuf//types/known/timestamppb", + "@org_golang_google_protobuf//types/known/typepb", + ], +) diff --git a/tests/bcr/go_work/proto/foo.proto b/tests/bcr/go_work/proto/foo.proto new file mode 100644 index 000000000..b4674e4f7 --- /dev/null +++ b/tests/bcr/go_work/proto/foo.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +option go_package = "github.com/bazelbuild/bazel-gazelle/tests/bcr/proto/foo"; + +import "google/protobuf/timestamp.proto"; +import "google/protobuf/type.proto"; + +message Foo { + string name = 1; + google.protobuf.Timestamp last_updated = 2; + google.protobuf.Type type = 3; +} diff --git a/tests/bcr/go_work/proto/foo_test.go b/tests/bcr/go_work/proto/foo_test.go new file mode 100644 index 000000000..ee5bd456b --- /dev/null +++ b/tests/bcr/go_work/proto/foo_test.go @@ -0,0 +1,23 @@ +package proto + +import ( + "testing" + + "github.com/bazelbuild/bazel-gazelle/tests/bcr/proto/foo" + "google.golang.org/protobuf/types/known/sourcecontextpb" + "google.golang.org/protobuf/types/known/timestamppb" + "google.golang.org/protobuf/types/known/typepb" +) + +func TestWellKnownTypes(t *testing.T) { + var foo foo.Foo + foo.Name = "foo" + foo.Type = &typepb.Type{ + Name: "my_type", + SourceContext: &sourcecontextpb.SourceContext{}, + } + foo.LastUpdated = ×tamppb.Timestamp{ + Seconds: 12345, + Nanos: 67890, + } +} diff --git a/tests/bcr/go_work/proto/go.mod b/tests/bcr/go_work/proto/go.mod new file mode 100644 index 000000000..344688491 --- /dev/null +++ b/tests/bcr/go_work/proto/go.mod @@ -0,0 +1,2 @@ +module github.com/bazelbuild/bazel-gazelle/tests/bcr/go_work/proto +go 1.21.5 diff --git a/tests/bcr/go_work/test_dep/BUILD.bazel b/tests/bcr/go_work/test_dep/BUILD.bazel new file mode 100644 index 000000000..a4ab157c1 --- /dev/null +++ b/tests/bcr/go_work/test_dep/BUILD.bazel @@ -0,0 +1,8 @@ +load("@rules_go//go:def.bzl", "go_test") + +go_test( + name = "test", + srcs = ["test.go"], + visibility = ["//visibility:public"], + deps = ["@com_github_stretchr_testify//require"], +) diff --git a/tests/bcr/go_work/test_dep/MODULE.bazel b/tests/bcr/go_work/test_dep/MODULE.bazel new file mode 100644 index 000000000..340ade58b --- /dev/null +++ b/tests/bcr/go_work/test_dep/MODULE.bazel @@ -0,0 +1,49 @@ +module( + name = "test_dep", + version = "1.0.0", +) + +bazel_dep(name = "rules_go", version = "0.41.0") +bazel_dep(name = "gazelle", version = "0.31.0") + +# An isolated extension usage does not share any tags with any other usage and results in a +# completely separate set of repos. We explicitly use different directives and patches here to +# verify that they do not interfere with those of the root test module. +go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True) +go_deps.module( + path = "github.com/stretchr/testify", + sum = "h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=", + version = "v1.8.0", +) +go_deps.gazelle_override( + directives = [ + "gazelle:go_naming_convention import", + ], + path = "github.com/stretchr/testify", +) +go_deps.module_override( + patch_strip = 1, + patches = [ + "//patches:testify.patch", + ], + path = "github.com/stretchr/testify", +) +go_deps.module( + indirect = True, + path = "gopkg.in/yaml.v3", + sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=", + version = "v3.0.1", +) +go_deps.gazelle_override( + directives = [ + "gazelle:go_naming_convention import", + ], + path = "gopkg.in/yaml.v3", +) +go_deps.module( + indirect = True, + path = "github.com/davecgh/go-spew", + sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=", + version = "v1.1.1", +) +use_repo(go_deps, "com_github_stretchr_testify") diff --git a/tests/bcr/go_work/test_dep/WORKSPACE b/tests/bcr/go_work/test_dep/WORKSPACE new file mode 100644 index 000000000..e69de29bb diff --git a/tests/bcr/go_work/test_dep/go.mod b/tests/bcr/go_work/test_dep/go.mod new file mode 100644 index 000000000..389c77c0c --- /dev/null +++ b/tests/bcr/go_work/test_dep/go.mod @@ -0,0 +1,11 @@ +module github.com/bazelbuild/bazel-gazelle/tests/bcr/go_work/test_dep + +go 1.21.5 + +require github.com/stretchr/testify v1.8.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/tests/bcr/go_work/test_dep/go.sum b/tests/bcr/go_work/test_dep/go.sum new file mode 100644 index 000000000..fa4b6e682 --- /dev/null +++ b/tests/bcr/go_work/test_dep/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tests/bcr/go_work/test_dep/patches/BUILD.bazel b/tests/bcr/go_work/test_dep/patches/BUILD.bazel new file mode 100644 index 000000000..e69de29bb diff --git a/tests/bcr/go_work/test_dep/patches/testify.patch b/tests/bcr/go_work/test_dep/patches/testify.patch new file mode 100644 index 000000000..4c8db505e --- /dev/null +++ b/tests/bcr/go_work/test_dep/patches/testify.patch @@ -0,0 +1,13 @@ +diff --git a/require/require.go b/require/require.go +index a6e1b7c..bc21879 100644 +--- a/require/require.go ++++ b/require/require.go +@@ -12,6 +12,8 @@ import ( + time "time" + ) + ++const Hello = "not_hello" ++ + // Condition uses a Comparison to assert a complex condition. + func Condition(t TestingT, comp assert.Comparison, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { diff --git a/tests/bcr/go_work/test_dep/test.go b/tests/bcr/go_work/test_dep/test.go new file mode 100644 index 000000000..586b5deac --- /dev/null +++ b/tests/bcr/go_work/test_dep/test.go @@ -0,0 +1,13 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPatch(t *testing.T) { + // A patch is used to add this constant with a value that differs from that patched into the + // root module's version of this dep. + require.Equal(t, "not_hello", require.Hello) +} diff --git a/tests/bcr/go_work/tools/BUILD.bazel b/tests/bcr/go_work/tools/BUILD.bazel new file mode 100644 index 000000000..dd60391b6 --- /dev/null +++ b/tests/bcr/go_work/tools/BUILD.bazel @@ -0,0 +1,10 @@ +# Verify that the tools builds. +alias( + name = "gqlgen", + actual = "@gqlgen", +) + +alias( + name = "buildifier", + actual = "@buildtools//buildifier", +) diff --git a/tests/bcr/go_work/tools/go.mod b/tests/bcr/go_work/tools/go.mod new file mode 100644 index 000000000..4cc88570d --- /dev/null +++ b/tests/bcr/go_work/tools/go.mod @@ -0,0 +1,26 @@ +module github.com/bazelbuild/bazel-gazelle/tests/bcr/go_work/tools + +go 1.21.0 + +require ( + github.com/99designs/gqlgen v0.17.40 + github.com/bazelbuild/buildtools v0.0.0-20240207142252-03bf520394af +) + +require ( + github.com/agnivade/levenshtein v1.1.1 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/golang/protobuf v1.5.0 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/sosodev/duration v1.1.0 // indirect + github.com/urfave/cli/v2 v2.25.5 // indirect + github.com/vektah/gqlparser/v2 v2.5.10 // indirect + github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect + golang.org/x/mod v0.10.0 // indirect + golang.org/x/sys v0.17.0 // indirect + golang.org/x/text v0.9.0 // indirect + golang.org/x/tools v0.9.3 // indirect + google.golang.org/protobuf v1.32.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/tests/bcr/go_work/tools/go.sum b/tests/bcr/go_work/tools/go.sum new file mode 100644 index 000000000..589dc7022 --- /dev/null +++ b/tests/bcr/go_work/tools/go.sum @@ -0,0 +1,134 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/99designs/gqlgen v0.17.40 h1:/l8JcEVQ93wqIfmH9VS1jsAkwm6eAF1NwQn3N+SDqBY= +github.com/99designs/gqlgen v0.17.40/go.mod h1:b62q1USk82GYIVjC60h02YguAZLqYZtvWml8KkhJps4= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8= +github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo= +github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= +github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= +github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q= +github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE= +github.com/bazelbuild/buildtools v0.0.0-20240207142252-03bf520394af h1:wycIfuqZJzkloRT+fcazTM3NjvAMyAi1qC2QXmEZP4s= +github.com/bazelbuild/buildtools v0.0.0-20240207142252-03bf520394af/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g= +github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/golang-lru/v2 v2.0.3 h1:kmRrRLlInXvng0SmLxmQpQkpbYAvcXm7NPDrgxJa9mE= +github.com/hashicorp/golang-lru/v2 v2.0.3/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= +github.com/sosodev/duration v1.1.0 h1:kQcaiGbJaIsRqgQy7VGlZrVw1giWO+lDoX3MCPnpVO4= +github.com/sosodev/duration v1.1.0/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/urfave/cli/v2 v2.25.5 h1:d0NIAyhh5shGscroL7ek/Ya9QYQE0KNabJgiUinIQkc= +github.com/urfave/cli/v2 v2.25.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= +github.com/vektah/gqlparser/v2 v2.5.10 h1:6zSM4azXC9u4Nxy5YmdmGu4uKamfwsdKTwp5zsEealU= +github.com/vektah/gqlparser/v2 v2.5.10/go.mod h1:1rCcfwB2ekJofmluGWXMSEnPMZgbxzwj6FaZ/4OT8Cc= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +go.starlark.net v0.0.0-20210223155950-e043a3d3c984/go.mod h1:t3mmBBPzAVvK0L0n1drDmrQsJ8FoIx4INCqVMTr/Zo0= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= +golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM= +golang.org/x/tools v0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/tests/bcr/go_work/tools/tools.go b/tests/bcr/go_work/tools/tools.go new file mode 100644 index 000000000..c6650a8b6 --- /dev/null +++ b/tests/bcr/go_work/tools/tools.go @@ -0,0 +1,12 @@ +//go:build tools + +// Follows the best practice from https://github.com/golang/go/issues/25922#issuecomment-1038394599 +// to keep Go tools referenced in go.mod in a way that doesn't have `go mod tidy` remove them. + +package tools + +import ( + _ "github.com/99designs/gqlgen" + _ "github.com/99designs/gqlgen/graphql/introspection" + _ "github.com/bazelbuild/buildtools/buildifier" +) diff --git a/tests/bzlmod/go_mod_test.bzl b/tests/bzlmod/go_mod_test.bzl index c4287074a..e1abba0f9 100644 --- a/tests/bzlmod/go_mod_test.bzl +++ b/tests/bzlmod/go_mod_test.bzl @@ -1,5 +1,5 @@ load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") -load("//internal/bzlmod:go_mod.bzl", "parse_go_mod", "parse_go_sum") +load("//internal/bzlmod:go_mod.bzl", "parse_go_mod", "parse_go_sum", "parse_go_work", "use_spec_to_label") _GO_MOD_CONTENT = """ go 1.18 @@ -64,6 +64,17 @@ _EXPECTED_GO_MOD_21_PARSE_RESULT = struct( require = (), ) +def _use_spec_to_label_test_impl(ctx): + env = unittest.begin(ctx) + + asserts.equals(env, Label("@@org_example//go_mod_one:go.mod"), use_spec_to_label("org_example", "./go_mod_one")) + asserts.equals(env, Label("@@org_example//go_mod_one:go.mod"), use_spec_to_label("org_example", "./go_mod_one/")) + asserts.equals(env, Label("@@org_example//bar/go_mod_one:go.mod"), use_spec_to_label("org_example", "./bar/go_mod_one")) + + return unittest.end(env) + +use_spec_test = unittest.make(_use_spec_to_label_test_impl) + def _go_mod_21_test_impl(ctx): env = unittest.begin(ctx) asserts.equals(env, _EXPECTED_GO_MOD_21_PARSE_RESULT, parse_go_mod(_GO_MOD_21_CONTENT, "/go.mod")) @@ -91,10 +102,52 @@ def _go_sum_test_impl(ctx): go_sum_test = unittest.make(_go_sum_test_impl) +_GO_WORK_CONTENT = """go 1.18 +use ./go_mod_one +use ( + ./foo/go_mod_two + ./bar/baz/go_mod_three +) + +replace github.com/go-fsnotify/fsnotify => github.com/fsnotify/fsnotify v1.4.2 +replace github.com/bmatcuk/doublestar/v4 v4.0.2 => github.com/bmatcuk/doublestar/v4 v4.0.3 +""" + +_EXPECTED_GO_WORK_PARSE_RESULT = struct( + go = (1, 18), + from_file_tags = [ + struct(_is_dev_dependency = False, go_mod = Label("//go_mod_one:go.mod")), + struct(_is_dev_dependency = False, go_mod = Label("//foo/go_mod_two:go.mod")), + struct(_is_dev_dependency = False, go_mod = Label("//bar/baz/go_mod_three:go.mod")), + ], + module_tags = [ + struct(indirect = False, _parent_label = Label("//:go.work"), path = "github.com/fsnotify/fsnotify", version = "1.4.2"), + struct(indirect = False, _parent_label = Label("//:go.work"), path = "github.com/bmatcuk/doublestar/v4", version = "4.0.3"), + ], + replace_map = { + "github.com/go-fsnotify/fsnotify": struct(from_version = None, to_path = "github.com/fsnotify/fsnotify", version = "1.4.2"), + "github.com/bmatcuk/doublestar/v4": struct(from_version = "4.0.2", to_path = "github.com/bmatcuk/doublestar/v4", version = "4.0.3"), + }, + use = [ + "./go_mod_one", + "./foo/go_mod_two", + "./bar/baz/go_mod_three", + ], +) + +def _go_work_test_impl(ctx): + env = unittest.begin(ctx) + asserts.equals(env, _EXPECTED_GO_WORK_PARSE_RESULT, parse_go_work(_GO_WORK_CONTENT, Label("@@//:go.work"))) + return unittest.end(env) + +go_work_test = unittest.make(_go_work_test_impl) + def go_mod_test_suite(name): unittest.suite( name, go_mod_test, go_mod_21_test, go_sum_test, + go_work_test, + use_spec_test, ) diff --git a/tests/bzlmod/utils_test.bzl b/tests/bzlmod/utils_test.bzl index 99239822f..87cd9f98b 100644 --- a/tests/bzlmod/utils_test.bzl +++ b/tests/bzlmod/utils_test.bzl @@ -11,7 +11,7 @@ _EXPECT_REPLACED_STRUCT = struct( direct = True, path = "github.com/bazelbuild/buildtools", replace = "path/to/add/replace", - version = "v1.2.2" + version = "v1.2.2", ) def _with_replaced_or_new_fields_test_impl(ctx):