Repository: conan-io/conan
Pull request #17421 (2024-12-05 14:55:02)

[auto_location][fix] Symlinks
Changelog: Fix: Trusting the real path coming from a symlink is a good one.
Docs: omit
Closes: https://github.com/conan-io/conan/issues/17417


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/build_info.py b/conans/model/build_info.py
index dc8e79a6c..dc180a96d 100644
--- a/conans/model/build_info.py
+++ b/conans/model/build_info.py
@@ -510,8 +510,7 @@ class _Component:
                         # then os.path.realpath("libmylib.1.0.0.dylib") == "libmylib.dylib"
                         # Note: os.readlink() returns the path which the symbolic link points to.
                         real_path = os.path.realpath(full_path)
-                        if pattern.match(os.path.basename(real_path)):
-                            ret.add(real_path)
+                        ret.add(real_path)
                     else:
                         ret.add(full_path)
             return list(ret)


Test patch:
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
diff --git a/test/unittests/model/build_info/test_deduce_locations.py b/test/unittests/model/build_info/test_deduce_locations.py
index 5aeeff77f..33dfd1b2a 100644
--- a/test/unittests/model/build_info/test_deduce_locations.py
+++ b/test/unittests/model/build_info/test_deduce_locations.py
@@ -160,26 +160,33 @@ def test_windows_several_shared_link_locations(lib_info, conanfile):
         assert result.components[f"_{lib_name}"].type == "shared-library"
 
 
+@pytest.mark.parametrize("lib, symlinks", [
+    # symlinks == "real_file <- symlink1 <- symlink2 <- ... <- symlinkN"
+    # Issue related: https://github.com/conan-io/conan/issues/17417
+    ("png", "libpng16.so.16.44.0 <- libpng16.so.16 <- libpng16.so <- libpng.so")
+])
 @pytest.mark.skipif(platform.system() == "Windows", reason="Can't apply symlink on Windows")
-def test_shared_link_locations_symlinks(conanfile):
+def test_shared_link_locations_symlinks(lib, symlinks, conanfile):
     """
     Tests auto deduce location is able to find the real path of
     any symlink created in the libs folder
     """
     folder = temp_folder()
+    all_files = symlinks.split(" <- ")  # [real_one, sym1, sym2, ...]
     # forcing a folder that it's not going to be analysed by the deduce_location() function
-    real_location = os.path.join(folder, "other", "mylib.so")
+    real_location = os.path.join(folder, "other", all_files.pop(0))
     save(real_location, "")
     # Symlinks
     os.makedirs(os.path.join(folder, "libdir"))
-    sym_1 = os.path.join(folder, "libdir", "mylib.1.0.0.so")
-    sym_2 = os.path.join(folder, "libdir", "mylib.2.0.0.so")
-    os.symlink(real_location, sym_1)
-    os.symlink(sym_1, sym_2)
+    prev_path = real_location
+    for file in all_files:
+        sym = os.path.join(folder, "libdir", file)
+        os.symlink(prev_path, sym)
+        prev_path = sym
 
     cppinfo = CppInfo()
     cppinfo.libdirs = ["libdir"]
-    cppinfo.libs = ["mylib"]
+    cppinfo.libs = [lib]
     cppinfo.set_relative_base_folder(folder)
 
     result = cppinfo.deduce_full_cpp_info(conanfile)