Skip to content

feat: add support for environment variables in new-session command#208

Closed
pbolduc wants to merge 1 commit into
psmux:masterfrom
pbolduc:feature/new-session-env
Closed

feat: add support for environment variables in new-session command#208
pbolduc wants to merge 1 commit into
psmux:masterfrom
pbolduc:feature/new-session-env

Conversation

@pbolduc

@pbolduc pbolduc commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Here is my attempt at adding this feature. I dont really code in Rust, so if there is any AI slop, please point it out. I didn't like these big blocks added to the -e parsing in main.rs, commands.rs and connection.rs. Please advise of any changes you would like to see.

  • Enhanced the new-session command to accept -e VAR=value flags for setting environment variables.
  • Implemented parsing for environment variable assignments and integrated them into the session environment.
  • Updated relevant functions to handle the new session environment variables, ensuring they are applied after loading the configuration.
  • Added tests for parsing and collecting environment variable arguments to ensure correctness.

Fixes #205

- Enhanced the `new-session` command to accept `-e VAR=value` flags for setting environment variables.
- Implemented parsing for environment variable assignments and integrated them into the session environment.
- Updated relevant functions to handle the new session environment variables, ensuring they are applied after loading the configuration.
- Added tests for parsing and collecting environment variable arguments to ensure correctness.

This feature allows users to define environment variables for new sessions directly from the command line, improving flexibility and usability.
@psmux

psmux commented Apr 13, 2026

Copy link
Copy Markdown
Owner

Hey @pbolduc, really appreciate you taking the time to write this PR! Your code quality is genuinely impressive, especially for someone who says they do not normally code in Rust.

I had already started working on a fix for #205 before I saw your PR (commit 38d7cfa), but after reviewing your approach I immediately saw several things you did better than my initial implementation:

What I adopted from your PR (commit 9926b85):

  • Utility functions in util.rs: Your parse_env_assignment(), parse_new_session_e_value_token(), collect_server_session_env_args(), and merge_session_env_into_app() are much cleaner than my original inline parsing. I adopted all of them.
  • Vec<(String, String)> type: Much better than my original Vec<String> approach. Pre-parsing the key/value pairs gives type safety and avoids repeated find('=') calls.
  • is_valid_env_var_name() validation: My first pass silently ignored malformed values. Your validation with clear error messages ("invalid -e: expected VARIABLE=value", "invalid -e: invalid environment variable name") is the right behavior.
  • Error reporting in connection.rs: Your pattern of storing session_env_parse_err and reporting it to both persistent and non-persistent clients is exactly right.
  • Tests: Adopted your test_new_session_env.rs and all the util::tests for parsing, -- boundary handling, and duplicate key behavior.

One timing difference I kept from my implementation:

Your PR applies env vars AFTER load_config():

crate::config::populate_default_bindings(&mut app);
load_config(&mut app);
// new-session -e: apply after config so CLI overrides set-environment from config
crate::util::merge_session_env_into_app(&mut app, &session_env);

The issue is that spawn_warm_pane() runs before load_config(), so the warm pane's shell process would start without the env vars. In my implementation, env vars are applied BEFORE spawn_warm_pane(), and when -e is specified, warm server claiming is bypassed entirely to force a cold start. This ensures the first pane always inherits the env vars.

Your idea of "CLI overrides config" is a valid design choice though. Since we skip warm pane entirely when -e is present (cold start path), the config loads first and then gets overridden by the env vars naturally in the create_window() path.

Since I have already merged the core ideas from your PR into master, I will close this PR. But seriously, thank you for the thorough contribution. The validation, utility functions, and tests made the implementation significantly better. Looking forward to more contributions from you!

@psmux

psmux commented Apr 13, 2026

Copy link
Copy Markdown
Owner

Closing as the improvements have been incorporated into master (38d7cfa + 9926b85). Thank you for the excellent contribution!

@psmux psmux closed this Apr 13, 2026
@psmux psmux reopened this Apr 13, 2026
psmux added a commit that referenced this pull request Apr 13, 2026
Incorporates the best ideas from @pbolduc's PR #208:

- Added utility functions in util.rs: parse_env_assignment(),
  parse_new_session_e_value_token(), collect_server_session_env_args(),
  merge_session_env_into_app(), is_valid_env_var_name()
- Switched from Vec<String> to Vec<(String, String)> for type safety
- Added validation: rejects invalid env var names (123=x, bad-name=x)
  and missing values with clear error messages
- Added comprehensive unit tests from PR #208 plus additional tests
- Kept correct timing: env vars applied BEFORE spawn_warm_pane() so
  the first pane inherits them (PR #208 applied after, which would
  miss the warm pane)

Ref: #205, PR #208
@pbolduc

pbolduc commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

Completed by maintainer in another PR.

@pbolduc pbolduc closed this Apr 13, 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.

new-session -e is parsed for argv compatibility but does not set session environment

2 participants