Skip to content

Commit e01aac3

Browse files
committed
expand
1 parent 128c5e7 commit e01aac3

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

agent/agent.go

+30-13
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
10751075
//
10761076
// An example is VS Code Remote, which must know the directory
10771077
// before initializing a connection.
1078-
manifest.Directory, err = expandDirectory(manifest.Directory)
1078+
manifest.Directory, err = expandPathToAbs(manifest.Directory)
10791079
if err != nil {
10801080
return xerrors.Errorf("expand directory: %w", err)
10811081
}
@@ -1119,13 +1119,18 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11191119
// scripts have completed.
11201120
var postStartScripts []codersdk.WorkspaceAgentScript
11211121
for _, dc := range manifest.Devcontainers {
1122+
dc = expandDevcontainerPaths(a.logger, dc)
11221123
// TODO(mafredri): Verify `@devcontainers/cli` presence.
11231124
// TODO(mafredri): Verify workspace folder exists.
11241125
// TODO(mafredri): If set, verify config path exists.
11251126
postStartScripts = append(postStartScripts, agentcontainers.DevcontainerStartupScript(dc))
11261127
}
11271128

1128-
err = a.scriptRunner.Init(manifest.Scripts, aAPI.ScriptCompleted, agentscripts.WithPostStartScripts(postStartScripts...))
1129+
err = a.scriptRunner.Init(
1130+
manifest.Scripts,
1131+
aAPI.ScriptCompleted,
1132+
agentscripts.WithPostStartScripts(postStartScripts...),
1133+
)
11291134
if err != nil {
11301135
return xerrors.Errorf("init script runner: %w", err)
11311136
}
@@ -1864,30 +1869,42 @@ func userHomeDir() (string, error) {
18641869
return u.HomeDir, nil
18651870
}
18661871

1867-
// expandDirectory converts a directory path to an absolute path.
1868-
// It primarily resolves the home directory and any environment
1869-
// variables that may be set
1870-
func expandDirectory(dir string) (string, error) {
1871-
if dir == "" {
1872+
// expandPathToAbs converts a path to an absolute path. It primarily resolves
1873+
// the home directory and any environment variables that may be set.
1874+
func expandPathToAbs(path string) (string, error) {
1875+
if path == "" {
18721876
return "", nil
18731877
}
1874-
if dir[0] == '~' {
1878+
if path[0] == '~' {
18751879
home, err := userHomeDir()
18761880
if err != nil {
18771881
return "", err
18781882
}
1879-
dir = filepath.Join(home, dir[1:])
1883+
path = filepath.Join(home, path[1:])
18801884
}
1881-
dir = os.ExpandEnv(dir)
1885+
path = os.ExpandEnv(path)
18821886

1883-
if !filepath.IsAbs(dir) {
1887+
if !filepath.IsAbs(path) {
18841888
home, err := userHomeDir()
18851889
if err != nil {
18861890
return "", err
18871891
}
1888-
dir = filepath.Join(home, dir)
1892+
path = filepath.Join(home, path)
1893+
}
1894+
return path, nil
1895+
}
1896+
1897+
func expandDevcontainerPaths(logger slog.Logger, dc codersdk.WorkspaceAgentDevcontainer) codersdk.WorkspaceAgentDevcontainer {
1898+
var err error
1899+
if dc.WorkspaceFolder, err = expandPathToAbs(dc.WorkspaceFolder); err != nil {
1900+
logger.Warn(context.Background(), "expand devcontainer workspace folder failed", slog.Error(err))
1901+
}
1902+
if dc.ConfigPath != "" {
1903+
if dc.ConfigPath, err = expandPathToAbs(dc.ConfigPath); err != nil {
1904+
logger.Warn(context.Background(), "expand devcontainer config path failed", slog.Error(err))
1905+
}
18891906
}
1890-
return dir, nil
1907+
return dc
18911908
}
18921909

18931910
// EnvAgentSubsystem is the environment variable used to denote the

0 commit comments

Comments
 (0)