Golang - change build working path on Windows
Asked Answered
M

4

19

I'm testing simple Go program on Windows 8 with SublimeText3 (GoSublime plugin)

go run -v example.go

and before run it's being compiled inside ..AppData\Local\Temp.. directory. My antivirus program thinks that it's a virus and blocks it:

fork/exec C:\Users\D24F7~1.KAP\AppData\Local\Temp\go-build333212398\command-line-arguments_obj\exe\example.exe: Access is denied.

I can't disable it and my solution is to change the folder where it's being compiled. How can I do that?

Marya answered 5/8, 2016 at 6:41 Comment(0)
Z
14

The GOTMPDIR environment var can be used to control the work directory. This is usually preferable to modifying the system-wide temporary directory. GOTMPDIR was introduced in go 1.10.

Before

> go run -work .\example.go
WORK=C:\Users\MyUserName\AppData\Local\Temp\go-build1002945170
...

Modify permanently in the System Properties > Environment Variables window or temporarily in the shell

# powershell
$env:GOTMPDIR = "C:\Users\MyUserName\MyGoBuilds"

After

> go run -work .\example.go
WORK=C:\Users\MyUserName\MyGoBuilds\go-build1381354702
...

Then you can make the needed antivirus or other security exceptions on the GOTMPDIR directory.

Zymogenic answered 20/2, 2022 at 18:32 Comment(0)
P
3

The WORK directory (seen if building or running with the -x flag) is taken from the TMP environment variable. Updating that variable through the system properties will change the working directory.

Prager answered 11/8, 2020 at 3:0 Comment(0)
H
0

I change ouput directory

go build -i -o D:\Users\MyProj\out\

-o flag

and put to antivirus ignoring D:\Users\MyProj\out\ directory

Harville answered 31/12, 2017 at 7:35 Comment(1)
No, the OP asked about temporary build files, they have nothing to do with -o option.Chiquia
W
-1

You can build a binary directly using go build (out binary is at current dir) or go build -o /your/custom/path. Then just run the output binary.

Wallis answered 5/8, 2016 at 6:49 Comment(1)
It's not a solution because in Windows Go compiles in temp folder - C:\Users\D24F7~1.KAP\AppData\Local\Temp_ - and only then copies it into _/your/custom/path. So it's sill Access denied error.Marya

© 2022 - 2025 — McMap. All rights reserved.