From 8a258a2fcb48308f7aa3f1f864aee12e91e1ce3b Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 26 Jan 2026 23:32:31 +0300 Subject: enhance shell detection for Linux by checking parent process --- xmake/core/base/tty.lua | 75 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index dd053b3e6..a605a4a9e 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -23,6 +23,7 @@ local tty = tty or {} -- load modules local io = require("base/io") +local path = require("base/path") -- save metatable and builtin functions tty._term_mode = tty._term_mode or tty.term_mode @@ -256,13 +257,85 @@ function tty.shell() end end end + -- try to find the shell from the parent process (linux) + if not shell and os.host() == "linux" and os.isfile("/proc/self/stat") then + local pid = os.getpid() + for i = 1, 10 do + local stat = io.readfile("/proc/" .. pid .. "/stat") + if not stat or #stat == 0 then + local tmpfile = path.join(os.tmpdir(), "xmake_stat_" .. os.getpid()) + os.execv("cp", {"/proc/" .. pid .. "/stat", tmpfile}) + stat = io.readfile(tmpfile) + os.rm(tmpfile) + end + + -- find last ')' to handle "pid (comm) state ppid" + local start = stat and stat:find(")", 1, true) + while start do + local next_p = stat:find(")", start + 1, true) + if not next_p then break end + start = next_p + end + + if start then + local suffix = stat:sub(start + 1) + local fields = {} + for field in suffix:gmatch("%S+") do + table.insert(fields, field) + if #fields >= 2 then break end + end + + local ppid = tonumber(fields[2]) + if not ppid or ppid == 0 then break end + + local shell_name = nil + local shell_path = nil + if os.isfile("/proc/" .. ppid .. "/exe") then + local ok, link = pcall(os.readlink, "/proc/" .. ppid .. "/exe") + if ok and link then + shell_path = link + end + end + if not shell_path and os.isfile("/proc/" .. ppid .. "/comm") then + shell_name = io.readfile("/proc/" .. ppid .. "/comm") + if not shell_name or #shell_name == 0 then + local tmpfile = path.join(os.tmpdir(), "xmake_comm_" .. os.getpid()) + os.execv("cp", {"/proc/" .. ppid .. "/comm", tmpfile}) + shell_name = io.readfile(tmpfile) + os.rm(tmpfile) + end + if shell_name then + shell_name = shell_name:match("^%s*(.-)%s*$") + end + end + if shell_path then + shell_name = path.filename(shell_path) + end + + if shell_name then + shell_name = shell_name:gsub("^-", "") + for _, name in ipairs({"zsh", "bash", "fish", "nu", "elvish", "sh"}) do + if shell_name == name then + shell = name + break + end + end + if shell then break end + end + pid = ppid + else + break + end + end + end + if not shell then shell = os.getenv("XMAKE_SHELL") end if not shell then shell = os.getenv("SHELL") if shell then - for _, shellname in ipairs({"zsh", "bash", "sh"}) do + for _, shellname in ipairs({"zsh", "bash", "fish", "nu", "elvish", "sh"}) do if shell:find(shellname) then shell = shellname break -- cgit v1.3.1 From 8855fb1173ed118bda8e75ccd75c1b0db26bdcf7 Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 00:19:16 +0300 Subject: add support for Alacritty terminal in tty.term function --- xmake/core/base/tty.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index a605a4a9e..39c4c0d31 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -396,6 +396,8 @@ function tty.term() term = "xterm" elseif TERM == "cygwin" then term = "cygwin" + elseif TERM:find("alacritty", 1, true) then + term = "alacritty" end end end -- cgit v1.3.1 From 14b0f24fc76004d1498fa7f85a6c2377b2fbd722 Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 00:44:11 +0300 Subject: add support for PowerShell in shell detection --- xmake/core/base/tty.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 39c4c0d31..51c301ab4 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -314,7 +314,7 @@ function tty.shell() if shell_name then shell_name = shell_name:gsub("^-", "") - for _, name in ipairs({"zsh", "bash", "fish", "nu", "elvish", "sh"}) do + for _, name in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "sh"}) do if shell_name == name then shell = name break @@ -335,7 +335,7 @@ function tty.shell() if not shell then shell = os.getenv("SHELL") if shell then - for _, shellname in ipairs({"zsh", "bash", "fish", "nu", "elvish", "sh"}) do + for _, shellname in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "sh"}) do if shell:find(shellname) then shell = shellname break -- cgit v1.3.1 From 6e6cf755b207fe128e6e8a534d3df15f56d9c8a4 Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 05:45:37 +0300 Subject: refactor tty.shell to use os.tmpfile and os.runv for improved file handling --- xmake/core/base/tty.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 51c301ab4..27788419b 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -263,8 +263,8 @@ function tty.shell() for i = 1, 10 do local stat = io.readfile("/proc/" .. pid .. "/stat") if not stat or #stat == 0 then - local tmpfile = path.join(os.tmpdir(), "xmake_stat_" .. os.getpid()) - os.execv("cp", {"/proc/" .. pid .. "/stat", tmpfile}) + local tmpfile = os.tmpfile() + os.runv("cp", {"/proc/" .. pid .. "/stat", tmpfile}) stat = io.readfile(tmpfile) os.rm(tmpfile) end @@ -299,8 +299,8 @@ function tty.shell() if not shell_path and os.isfile("/proc/" .. ppid .. "/comm") then shell_name = io.readfile("/proc/" .. ppid .. "/comm") if not shell_name or #shell_name == 0 then - local tmpfile = path.join(os.tmpdir(), "xmake_comm_" .. os.getpid()) - os.execv("cp", {"/proc/" .. ppid .. "/comm", tmpfile}) + local tmpfile = os.tmpfile() + os.runv("cp", {"/proc/" .. ppid .. "/comm", tmpfile}) shell_name = io.readfile(tmpfile) os.rm(tmpfile) end -- cgit v1.3.1 From 41e460c869c15699d63f39e4fe433d88225818dc Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 05:54:28 +0300 Subject: add function to find shell from parent process in Linux --- xmake/core/base/tty.lua | 149 ++++++++++++++++++++++++++---------------------- 1 file changed, 80 insertions(+), 69 deletions(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 27788419b..788327d20 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -235,6 +235,84 @@ function tty.flush() return tty end +-- find the shell from the parent process (linux) +function tty._find_shell_from_parent() + if os.host() ~= "linux" or not os.isfile("/proc/self/stat") then + return + end + + local shell + local pid = os.getpid() + for i = 1, 10 do + local stat = io.readfile("/proc/" .. pid .. "/stat") + if not stat or #stat == 0 then + local tmpfile = os.tmpfile() + os.runv("cp", {"/proc/" .. pid .. "/stat", tmpfile}) + stat = io.readfile(tmpfile) + os.rm(tmpfile) + end + + -- find last ')' to handle "pid (comm) state ppid" + local start = stat and stat:find(")", 1, true) + while start do + local next_p = stat:find(")", start + 1, true) + if not next_p then break end + start = next_p + end + + if start then + local suffix = stat:sub(start + 1) + local fields = {} + for field in suffix:gmatch("%S+") do + table.insert(fields, field) + if #fields >= 2 then break end + end + + local ppid = tonumber(fields[2]) + if not ppid or ppid == 0 then break end + + local shell_name = nil + local shell_path = nil + if os.isfile("/proc/" .. ppid .. "/exe") then + local ok, link = pcall(os.readlink, "/proc/" .. ppid .. "/exe") + if ok and link then + shell_path = link + end + end + if not shell_path and os.isfile("/proc/" .. ppid .. "/comm") then + shell_name = io.readfile("/proc/" .. ppid .. "/comm") + if not shell_name or #shell_name == 0 then + local tmpfile = os.tmpfile() + os.runv("cp", {"/proc/" .. ppid .. "/comm", tmpfile}) + shell_name = io.readfile(tmpfile) + os.rm(tmpfile) + end + if shell_name then + shell_name = shell_name:match("^%s*(.-)%s*$") + end + end + if shell_path then + shell_name = path.filename(shell_path) + end + + if shell_name then + shell_name = shell_name:gsub("^-", "") + for _, name in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "sh"}) do + if shell_name == name then + shell = name + break + end + end + if shell then break end + end + pid = ppid + else + break + end + end + return shell +end + -- get shell name function tty.shell() local shell = tty._SHELL @@ -258,75 +336,8 @@ function tty.shell() end end -- try to find the shell from the parent process (linux) - if not shell and os.host() == "linux" and os.isfile("/proc/self/stat") then - local pid = os.getpid() - for i = 1, 10 do - local stat = io.readfile("/proc/" .. pid .. "/stat") - if not stat or #stat == 0 then - local tmpfile = os.tmpfile() - os.runv("cp", {"/proc/" .. pid .. "/stat", tmpfile}) - stat = io.readfile(tmpfile) - os.rm(tmpfile) - end - - -- find last ')' to handle "pid (comm) state ppid" - local start = stat and stat:find(")", 1, true) - while start do - local next_p = stat:find(")", start + 1, true) - if not next_p then break end - start = next_p - end - - if start then - local suffix = stat:sub(start + 1) - local fields = {} - for field in suffix:gmatch("%S+") do - table.insert(fields, field) - if #fields >= 2 then break end - end - - local ppid = tonumber(fields[2]) - if not ppid or ppid == 0 then break end - - local shell_name = nil - local shell_path = nil - if os.isfile("/proc/" .. ppid .. "/exe") then - local ok, link = pcall(os.readlink, "/proc/" .. ppid .. "/exe") - if ok and link then - shell_path = link - end - end - if not shell_path and os.isfile("/proc/" .. ppid .. "/comm") then - shell_name = io.readfile("/proc/" .. ppid .. "/comm") - if not shell_name or #shell_name == 0 then - local tmpfile = os.tmpfile() - os.runv("cp", {"/proc/" .. ppid .. "/comm", tmpfile}) - shell_name = io.readfile(tmpfile) - os.rm(tmpfile) - end - if shell_name then - shell_name = shell_name:match("^%s*(.-)%s*$") - end - end - if shell_path then - shell_name = path.filename(shell_path) - end - - if shell_name then - shell_name = shell_name:gsub("^-", "") - for _, name in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "sh"}) do - if shell_name == name then - shell = name - break - end - end - if shell then break end - end - pid = ppid - else - break - end - end + if not shell then + shell = tty._find_shell_from_parent() end if not shell then -- cgit v1.3.1 From 666f5c5b7f29beb474b1c12726f8dd39cd1babe9 Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 06:04:17 +0300 Subject: refactor _find_shell_from_parent function to simplify error handling for shell path retrieval --- xmake/core/base/tty.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 788327d20..5c7f360c2 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -274,8 +274,8 @@ function tty._find_shell_from_parent() local shell_name = nil local shell_path = nil if os.isfile("/proc/" .. ppid .. "/exe") then - local ok, link = pcall(os.readlink, "/proc/" .. ppid .. "/exe") - if ok and link then + local link = os.readlink("/proc/" .. ppid .. "/exe") + if link then shell_path = link end end -- cgit v1.3.1 From b6578fbe40e03b075c9581895d55d256c0c4205b Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 12:17:41 +0300 Subject: refactor _find_shell_from_parent function for improved shell detection and error handling --- xmake/core/base/tty.lua | 96 ++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 58 deletions(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 5c7f360c2..9f339e4f4 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -243,73 +243,53 @@ function tty._find_shell_from_parent() local shell local pid = os.getpid() - for i = 1, 10 do - local stat = io.readfile("/proc/" .. pid .. "/stat") - if not stat or #stat == 0 then - local tmpfile = os.tmpfile() - os.runv("cp", {"/proc/" .. pid .. "/stat", tmpfile}) - stat = io.readfile(tmpfile) - os.rm(tmpfile) + local tmpfile = os.tmpfile() + while pid ~= 0 do + local shell_name = nil + local shell_path = nil + if os.isfile("/proc/" .. pid .. "/exe") then + local link = os.readlink("/proc/" .. pid .. "/exe") + if link then + shell_path = link + end end - -- find last ')' to handle "pid (comm) state ppid" - local start = stat and stat:find(")", 1, true) - while start do - local next_p = stat:find(")", start + 1, true) - if not next_p then break end - start = next_p + if not shell_path and os.isfile("/proc/" .. pid .. "/comm") then + shell_name = io.readfile("/proc/" .. pid .. "/comm") + if not shell_name or #shell_name == 0 then + os.runv("cp", {"/proc/" .. pid .. "/comm", tmpfile}) + shell_name = io.readfile(tmpfile) + end + if shell_name then + shell_name = shell_name:match("^%s*(.-)%s*$") + end + end + if shell_path then + shell_name = path.filename(shell_path) end - if start then - local suffix = stat:sub(start + 1) - local fields = {} - for field in suffix:gmatch("%S+") do - table.insert(fields, field) - if #fields >= 2 then break end - end - - local ppid = tonumber(fields[2]) - if not ppid or ppid == 0 then break end - - local shell_name = nil - local shell_path = nil - if os.isfile("/proc/" .. ppid .. "/exe") then - local link = os.readlink("/proc/" .. ppid .. "/exe") - if link then - shell_path = link - end - end - if not shell_path and os.isfile("/proc/" .. ppid .. "/comm") then - shell_name = io.readfile("/proc/" .. ppid .. "/comm") - if not shell_name or #shell_name == 0 then - local tmpfile = os.tmpfile() - os.runv("cp", {"/proc/" .. ppid .. "/comm", tmpfile}) - shell_name = io.readfile(tmpfile) - os.rm(tmpfile) - end - if shell_name then - shell_name = shell_name:match("^%s*(.-)%s*$") + if shell_name then + shell_name = shell_name:gsub("^-", "") + for _, name in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "sh"}) do + if shell_name == name then + shell = name + break end end - if shell_path then - shell_name = path.filename(shell_path) - end + if shell then break end + end - if shell_name then - shell_name = shell_name:gsub("^-", "") - for _, name in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "sh"}) do - if shell_name == name then - shell = name - break - end - end - if shell then break end - end - pid = ppid - else - break + local stat = io.readfile("/proc/" .. pid .. "/stat") + if not stat or #stat == 0 then + os.runv("cp", {"/proc/" .. pid .. "/stat", tmpfile}) + stat = io.readfile(tmpfile) end + + local ppid = stat and tonumber(stat:match(".*%) %S+ (%d+)")) + if not ppid or ppid == 0 then break end + pid = ppid end + os.rm(tmpfile) return shell end -- cgit v1.3.1 From 93b10aae53d6a3fc751894695161b8b472525a4b Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 12:35:58 +0300 Subject: limit shell detection loop to prevent infinite iterations --- xmake/core/base/tty.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 9f339e4f4..a162e6341 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -244,7 +244,10 @@ function tty._find_shell_from_parent() local shell local pid = os.getpid() local tmpfile = os.tmpfile() - while pid ~= 0 do + local count = 0 + while pid ~= 0 and count < 10 do + count = count + 1 + print(count) local shell_name = nil local shell_path = nil if os.isfile("/proc/" .. pid .. "/exe") then -- cgit v1.3.1 From 33ec7a0a2ea8b21b8c896e828ecb80b6da6398d1 Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 12:36:23 +0300 Subject: remove debug print statement from _find_shell_from_parent function --- xmake/core/base/tty.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index a162e6341..1976a8bb2 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -247,7 +247,6 @@ function tty._find_shell_from_parent() local count = 0 while pid ~= 0 and count < 10 do count = count + 1 - print(count) local shell_name = nil local shell_path = nil if os.isfile("/proc/" .. pid .. "/exe") then -- cgit v1.3.1 From 9914c5cb87077a289f2ec32e360762e91a4378e4 Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 12:39:49 +0300 Subject: limit shell detection loop to prevent excessive iterations --- xmake/core/base/tty.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 1976a8bb2..0144795f9 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -245,7 +245,7 @@ function tty._find_shell_from_parent() local pid = os.getpid() local tmpfile = os.tmpfile() local count = 0 - while pid ~= 0 and count < 10 do + while pid ~= 0 and count < 4 do count = count + 1 local shell_name = nil local shell_path = nil -- cgit v1.3.1 From 5c99e2d9e452e08de4fd7595f4429790ce8efed7 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 28 Jan 2026 22:00:56 +0800 Subject: Update tty.lua --- xmake/core/base/tty.lua | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 0144795f9..b3ac35b2b 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -243,7 +243,6 @@ function tty._find_shell_from_parent() local shell local pid = os.getpid() - local tmpfile = os.tmpfile() local count = 0 while pid ~= 0 and count < 4 do count = count + 1 @@ -258,12 +257,8 @@ function tty._find_shell_from_parent() if not shell_path and os.isfile("/proc/" .. pid .. "/comm") then shell_name = io.readfile("/proc/" .. pid .. "/comm") - if not shell_name or #shell_name == 0 then - os.runv("cp", {"/proc/" .. pid .. "/comm", tmpfile}) - shell_name = io.readfile(tmpfile) - end if shell_name then - shell_name = shell_name:match("^%s*(.-)%s*$") + shell_name = shell_name:trim() end end if shell_path then @@ -271,27 +266,25 @@ function tty._find_shell_from_parent() end if shell_name then - shell_name = shell_name:gsub("^-", "") + shell_name = shell_name:ltrim("-") for _, name in ipairs({"zsh", "bash", "fish", "nu", "elvish", "pwsh", "sh"}) do if shell_name == name then shell = name break end end - if shell then break end + if shell then + break + end end local stat = io.readfile("/proc/" .. pid .. "/stat") - if not stat or #stat == 0 then - os.runv("cp", {"/proc/" .. pid .. "/stat", tmpfile}) - stat = io.readfile(tmpfile) - end - local ppid = stat and tonumber(stat:match(".*%) %S+ (%d+)")) - if not ppid or ppid == 0 then break end + if not ppid or ppid == 0 then + break + end pid = ppid end - os.rm(tmpfile) return shell end @@ -628,4 +621,3 @@ end -- return module return tty - -- cgit v1.3.1