From b86a88d31acbf64c4e6b5b2137c08d7b5b179b39 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 11:10:36 +0800 Subject: improve to detect vs/msvc to check env length limit --- xmake/modules/detect/sdks/find_vstudio.lua | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index cf56fcca9..488615a3f 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -93,6 +93,9 @@ local vsenvs = , ["4.2"] = "VS42COMNTOOLS" } +-- the original environment variables +local _env_orgs = {} + -- get all known Visual Studio environment variables function get_vcvars() local realvcvars = vcvars @@ -381,6 +384,40 @@ function _strip_toolset_ver(vs_toolset) return vs_toolset end +-- check if the environment variables are truncated +-- https://github.com/xmake-io/xmake/issues/7281 +function _check_vcvarsall_env(vars) + local check_vars = {"PATH", "INCLUDE", "LIBPATH"} + for _, name in ipairs(check_vars) do + local value_org = _env_orgs[name] + if value_org == nil then + value_org = os.getenv(name) + _env_orgs[name] = value_org or false + end + local value_new = vars[name] + if value_org and value_new and #value_org > 0 then + -- we only check the first/last 512 bytes to verify if the original path is present + -- because the path maybe too long and be truncated + local part = value_org + if #part > 512 then + part = part:sub(1, 512) + end + if not value_new:find(part, 1, true) then + wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!", name) + break + end + local part_end = value_org + if #part_end > 512 then + part_end = part_end:sub(#part_end - 512 + 1) + end + if not value_new:find(part_end, 1, true) then + wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!", name) + break + end + end + end +end + function _load_vcvarsall(vcvarsall, vsver, arch, opt) opt = opt or {} local vs_toolset = opt.toolset or opt.vcvars_ver @@ -407,6 +444,9 @@ function _load_vcvarsall(vcvarsall, vsver, arch, opt) result = _load_vcvarsall_impl(vcvarsall, vsver, arch, opt) end end + if result then + _check_vcvarsall_env(result) + end return result end @@ -414,6 +454,9 @@ end function _find_vstudio(opt) opt = opt or {} + -- clear local cache of environment variables + _env_orgs = {} + -- find the single current MSVC/VS from environment variables local VCInstallDir = os.getenv("VCInstallDir") if VCInstallDir and (VCInstallDir ~= "") then -- cgit v1.3.1 From a153273143ef8d3b8d43b6eb5b6b2dfb7c02944b Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 11:39:30 +0800 Subject: improve to check envs --- xmake/modules/detect/sdks/find_vstudio.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 488615a3f..c898657a1 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -313,6 +313,9 @@ function _load_vcvarsall_impl(vcvarsall, vsver, arch, opt) variables[name] = value end end + + -- check if the environment variables are truncated + _check_vcvarsall_env(variables) if not variables.path then return end @@ -394,7 +397,7 @@ function _check_vcvarsall_env(vars) value_org = os.getenv(name) _env_orgs[name] = value_org or false end - local value_new = vars[name] + local value_new = vars[name] or vars[name:lower()] if value_org and value_new and #value_org > 0 then -- we only check the first/last 512 bytes to verify if the original path is present -- because the path maybe too long and be truncated @@ -444,9 +447,6 @@ function _load_vcvarsall(vcvarsall, vsver, arch, opt) result = _load_vcvarsall_impl(vcvarsall, vsver, arch, opt) end end - if result then - _check_vcvarsall_env(result) - end return result end -- cgit v1.3.1 From 8588d0bdbc8d49cbef094a89538ac5daf96e6a1a Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 12:33:37 +0800 Subject: improve vs check --- xmake/modules/detect/sdks/find_vstudio.lua | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index c898657a1..70cf93d14 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -390,7 +390,7 @@ end -- check if the environment variables are truncated -- https://github.com/xmake-io/xmake/issues/7281 function _check_vcvarsall_env(vars) - local check_vars = {"PATH", "INCLUDE", "LIBPATH"} + local check_vars = {"PATH", "INCLUDE", "LIB", "LIBPATH"} for _, name in ipairs(check_vars) do local value_org = _env_orgs[name] if value_org == nil then @@ -399,23 +399,12 @@ function _check_vcvarsall_env(vars) end local value_new = vars[name] or vars[name:lower()] if value_org and value_new and #value_org > 0 then - -- we only check the first/last 512 bytes to verify if the original path is present - -- because the path maybe too long and be truncated - local part = value_org - if #part > 512 then - part = part:sub(1, 512) - end - if not value_new:find(part, 1, true) then - wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!", name) - break - end - local part_end = value_org - if #part_end > 512 then - part_end = part_end:sub(#part_end - 512 + 1) - end - if not value_new:find(part_end, 1, true) then - wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!", name) - break + for _, p in ipairs(path.splitenv(value_org)) do + if not value_new:find(p, 1, true) then + wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!", name) + wprint(" > %s", p) + break + end end end end -- cgit v1.3.1 From 5a257a3471bc29e1ad9ac496b6389f874f394227 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 12:35:42 +0800 Subject: improve tips --- xmake/modules/detect/sdks/find_vstudio.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 70cf93d14..be0eed658 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -401,8 +401,7 @@ function _check_vcvarsall_env(vars) if value_org and value_new and #value_org > 0 then for _, p in ipairs(path.splitenv(value_org)) do if not value_new:find(p, 1, true) then - wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!", name) - wprint(" > %s", p) + wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p) break end end -- cgit v1.3.1 From 9f2b25fb6b4682b0e82ca1bdcfc0ae24ff9e668d Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 12:37:57 +0800 Subject: improve tips --- xmake/modules/detect/sdks/find_vstudio.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index be0eed658..0035a88ce 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -401,7 +401,7 @@ function _check_vcvarsall_env(vars) if value_org and value_new and #value_org > 0 then for _, p in ipairs(path.splitenv(value_org)) do if not value_new:find(p, 1, true) then - wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p) + wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p:sub(1, 1024)) break end end -- cgit v1.3.1 From 58ca9ee7a7bb05fe5a8706f9c3966ded3534163f Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 12:50:41 +0800 Subject: improve tips --- xmake/modules/detect/sdks/find_vstudio.lua | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 0035a88ce..82cdfb643 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -21,6 +21,7 @@ -- imports import("core.base.option") import("core.base.semver") +import("core.base.hashset") import("core.project.config") import("lib.detect.find_file") import("lib.detect.find_tool") @@ -395,13 +396,24 @@ function _check_vcvarsall_env(vars) local value_org = _env_orgs[name] if value_org == nil then value_org = os.getenv(name) - _env_orgs[name] = value_org or false + if value_org then + _env_orgs[name] = path.splitenv(value_org) + else + _env_orgs[name] = false + end + value_org = _env_orgs[name] end local value_new = vars[name] or vars[name:lower()] if value_org and value_new and #value_org > 0 then - for _, p in ipairs(path.splitenv(value_org)) do - if not value_new:find(p, 1, true) then - wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p:sub(1, 1024)) + local values_new = hashset.from(path.splitenv(value_new)) + for _, p in ipairs(value_org) do + if not values_new:has(p) then + if option.get("diagnosis") then + if #p > 256 then + p = p:sub(1, 256) .. "..." + end + wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p) + end break end end -- cgit v1.3.1 From 609c7ebe102b01068a8e6bfa201c99fb660d8fc7 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 12:55:44 +0800 Subject: improve check --- xmake/modules/detect/sdks/find_vstudio.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 82cdfb643..b39c216ad 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -391,7 +391,10 @@ end -- check if the environment variables are truncated -- https://github.com/xmake-io/xmake/issues/7281 function _check_vcvarsall_env(vars) - local check_vars = {"PATH", "INCLUDE", "LIB", "LIBPATH"} + if not option.get("diagnosis") then + return + end + local check_vars = {"PATH", "INCLUDE", "LIBPATH"} for _, name in ipairs(check_vars) do local value_org = _env_orgs[name] if value_org == nil then @@ -408,12 +411,10 @@ function _check_vcvarsall_env(vars) local values_new = hashset.from(path.splitenv(value_new)) for _, p in ipairs(value_org) do if not values_new:has(p) then - if option.get("diagnosis") then - if #p > 256 then - p = p:sub(1, 256) .. "..." - end - wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p) + if #p > 256 then + p = p:sub(1, 256) .. "..." end + cprint("${color.warning}%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p) break end end -- cgit v1.3.1 From cc2157942e41b7fc19a965e997855b74a447981d Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 12:57:38 +0800 Subject: revert tips --- xmake/modules/detect/sdks/find_vstudio.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index b39c216ad..2ffe2bdf1 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -414,7 +414,7 @@ function _check_vcvarsall_env(vars) if #p > 256 then p = p:sub(1, 256) .. "..." end - cprint("${color.warning}%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p) + wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p) break end end -- cgit v1.3.1 From b9647966c8332c833113df9554a191e1db93a6af Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 13:19:47 +0800 Subject: fix _check_vcvarsall_env --- xmake/modules/detect/sdks/find_vstudio.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 2ffe2bdf1..127fb1322 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -398,9 +398,9 @@ function _check_vcvarsall_env(vars) for _, name in ipairs(check_vars) do local value_org = _env_orgs[name] if value_org == nil then - value_org = os.getenv(name) - if value_org then - _env_orgs[name] = path.splitenv(value_org) + local value_str = os.getenv(name) + if value_str then + _env_orgs[name] = path.splitenv(value_str) else _env_orgs[name] = false end -- cgit v1.3.1 From 9e715b0c81e3cec987b720f7c113a91302345f6c Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 13:21:08 +0800 Subject: fix tips --- xmake/modules/detect/sdks/find_vstudio.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 127fb1322..e7e76047d 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -414,7 +414,7 @@ function _check_vcvarsall_env(vars) if #p > 256 then p = p:sub(1, 256) .. "..." end - wprint("%%%s%% is too long and truncated, detect msvc may be failed, please clear some unused variables!\n > %s", name, p) + wprint("%%%s%% is too long and truncated, msvc detection may fail, please clear some unused variables!\n > %s", name, p) break end end -- cgit v1.3.1 From ac0a3e5909f3d0c4b6a630526b230c8a2085839b Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 1 Feb 2026 14:12:50 +0800 Subject: check lib in vs detection --- xmake/modules/detect/sdks/find_vstudio.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index e7e76047d..22f5dea6b 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -394,7 +394,7 @@ function _check_vcvarsall_env(vars) if not option.get("diagnosis") then return end - local check_vars = {"PATH", "INCLUDE", "LIBPATH"} + local check_vars = {"PATH", "INCLUDE", "LIB", "LIBPATH"} for _, name in ipairs(check_vars) do local value_org = _env_orgs[name] if value_org == nil then -- cgit v1.3.1