From 66f4ab2be323119488a5671142790381f193a25d Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Thu, 23 Feb 2023 15:56:42 +0100 Subject: [PATCH] Add README.md to explain the issue --- README.md | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..551a82d --- /dev/null +++ b/README.md @@ -0,0 +1,109 @@ +# Undetected cyclic dependency between module extensions + +This repository illustrates an issue where Bazel does not detect a cyclic +dependency between imported module extensions and instead silently disregards +tags on the affected extension. + +## Expected behavior + +You can observe the expected behavior with the following command: + +``` +$ (cd module_b; bazel fetch @extension_2//:defs.bzl) +DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_b TAG module_b_tag_1 +DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_b TAG module_b_tag_2 +DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_a TAG module_a_tag_1 +DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_a TAG module_a_tag_2 +DEBUG: .../module_a~override/extension_2.bzl:5:10: extension_2 found hello from extension_1, 4 tags +``` + +Note that 4 tags of `extension_1` are reported. + +## Faulty behavior + +You can observe the faulty behavior by changing the extension imports in +`module_b` as follows: + +```diff +diff --git a/module_b/MODULE.bazel b/module_b/MODULE.bazel +index 16a8f90..8f57311 100644 +--- a/module_b/MODULE.bazel ++++ b/module_b/MODULE.bazel +@@ -3,15 +3,15 @@ module(name = "module_b") + bazel_dep(name = "module_a") + local_path_override(module_name = "module_a", path = "../module_a") + +-# extension_1 = use_extension("@module_a//:extensions.bzl", "extension_1") +-# extension_2 = use_extension("@module_a//:extensions.bzl", "extension_2") ++extension_1 = use_extension("@module_a//:extensions.bzl", "extension_1") ++extension_2 = use_extension("@module_a//:extensions.bzl", "extension_2") + # $ (cd module_b; bazel fetch @extension_2//:defs.bzl) + # DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_a TAG module_a_tag_1 + # DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_a TAG module_a_tag_2 + # DEBUG: .../module_a~override/extension_2.bzl:5:10: extension_2 found hello from extension_1, 2 tags + +-extension_1 = use_extension("@module_a//:extension_1.bzl", "extension_1") +-extension_2 = use_extension("@module_a//:extension_2.bzl", "extension_2") ++# extension_1 = use_extension("@module_a//:extension_1.bzl", "extension_1") ++# extension_2 = use_extension("@module_a//:extension_2.bzl", "extension_2") + # $ (cd module_b; bazel fetch @extension_2//:defs.bzl) + # DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_b TAG module_b_tag_1 + # DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_b TAG module_b_tag_2 +``` + +And then executing the following command: + +``` +$ (cd module_b; bazel fetch @extension_2//:defs.bzl) +DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_a TAG module_a_tag_1 +DEBUG: .../module_a~override/extension_1.bzl:7:18: MODULE module_a TAG module_a_tag_2 +DEBUG: .../module_a~override/extension_2.bzl:5:10: extension_2 found hello from extension_1, 2 tags +``` + +Note that no only 2 tags of `extension_1` are reported. The tags defined in +`module_b` are lost. + +## For reference + +Bazel does detect the cyclic dependency from the context of `module_a` which +defines the module extensions `extension_1` and `extension_2`. + +First modify the imports in `module_a` as follows: + +```diff +diff --git a/module_a/MODULE.bazel b/module_a/MODULE.bazel +index 5ff4cb7..11259b0 100644 +--- a/module_a/MODULE.bazel ++++ b/module_a/MODULE.bazel +@@ -1,6 +1,6 @@ + module(name = "module_a") + +-# extension_1 = use_extension("//:extensions.bzl", "extension_1") ++extension_1 = use_extension("//:extensions.bzl", "extension_1") + # ``` + # $ (cd module_a; bazel fetch @extension_1//:defs.bzl) + # ERROR: Circular definition of repositories generated by module extensions and/or .bzl files: +@@ -13,7 +13,7 @@ module(name = "module_a") + # ERROR: cycles detected during target parsing + # ``` + +-extension_1 = use_extension("//:extension_1.bzl", "extension_1") ++# extension_1 = use_extension("//:extension_1.bzl", "extension_1") + + extension_1.tag(name = "module_a_tag_1") + extension_1.tag(name = "module_a_tag_2") +``` + +Then execute the following command to trigger the error: + +``` +$ (cd module_a; bazel fetch @extension_1//:defs.bzl) +ERROR: Circular definition of repositories generated by module extensions and/or .bzl files: +.-> @_main~extension_1~extension_1 +| extension 'extension_1' defined in //:extensions.bzl +| //:extensions.bzl +| //:extension_2.bzl +| @_main~extension_1~extension_1//:defs.bzl +`-- @_main~extension_1~extension_1 +ERROR: cycles detected during target parsing +```