@@ -252,12 +252,27 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
252252 continue
253253 }
254254 env [pte .Name ] = val
255- if pte .IsPath {
256- passthroughMounts = append (passthroughMounts , sandbox.Mount {
257- Path : val ,
258- Writable : pte .Writeable ,
255+ if ! pte .IsPath {
256+ continue
257+ }
258+
259+ dest := val
260+ // Mount at a temporary directory.
261+ if val == filepath .Dir (val ) {
262+ if dest , err = s .newTempDir (); err != nil {
263+ return nil , err
264+ }
265+ cleanupFuncs = append (cleanupFuncs , func () error {
266+ return os .RemoveAll (tempDir )
259267 })
260268 }
269+
270+ passthroughMounts = append (passthroughMounts , sandbox.Mount {
271+ Path : val ,
272+ Writable : pte .Writeable ,
273+ Dest : dest ,
274+ Why : "passthrough_env: " + pte .Name ,
275+ })
261276 }
262277
263278 for _ , item := range argenv .Items () {
@@ -343,7 +358,7 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
343358 if runtime .GOOS != "windows" {
344359 config .Mounts = []sandbox.Mount {
345360 // TODO(olivernewman): Mount the checkout read-only unconditionally.
346- {Path : s .root , Writable : s .writableRoot },
361+ {Path : s .root , Writable : s .writableRoot , Why : "checkout_root" },
347362 // OS-provided utilities.
348363 {Path : "/dev/null" , Writable : true },
349364 {Path : "/dev/urandom" },
@@ -363,7 +378,7 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
363378 // Make the parent directory of tempDir available, since it is the root
364379 // of all ctx.os.tempdir() calls, which can be used as scratch pads for
365380 // this executable.
366- {Path : filepath .Dir (tempDir ), Writable : true },
381+ {Path : filepath .Dir (tempDir ), Writable : true , Why : "tempdir_parent" },
367382 }
368383 config .Mounts = append (config .Mounts , passthroughMounts ... )
369384
@@ -372,7 +387,7 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
372387 // installs Go in the checkout directory, and stop explicitly mounting
373388 // $GOROOT and adding it to $PATH.
374389 if runtime .GOROOT () != "" {
375- config .Mounts = append (config .Mounts , sandbox.Mount {Path : runtime .GOROOT ()})
390+ config .Mounts = append (config .Mounts , sandbox.Mount {Path : runtime .GOROOT (), Why : "GOROOT" })
376391 }
377392
378393 // Mount all directories listed in $PATH.
@@ -382,13 +397,18 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
382397 // Relative paths in $PATH are not allowed.
383398 continue
384399 }
400+ if p == filepath .Dir (p ) {
401+ // Skip trying to mount the filesystem root if it happens to be
402+ // included in $PATH, since that will cause lots of issues.
403+ continue
404+ }
385405 var fi os.FileInfo
386406 if fi , err = os .Stat (p ); err != nil || ! fi .IsDir () {
387407 // Skip $PATH elements that don't exist or point to
388408 // non-directories.
389409 continue
390410 }
391- config .Mounts = append (config .Mounts , sandbox.Mount {Path : p })
411+ config .Mounts = append (config .Mounts , sandbox.Mount {Path : p , Why : "PATH_element" })
392412 }
393413 }
394414
0 commit comments