Bazel: TMP: Tools used for Actions on Windows can't use tempdir() easily

Created on 24 Apr 2017  路  6Comments  路  Source: bazelbuild/bazel

(Bazel v0.4.5 on Windows)

Summary: If you have a custom Bazel tool written in (e.g.) C++ that needs to use a temporary directory, you currently need to explicitly manage environment vars to do so.

Detail:

  1. Write a tool in C++ that needs to use a temporary directory for some reason.
  2. On Posixy systems, Bazel guarantees /tmp is writable, but since that's not a thing on Windows, the best option is generally calling Win32 GetTempPath(), or some wrapper code that ends up calling it (e.g. posix tempdir())
  3. GetTempPath() is documented as using values from env vars $TMP, $TEMP, and $USER_PROFILE; if none of those are set, it falls back to C:\Windows
  4. Running the tool as an Action hides the default shell env, so none of the env vars in Step 3 are visible; on modern Windows systems, C:\Windows isn't necessarily writable by user code.
  5. Watch your tool fail because it can't find a writable temporary directory.

Workarounds:

  1. Explicitly specify a Windows-happy value for $TMP in the env argument to your action, taking care to add select() usage to only do this on Windows hosts. Or copy it from host_configuration.default_shell_env.
  2. set use_default_shell_env=True, if you can live with non-hermetic builds
  3. Rewrite your tool to avoid needing access to /tmp
  4. Something else I haven't thought of.

My first thought for a reasonable fix here would be for ctx.action to ensure that a reasonable value is set for $TMP when running on Windows hosts; that might have unforeseen side-effects, though (e.g. portable code that directly calls getenv("TMP") could behave different on Windows vs non-Windows hosts).

If there was a Skylark rule to return a path to a temp directory, Workaround 1 could be done more simply, via something like env={"TMP": ctx.tmp_dir()}. (Arguably, you could do this now by using ctx.genfiles_dir(), if you're certain the tool will clean up after itself...)

P1 windows feature request

Most helpful comment

Hang in there, I'm working on it! :)

I wrote a design that's currently under review internally.

All 6 comments

This is a blocker for https://github.com/bazelbuild/bazel/issues/3264, so I'm making it P1.

@laszlocsomor the other 2 you linked are in milestone 0.6. I'm hoping strength of numbers pulls the fix for this into 0.6 rather than this pulling them into 0.7? ;)

Hang in there, I'm working on it! :)

I wrote a design that's currently under review internally.

Design is approved, I'll start implementing it this week. Stay tuned!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ttsugriy picture ttsugriy  路  3Comments

mdzoba picture mdzoba  路  3Comments

kastiglione picture kastiglione  路  3Comments

sandipmgiri picture sandipmgiri  路  3Comments

cyberbono3 picture cyberbono3  路  3Comments