Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions cmd/codeaf/app_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,35 @@ func newAppFlags() appFlags {
}
}

// useAppHomeEarly applies `codeaf app --home <dir>` at the very start of the
// process, before anything reads the state root.
// useAppHomeEarly applies `codeaf app --home <dir>` and
// `codeaf phone serve --home <dir>` at the very start of the process, before
// anything reads the state root.
//
// main reads the state root before runApp has parsed a flag: the legacy-folder
// adoption, the first look at the root's identity (home.Witness), and the
// crash-telemetry consent, which is read from the profile under it. Left to
// runApp, an app started with --home would have its consent read from the
// command line's folder and could adopt the command line's legacy folder.
// Whatever is wrong with the rest of the command line is runApp's to say: this
// reads the flag the way runApp will and acts on nothing else.
// main reads the state root before runApp or runPhoneContext has parsed a flag:
// the legacy-folder adoption, the first look at the root's identity
// (home.Witness), and the crash-telemetry consent, which is read from the
// profile under it. Left to the command handler, a run started with --home
// would have its consent read from the default folder and could adopt the
// default folder's legacy folder. Whatever is wrong with the rest of the command
// line is the command handler's to say: this reads the flag the way the command
// will and acts on nothing else.
func useAppHomeEarly(args []string) {
if len(args) == 0 || args[0] != "app" {
if len(args) == 0 {
return
}
names := newAppFlags()
_ = names.set.Parse(args[1:])
_, _ = useAppHome(*names.home)
switch args[0] {
case "app":
names := newAppFlags()
_ = names.set.Parse(args[1:])
_, _ = useAppHome(*names.home)
case "phone":
if len(args) < 2 || args[1] != "serve" {
return
}
names := newPhoneServeFlags()
_ = names.set.Parse(reorder(names.set, args[2:]))
_, _ = useAppHome(*names.home)
}
}

// useAppHome points this process at the state folder the app asked for with
Expand Down
39 changes: 39 additions & 0 deletions cmd/codeaf/app_home_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func TestAppHomeFlagIsReadBeforeTheStateRootIsRead(t *testing.T) {
{"app", "--home=" + state},
{"app", "-home", state},
{"app", "--turn-long", "5s", "--home", state},
{"phone", "serve", "--home", state},
{"phone", "serve", "--home=" + state},
{"phone", "serve", "-home", state},
{"phone", "serve", "--accept-older-list", "--home", state},
} {
t.Setenv(home.EnvVar, "")
t.Setenv(home.LoginEnvVar, "")
Expand All @@ -89,12 +93,47 @@ func TestAppHomeFlagIsReadBeforeTheStateRootIsRead(t *testing.T) {
}
}

func TestPhoneServeHomeFlagAppliesBeforeTelemetryChoiceAndDefaultHome(t *testing.T) {
login := t.TempDir()
state := filepath.Join(t.TempDir(), "phone-state")
defaultProfile := filepath.Join(login, ".codeaf")
if err := os.MkdirAll(defaultProfile, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(defaultProfile, "config.yaml"), []byte("telemetry: false\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(state, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(state, "config.yaml"), []byte("telemetry: true\n"), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("HOME", login)
t.Setenv(home.EnvVar, "")
t.Setenv(home.LoginEnvVar, "")
t.Setenv("DO_NOT_TRACK", "")
t.Setenv("CODEAF_TELEMETRY", "")

useAppHomeEarly([]string{"phone", "serve", "--home", state})
if got := home.Dir(); got != state {
t.Fatalf("home.Dir() = %q, want %q", got, state)
}
if telemetryConfiguredOff() {
t.Fatalf("telemetryConfiguredOff() read default home (%s) instead of --home (%s)", defaultProfile, state)
}
}

func TestAppHomeFlagIsLeftAloneByEveryOtherCommand(t *testing.T) {
t.Setenv("HOME", t.TempDir())
for _, args := range [][]string{
nil,
{"app"},
{"app", "--schema"},
{"phone"},
{"phone", "serve"},
{"phone", "--home", "/elsewhere"},
{"phone", "other", "--home", "/elsewhere"},
{"chat", "--home", "/elsewhere"},
{"exec", "--home", "/elsewhere", "say hi"},
{"version"},
Expand Down
10 changes: 10 additions & 0 deletions docs/changes/unreleased/3022-phone-serve-home-early.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
kind: fixed
title: codeaf phone serve applies --home before startup reads the default home
pr: 3022
surface: [remote]
invalidates:
- "Was true: `codeaf phone serve --home <dir>` applied `--home` only inside `runPhoneContext`, so startup home adoption, `home.Witness`, and telemetry consent read the default state root first. Is true now: `useAppHomeEarly` applies `codeaf phone serve --home <dir>` at the start of `main` alongside `codeaf app --home <dir>`."
---

`useAppHomeEarly` now parses `codeaf phone serve`'s `--home` flag (using the same `newPhoneServeFlags` and flag reordering as `runPhoneContext`) before `main` runs `home.Adopt`, `home.Witness`, or telemetry configuration.