@@ -75,7 +75,7 @@ type Client interface {
75
75
ReportStats (ctx context.Context , log slog.Logger , stats func () * agentsdk.Stats ) (io.Closer , error )
76
76
PostLifecycle (ctx context.Context , state agentsdk.PostLifecycleRequest ) error
77
77
PostAppHealth (ctx context.Context , req agentsdk.PostAppHealthsRequest ) error
78
- PostVersion (ctx context.Context , version string ) error
78
+ PostStartup (ctx context.Context , req agentsdk. PostStartupRequest ) error
79
79
}
80
80
81
81
func New (options Options ) io.Closer {
@@ -236,16 +236,29 @@ func (a *agent) run(ctx context.Context) error {
236
236
}
237
237
a .sessionToken .Store (& sessionToken )
238
238
239
- err = a .client .PostVersion (ctx , buildinfo .Version ())
240
- if err != nil {
241
- return xerrors .Errorf ("update workspace agent version: %w" , err )
242
- }
243
-
244
239
metadata , err := a .client .Metadata (ctx )
245
240
if err != nil {
246
241
return xerrors .Errorf ("fetch metadata: %w" , err )
247
242
}
248
243
a .logger .Info (ctx , "fetched metadata" )
244
+
245
+ // Expand the directory and send it back to coderd so external
246
+ // applications that rely on the directory can use it.
247
+ //
248
+ // An example is VS Code Remote, which must know the directory
249
+ // before initializing a connection.
250
+ metadata .Directory , err = expandDirectory (metadata .Directory )
251
+ if err != nil {
252
+ return xerrors .Errorf ("expand directory: %w" , err )
253
+ }
254
+ err = a .client .PostStartup (ctx , agentsdk.PostStartupRequest {
255
+ Version : buildinfo .Version (),
256
+ ExpandedDirectory : metadata .Directory ,
257
+ })
258
+ if err != nil {
259
+ return xerrors .Errorf ("update workspace agent version: %w" , err )
260
+ }
261
+
249
262
oldMetadata := a .metadata .Swap (metadata )
250
263
251
264
// The startup script should only execute on the first run!
@@ -803,7 +816,11 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
803
816
804
817
cmd := exec .CommandContext (ctx , shell , args ... )
805
818
cmd .Dir = metadata .Directory
806
- if cmd .Dir == "" {
819
+
820
+ // If the metadata directory doesn't exist, we run the command
821
+ // in the users home directory.
822
+ _ , err = os .Stat (cmd .Dir )
823
+ if cmd .Dir == "" || err != nil {
807
824
// Default to user home if a directory is not set.
808
825
homedir , err := userHomeDir ()
809
826
if err != nil {
@@ -1314,3 +1331,20 @@ func userHomeDir() (string, error) {
1314
1331
}
1315
1332
return u .HomeDir , nil
1316
1333
}
1334
+
1335
+ // expandDirectory converts a directory path to an absolute path.
1336
+ // It primarily resolves the home directory and any environment
1337
+ // variables that may be set
1338
+ func expandDirectory (dir string ) (string , error ) {
1339
+ if dir == "" {
1340
+ return "" , nil
1341
+ }
1342
+ if dir [0 ] == '~' {
1343
+ home , err := userHomeDir ()
1344
+ if err != nil {
1345
+ return "" , err
1346
+ }
1347
+ dir = filepath .Join (home , dir [1 :])
1348
+ }
1349
+ return os .ExpandEnv (dir ), nil
1350
+ }
0 commit comments