SpawnCommand.set_environment_variables vs os.getenv #6132
-
What Operating System(s) are you running on?Windows Which Wayland compositor or X11 Window manager(s) are you using?No response WezTerm version20240203-110809-5046fc22 Ask your question!My goal is to create a few entries in the launcher menu. Each will spawn a shell in a different directory with a different Python virtual environment active (managed by the Poetry command), and I'd like each to have a different tab title so I can keep track of them better. My idea for now is to use process-level environment variables to keep track of which process is which. However, it seems like os.getenv is not picking up on the variables set in my SpawnCommand config, even though the shell itself knows about them. Right now, I'm using entries in the launch_menu that look like this: {
label = "Blocks Shell",
args = { "pwsh", "-NoProfile", "-Command", "poetry", "shell" },
cwd = "C:/Users/username/Documents/Code/blocks",
set_environment_variables = {
MY_WEZTERM_TAB_TITLE = "BLOCKS",
},
},
{
label = "Web Shell",
args = { "pwsh", "-NoProfile", "-Command", "poetry", "shell" },
cwd = "C:/Users/username/Documents/Code/web",
set_environment_variables = {
MY_WEZTERM_TAB_TITLE = "WEB",
},
} I also have a custom function set up for format_tab_title. There's a bunch of formatting shenanigans that are working as expected, but here's the relevant part of the function: local full_title = os.getenv("MY_WEZTERM_TAB_TITLE") or tab.active_pane.title
local title_without_exe = string.gsub(full_title, "%.exe$", "")
local trimmed_title = wezterm.truncate_right(title_without_exe, max_width - 6)
-- ...
return {
-- ...
{ Text = " " .. trimmed_title .. " " },
-- ...
} Here's the problem: the For testing, I went into my Windows user-level environment variables and set the variable there - and the tab formatting worked. Every tab was titled with the test value I set. Am I using the set_environment_variables key wrong? Is there a different way to get process-level environment variables instead of using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
In general, changing the environment in a running program is fraught with peril, as it leads to race conditions in multi-threaded programs, so it is something to be avoided. Also: it is impossible for one program to change the environment of another running program; you can only change your own environment, or prepare the environment to be used for a child process that you spawn, once, at the time that you spawn it. I'm not super clear on what your higher level goal is. If you want to declare a "variable" (or rather, a constant, that doesn't change) centrally in your config, you can use the lua If you want to maintain a variable that updates in response to things happening in your config, you might consider looking at wezterm.GLOBAL. I think that you might benefit from looking at user-vars here. https://wezfurlong.org/wezterm/recipes/passing-data.html has some information on passing state back from a program and into wezterm. You might consider having your powershell startup stuff look at the |
Beta Was this translation helpful? Give feedback.
set_environment_variables
sets environment variables for the child processes that are spawned in the default execution domain. It does not set them in the wezterm process, which is whatos.getenv
reads.In general, changing the environment in a running program is fraught with peril, as it leads to race conditions in multi-threaded programs, so it is something to be avoided. Also: it is impossible for one program to change the environment of another running program; you can only change your own environment, or prepare the environment to be used for a child process that you spawn, once, at the time that you spawn it.
I'm not super clear on what your higher level goal is.
If you want to decl…