Skip to content

terminal: Remove SHLVL from terminal environment to fix incorrect shell level#44835

Merged
Veykril merged 1 commit intozed-industries:mainfrom
edlsh:fix/shlvl-starts-at-2
Dec 15, 2025
Merged

terminal: Remove SHLVL from terminal environment to fix incorrect shell level#44835
Veykril merged 1 commit intozed-industries:mainfrom
edlsh:fix/shlvl-starts-at-2

Conversation

@edlsh
Copy link
Contributor

@edlsh edlsh commented Dec 15, 2025

Fixes #33958

Problem

When opening a terminal in Zed, SHLVL incorrectly starts at 2 instead of 1. On workspace: reload, it increases by 2 instead of 1.

Root Cause

  1. Zed's shell_env::capture() spawns a login shell (-l -i -c) to capture the user's environment, which increments SHLVL
  2. The captured SHLVL is passed through to the PTY options
  3. When alacritty_terminal spawns the user's shell, it increments SHLVL again

Result: SHLVL = captured value + 1 = 2 (when launched from Finder)

Solution

Remove SHLVL from the environment in TerminalBuilder::new() before passing it to alacritty_terminal. This allows the spawned shell to initialize SHLVL to 1 on its own, matching the behavior of standalone terminal emulators like iTerm2, Kitty, and Alacritty.

Testing

  • Launch Zed from Finder → open terminal → echo $SHLVL → should output 1
  • Launch Zed from shell → open terminal → echo $SHLVL → should output 1
  • workspace: reload → open terminal → echo $SHLVL → should remain 1
  • Tested with bash, zsh, fish

Release Notes:

  • Fixed terminal $SHLVL starting at 2 instead of 1 (#33958)

Fixes #33958

When opening a terminal in Zed, SHLVL incorrectly starts at 2 instead of 1.
This happens because:

1. Zed's shell_env::capture() spawns a login shell to capture environment,
   which increments SHLVL
2. The captured SHLVL is passed to the PTY, and the spawned shell increments
   it again

The fix removes SHLVL from the environment before passing it to
alacritty_terminal, allowing the shell to initialize SHLVL to 1 on its own.
This matches the behavior of standalone terminal emulators like iTerm2,
Kitty, and Alacritty.
@cla-bot
Copy link

cla-bot bot commented Dec 15, 2025

We require contributors to sign our Contributor License Agreement, and we don't have @edlsh on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@maxdeviant maxdeviant changed the title fix: remove SHLVL from terminal environment to fix incorrect shell level Remove SHLVL from terminal environment to fix incorrect shell level Dec 15, 2025
@edlsh
Copy link
Contributor Author

edlsh commented Dec 15, 2025

@cla-bot check

@Veykril Veykril self-assigned this Dec 15, 2025
@Veykril
Copy link
Member

Veykril commented Dec 15, 2025

@cla-bot check

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Dec 15, 2025
@cla-bot
Copy link

cla-bot bot commented Dec 15, 2025

The cla-bot has been summoned, and re-checked this pull request!

@Veykril Veykril changed the title Remove SHLVL from terminal environment to fix incorrect shell level terminal: Remove SHLVL from terminal environment to fix incorrect shell level Dec 15, 2025
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Veykril Veykril merged commit 6cab835 into zed-industries:main Dec 15, 2025
27 checks passed
CherryWorm pushed a commit to CherryWorm/zed that referenced this pull request Dec 16, 2025
…ll level (zed-industries#44835)

Fixes zed-industries#33958

## Problem

When opening a terminal in Zed, `SHLVL` incorrectly starts at 2 instead
of 1. On `workspace: reload`, it increases by 2 instead of 1.

## Root Cause

1. Zed's `shell_env::capture()` spawns a login shell (`-l -i -c`) to
capture the user's environment, which increments `SHLVL`
2. The captured `SHLVL` is passed through to the PTY options
3. When alacritty_terminal spawns the user's shell, it increments
`SHLVL` again

Result: `SHLVL` = captured value + 1 = 2 (when launched from Finder)

## Solution

Remove `SHLVL` from the environment in `TerminalBuilder::new()` before
passing it to alacritty_terminal. This allows the spawned shell to
initialize `SHLVL` to 1 on its own, matching the behavior of standalone
terminal emulators like iTerm2, Kitty, and Alacritty.

## Testing

- Launch Zed from Finder → open terminal → `echo $SHLVL` → should output
`1`
- Launch Zed from shell → open terminal → `echo $SHLVL` → should output
`1`
- `workspace: reload` → open terminal → `echo $SHLVL` → should remain
`1`
- Tested with bash, zsh, fish

Release Notes:

- Fixed terminal `$SHLVL` starting at 2 instead of 1
([zed-industries#33958](zed-industries#33958))
@injust
Copy link
Contributor

injust commented Jan 7, 2026

@edlsh Seems like this change (which shipped in 0.218.5) didn't fix #33958.

@edlsh
Copy link
Contributor Author

edlsh commented Jan 7, 2026

I'm looking into it now

Veykril pushed a commit that referenced this pull request Jan 8, 2026
Fixes #33958

## Problem

PR #44835 attempted to fix SHLVL starting at 2 by removing it from the
`env` HashMap passed to alacritty. However, that HashMap is only an
**overlay** — alacritty uses the parent process environment as a base
and applies the overlay on top. Since alacritty never calls
`env_remove("SHLVL")`, the terminal shell still inherits `SHLVL` from
Zed's process environment.

## Root Cause

The real issue is in `load_login_shell_environment()`:

1. `shell_env::capture()` spawns a login shell to capture environment
variables
2. That login shell increments `SHLVL` (from 0→1 or n→n+1)
3. The captured env (including the incremented `SHLVL`) is written to
Zed's **process environment** via `env::set_var`
4. When you open a terminal, the shell inherits Zed's `SHLVL` and
increments it again → starts at 2
5. On reload, `shell_env::capture()` runs again with the
already-elevated `SHLVL`, incrementing it further → +2 per reload

## Fix

Skip `SHLVL` when setting Zed's process environment in
`load_login_shell_environment()`. This prevents the login-shell capture
from polluting Zed's env, so terminals start fresh with the correct
`SHLVL`.
rtfeldman pushed a commit that referenced this pull request Jan 9, 2026
Fixes #33958

## Problem

PR #44835 attempted to fix SHLVL starting at 2 by removing it from the
`env` HashMap passed to alacritty. However, that HashMap is only an
**overlay** — alacritty uses the parent process environment as a base
and applies the overlay on top. Since alacritty never calls
`env_remove("SHLVL")`, the terminal shell still inherits `SHLVL` from
Zed's process environment.

## Root Cause

The real issue is in `load_login_shell_environment()`:

1. `shell_env::capture()` spawns a login shell to capture environment
variables
2. That login shell increments `SHLVL` (from 0→1 or n→n+1)
3. The captured env (including the incremented `SHLVL`) is written to
Zed's **process environment** via `env::set_var`
4. When you open a terminal, the shell inherits Zed's `SHLVL` and
increments it again → starts at 2
5. On reload, `shell_env::capture()` runs again with the
already-elevated `SHLVL`, incrementing it further → +2 per reload

## Fix

Skip `SHLVL` when setting Zed's process environment in
`load_login_shell_environment()`. This prevents the login-shell capture
from polluting Zed's env, so terminals start fresh with the correct
`SHLVL`.
@edlsh
Copy link
Contributor Author

edlsh commented Jan 9, 2026

@injust This should have resolved the issue 😁

LivioGama pushed a commit to LivioGama/zed that referenced this pull request Jan 20, 2026
…ries#46273)

Fixes zed-industries#33958

## Problem

PR zed-industries#44835 attempted to fix SHLVL starting at 2 by removing it from the
`env` HashMap passed to alacritty. However, that HashMap is only an
**overlay** — alacritty uses the parent process environment as a base
and applies the overlay on top. Since alacritty never calls
`env_remove("SHLVL")`, the terminal shell still inherits `SHLVL` from
Zed's process environment.

## Root Cause

The real issue is in `load_login_shell_environment()`:

1. `shell_env::capture()` spawns a login shell to capture environment
variables
2. That login shell increments `SHLVL` (from 0→1 or n→n+1)
3. The captured env (including the incremented `SHLVL`) is written to
Zed's **process environment** via `env::set_var`
4. When you open a terminal, the shell inherits Zed's `SHLVL` and
increments it again → starts at 2
5. On reload, `shell_env::capture()` runs again with the
already-elevated `SHLVL`, incrementing it further → +2 per reload

## Fix

Skip `SHLVL` when setting Zed's process environment in
`load_login_shell_environment()`. This prevents the login-shell capture
from polluting Zed's env, so terminals start fresh with the correct
`SHLVL`.
LivioGama pushed a commit to LivioGama/zed that referenced this pull request Jan 20, 2026
…ries#46273)

Fixes zed-industries#33958

## Problem

PR zed-industries#44835 attempted to fix SHLVL starting at 2 by removing it from the
`env` HashMap passed to alacritty. However, that HashMap is only an
**overlay** — alacritty uses the parent process environment as a base
and applies the overlay on top. Since alacritty never calls
`env_remove("SHLVL")`, the terminal shell still inherits `SHLVL` from
Zed's process environment.

## Root Cause

The real issue is in `load_login_shell_environment()`:

1. `shell_env::capture()` spawns a login shell to capture environment
variables
2. That login shell increments `SHLVL` (from 0→1 or n→n+1)
3. The captured env (including the incremented `SHLVL`) is written to
Zed's **process environment** via `env::set_var`
4. When you open a terminal, the shell inherits Zed's `SHLVL` and
increments it again → starts at 2
5. On reload, `shell_env::capture()` runs again with the
already-elevated `SHLVL`, incrementing it further → +2 per reload

## Fix

Skip `SHLVL` when setting Zed's process environment in
`load_login_shell_environment()`. This prevents the login-shell capture
from polluting Zed's env, so terminals start fresh with the correct
`SHLVL`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$SHLVL starts at 2 and increases by 2 every time Zed is reloaded

3 participants