Skip to content

[Bug] --profile flag does not override inherited OPENCLAW_* environment variables during gateway install #28236

@Fionaaa0124

Description

@Fionaaa0124

Bug Description

When running openclaw --profile hanyu gateway install, the generated LaunchAgent plist contains environment variables (OPENCLAW_STATE_DIR, OPENCLAW_CONFIG_PATH, OPENCLAW_GATEWAY_PORT) from the default profile instead of the specified profile.

Root Cause

In entry.js, applyCliProfileEnv uses conditional assignment:

if (!env.OPENCLAW_STATE_DIR?.trim()) env.OPENCLAW_STATE_DIR = stateDir;
if (!env.OPENCLAW_CONFIG_PATH?.trim()) env.OPENCLAW_CONFIG_PATH = path.join(stateDir, "openclaw.json");

When the shell already has OPENCLAW_STATE_DIR and OPENCLAW_CONFIG_PATH set (e.g., inherited from a running default gateway session or .zshrc), the --profile flag silently fails to override them.

Steps to Reproduce

  1. Have a default gateway running (which sets OPENCLAW_STATE_DIR=~/.openclaw in the environment)
  2. Run: openclaw --profile hanyu gateway install
  3. Inspect the generated plist: cat ~/Library/LaunchAgents/ai.openclaw.hanyu.plist
  4. Observe that OPENCLAW_STATE_DIR is ~/.openclaw instead of ~/.openclaw-hanyu
  5. OPENCLAW_CONFIG_PATH, OPENCLAW_GATEWAY_PORT, --port, StandardOutPath, and StandardErrorPath are also wrong

Expected Behavior

--profile <name> should unconditionally override OPENCLAW_STATE_DIR, OPENCLAW_CONFIG_PATH, and related variables, since the user explicitly requested a different profile.

Suggested Fix

function applyCliProfileEnv(params) {
    const env = params.env ?? process.env;
    const homedir = params.homedir ?? os.homedir;
    const profile = params.profile.trim();
    if (!profile) return;
    env.OPENCLAW_PROFILE = profile;
    // Always override when --profile is explicitly provided
    const stateDir = resolveProfileStateDir(profile, env, homedir);
    env.OPENCLAW_STATE_DIR = stateDir;
    env.OPENCLAW_CONFIG_PATH = path.join(stateDir, "openclaw.json");
    if (profile === "dev" && !env.OPENCLAW_GATEWAY_PORT?.trim()) {
        env.OPENCLAW_GATEWAY_PORT = "19001";
    }
}

Impact

  • Any multi-gateway / multi-profile deployment
  • Any shell session that inherits OPENCLAW_* env vars before running --profile

Environment

  • OpenClaw: 2026.2.26
  • Platform: macOS Darwin 25.2.0 (arm64)
  • Node: v22.22.0

Workaround

Manually edit the generated plist file to correct the environment variables, paths, and port.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions