Skip to content

Nit: Refactor buildSandboxEnvArgs to avoid parsing CLI args back to key-value pairs #1016

@acoliver

Description

@acoliver

Description

CodeRabbit suggested refactoring buildSandboxEnvArgs to return key-value pairs instead of CLI-formatted args.

The function buildSandboxEnvArgs produces CLI-formatted args (--env, K=V), which are then parsed back into key-value pairs at the call site. This is an indirect approach.

Suggested Refactor

Create a helper that returns the raw key-value pairs, then format as CLI args where needed:

// Returns { key: value } pairs for passthrough env vars
function getPassthroughEnvVars(env: NodeJS.ProcessEnv): Record<string, string> {
  const passthroughVariables = [
    'LLXPRT_CODE_IDE_SERVER_PORT',
    'LLXPRT_CODE_IDE_WORKSPACE_PATH',
    'LLXPRT_CODE_WELCOME_CONFIG_PATH',
    'TERM_PROGRAM',
  ];
  const result: Record<string, string> = {};
  for (const envVar of passthroughVariables) {
    const value = env[envVar];
    if (value) {
      result[envVar] = value;
    }
  }
  return result;
}

export function buildSandboxEnvArgs(env: NodeJS.ProcessEnv): string[] {
  const args: string[] = [];
  for (const [key, value] of Object.entries(getPassthroughEnvVars(env))) {
    args.push('--env', `${key}=${value}`);
  }
  return args;
}

Then use Object.assign(sandboxEnv, getPassthroughEnvVars(process.env)) instead of the parsing loop at the call site.

Location

packages/cli/src/utils/sandbox.ts - buildSandboxEnvArgs function around line 56-73, and usage around line 334-340.

Reference

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions