Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-6476 fix(execenv): expand $OPENCLAW_HOME in the openclaw active config path (#7310)

VvVad2bdd3e38b8
diff --git a/server/internal/daemon/execenv/openclaw_config.go b/server/internal/daemon/execenv/openclaw_config.go
index 46ec7b1228..5fcbb79000 100644
--- a/server/internal/daemon/execenv/openclaw_config.go
+++ b/server/internal/daemon/execenv/openclaw_config.go
@@ -814,11 +814,93 @@ func openclawTildeRest(path string) (string, bool) {
return "", false
}
+// openclawHomeRest recognizes the symbolic shape current OpenClaw releases
+// print when OPENCLAW_HOME is set: the variable name rather than its value, for
+// example `$OPENCLAW_HOME\.openclaw\openclaw.json`. Nothing downstream expands
+// it, so the line reaches filepath.Abs as an ordinary relative path and lands
+// under the daemon's working directory — the same stat miss #6630 fixed for the
+// tilde form, with the same consequence: indistinguishable from a fresh
+// install, so the wrapper drops the user's `$include` and the task boots
+// without their model providers or auth profiles.
+//
+// The shape is guaranteed rather than incidental. At `v2026.5.27`,
+// `src/cli/config-cli.ts`'s `runConfigFile` prints `shortenHomePath(...)`, and
+// `src/utils.ts:147-157` uses the `$OPENCLAW_HOME` prefix whenever that variable
+// is non-empty and `~` otherwise — so setting the variable is what selects this
+// form.
+//
+// Only the bare spelling is emitted by that code path. `${OPENCLAW_HOME}` is
+// accepted as defense against a release that spells it the other way, not
+// because anything is known to print it. Both separators are accepted
+// regardless of runtime.GOOS, for the reason openclawTildeRest gives above.
+func openclawHomeRest(path string) (string, bool) {
+ for _, prefix := range []string{"$OPENCLAW_HOME", "${OPENCLAW_HOME}"} {
+ if path == prefix {
+ return "", true
+ }
+ if len(path) > len(prefix) && strings.HasPrefix(path, prefix) &&
+ (path[len(prefix)] == '/' || path[len(prefix)] == '\\') {
+ return path[len(prefix)+1:], true
+ }
+ }
+ return "", false
+}
+
+// openclawHomeFromEnv resolves OPENCLAW_HOME to the directory the CLI's printed
+// `$OPENCLAW_HOME` prefix actually stands for.
+//
+// The value itself may be a tilde path. Upstream documents it
+// (`docs/help/environment.md` at `v2026.5.27`: "OPENCLAW_HOME can also be set to
+// a tilde path (e.g. `~/svc`), which gets expanded using the same OS home
+// fallback chain before use") and implements it in `src/infra/home-dir.ts:41-47`.
+// It expands the value *before* computing the home that `config file` then
+// shortens, so on such a host the printed `$OPENCLAW_HOME` stands for
+// `<os-home>/svc`, and landing on the same file means resolving it the same way.
+// Joining the raw value instead leaves the `~` embedded, filepath.Abs turns that
+// into a confident absolute path under the daemon's working directory, and the
+// stat miss reproduces the silent fresh-install failure this whole branch exists
+// to remove.
+//
+// Only the tilde is expanded here, deliberately — not the variable form — so a
+// self-referential value cannot resolve against itself.
+func openclawHomeFromEnv() (string, error) {
+ home := strings.TrimSpace(os.Getenv("OPENCLAW_HOME"))
+ if home == "" {
+ return "", errors.New("environment variable is empty")
+ }
+ rest, isTilde := openclawTildeRest(home)
+ if !isTilde {
+ return home, nil
+ }
+ osHome, err := os.UserHomeDir()
+ if err != nil {
+ return "", fmt.Errorf("expand `~` in OPENCLAW_HOME: %w", err)
+ }
+ if rest == "" {
+ return osHome, nil
+ }
+ return filepath.Join(osHome, rest), nil
+}
+
func expandOpenclawPath(path string) (string, error) {
- if rest, isTilde := openclawTildeRest(path); isTilde {
+ // OPENCLAW_HOME before `~`: the two shapes are mutually exclusive, but the
+ // variable form is checked first because an empty OPENCLAW_HOME has to fail
+ // loudly rather than fall through to a relative-path resolution that would
+ // quietly produce a wrong absolute path.
+ if rest, isOpenclawHome := openclawHomeRest(path); isOpenclawHome {
+ home, herr := openclawHomeFromEnv()
+ if herr != nil {
+ return "", fmt.Errorf("expand OPENCLAW_HOME in openclaw config path %q: %w", path, herr)
+ }
+ if rest == "" {
+ path = home
+ } else {
+ path = filepath.Join(home, rest)
+ }
+ } else if rest, isTilde := openclawTildeRest(path); isTilde {
home, herr := os.UserHomeDir()
if herr != nil {
- return "", fmt.Errorf("expand `~` in openclaw config path: %w", herr)
+ return "", fmt.Errorf("expand `~` in openclaw config path %q: %w", path, herr)
}
if rest == "" {
path = home
diff --git a/server/internal/daemon/execenv/openclaw_config_test.go b/server/internal/daemon/execenv/openclaw_config_test.go
index 445d0ef782..682a958a5e 100644
--- a/server/internal/daemon/execenv/openclaw_config_test.go
+++ b/server/internal/daemon/execenv/openclaw_config_test.go
@@ -1746,6 +1746,255 @@ func TestExpandOpenclawPathTildeSeparators(t *testing.T) {
}
}
+// TestExpandOpenclawPathOpenclawHome — the same failure as #6630 in a second
+// shape. When OPENCLAW_HOME is set, current releases print the variable name
+// instead of its value, and an unexpanded `$OPENCLAW_HOME\...` line is not
+// absolute, so it lands under the daemon's working directory and stats as
+// missing — reported as a fresh install, wrapper without the user's $include.
+func TestExpandOpenclawPathOpenclawHome(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("OPENCLAW_HOME", home)
+
+ cases := []struct {
+ name string
+ in string
+ want string
+ }{
+ {name: "dollar Windows separator", in: `$OPENCLAW_HOME\.openclaw\openclaw.json`, want: filepath.Join(home, `.openclaw\openclaw.json`)},
+ {name: "braced POSIX separator", in: `${OPENCLAW_HOME}/.openclaw/openclaw.json`, want: filepath.Join(home, ".openclaw", "openclaw.json")},
+ {name: "bare variable", in: `$OPENCLAW_HOME`, want: home},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := expandOpenclawPath(tc.in)
+ if err != nil {
+ t.Fatalf("expandOpenclawPath(%q): %v", tc.in, err)
+ }
+ if got != tc.want {
+ t.Errorf("expandOpenclawPath(%q) = %q, want %q", tc.in, got, tc.want)
+ }
+ })
+ }
+}
+
+// TestExpandOpenclawPathOpenclawHomeUnsetFailsLoudly — the variable form with
+// nothing to expand it to must be an error, not a relative-path fallback.
+// Falling through would resolve the literal `$OPENCLAW_HOME` segment against
+// the daemon's working directory and hand back a confident absolute path to a
+// file that cannot exist, which is exactly the silent-fresh-install failure
+// this shape causes in the first place.
+func TestExpandOpenclawPathOpenclawHomeUnsetFailsLoudly(t *testing.T) {
+ t.Setenv("OPENCLAW_HOME", "")
+
+ got, err := expandOpenclawPath(`$OPENCLAW_HOME/.openclaw/openclaw.json`)
+ if err == nil {
+ t.Fatalf("expandOpenclawPath returned %q, want an error when OPENCLAW_HOME is empty", got)
+ }
+ if !strings.Contains(err.Error(), "OPENCLAW_HOME") {
+ t.Errorf("error %q does not name the variable that could not be expanded", err.Error())
+ }
+ // The path is what the reader of a daemon log needs in order to act.
+ if !strings.Contains(err.Error(), `"$OPENCLAW_HOME/.openclaw/openclaw.json"`) {
+ t.Errorf("error %q does not name the path being expanded", err.Error())
+ }
+}
+
+// TestPrepareOpenclawConfigExpandsOpenclawHome — end-to-end guard, mirroring
+// TestPrepareOpenclawConfigExpandsWindowsTilde below: the banner-then-path
+// output shape with the variable form must still produce a wrapper that
+// $includes the user's config, and the include-root grant that goes with it.
+func TestPrepareOpenclawConfigExpandsOpenclawHome(t *testing.T) {
+ envRoot := t.TempDir()
+ workDir := filepath.Join(envRoot, "workdir")
+ if err := os.MkdirAll(workDir, 0o755); err != nil {
+ t.Fatalf("mkdir workdir: %v", err)
+ }
+
+ openclawHome := t.TempDir()
+ t.Setenv("OPENCLAW_HOME", openclawHome)
+
+ // Built with filepath.Join for the reason the tilde test gives: the
+ // remainder arrives with the CLI's separators and production normalizes it
+ // the same way, so on a non-Windows host this is one oddly-named file.
+ wantPath := filepath.Join(openclawHome, `.openclaw\openclaw.json`)
+ if err := os.MkdirAll(filepath.Dir(wantPath), 0o755); err != nil {
+ t.Fatalf("mkdir user cfg dir: %v", err)
+ }
+ if err := os.WriteFile(wantPath, []byte(`{}`), 0o600); err != nil {
+ t.Fatalf("write user cfg: %v", err)
+ }
+
+ banner := "|\no Config warnings ---------------------------------+\n" +
+ "| - plugins.entries.duckduckgo: plugin not found |\n" +
+ "+--------------------------------------------------+\n" +
+ `$OPENCLAW_HOME\.openclaw\openclaw.json` + "\n"
+ stub := installOpenclawStub(t, map[string]openclawResponse{
+ "config file": {stdout: banner},
+ "config get agents.list --json": {stdout: "null"},
+ })
+
+ result, err := prepareOpenclawConfig(envRoot, workDir, OpenclawConfigPrep{OpenclawBin: stub.bin})
+ if err != nil {
+ t.Fatalf("prepareOpenclawConfig: %v", err)
+ }
+ got := mustReadJSON(t, result.ConfigPath)
+ include, ok := got["$include"].([]any)
+ if !ok {
+ t.Fatalf("wrapper has no $include — the user's models and auth profiles would be lost: %#v", got)
+ }
+ if include[0] != wantPath {
+ t.Errorf("$include[0] = %v, want %q", include[0], wantPath)
+ }
+ if result.IncludeRoot != filepath.Dir(wantPath) {
+ t.Errorf("IncludeRoot = %q, want %q", result.IncludeRoot, filepath.Dir(wantPath))
+ }
+}
+
+// TestExpandOpenclawPathOpenclawHomeIsItselfATilde — the variable's *value* may
+// be a tilde path. Upstream documents that and expands it before computing the
+// home `config file` then shortens (`docs/help/environment.md` and
+// `src/infra/home-dir.ts:41-47` at `v2026.5.27`), so the printed
+// `$OPENCLAW_HOME` stands for `<os-home>/svc` and the daemon has to land on the
+// same file. Joining the raw value leaves the `~` embedded, filepath.Abs makes
+// that absolute under the daemon's working directory, and the stat miss is once
+// again reported as a fresh install — the failure this whole branch removes,
+// reached through the branch itself.
+func TestExpandOpenclawPathOpenclawHomeIsItselfATilde(t *testing.T) {
+ osHome := t.TempDir()
+ t.Setenv("HOME", osHome)
+ t.Setenv("USERPROFILE", osHome)
+
+ cases := []struct {
+ name string
+ env string
+ in string
+ want string
+ }{
+ {
+ name: "tilde value, posix remainder",
+ env: "~/svc",
+ in: `$OPENCLAW_HOME/.openclaw/openclaw.json`,
+ want: filepath.Join(osHome, "svc", ".openclaw", "openclaw.json"),
+ },
+ {
+ name: "tilde value, windows remainder",
+ env: "~/svc",
+ in: `$OPENCLAW_HOME\.openclaw\openclaw.json`,
+ want: filepath.Join(osHome, "svc", `.openclaw\openclaw.json`),
+ },
+ {
+ name: "tilde value with windows separator",
+ env: `~\svc`,
+ in: `$OPENCLAW_HOME/.openclaw/openclaw.json`,
+ want: filepath.Join(osHome, "svc", ".openclaw", "openclaw.json"),
+ },
+ {
+ name: "bare tilde value",
+ env: "~",
+ in: `$OPENCLAW_HOME/.openclaw/openclaw.json`,
+ want: filepath.Join(osHome, ".openclaw", "openclaw.json"),
+ },
+ {
+ name: "bare variable, tilde value",
+ env: "~/svc",
+ in: `$OPENCLAW_HOME`,
+ want: filepath.Join(osHome, "svc"),
+ },
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Setenv("OPENCLAW_HOME", tc.env)
+ got, err := expandOpenclawPath(tc.in)
+ if err != nil {
+ t.Fatalf("expandOpenclawPath(%q) with OPENCLAW_HOME=%q: %v", tc.in, tc.env, err)
+ }
+ if strings.Contains(got, "~") {
+ t.Errorf("expandOpenclawPath(%q) = %q, want the value's `~` expanded (a literal ~ can never stat)", tc.in, got)
+ }
+ if got != tc.want {
+ t.Errorf("expandOpenclawPath(%q) with OPENCLAW_HOME=%q = %q, want %q", tc.in, tc.env, got, tc.want)
+ }
+ })
+ }
+}
+
+// TestExpandOpenclawPathLeavesLookalikePrefixesAlone — the risk claim is that no
+// path which works today changes, and this is what guards it. A variable whose
+// name merely starts with OPENCLAW_HOME must not be treated as the shape.
+func TestExpandOpenclawPathLeavesLookalikePrefixesAlone(t *testing.T) {
+ t.Setenv("OPENCLAW_HOME", t.TempDir())
+
+ for _, in := range []string{
+ `$OPENCLAW_HOMEX/y`,
+ `$OPENCLAW_HOME_EXTRA/y`,
+ `${OPENCLAW_HOMEX}/y`,
+ `$OPENCLAW/y`,
+ } {
+ t.Run(in, func(t *testing.T) {
+ got, err := expandOpenclawPath(in)
+ if err != nil {
+ t.Fatalf("expandOpenclawPath(%q): %v", in, err)
+ }
+ // Untouched by the new branch: still resolved as an ordinary
+ // relative path, exactly as on `main`.
+ want, aerr := filepath.Abs(in)
+ if aerr != nil {
+ t.Fatalf("filepath.Abs(%q): %v", in, aerr)
+ }
+ if got != want {
+ t.Errorf("expandOpenclawPath(%q) = %q, want it left as a relative path -> %q", in, got, want)
+ }
+ })
+ }
+}
+
+// TestPrepareOpenclawConfigExpandsTildeValuedOpenclawHome — the same defect at
+// the level that decides what the user actually gets. A unit assertion on
+// expandOpenclawPath would not have caught it: the value's `~` survived into a
+// path that looks absolute, so only following it through to the wrapper shows
+// the `$include` going missing.
+func TestPrepareOpenclawConfigExpandsTildeValuedOpenclawHome(t *testing.T) {
+ envRoot := t.TempDir()
+ workDir := filepath.Join(envRoot, "workdir")
+ if err := os.MkdirAll(workDir, 0o755); err != nil {
+ t.Fatalf("mkdir workdir: %v", err)
+ }
+
+ osHome := t.TempDir()
+ t.Setenv("HOME", osHome)
+ t.Setenv("USERPROFILE", osHome)
+ t.Setenv("OPENCLAW_HOME", "~/svc")
+
+ wantPath := filepath.Join(osHome, "svc", ".openclaw", "openclaw.json")
+ if err := os.MkdirAll(filepath.Dir(wantPath), 0o755); err != nil {
+ t.Fatalf("mkdir user cfg dir: %v", err)
+ }
+ if err := os.WriteFile(wantPath, []byte(`{}`), 0o600); err != nil {
+ t.Fatalf("write user cfg: %v", err)
+ }
+
+ stub := installOpenclawStub(t, map[string]openclawResponse{
+ "config file": {stdout: `$OPENCLAW_HOME/.openclaw/openclaw.json` + "\n"},
+ "config get agents.list --json": {stdout: "null"},
+ })
+
+ result, err := prepareOpenclawConfig(envRoot, workDir, OpenclawConfigPrep{OpenclawBin: stub.bin})
+ if err != nil {
+ t.Fatalf("prepareOpenclawConfig: %v", err)
+ }
+ got := mustReadJSON(t, result.ConfigPath)
+ include, ok := got["$include"].([]any)
+ if !ok {
+ t.Fatalf("wrapper has no $include — the user's models and auth profiles would be lost: %#v", got)
+ }
+ if include[0] != wantPath {
+ t.Errorf("$include[0] = %v, want %q", include[0], wantPath)
+ }
+ if result.IncludeRoot != filepath.Dir(wantPath) {
+ t.Errorf("IncludeRoot = %q, want %q", result.IncludeRoot, filepath.Dir(wantPath))
+ }
+}
+
// TestPrepareOpenclawConfigExpandsWindowsTilde — end-to-end guard for #6630:
// the reporter's exact `openclaw config file` output (a config-warning banner
// followed by a Windows-shortened path) must still produce a wrapper that