From 8195b9e537ef00ec553c363b15959f08031381b9 Mon Sep 17 00:00:00 2001 From: Dean Sheather Date: Tue, 11 Jun 2019 15:07:49 +0000 Subject: [PATCH] path compatibility fixes to improve WSL compat. (#211) - Add `VSCODE_CONFIG_DIR` and `VSCODE_EXTENSIONS_DIR` environment variables to change the bind mounted code-server and config and extensions directories - Change the default bind mounted code-server config and extension paths on darwin - Change the code-server cache path to use os.TempDir() instead of hard-coded /tmp - Change the resolvePath() function to be compatible with shells --- codeserver.go | 2 +- config.go | 13 ++++++------- runner.go | 7 +++---- vscode.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 vscode.go diff --git a/codeserver.go b/codeserver.go index f2559da..afefe2c 100644 --- a/codeserver.go +++ b/codeserver.go @@ -24,7 +24,7 @@ import ( func loadCodeServer(ctx context.Context) (string, error) { start := time.Now() - const cachePath = "/tmp/sail-code-server-cache/code-server" + cachePath := filepath.Join(os.TempDir(), "sail-code-server-cache/code-server") // downloadURLPath stores the download URL, so we know whether we should update // the binary. diff --git a/config.go b/config.go index 5468eb4..4a1f653 100644 --- a/config.go +++ b/config.go @@ -18,15 +18,14 @@ func resolvePath(homedir string, path string) string { return path } - list := strings.Split(path, string(filepath.Separator)) - - for i, seg := range list { - if seg == "~" { - list[i] = homedir - } + // Replace tilde notation in path with homedir. + if path == "~" { + path = homedir + } else if strings.HasPrefix(path, "~/") { + path = filepath.Join(homedir, path[2:]) } - return filepath.Join(list...) + return filepath.Clean(path) } // config describes the config.toml. diff --git a/runner.go b/runner.go index 75802d1..d8199a3 100644 --- a/runner.go +++ b/runner.go @@ -236,12 +236,12 @@ func (r *runner) mounts(mounts []mount.Mount, image string) ([]mount.Mount, erro // Mount in VS Code configs. mounts = append(mounts, mount.Mount{ Type: "bind", - Source: "~/.config/Code", + Source: vscodeConfigDir(), Target: "~/.config/Code", }) mounts = append(mounts, mount.Mount{ Type: "bind", - Source: "~/.vscode/extensions", + Source: vscodeExtensionsDir(), Target: hostExtensionsDir, }) @@ -251,8 +251,7 @@ func (r *runner) mounts(mounts []mount.Mount, image string) ([]mount.Mount, erro // socket to the container allows for using the user's existing setup for // ssh authentication instead of having to create a new keys or explicity // pass them in. - sshAuthSock, exists := os.LookupEnv("SSH_AUTH_SOCK") - if exists { + if sshAuthSock, exists := os.LookupEnv("SSH_AUTH_SOCK"); exists { mounts = append(mounts, mount.Mount{ Type: "bind", Source: sshAuthSock, diff --git a/vscode.go b/vscode.go new file mode 100644 index 0000000..b76a8d3 --- /dev/null +++ b/vscode.go @@ -0,0 +1,33 @@ +package main + +import ( + "os" + "path/filepath" + "runtime" +) + +const ( + vsCodeConfigDirEnv = "VSCODE_CONFIG_DIR" + vsCodeExtensionsDirEnv = "VSCODE_EXTENSIONS_DIR" +) + +func vscodeConfigDir() string { + if env, ok := os.LookupEnv(vsCodeConfigDirEnv); ok { + return os.ExpandEnv(env) + } + + path := os.ExpandEnv("$HOME/.config/Code/") + if runtime.GOOS == "darwin" { + path = os.ExpandEnv("$HOME/Library/Application Support/Code/") + } + return filepath.Clean(path) +} + +func vscodeExtensionsDir() string { + if env, ok := os.LookupEnv(vsCodeExtensionsDirEnv); ok { + return os.ExpandEnv(env) + } + + path := os.ExpandEnv("$HOME/.vscode/extensions/") + return filepath.Clean(path) +}