Files
magpie/internal/autostart
e1b70c13de fix(autostart): a launch agent left half-written reads as Open at log… (#1079)
* fix(autostart): a launch agent left half-written reads as Open at login off, not on

The Mac's launch agent, the Linux .desktop and the Android boot script were
written with a plain write, which empties the file before it fills it, so a
crash or a full disk part way through left a record that is empty or half
there. launchd refuses a job that names neither its own label nor a program,
and the desktop starts no entry with no Exec, so magpie simply never opened
at login. Enabled() on the Mac and on Linux asked only whether the file was
there, so Settings kept showing the switch on; and Refresh(), which the app
runs at every start, rewrites only a record its ProgramArguments regexp can
still find a program in, which a half-written one does not have. Nothing
repaired it and nothing said so, so the user was left believing a switch
that did nothing.

The four writes of these records now go through edit.WriteAtomic, which
writes a temp file beside the record and renames it over: a write lands
whole or not at all, and one cut short leaves the previous record, still
loadable, where it was. No import cycle: internal/edit brings in only
internal/steady and internal/filememo.

enabled() on the Mac and on Linux now asks whether the record is one that
starts magpie — the launch agent names both its label and its program, the
desktop entry has both its Type and its Exec — instead of whether the file
is there. A record a write cut short now reads as off, so the switch stops
saying magpie opens at login when it does not, and the user sees the off
that Refresh() cannot repair. A record magpie did not write is still left
alone by Refresh() (TestRefreshOlderLaunchAgent), which is unchanged:
refresh() rewrites what it can parse, and enabled() reports off for what it
cannot. Windows' Run key is not touched here.

TestSetThenTruncatedRecordReadsAsOff is new, once for the Mac
(autostart_darwin_test.go) and once for Linux (autostart_other_test.go),
where the record is written whole and then left as a write cut short leaves
it: the head, stopping before the program it names, and the same record
emptied. Against the old code, whose check was os.Stat, the Mac one fails
with "a launch agent with no ProgramArguments reads as on" and the Linux one
with "a desktop entry with no Exec reads as on", each on both records. With
the change both pass, and TestSet, TestRefreshOlderLaunchAgent and
TestLaunchAgentLetsTheRelaunchLive are unchanged.

go vet, the windows, darwin and linux builds and test compiles, and gofmt
on the seven files (LF-normalized first, since the worktree is CRLF) pass.
Not run on a Mac or with plutil: this is a Windows box, so the darwin and
linux behaviour is checked by GOOS=darwin and GOOS=linux builds plus their
test compiles, and the two new predicates were run here against the bytes
those platforms write from a scratch harness, which showed the old os.Stat
check reading all three records as on. That harness is not committed.

* fix(autostart): a record that names nothing to run reads as off

The Linux and Mac checks the last commit added ask for the Exec key and
the ProgramArguments array, not for anything in them, so a record whose
Exec is present and empty still read as on. A desktop starts no entry
that names nothing to run and launchd refuses a job whose program is an
empty string, so that is the same lie the truncated record told, in the
one shape the truncation could leave that the check didn't cover.

entryExec now wants a non-space after the key, and program wants
something other than the closing tag in the first string. Android's
check needs nothing here: it reads the file's exec bit, which a write
cut short does not leave on it.

Each of the two tests now writes the empty-valued record beside the
truncated one and the empty one, so all three shapes are covered.

* fix(autostart): the darwin check's new block sits inside the function

The block added to TestSetThenTruncatedRecordReadsAsOff landed after the
closing brace, so the darwin and linux jobs answered
"autostart_darwin_test.go:134:2: expected declaration, found empty" at the
gofmt step and nothing of the package was ever compiled there. Windows
never compiled the file, so the branch was green on the one platform that
could not see it.

The block now sits before the closing brace with the rest of the check.

Verified on Windows with go1.27.2: go vet ./internal/autostart passes for
windows/amd64, darwin/arm64 and linux/amd64, and the package's tests pass.

---------

Co-authored-by: TryWorld <TryWorld2026@users.noreply.github.com>
Co-authored-by: yetone <yetoneful@gmail.com>
2026-10-13 12:36:24 +08:00
..