diff options
| author | ruki <[email protected]> | 2026-02-01 16:37:56 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-01 16:37:56 +0800 |
| commit | d7bdbc99bcc5b64a3fa257c5835014f5d8878150 (patch) | |
| tree | cf0dcb591ef2a766713506c04719445cb535238b | |
| parent | 8e9e8d05fff5fd00c206e77617422d853030831e (diff) | |
| parent | ac0a3e5909f3d0c4b6a630526b230c8a2085839b (diff) | |
Merge pull request #7286 from xmake-io/vs
Check long env values when detecting vs
| -rw-r--r-- | xmake/modules/detect/sdks/find_vstudio.lua | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index cf56fcca9..22f5dea6b 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") @@ -93,6 +94,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 @@ -310,6 +314,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 @@ -381,6 +388,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) + if not option.get("diagnosis") then + return + end + 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 + local value_str = os.getenv(name) + if value_str then + _env_orgs[name] = path.splitenv(value_str) + 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 + local values_new = hashset.from(path.splitenv(value_new)) + for _, p in ipairs(value_org) do + if not values_new:has(p) then + if #p > 256 then + p = p:sub(1, 256) .. "..." + end + wprint("%%%s%% is too long and truncated, msvc detection may fail, please clear some unused variables!\n > %s", name, p) + break + end + end + end + end +end + function _load_vcvarsall(vcvarsall, vsver, arch, opt) opt = opt or {} local vs_toolset = opt.toolset or opt.vcvars_ver @@ -414,6 +455,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 |
