From 4be10d436233bd7dffc0c91f5c5bd2d0806156ba Mon Sep 17 00:00:00 2001 From: Mauro Tortonesi Date: Thu, 3 Nov 2022 17:09:14 +0100 Subject: Fix sandbox_lib_detect_find_path._find function. Function sandbox_lib_detect_find_path._find function currently searches the first occurrence of substring path.pattern(path.translate(name)) within filepath. This behavior breaks in case one wants, e.g., to find packages named libsomething. The correct behavior would be to find the last occurrence of substring path.pattern(path.translate(name)) within filepath. Hence this patch. --- xmake/core/sandbox/modules/import/lib/detect/find_path.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua index d881d1072..98edd8aa4 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua @@ -37,7 +37,18 @@ function sandbox_lib_detect_find_path._find(filedir, name) local filepath = results[1] if filepath then -- we need translate name first, https://github.com/xmake-io/xmake-repo/issues/1315 - local p = filepath:find(path.pattern(path.translate(name))) + local to_search = path.pattern(path.translate(name)) + + -- need to find the last occurrence of substring to_search within filepath + local last_index = 0 + local idx = 0 + while true do + idx = filepath:find(to_search, idx+1) + if idx == nil then break end + last_index = idx + end + + local p = filepath:find(to_search, last_index) if p then filepath = path.translate(filepath:sub(1, p - 1)) if os.isdir(filepath) then -- cgit v1.3.1 From e97189c91af7eb44883894d6b8cff04a28ec9700 Mon Sep 17 00:00:00 2001 From: Mauro Tortonesi Date: Fri, 4 Nov 2022 14:27:23 +0100 Subject: Refactored code to use string:lastof. --- xmake/core/sandbox/modules/import/lib/detect/find_path.lua | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua index 98edd8aa4..a88562a76 100644 --- a/xmake/core/sandbox/modules/import/lib/detect/find_path.lua +++ b/xmake/core/sandbox/modules/import/lib/detect/find_path.lua @@ -37,18 +37,7 @@ function sandbox_lib_detect_find_path._find(filedir, name) local filepath = results[1] if filepath then -- we need translate name first, https://github.com/xmake-io/xmake-repo/issues/1315 - local to_search = path.pattern(path.translate(name)) - - -- need to find the last occurrence of substring to_search within filepath - local last_index = 0 - local idx = 0 - while true do - idx = filepath:find(to_search, idx+1) - if idx == nil then break end - last_index = idx - end - - local p = filepath:find(to_search, last_index) + local p = filepath:lastof(path.pattern(path.translate(name))) if p then filepath = path.translate(filepath:sub(1, p - 1)) if os.isdir(filepath) then -- cgit v1.3.1