Skip to content

feat: start CLI updates in a detached background process - #817

Open
zimeg wants to merge 9 commits into
mainfrom
zimeg-feat-auto-update-spawn
Open

zimeg wants to merge 9 commits into
mainfrom
zimeg-feat-auto-update-spawn

Conversation

@zimeg

@zimeg zimeg commented Nov 20, 2026

Copy link
Copy Markdown
Member

Changelog

N/A, groundwork for the auto-update experiment.

Summary

This pull request adds the hidden _autoupdate <version> command and startBackgroundUpdate, which starts that command in a detached process so a CLI update can install after a command without the developer waiting for it.

  • startBackgroundUpdate runs the binary at <install-script folder>/<binary name>, the folder the install script check matched, instead of os.Executable(). On Linux os.Executable reads /proc/self/exe, which follows a binary that an update moved into backups/<old>/. A running binary that resolves outside the install script folders, backups included, starts nothing.
  • The process starts with _autoupdate <version> plus --config-dir <absolute path> when the parent had one, so the result is recorded where the next command reads it.
  • It's detached: Setsid on Unix, and CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP on Windows. Stdin is empty, and stdout and stderr go to <logs>/autoupdate.log, truncated each time and opened in append mode. The parent calls Start then Release and doesn't wait.
  • The process runs from the install script folder rather than the parent's working directory. Its root pre-run and post-run would otherwise read the project there, set a missing project_id, and run the .slack folder cleanup after the developer's command has finished.
  • _autoupdate calls update.AutoUpdate with the running version and the target, and turns off telemetry before it runs.
  • The update check skips _autoupdate by the parsed command, as it does for upgrade, so the background process never starts its own check or prints a notice.
  • isInstallScript now wraps installScriptPath, which also returns the resolved path it matched. Its callers don't change.
  • Follows feat: install CLI updates with a lock and record the result #807. Nothing calls startBackgroundUpdate yet; the next PR starts it after commands with the auto-update experiment.

Preview

N/A, nothing changes for developers yet.

Testing

The unit tests cover the review focus for this step:

  • The background process runs the root pre-run and post-run: Test_UpdateNotification_isEnabled "skips the background update" and "… with a custom config directory" (--config-dir after the command) are false.
  • --config-dir: Test_backgroundUpdateArgs keeps a custom config directory and makes a relative one absolute. Test_startBackgroundUpdate "starts the binary with a custom config directory" checks the arguments the started process got.
  • The Linux rename: Test_startBackgroundUpdate "skips a running binary that moved to the backups folder" mocks os.Executable() to home/.slack/bin/backups/v4.11.0/slack and starts nothing. "… from a symbolic link" starts the install script binary, not the link.
  • Detach: Test_startBackgroundUpdate re-runs the test binary as the started process. It waits for a file the test writes only after startBackgroundUpdate returns, then reports its arguments, working directory, and whether it leads its own session. The log holds only its output. Test_detach checks the SysProcAttr on each platform.
  • Telemetry: Test_NewAutoUpdateCommand checks telemetry is off before AutoUpdate runs. The usage event is sent after the command returns and reads that flag then.

Manual steps, in a sandboxed HOME (a dev build counts as newer than the latest release, so build it with an older version):

go build -ldflags "-X 'github.com/slackapi/slack-cli/internal/version.Version=v4.13.0'" -o /tmp/slack .
mkdir -p /tmp/c3home/.slack/bin /tmp/c3home/config && cp /tmp/slack /tmp/c3home/.slack/bin/slack
HOME=/tmp/c3home /tmp/c3home/.slack/bin/slack _autoupdate 4.14.0 --config-dir /tmp/c3home/config --apihost https://dev.slack.com --verbose
  • The binary is updated to v4.14.0, the old one is backed up to /tmp/c3home/config/bin/backups/v4.13.0, and /tmp/c3home/config/autoupdate.json is {"from":"v4.13.0","to":"v4.14.0"}.
  • No FlushToLogstash line is printed. The same flags on auth list print one.
  • With a build stamped v4.14.0 in that folder, _autoupdate 4.14.0 prints "Skipping the update since 4.14.0 is already installed", with no backup and no record.

Notes

  • The log is opened with os rather than clients.Fs, since the process needs a file descriptor, as with the lock in feat: install CLI updates with a lock and record the result #807.
  • Windows uses CREATE_NO_WINDOW rather than DETACHED_PROCESS from the plan. A process with no console gives each console program it starts a new console window, and the background process runs slack --version twice (the already-installed check and the verify step), so each would flash a window. With a hidden console of its own, it still doesn't share the terminal's console or receive its interrupts. Windows wasn't run by hand; GOOS=windows builds and vets.
  • The log is opened in append mode, so if a second spawn truncates it while an earlier update is still writing, that output continues at the end instead of after a run of null bytes.
  • backgroundUpdateArgs returns an error too, from making the config directory absolute. It trims the flag first, like the parent does.
  • startBackgroundUpdateFunc comes with its caller in the next PR, since an unused variable fails lint.
  • On Unix the parent doesn't reap the finished process after Release, so it stays a zombie until the parent exits, right after the command in the next PR.
  • Tracing to Jaeger starts in main.go before flags are read and doesn't honor SLACK_DISABLE_TELEMETRY for any command, so the background process sends spans too. That's unchanged here.

Requirements

🤖 Generated with Claude Code

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@zimeg
zimeg requested a review from a team as a code owner November 20, 2026 10:15
@zimeg zimeg added this to the Next Release milestone Nov 20, 2026
@zimeg zimeg added enhancement M-T: A feature request for new functionality experiment Experimental feature accessed behind the --experiment flag or toggle semver:patch Use on pull requests to describe the release version increment labels Nov 20, 2026
@zimeg zimeg self-assigned this Nov 20, 2026
@zimeg
zimeg requested a review from mwbrooks November 20, 2026 10:15
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.53%. Comparing base (700a112) to head (d6126d7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/update/spawn.go 84.84% 5 Missing ⚠️
internal/update/install_script.go 71.42% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #817      +/-   ##
==========================================
+ Coverage   80.46%   80.53%   +0.06%     
==========================================
  Files         246      249       +3     
  Lines       18882    18931      +49     
==========================================
+ Hits        15194    15246      +52     
+ Misses       3688     3685       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cchensh cchensh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The detach is right: Setsid on Unix and CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP on Windows, stdin is /dev/null with output to the log, and Release without Wait leaves no zombie. Starting in the install folder can't pick up a project's .slack. The tests would fail without the change. Some nits and follow-ups, none blocking:

  1. spawn_windows.go:31: without CREATE_BREAKAWAY_FROM_JOB, a kill-on-close job (OpenSSH sessions, CI runners, some IDE terminals) kills the update when the parent's session ends, possibly mid-install. You could try breakaway first and fall back on ERROR_ACCESS_DENIED.
  2. Pre-existing, but it matters more now that installs run unattended (cli_autoupdate.go:251 then :176): the install renames the binary to the backup, then hard-links the new one. A SIGTERM in that gap (logout or shutdown) leaves no ~/.slack/bin/slack. Linking to a temp file and renaming over the old binary would make the swap atomic on Unix.
  3. spawn.go:55: O_TRUNC runs on every spawn before the child checks the lock, so a second spawn wipes the log of the update that's actually running. Truncating after the lock is taken (or skipping the spawn when TryLock fails) would keep it.
  4. spawn_test.go:121,129: the copied helper binaries have no .exe, and os.Symlink needs Developer Mode, so these would fail on Windows. They don't today only because unit tests run on macOS alone. A //go:build !windows or the .exe suffix would fix it.
  5. autoupdate.go:34: _autoupdate <version> takes any string into the download URL and the bin/<version> path. A semver check is cheap.

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 Thanks @zimeg! The re-exec test that checks the child's session, working directory and arguments makes the detach easy to trust.
One blocking comment on SLACK_CONFIG_DIR reaching the child, the rest are optional 🙏

Comment thread internal/update/spawn.go Resolved
Comment thread internal/update/spawn.go Resolved
Comment thread cmd/upgrade/autoupdate.go Resolved
Comment thread internal/update/update_test.go Resolved
Comment thread cmd/upgrade/autoupdate.go Resolved
Comment thread internal/update/spawn.go
// isn't read or changed. Output goes to the autoupdate log in the logs folder, which is opened
// with os because the process writes to a file descriptor.
func startBackgroundUpdate(ctx context.Context, clients *shared.ClientFactory, targetVersion string) error {
binaryPath, ok := installScriptPath(ctx, clients)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 praise: Resolving through installScriptPath means a binary moved into backups/ never starts an update, and the test pins that case.

Comment thread internal/update/spawn.go
}
defer logFile.Close()
command := exec.Command(binaryPath, args...)
command.Dir = filepath.Dir(binaryPath)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 praise: Running from the binary's folder keeps the child's InitConfig away from any project in the developer's directory.

…lid version

- Set SLACK_CONFIG_DIR to the absolute config directory, since it overrides --config-dir
- Return an invalid_semver error for a target that isn't a semantic version
- Let LogsDir create the logs folder
- Run the re-exec spawn test on Unix only and drop a duplicate isEnabled case

Co-authored-by: William Bergamin <25348381+WilliamBergamin@users.noreply.github.com>
Co-authored-by: Cheng Chen <35309699+cchensh@users.noreply.github.com>
Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@zimeg

zimeg commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

🌟 Thanks for the review @cchensh!

  • 4. In e982e86 the re-exec spawn test is in spawn_unix_test.go, so it builds only where unit tests run. Test_detach still checks the Windows flags.
  • 5. Also in e982e86: a target that isn't a semantic version returns invalid_semver.
  • 1, 2, 3. Saved as follow-ups: breaking away from a kill-on-close job on Windows, an atomic swap of the binary, and truncating the log only after the lock is taken.

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 @zimeg thanks for the quick turnaround! The child now gets the absolute config dir in both the env and the flag, and the test pins the relative SLACK_CONFIG_DIR case.

Left two small comments, nothing blocking 🙏

Comment thread internal/update/spawn.go Outdated
command := exec.Command(binaryPath, backgroundUpdateArgs(targetVersion, configDir)...)
command.Dir = filepath.Dir(binaryPath)
if configDir != "" {
command.Env = append(os.Environ(), slackConfigDirEnv+"="+configDir)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 praise: Setting SLACK_CONFIG_DIR to the same absolute path means an inherited relative value can't override --config-dir anymore, and the spawn test checks exactly that.

Comment thread cmd/upgrade/autoupdate.go Resolved
@WilliamBergamin
WilliamBergamin dismissed their stale review November 20, 2026 15:18

The blocking concerns are resolved; see the latest review.

@mwbrooks mwbrooks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Thanks @zimeg! Running the update from the install folder keeps it well away from a developer's project, and the SLACK_CONFIG_DIR fix landed quickly.

Comment thread cmd/upgrade/autoupdate.go Resolved
Comment thread internal/update/spawn.go Resolved

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 Thanks @zimeg! The main merge leaves this change exactly as reviewed, and the suite plus a Windows vet stay green on top of the tracer refactor.
Nothing new here, the earlier version-check suggestion is still optional 🚀

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 Thanks @zimeg! This merge only brings in the setup-go bump, so the change is exactly as reviewed, and the tests plus a Windows vet are still green.
Nothing new from me, the version-check suggestion is still optional 🚀

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏 @zimeg thanks for keeping this current with main; the new tracer flush doesn't change anything for the background process, and the tests still pass.
The open version-check suggestion from earlier is the only thing left, nothing blocking 💯

zimeg and others added 3 commits November 20, 2026 16:44
Co-authored-by: William Bergamin <25348381+WilliamBergamin@users.noreply.github.com>
Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
…config directory variable

Co-authored-by: William Bergamin <25348381+WilliamBergamin@users.noreply.github.com>
Co-authored-by: Michael Brooks <21328+mwbrooks@users.noreply.github.com>
Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@zimeg

zimeg commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

📌 leaving this open while i'm out next week (11-23 to 11-27). no rush to land it before the next release, so here's where it stands for anyone who wants to help:

  • reviews: approved by Michael and Cheng, and CI is green at d6126d7. every comment is applied or saved as a follow-up, and the 3 open threads are praise.
  • open question: should the background install be a hidden flag on upgrade (slack upgrade --auto-update <version>) instead of the hidden _autoupdate command? one command would keep every install in upgrade next to feat: install CLI updates without a prompt from the upgrade command with the auto-update experiment #818, and the _autoupdate skip in the update check would go away. it's a small rework, but the shape changes, so it'd want another look from reviewers.
  • saved follow-ups: breaking away from a kill-on-close job on Windows, an atomic swap of the binary, and truncating the log only after the lock is taken.

nothing calls startBackgroundUpdate yet, so this lands as groundwork either way 🧪

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪞 @zimeg thanks, IsReleaseVersion puts the check beside the other semver helpers and now turns away 4.12 and v4.12.0 before any download. One optional cleanup below, nothing blocking 💯

Comment thread internal/config/dotenv.go Resolved
Co-authored-by: William Bergamin <25348381+WilliamBergamin@users.noreply.github.com>
Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪓 Thanks @zimeg! With that last restating comment gone, the background update reads cleanly end to end, and the release check and config dir each have one home.
Nothing left from me, this looks good to land 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement M-T: A feature request for new functionality experiment Experimental feature accessed behind the --experiment flag or toggle semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants