88 "regexp"
99 "strings"
1010
11+ "github.com/stuartleeks/devcontainer-cli/internal/pkg/git"
1112 "github.com/stuartleeks/devcontainer-cli/internal/pkg/wsl"
1213)
1314
@@ -72,8 +73,11 @@ func GetWorkspaceMountPath(folderPath string) (string, error) {
7273 }
7374
7475 // No `workspaceFolder` found in devcontainer.json - use default
75- _ , folderName := filepath .Split (folderPath )
76- return fmt .Sprintf ("/workspaces/%s" , folderName ), nil
76+ devcontainerPath , err := getDefaultWorkspaceFolderForPath (folderPath )
77+ if err != nil {
78+ return "" , fmt .Errorf ("Error getting default workspace path: %s" , err )
79+ }
80+ return fmt .Sprintf ("/workspaces/%s" , devcontainerPath ), nil
7781}
7882
7983// TODO: add tests (and implementation) to handle JSON parsing with comments
@@ -92,3 +96,26 @@ func getWorkspaceMountPathFromDevcontainerDefinition(definition []byte) (string,
9296 }
9397 return "" , nil
9498}
99+
100+ func getDefaultWorkspaceFolderForPath (path string ) (string , error ) {
101+
102+ // get the git repo-root
103+ rootPath , err := git .GetTopLevelPath (path )
104+ if err != nil {
105+ return "" , err
106+ }
107+ if rootPath == "" {
108+ // not a git repo, default to path
109+ rootPath = path
110+ }
111+
112+ // get parent to root
113+ rootParent , _ := filepath .Split (rootPath )
114+
115+ // return path relative to rootParent
116+ relativePath , err := filepath .Rel (rootParent , path )
117+ if err != nil {
118+ return "" , err
119+ }
120+ return relativePath , nil
121+ }
0 commit comments