Skip to content

Commit 6153df2

Browse files
committed
Handle \\wsl.localhost as well as \\wsl$
1 parent 5c3f561 commit 6153df2

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

internal/pkg/devcontainers/dockerutils.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func ListDevcontainers() ([]DevcontainerInfo, error) {
7070
}
7171
}
7272
localPath := parts[listPartLocalFolder]
73-
if strings.HasPrefix(localPath, "\\\\wsl$") && wsl.IsWsl() {
73+
if wsl.HasWslPathPrefix(localPath) && wsl.IsWsl() {
7474
localPath, err = wsl.ConvertWindowsPathToWslPath(localPath)
7575
if err != nil {
7676
return []DevcontainerInfo{}, fmt.Errorf("error converting path: %s", err)
@@ -143,7 +143,7 @@ func GetSourceInfoFromDevContainer(containerIDOrName string) (SourceInfo, error)
143143
return SourceInfo{}, err
144144
}
145145

146-
if strings.HasPrefix(localPath, "\\\\wsl$") && wsl.IsWsl() {
146+
if wsl.HasWslPathPrefix(localPath) && wsl.IsWsl() {
147147
localPath, err = wsl.ConvertWindowsPathToWslPath(localPath)
148148
if err != nil {
149149
return SourceInfo{}, fmt.Errorf("error converting path: %s", err)
@@ -165,7 +165,7 @@ func GetSourceInfoFromDevContainer(containerIDOrName string) (SourceInfo, error)
165165
var mount DockerMount
166166
err = json.Unmarshal(output, &mount)
167167
if err != nil {
168-
return SourceInfo{}, fmt.Errorf("failed to get parse JSON getting mount folder for container %q (path=%q): %s", containerIDOrName, mountFolder, err)
168+
return SourceInfo{}, fmt.Errorf("failed to parse JSON getting mount folder for container %q (path=%q): %s", containerIDOrName, mountFolder, err)
169169
}
170170

171171
return SourceInfo{
@@ -199,13 +199,22 @@ func GetClosestPathMatchForPath(devContainers []DevcontainerInfo, devcontainerPa
199199
matchingPaths := byLocalPathLength{}
200200
for _, devcontainer := range devContainers {
201201
// Treat as match if the specified path is within the devcontainer path
202-
if strings.HasPrefix(absPath, devcontainer.LocalFolderPath) {
202+
testPath := devcontainer.LocalFolderPath
203+
if wsl.IsWsl() && wsl.HasWslPathPrefix(testPath) {
204+
testPath, err = wsl.ConvertWindowsPathToWslPath(testPath)
205+
fmt.Println("Converted to..")
206+
if err != nil {
207+
return DevcontainerInfo{}, fmt.Errorf("Error converting path from dev container list (%q): %s", testPath, err)
208+
}
209+
}
210+
if strings.HasPrefix(absPath, testPath) {
203211
matchingPaths = append(matchingPaths, devcontainer)
204212
}
205213
}
206214
if len(matchingPaths) == 0 {
207215
return DevcontainerInfo{}, fmt.Errorf("Could not find running container for path %q", devcontainerPath)
208216
}
217+
209218
// return longest prefix match
210219
sort.Sort(matchingPaths)
211220
return matchingPaths[len(matchingPaths)-1], nil

internal/pkg/devcontainers/remoteuri.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io/ioutil"
77
"path/filepath"
88
"regexp"
9-
"strings"
109

1110
"github.com/stuartleeks/devcontainer-cli/internal/pkg/git"
1211
"github.com/stuartleeks/devcontainer-cli/internal/pkg/wsl"
@@ -49,7 +48,7 @@ func GetWorkspaceMountPath(folderPath string) (string, error) {
4948

5049
// If we're called from WSL we want a WSL Path but will also handle a Windows Path
5150
if wsl.IsWsl() {
52-
if strings.HasPrefix(folderPath, "\\\\wsl$\\") {
51+
if wsl.HasWslPathPrefix(folderPath) {
5352
convertedPath, err := wsl.ConvertWindowsPathToWslPath(folderPath)
5453
if err != nil {
5554
return "", err

internal/pkg/wsl/wsl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ func ConvertWindowsPathToWslPath(path string) (string, error) {
3434
}
3535
return strings.TrimSpace(string(buf)), nil
3636
}
37+
38+
func HasWslPathPrefix(path string) bool {
39+
return strings.HasPrefix(path, "\\\\wsl$\\") || strings.HasPrefix(path, "\\\\wsl.localhost\\")
40+
}

0 commit comments

Comments
 (0)