Repository: conan-io/conan
Pull request #17408 (2024-12-04 00:16:41)

Fix user/channel when searching patterns in a local-recipes-index
Changelog: Fix: Fix user/channel when searching patterns in a local-recipes-index
Docs: Omit

Some minor edits to the code still need to be made, but I think this is a better approach than the current one

Closes https://github.com/conan-io/conan/issues/17403


Patch:
diff --git a/conans/client/rest_client_local_recipe_index.py b/conans/client/rest_client_local_recipe_index.py
index 8615781bd..876b00c21 100644
--- a/conans/client/rest_client_local_recipe_index.py
+++ b/conans/client/rest_client_local_recipe_index.py
@@ -200,6 +200,16 @@ class _LocalRecipesIndexLayout:
         ret = []
         excluded = set()
 
+        original_pattern = pattern
+        try:
+            pattern_ref = RecipeReference.loads(pattern)
+            # We don't care about user/channel for now, we'll check for those below,
+            # just add all candidates for now
+            pattern = f"{pattern_ref.name}/{pattern_ref.version}"
+        except:
+            # pattern = pattern
+            pass
+
         loader = ConanFileLoader(None)
         for r in recipes:
             if r.startswith("."):
@@ -232,7 +242,9 @@ class _LocalRecipesIndexLayout:
                     ref.channel = recipe.channel
                 except Exception as e:
                     ConanOutput().warning(f"Couldn't load recipe {conanfile}: {e}")
-                ret.append(ref)
+                if ref.matches(original_pattern, None):
+                    # Check again the pattern with the user/channel
+                    ret.append(ref)
         if excluded:
             ConanOutput().warning(f"Excluding recipes not Conan 2.0 ready: {', '.join(excluded)}")
         return ret
diff --git a/conans/model/dependencies.py b/conans/model/dependencies.py
index 82e9a0e95..443eeee54 100644
--- a/conans/model/dependencies.py
+++ b/conans/model/dependencies.py
@@ -92,15 +92,19 @@ class ConanFileDependencies(UserRequirementsDict):
         d = OrderedDict((require, ConanFileInterface(transitive.node.conanfile))
                         for require, transitive in node.transitive_deps.items())
         if node.replaced_requires:
+            cant_be_removed = set()
             for old_req, new_req in node.replaced_requires.items():
                 # Two different replaced_requires can point to the same real requirement
                 existing = d[new_req]
                 added_req = new_req.copy_requirement()
                 added_req.ref = RecipeReference.loads(old_req)
                 d[added_req] = existing
+                if new_req.ref.name == added_req.ref.name:
+                    cant_be_removed.add(new_req)
             # Now remove the replaced from dependencies dict
             for new_req in node.replaced_requires.values():
-                d.pop(new_req, None)
+                if new_req not in cant_be_removed:
+                    d.pop(new_req, None)
         return ConanFileDependencies(d)
 
     def filter(self, require_filter, remove_system=True):


Test patch:
diff --git a/.github/workflows/linux-tests.yml b/.github/workflows/linux-tests.yml
index fb229df97..6fa3f8606 100644
--- a/.github/workflows/linux-tests.yml
+++ b/.github/workflows/linux-tests.yml
@@ -8,6 +8,7 @@ on:
   pull_request:
     branches:
       - '*'
+      - 'release/*'
   workflow_dispatch:
 
 concurrency:
@@ -69,7 +70,7 @@ jobs:
       matrix:
         python-version: ['3.12.3', '3.9.2', '3.8.6', '3.6.15']
         test-type: ['unittests', 'integration', 'functional']
-    name: Conan ${{ matrix.test-type }} (${{ matrix.python-version }}) 
+    name: Conan ${{ matrix.test-type }} (${{ matrix.python-version }})
     steps:
       - name: Checkout code
         uses: actions/checkout@v4
diff --git a/.github/workflows/osx-tests.yml b/.github/workflows/osx-tests.yml
index f54c904f9..550bad222 100644
--- a/.github/workflows/osx-tests.yml
+++ b/.github/workflows/osx-tests.yml
@@ -8,6 +8,7 @@ on:
   pull_request:
     branches:
       - '*'
+      - 'release/*'
 
 concurrency:
   group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
diff --git a/.github/workflows/win-tests.yml b/.github/workflows/win-tests.yml
index 6ddbf7678..044af919d 100644
--- a/.github/workflows/win-tests.yml
+++ b/.github/workflows/win-tests.yml
@@ -8,6 +8,7 @@ on:
   pull_request:
     branches:
       - '*'
+      - 'release/*'
   workflow_dispatch:
 
 concurrency:
diff --git a/test/integration/graph/test_replace_requires.py b/test/integration/graph/test_replace_requires.py
index 7f9d4bdd4..affa15e94 100644
--- a/test/integration/graph/test_replace_requires.py
+++ b/test/integration/graph/test_replace_requires.py
@@ -36,6 +36,8 @@ from conan.test.utils.tools import TestClient
     ("dep/[>=1.0 <2]", "dep/2.1", "dep/2.1@system", "dep/1.1"),
     # PATTERN - PATTERN REPLACE
     ("dep/[>=1.3 <2]", "dep/*", "dep/[>=1.0 <1.9]", "dep/1.1"),
+    # DIRECT REPLACE OF PINNED VERSIONS
+    ("dep/1.3", "dep/1.3", "dep/1.5", "dep/1.5"),
 ])
 @pytest.mark.parametrize("tool_require", [False, True])
 class TestReplaceRequires:
@@ -160,14 +162,16 @@ def test_replace_requires_test_requires():
     assert "gtest/0.1: gtest/0.2" in c.out  # The replacement happens
 
 
-def test_replace_requires_consumer_references():
+# We test even replacing by itself, not great, but shouldn't crash
+@pytest.mark.parametrize("name, version", [("zlib", "0.1"), ("zlib", "0.2"), ("zlib-ng", "0.1")])
+def test_replace_requires_consumer_references(name, version):
     c = TestClient()
     # IMPORTANT: The replacement package must be target-compatible
-    zlib_ng = textwrap.dedent("""
+    dep = textwrap.dedent(f"""
         from conan import ConanFile
         class ZlibNG(ConanFile):
-            name = "zlib-ng"
-            version = "0.1"
+            name = "{name}"
+            version = "{version}"
             def package_info(self):
                 self.cpp_info.set_property("cmake_file_name", "ZLIB")
                 self.cpp_info.set_property("cmake_target_name", "ZLIB::ZLIB")
@@ -189,14 +193,14 @@ def test_replace_requires_consumer_references():
                 self.output.info(f"DEP ZLIB package_info: {self.dependencies['zlib'].ref.name}!")
                 self.cpp_info.requires = ["zlib::zlib"]
         """)
-    c.save({"zlibng/conanfile.py": zlib_ng,
+    c.save({"dep/conanfile.py": dep,
             "app/conanfile.py": conanfile,
-            "profile": "[replace_requires]\nzlib/0.1: zlib-ng/0.1"})
-    c.run("create zlibng")
+            "profile": f"[replace_requires]\nzlib/0.1: {name}/{version}"})
+    c.run("create dep")
     c.run("build app -pr=profile")
-    assert "zlib/0.1: zlib-ng/0.1" in c.out
-    assert "conanfile.py (app/0.1): DEP ZLIB generate: zlib-ng!" in c.out
-    assert "conanfile.py (app/0.1): DEP ZLIB build: zlib-ng!" in c.out
+    assert f"zlib/0.1: {name}/{version}" in c.out
+    assert f"conanfile.py (app/0.1): DEP ZLIB generate: {name}!" in c.out
+    assert f"conanfile.py (app/0.1): DEP ZLIB build: {name}!" in c.out
     # Check generated CMake code. If the targets are NOT compatible, then the replacement
     # Cannot happen
     assert "find_package(ZLIB)" in c.out
@@ -204,9 +208,63 @@ def test_replace_requires_consumer_references():
     cmake = c.load("app/ZLIBTargets.cmake")
     assert "add_library(ZLIB::ZLIB INTERFACE IMPORTED)" in cmake
     c.run("create app -pr=profile")
-    assert "zlib/0.1: zlib-ng/0.1" in c.out
-    assert "app/0.1: DEP ZLIB generate: zlib-ng!" in c.out
-    assert "app/0.1: DEP ZLIB build: zlib-ng!" in c.out
+    assert f"zlib/0.1: {name}/{version}" in c.out
+    assert f"app/0.1: DEP ZLIB generate: {name}!" in c.out
+    assert f"app/0.1: DEP ZLIB build: {name}!" in c.out
+
+
+def test_replace_requires_consumer_references_error_multiple():
+    # https://github.com/conan-io/conan/issues/17407
+    c = TestClient()
+    # IMPORTANT: The replacement package must be target-compatible
+    zlib = textwrap.dedent("""
+        from conan import ConanFile
+        class ZlibNG(ConanFile):
+            name = "zlib"
+            version = "0.2"
+            def package_info(self):
+                self.cpp_info.set_property("cmake_file_name", "ZLIB")
+                self.cpp_info.set_property("cmake_target_name", "ZLIB::ZLIB")
+        """)
+    conanfile = textwrap.dedent("""
+        from conan import ConanFile
+        class App(ConanFile):
+            name = "app"
+            version = "0.1"
+            settings = "build_type"
+            requires = "zlib/0.1", "bzip2/0.1"
+            generators = "CMakeDeps"
+
+            def generate(self):
+                self.output.info(f"DEP ZLIB generate: {self.dependencies['zlib'].ref.name}!")
+                self.output.info(f"DEP BZIP2 generate: {self.dependencies['bzip2'].ref.name}!")
+            def build(self):
+                self.output.info(f"DEP ZLIB build: {self.dependencies['zlib'].ref.name}!")
+                self.output.info(f"DEP BZIP2 build: {self.dependencies['bzip2'].ref.name}!")
+            def package_info(self):
+                self.output.info(f"DEP ZLIB package_info: {self.dependencies['zlib'].ref.name}!")
+                self.cpp_info.requires = ["zlib::zlib", "bzip2::bzip2"]
+        """)
+    c.save({"zlib/conanfile.py": zlib,
+            "app/conanfile.py": conanfile,
+            "profile": "[replace_requires]\nzlib/0.1: zlib/0.2\nbzip2/0.1: zlib/0.2"})
+    c.run("create zlib")
+    c.run("build app -pr=profile")
+    assert "zlib/0.1: zlib/0.2" in c.out
+    assert "conanfile.py (app/0.1): DEP ZLIB generate: zlib!" in c.out
+    assert "conanfile.py (app/0.1): DEP ZLIB build: zlib!" in c.out
+    assert "conanfile.py (app/0.1): DEP BZIP2 generate: zlib!" in c.out
+    assert "conanfile.py (app/0.1): DEP BZIP2 build: zlib!" in c.out
+    # Check generated CMake code. If the targets are NOT compatible, then the replacement
+    # Cannot happen
+    assert "find_package(ZLIB)" in c.out
+    assert "target_link_libraries(... ZLIB::ZLIB ZLIB::ZLIB)" in c.out
+    cmake = c.load("app/ZLIBTargets.cmake")
+    assert "add_library(ZLIB::ZLIB INTERFACE IMPORTED)" in cmake
+    c.run("create app -pr=profile")
+    assert "zlib/0.1: zlib/0.2" in c.out
+    assert "app/0.1: DEP ZLIB generate: zlib!" in c.out
+    assert "app/0.1: DEP ZLIB build: zlib!" in c.out
 
 
 def test_replace_requires_consumer_components_options():
diff --git a/test/integration/remote/test_local_recipes_index.py b/test/integration/remote/test_local_recipes_index.py
index 31103c014..12123742c 100644
--- a/test/integration/remote/test_local_recipes_index.py
+++ b/test/integration/remote/test_local_recipes_index.py
@@ -360,3 +360,50 @@ class TestPythonRequires:
         c.run("install --requires=pkg/1.0 --build missing -vvv")
         assert "pyreq/1.0#a0d63ca853edefa33582a24a1bb3c75f - Downloaded (local)" in c.out
         assert "pkg/1.0: Created package" in c.out
+
+class TestUserChannel:
+    @pytest.fixture(scope="class")
+    def c3i_user_channel_folder(self):
+        folder = temp_folder()
+        recipes_folder = os.path.join(folder, "recipes")
+        config = textwrap.dedent("""
+                versions:
+                  "1.0":
+                    folder: all
+                  "2.0":
+                    folder: other
+                """)
+        pkg = str(GenConanfile("pkg").with_class_attribute("user='myuser'")\
+                                    .with_class_attribute("channel='mychannel'"))
+        save_files(recipes_folder,
+                   {"pkg/config.yml": config,
+                    "pkg/all/conanfile.py": pkg,
+                    "pkg/other/conanfile.py": str(GenConanfile("pkg"))})
+        return folder
+
+    def test_user_channel_requirement(self, c3i_user_channel_folder):
+        tc = TestClient(light=True)
+        tc.run(f"remote add local '{c3i_user_channel_folder}'")
+        tc.run("graph info --requires=pkg/[*]@myuser/mychannel")
+        assert "Version range '*' from requirement 'pkg/[*]@myuser/mychannel'" not in tc.out
+        assert "pkg/[*]@myuser/mychannel: pkg/1.0@myuser/mychannel" in tc.out
+        assert "pkg/1.0@myuser/mychannel#0b23a5938afb0457079e41aac8991595 - Downloaded (local)" in tc.out
+
+    @pytest.mark.parametrize("user_channel", ["@foo/bar", "@foo"])
+    def test_user_channel_requirement_no_match(self, c3i_user_channel_folder, user_channel):
+        tc = TestClient(light=True)
+        tc.run(f"remote add local '{c3i_user_channel_folder}'")
+        tc.run(f"graph info --requires=pkg/[*]{user_channel}", assert_error=True)
+        assert f"Version range '*' from requirement 'pkg/[*]{user_channel}' required by 'None' could not be resolved." in tc.out
+
+    def test_user_channel_requirement_only_at(self, c3i_user_channel_folder):
+        tc = TestClient(light=True)
+        tc.run(f"remote add local '{c3i_user_channel_folder}'")
+        tc.run(f"graph info --requires=pkg/[*]@")
+        assert f"pkg/[*]: pkg/2.0" in tc.out
+
+        tc.run(f"graph info --requires=pkg/[<2]@", assert_error=True)
+        assert f" Package 'pkg/[<2]' not resolved" in tc.out
+
+        tc.run(f"graph info --requires=pkg/[<2]", assert_error=True)
+        assert f" Package 'pkg/[<2]' not resolved" in tc.out