Skip to content

fix(shellexec): prevent empty XDG_* env vars when $SNAP is inherited from non-Wave snaps#3423

Closed
Jason-Shen2 wants to merge 1 commit into
wavetermdev:mainfrom
Jason-Shen2:fix/shellexec-xdg-empty-vars
Closed

fix(shellexec): prevent empty XDG_* env vars when $SNAP is inherited from non-Wave snaps#3423
Jason-Shen2 wants to merge 1 commit into
wavetermdev:mainfrom
Jason-Shen2:fix/shellexec-xdg-empty-vars

Conversation

@Jason-Shen2

Copy link
Copy Markdown

Summary

Fixes #3336.

When wavesrv was launched from a process that had $SNAP set (e.g., any CLI tool installed as a snap), the snap XDG-correction code would trigger erroneously. It then wrote empty-string values for XDG_CONFIG_HOME, XDG_DATA_HOME, XDG_CACHE_HOME, etc. into the spawned shell environment, because PAM env typically doesn't set these variables on modern systems. Tools that path-join these vars without spec-compliant fallback (e.g. mise) then wrote their data into relative paths in the CWD.

Changes

Two fixes:

  1. Accurate snap detection (shellexec.go): Changed os.Getenv("SNAP") != "" to os.Getenv("SNAP_NAME") == "waveterm". The $SNAP var propagates to child processes from any snap (e.g. task CLI), causing false positives. $SNAP_NAME is set to the snap's own name and reliably identifies Wave-as-snap.

  2. Skip empty values for new env vars (shellutil.go): UpdateCmdEnv already correctly skipped empty values when updating existing env vars (unsetting them), but would add empty KEY= entries for vars not already present. Added the same envVal != "" guard for the new-var branch, preventing empty-string variables from being injected into the child process environment.

Testing

  • go build ./pkg/shellexec/... ./pkg/util/shellutil/... compiles cleanly
  • go test ./pkg/util/shellutil/... passes

…from non-Wave snaps

Fixes wavetermdev#3336.

Two compounding bugs:

1. Snap detection used os.Getenv("SNAP") != "", which triggers whenever
   wavesrv is launched from any snap process (not just Wave-as-snap).
   Changed to os.Getenv("SNAP_NAME") == "waveterm" for accurate detection.

2. UpdateCmdEnv would add empty-string values for env vars that didn't
   already exist in the command environment (e.g. KEY=). This caused
   tools like mise to write cache/config/data into relative CWD paths
   when the false-positive snap detection unset valid XDG vars. Added
   a check to skip adding new vars with empty values, matching the
   existing behavior for already-present vars.
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f531120b-33aa-4b53-be7a-bd425d8b782a

📥 Commits

Reviewing files that changed from the base of the PR and between c99022c and 9d349c9.

📒 Files selected for processing (2)
  • pkg/shellexec/shellexec.go
  • pkg/util/shellutil/shellutil.go

Walkthrough

The Snap environment correction in StartLocalShellProc now requires SNAP_NAME to equal waveterm. UpdateCmdEnv now skips adding missing environment variables when their values are empty, avoiding entries formatted as KEY=.

Estimated code review effort: 2 (Simple) | ~10 minutes

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jason-Shen2

Copy link
Copy Markdown
Author

Duplicate of #3402 which already fixes this issue.

@Jason-Shen2 Jason-Shen2 closed this Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: shellexec writes empty XDG_*_HOME when $SNAP is inherited from any snap (not just Wave-as-snap)

1 participant