From d51ef253a92e72513986731d7c08486ff60915c4 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 12 Nov 2024 10:52:14 +0100 Subject: [PATCH] builder: whitelist temporary directory env var for Clang invocation It looks like this breaks on Windows: https://github.com/tinygo-org/tinygo/issues/4557 I haven't confirmed this is indeed the problem, but it would make sense. And passing through the temporary directory seems like a good idea regardless, there's not much that could break due to that. --- builder/tools.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/builder/tools.go b/builder/tools.go index b4087828f1..66329a099b 100644 --- a/builder/tools.go +++ b/builder/tools.go @@ -32,10 +32,26 @@ func runCCompiler(flags ...string) error { cmd.Stderr = os.Stderr // Make sure the command doesn't use any environmental variables. - // Most importantly, it should not use C_INCLUDE_PATH and the like. But - // removing all environmental variables also works. + // Most importantly, it should not use C_INCLUDE_PATH and the like. cmd.Env = []string{} + // Let some environment variables through. One important one is the + // temporary directory, especially on Windows it looks like Clang breaks if + // the temporary directory has not been set. + // See: https://github.com/tinygo-org/tinygo/issues/4557 + // Also see: https://github.com/llvm/llvm-project/blob/release/18.x/llvm/lib/Support/Unix/Path.inc#L1435 + for _, env := range os.Environ() { + // We could parse the key and look it up in a map, but since there are + // only a few keys iterating through them is easier and maybe even + // faster. + for _, prefix := range []string{"TMPDIR=", "TMP=", "TEMP=", "TEMPDIR="} { + if strings.HasPrefix(env, prefix) { + cmd.Env = append(cmd.Env, env) + break + } + } + } + return cmd.Run() }