Repository: conan-io/conan
Pull request #12543 (2022-11-15 11:39:08)

[BazelDeps][bugfix] Reading `dependencies.direct_host`
Changelog: Bugfix: Not including build-context dependencies in Bazel BUILD file
Docs: https://github.com/conan-io/docs/pull/XXXX
Closes: https://github.com/conan-io/conan/issues/12444
Closes: https://github.com/conan-io/conan/issues/12236


Note: this PR should supersede https://github.com/conan-io/conan/pull/12445


Patch:
diff --git a/conan/tools/env/environment.py b/conan/tools/env/environment.py
index 4d43ed19a..a4b96ea9f 100644
--- a/conan/tools/env/environment.py
+++ b/conan/tools/env/environment.py
@@ -251,16 +251,34 @@ class EnvVars:
     def keys(self):
         return self._values.keys()
 
-    def get(self, name, default=None):
+    def get(self, name, default=None, variable_reference=None):
+        """
+        :param name: The name of the environment variable
+        :param default: The returned value if the variable doesn't exist, by default None
+        :param variable_reference: if specified, use a variable reference instead of the
+        pre-existing value of environment variable, where {name} can be used to refer to the
+        name of the variable.
+        """
         v = self._values.get(name)
         if v is None:
             return default
-        return v.get_value(self._subsystem, self._pathsep)
+        if variable_reference:
+            return v.get_str(variable_reference, self._subsystem, self._pathsep)
+        else:
+            return v.get_value(self._subsystem, self._pathsep)
 
-    def items(self):
-        """returns {str: str} (varname: value)"""
-        return {k: v.get_value(self._subsystem, self._pathsep)
-                for k, v in self._values.items()}.items()
+    def items(self, variable_reference=None):
+        """returns {str: str} (varname: value)
+         :param variable_reference: if specified, use a variable reference instead of the
+        pre-existing value of environment variable, where {name} can be used to refer to the
+        name of the variable.
+        """
+        if variable_reference:
+            return {k: v.get_str(variable_reference, self._subsystem, self._pathsep)
+                    for k, v in self._values.items()}.items()
+        else:
+            return {k: v.get_value(self._subsystem, self._pathsep)
+                    for k, v in self._values.items()}.items()
 
     @contextmanager
     def apply(self):
diff --git a/conan/tools/google/bazeldeps.py b/conan/tools/google/bazeldeps.py
index c6966c043..4436993de 100644
--- a/conan/tools/google/bazeldeps.py
+++ b/conan/tools/google/bazeldeps.py
@@ -146,7 +146,7 @@ class BazelDeps(object):
         lib_dir = 'lib'
 
         dependencies = []
-        for req, dep in dependency.dependencies.items():
+        for dep in dependency.dependencies.direct_host.values():
             dependencies.append(dep.ref.name)
 
         shared_library = dependency.options.get_safe("shared") if dependency.options else False


Test patch:
diff --git a/conans/test/integration/toolchains/google/test_bazel.py b/conans/test/integration/toolchains/google/test_bazel.py
index 6140881ff..aab061406 100644
--- a/conans/test/integration/toolchains/google/test_bazel.py
+++ b/conans/test/integration/toolchains/google/test_bazel.py
@@ -76,3 +76,48 @@ def test_bazel_exclude_folders():
     c.run("install dep/0.1@ -g BazelDeps")
     build_file = c.load("dep/BUILD")
     assert 'static_library = "lib/libmymath.a"' in build_file
+
+
+def test_bazeldeps_and_build_requires():
+    """
+    Testing that direct build requires are not included in dependencies BUILD files
+
+    Issues related:
+        * https://github.com/conan-io/conan/issues/12444
+        * https://github.com/conan-io/conan/issues/12236
+    """
+    c = TestClient()
+    tool = textwrap.dedent("""
+        import os
+        from conan import ConanFile
+
+        class ToolConanfile(ConanFile):
+            name = "tool"
+            version = "0.1"
+
+            def package_info(self):
+                self.cpp_info.libs = ["mymath"]
+        """)
+    c.save({"tool/conanfile.py": tool})
+    c.run("create tool")
+    dep = textwrap.dedent("""
+        import os
+        from conan import ConanFile
+        from conan.tools.files import save
+        class ExampleConanIntegration(ConanFile):
+            name = "dep"
+            version = "0.1"
+
+            def build_requirements(self):
+                self.build_requires("tool/0.1")
+            def package(self):
+                save(self, os.path.join(self.package_folder, "lib", "mymath", "otherfile.a"), "")
+                save(self, os.path.join(self.package_folder, "lib", "libmymath.a"), "")
+            def package_info(self):
+                self.cpp_info.libs = ["mymath"]
+        """)
+    c.save({"dep/conanfile.py": dep})
+    c.run("export dep")
+    c.run("install dep/0.1@ -g BazelDeps --build=missing")
+    build_file = c.load("dep/BUILD")
+    assert '@tool' not in build_file
diff --git a/conans/test/unittests/tools/env/test_env.py b/conans/test/unittests/tools/env/test_env.py
index 0545fd8ed..fb40a69be 100644
--- a/conans/test/unittests/tools/env/test_env.py
+++ b/conans/test/unittests/tools/env/test_env.py
@@ -410,3 +410,14 @@ class TestProfileEnvRoundTrip:
             MyPath1=+(path)/my/path12
             MyPath1+=(path)/my/path11
             """)
+
+
+def test_custom_placeholder():
+    # https://github.com/conan-io/conan/issues/12427
+    env = Environment()
+    env.append_path("MyVar", "MyValue")
+    env = env.vars(ConanFileMock())
+    assert env.get("MyVar", variable_reference="$penv{{{name}}}") == \
+           f"$penv{{MyVar}}{os.pathsep}MyValue"
+    items = {k: v for k, v in env.items(variable_reference="$penv{{{name}}}")}
+    assert  items == {"MyVar": f"$penv{{MyVar}}{os.pathsep}MyValue"}