Skip to content

fix: Make coder dotfiles symlinking idempotent #7525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2023
Merged
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
4 changes: 4 additions & 0 deletions cli/dotfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func (r *RootCmd) dotfiles() *clibase.Cmd {
}
}

// attempt to delete the file before creating a new symlink. This overwrites any existing symlinks
// which are typically leftover from a previous call to coder dotfiles. We do this best effort and
// ignore errors because the symlink may or may not exist. Any regular files are backed up above.
_ = os.Remove(to)
err = os.Symlink(from, to)
if err != nil {
return xerrors.Errorf("symlinking %s to %s: %w", from, to, err)
Expand Down
11 changes: 11 additions & 0 deletions cli/dotfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ func TestDotfiles(t *testing.T) {
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")
})
}

Expand Down