|
7 | 7 | "bytes"
|
8 | 8 | _ "embed"
|
9 | 9 | "fmt"
|
| 10 | + "io/fs" |
10 | 11 | "os"
|
11 | 12 | "os/exec"
|
12 | 13 | "path/filepath"
|
@@ -202,6 +203,9 @@ func (s *Shell) execCommand() string {
|
202 | 203 | return strings.Join(append(args, s.binPath), " ")
|
203 | 204 | }
|
204 | 205 |
|
| 206 | + // Link other files that affect the shell settings and environments. |
| 207 | + s.linkShellStartupFiles(filepath.Dir(shellrc)) |
| 208 | + |
205 | 209 | // Shells have different ways of overriding the shellrc, so we need to
|
206 | 210 | // look at the name to know which env vars or args to set.
|
207 | 211 | var (
|
@@ -286,6 +290,38 @@ func (s *Shell) writeDevboxShellrc() (path string, err error) {
|
286 | 290 | return path, nil
|
287 | 291 | }
|
288 | 292 |
|
| 293 | +// linkShellStartupFiles will link files used by the shell for initialization. |
| 294 | +// We choose to link instead of copy so that changes made outside can be reflected |
| 295 | +// within the devbox shell. |
| 296 | +// |
| 297 | +// We do not link the .{shell}rc files, since devbox modifies them. See writeDevboxShellrc |
| 298 | +func (s *Shell) linkShellStartupFiles(shellSettingsDir string) { |
| 299 | + |
| 300 | + // For now, we only need to do this for zsh shell |
| 301 | + if s.name == shZsh { |
| 302 | + // Useful explanation of zsh startup files: https://zsh.sourceforge.io/FAQ/zshfaq03.html#l20 |
| 303 | + filenames := []string{".zshenv", ".zprofile", ".zlogin"} |
| 304 | + for _, filename := range filenames { |
| 305 | + fileOld := filepath.Join(filepath.Dir(s.userShellrcPath), filename) |
| 306 | + if _, err := os.Stat(fileOld); errors.Is(err, fs.ErrNotExist) { |
| 307 | + // this file may not be relevant for the user's setup. |
| 308 | + continue |
| 309 | + } else if err != nil { |
| 310 | + debug.Log("os.Stat error for %s is %v", fileOld, err) |
| 311 | + } |
| 312 | + |
| 313 | + fileNew := filepath.Join(shellSettingsDir, filename) |
| 314 | + |
| 315 | + if err := os.Link(fileOld, fileNew); err == nil { |
| 316 | + debug.Log("Linked shell startup file %s to %s", fileOld, fileNew) |
| 317 | + } else { |
| 318 | + // This is a best-effort operation. If there's an error then log it for visibility but continue. |
| 319 | + debug.Log("Error linking zsh setting file from %s to %s: %v", fileOld, fileNew, err) |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | +} |
| 324 | + |
289 | 325 | // envToKeep is the set of environment variables that we want to copy verbatim
|
290 | 326 | // to the new devbox shell.
|
291 | 327 | var envToKeep = map[string]bool{
|
|
0 commit comments