Fixes #32336 When a dotfiles repo has no install script and a dotfile matches an existing directory in the symlink dir (almost always `~/.config`, because Coder clones the repo into `~/.config/coderv2/dotfiles`), `coder dotfiles` used to fail halfway with a raw `symlink ...: file exists`. Files before the conflict were already moved to `.bak` and relinked, and the rest were never linked. `coder dotfiles` now checks every target before it backs up or links anything. If any target is a non-empty directory (a symlink doesn't count), the command exits non-zero without creating symlinks or backups. The error lists every conflicting path and the recognized install script names. Empty directories are still replaced, and with no conflicts, behavior is the same as before. This PR doesn't add skipping or merging: custom installation through an install script is the supported way to manage directories, and the docs now say so. ``` cannot symlink dotfiles, these paths already exist as non-empty directories: /home/coder/.config Add an install script to your dotfiles repository to install them instead. Recognized names: install.sh, install, bootstrap.sh, ... ``` The new `TestDotfiles/SymlinkConflictingDir` fails on `main`, where `.bashrc` gets moved to `.bak` before `.config` errors, and passes with the fix. <details> <summary>Implementation plan and decisions</summary> ### Decisions - A target that exists as a non-empty directory (an `os.Lstat` result, so a symlink doesn't count) is a conflict, whatever the repo entry is. Empty directories are replaced, as on `main`. - All or nothing: check every target before any rename or symlink. On any conflict, create no symlinks or backups and exit non-zero. - The error lists every conflicting path and the names from `installScriptFiles()`, so it's right on Windows too. - No docs URL in the error. `coder dotfiles` has no API client, so it can't resolve a deployment's custom docs URL, and the error already gives the user what they need to act. - No merging and no skipping. Warning and continuing would leave the user's config silently unapplied. Merging adds behavior with edge cases (nested directories, existing files inside). An install script already covers custom installation. - With no conflicts, behavior is unchanged: existing files are backed up to `.bak`, and existing symlinks are replaced. ### Changes - `cli/dotfiles.go`: a check before the symlink loop that collects every conflicting directory and returns one error. - `cli/dotfiles_test.go`: `SymlinkEmptyDir` covers replacing an empty directory, then replacing the resulting directory symlink on a second run. `SymlinkConflictingDir`: The repo has `.config/foo` and `.bashrc`, and the symlink dir already has a `.config` directory and a regular `.bashrc`. The test asserts the error names `.config`, the existing directory and file are untouched, and no `.bak` file is created. - `docs/workspaces/workspace-dotfiles.md`: describes the symlink fallback, the `--symlink-dir` flag, the directory conflict failure, and that an install script replaces symlinking entirely. ### Verification - The test fails on `main` and passes with the fix. It also fails again when the conflict return is disabled. - `go test ./cli -run TestDotfiles`, `make pre-commit`, markdownlint, and Vale on the changed docs. - Doc-check self-check: all blocking findings fixed. </details> --- _This PR was generated by Coder Agents on behalf of @jeremyruppel._
431 lines
13 KiB
Go
431 lines
13 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/cli/clitest"
|
|
"github.com/coder/coder/v2/cli/config"
|
|
"github.com/coder/coder/v2/cryptorand"
|
|
)
|
|
|
|
func TestDotfiles(t *testing.T) {
|
|
t.Parallel()
|
|
// This test will time out if the user has commit signing enabled.
|
|
if _, gpgTTYFound := os.LookupEnv("GPG_TTY"); gpgTTYFound {
|
|
t.Skip("GPG_TTY is set, skipping test to avoid hanging")
|
|
}
|
|
t.Run("MissingArg", func(t *testing.T) {
|
|
t.Parallel()
|
|
inv, _ := clitest.New(t, "dotfiles")
|
|
err := inv.Run()
|
|
require.Error(t, err)
|
|
})
|
|
t.Run("NoInstallScript", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
// nolint:gosec
|
|
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", ".bashrc")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add .bashrc"`)
|
|
c.Dir = testRepo
|
|
out, err := c.CombinedOutput()
|
|
require.NoError(t, err, string(out))
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "wow")
|
|
})
|
|
t.Run("SwitchRepoDir", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
// nolint:gosec
|
|
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", ".bashrc")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add .bashrc"`)
|
|
c.Dir = testRepo
|
|
out, err := c.CombinedOutput()
|
|
require.NoError(t, err, string(out))
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "--repo-dir", "testrepo", "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "wow")
|
|
|
|
stat, staterr := os.Stat(filepath.Join(string(root), "testrepo"))
|
|
require.NoError(t, staterr)
|
|
require.True(t, stat.IsDir())
|
|
})
|
|
t.Run("SwitchRepoDirRelative", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
// nolint:gosec
|
|
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", ".bashrc")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add .bashrc"`)
|
|
c.Dir = testRepo
|
|
out, err := c.CombinedOutput()
|
|
require.NoError(t, err, string(out))
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "--repo-dir", "./relrepo", "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "wow")
|
|
|
|
stat, staterr := os.Stat(filepath.Join(string(root), "relrepo"))
|
|
require.NoError(t, staterr)
|
|
require.True(t, stat.IsDir())
|
|
})
|
|
t.Run("SymlinkBackup", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
// nolint:gosec
|
|
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
// add a conflicting file at destination
|
|
// nolint:gosec
|
|
err = os.WriteFile(filepath.Join(string(root), ".bashrc"), []byte("backup"), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", ".bashrc")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add .bashrc"`)
|
|
c.Dir = testRepo
|
|
out, err := c.CombinedOutput()
|
|
require.NoError(t, err, string(out))
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "wow")
|
|
|
|
// check for backup file
|
|
b, err = os.ReadFile(filepath.Join(string(root), ".bashrc.bak"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "backup")
|
|
|
|
// check for idempotency
|
|
inv, _ = clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
b, err = os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "wow")
|
|
b, err = os.ReadFile(filepath.Join(string(root), ".bashrc.bak"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "backup")
|
|
})
|
|
t.Run("SymlinkConflictingDir", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
err := os.MkdirAll(filepath.Join(testRepo, ".config"), 0o750)
|
|
require.NoError(t, err)
|
|
err = os.WriteFile(filepath.Join(testRepo, ".config", "foo"), []byte("foo"), 0o600)
|
|
require.NoError(t, err)
|
|
err = os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o600)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", ".")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add dotfiles"`)
|
|
c.Dir = testRepo
|
|
out, err := c.CombinedOutput()
|
|
require.NoError(t, err, string(out))
|
|
|
|
// add a conflicting directory and file at destination
|
|
configDir := filepath.Join(string(root), ".config")
|
|
err = os.MkdirAll(configDir, 0o750)
|
|
require.NoError(t, err)
|
|
err = os.WriteFile(filepath.Join(configDir, "existing"), []byte("existing"), 0o600)
|
|
require.NoError(t, err)
|
|
err = os.WriteFile(filepath.Join(string(root), ".bashrc"), []byte("original"), 0o600)
|
|
require.NoError(t, err)
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
|
|
err = inv.Run()
|
|
require.ErrorContains(t, err, configDir)
|
|
|
|
// nothing in the symlink dir was changed
|
|
fi, err := os.Lstat(configDir)
|
|
require.NoError(t, err)
|
|
require.True(t, fi.IsDir())
|
|
b, err := os.ReadFile(filepath.Join(configDir, "existing"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, "existing", string(b))
|
|
fi, err = os.Lstat(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.True(t, fi.Mode().IsRegular())
|
|
b, err = os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, "original", string(b))
|
|
require.NoFileExists(t, filepath.Join(string(root), ".bashrc.bak"))
|
|
})
|
|
t.Run("SymlinkEmptyDir", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
err := os.MkdirAll(filepath.Join(testRepo, ".vim"), 0o750)
|
|
require.NoError(t, err)
|
|
err = os.WriteFile(filepath.Join(testRepo, ".vim", "vimrc"), []byte("wow"), 0o600)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", ".")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add .vim"`)
|
|
c.Dir = testRepo
|
|
out, err := c.CombinedOutput()
|
|
require.NoError(t, err, string(out))
|
|
|
|
err = os.MkdirAll(filepath.Join(string(root), ".vim"), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
// the second run replaces the directory symlink from the first
|
|
for range 2 {
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), ".vim", "vimrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, "wow", string(b))
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestDotfilesInstallScriptUnix(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip()
|
|
}
|
|
|
|
t.Run("InstallScript", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
// nolint:gosec
|
|
err := os.WriteFile(filepath.Join(testRepo, "install.sh"), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", "install.sh")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add install.sh"`)
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "wow\n")
|
|
})
|
|
|
|
t.Run("NestedInstallScript", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
scriptPath := filepath.Join("script", "setup")
|
|
err := os.MkdirAll(filepath.Join(testRepo, "script"), 0o750)
|
|
require.NoError(t, err)
|
|
// nolint:gosec
|
|
err = os.WriteFile(filepath.Join(testRepo, scriptPath), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", scriptPath)
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add script"`)
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "wow\n")
|
|
})
|
|
|
|
t.Run("InstallScriptChangeBranch", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
// We need an initial commit to start the `main` branch
|
|
c := exec.Command("git", "commit", "--allow-empty", "-m", `"initial commit"`)
|
|
c.Dir = testRepo
|
|
err := c.Run()
|
|
require.NoError(t, err)
|
|
|
|
// nolint:gosec
|
|
err = os.WriteFile(filepath.Join(testRepo, "install.sh"), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "checkout", "-b", "other_branch")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "add", "install.sh")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add install.sh"`)
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "checkout", "main")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo, "-b", "other_branch")
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, string(b), "wow\n")
|
|
})
|
|
}
|
|
|
|
func TestDotfilesInstallScriptWindows(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if runtime.GOOS != "windows" {
|
|
t.Skip()
|
|
}
|
|
|
|
t.Run("InstallScript", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, root := clitest.New(t)
|
|
testRepo := testGitRepo(t, root)
|
|
|
|
// nolint:gosec
|
|
err := os.WriteFile(filepath.Join(testRepo, "install.ps1"), []byte("echo \"hello, computer!\" > "+filepath.Join(string(root), "greeting.txt")), 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "add", "install.ps1")
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "commit", "-m", `"add install.ps1"`)
|
|
c.Dir = testRepo
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
|
|
err = inv.Run()
|
|
require.NoError(t, err)
|
|
|
|
b, err := os.ReadFile(filepath.Join(string(root), "greeting.txt"))
|
|
require.NoError(t, err)
|
|
// If you squint, it does in fact say "hello, computer!" in here, but in
|
|
// UTF-16 and with a byte-order-marker at the beginning. Windows!
|
|
require.Equal(t, b, []byte("\xff\xfeh\x00e\x00l\x00l\x00o\x00,\x00 \x00c\x00o\x00m\x00p\x00u\x00t\x00e\x00r\x00!\x00\r\x00\n\x00"))
|
|
})
|
|
}
|
|
|
|
func testGitRepo(t *testing.T, root config.Root) string {
|
|
r, err := cryptorand.String(8)
|
|
require.NoError(t, err)
|
|
dir := filepath.Join(string(root), fmt.Sprintf("test-repo-%s", r))
|
|
err = os.MkdirAll(dir, 0o750)
|
|
require.NoError(t, err)
|
|
|
|
c := exec.Command("git", "init")
|
|
c.Dir = dir
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "config", "user.email", "ci@coder.com")
|
|
c.Dir = dir
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "config", "user.name", "C I")
|
|
c.Dir = dir
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
c = exec.Command("git", "checkout", "-b", "main")
|
|
c.Dir = dir
|
|
err = c.Run()
|
|
require.NoError(t, err)
|
|
|
|
return dir
|
|
}
|