`go test` - there is no way to specify a custom work/tmp directory

4,004 views
Skip to first unread message

zura.khe...@gmail.com

unread,
Apr 30, 2014, 7:57:05 AM4/30/14
to golan...@googlegroups.com
Hello,

As you can see in:
Line: 433

The work directory is generated inside the system temp dir, where the test executable is generated. There is no way to specify a different directory.

Now this is problematic because in some system setups, execution is not allowed within a temporary dir.

Thank you,
Zura

Andrew Gerrand

unread,
Apr 30, 2014, 10:00:45 AM4/30/14
to zura.khe...@gmail.com, golang-dev

On 30 April 2014 05:57, <zura.khe...@gmail.com> wrote:
Now this is problematic because in some system setups, execution is not allowed within a temporary dir.

Just set the TMPDIR environment variable before running "go test".

Andrew Gerrand

unread,
Apr 30, 2014, 12:51:33 PM4/30/14
to Zura Khetsuriani, golang-dev
+golang-dev (keep it on list because I know nothing about windows)

On 30 April 2014 12:47, Zura Khetsuriani <zura.khe...@gmail.com> wrote:
Thank you for the prompt reply, Andrew.

Replying just to you, to not to pollute golang-dev :)

I'm on Win7 x64, go 1.2.1 and setting TMPDIR didn't help.
`go test -work` still displays the standard windows system temp dir and creates temporary test executable there.

Right, TMPDIR is not a thing on Windows.

I'm not sure if there's some other way to change the temporary directory.

Otherwise you could modify your copy of the os package, or disable the feature of the operating system that prevents you from executing binaries from the temporary directory.

On 30.04.2014 18:00, Andrew Gerrand wrote:

On 30 April 2014 05:57, <zura.khe...@gmail.com

minux

unread,
Apr 30, 2014, 1:06:11 PM4/30/14
to Andrew Gerrand, Zura Khetsuriani, golang-dev


On Apr 30, 2014 12:52 PM, "Andrew Gerrand" <a...@golang.org> wrote:
>
> +golang-dev (keep it on list because I know nothing about windows)
>
> On 30 April 2014 12:47, Zura Khetsuriani <zura.khe...@gmail.com> wrote:
>>
>> Thank you for the prompt reply, Andrew.
>>
>> Replying just to you, to not to pollute golang-dev :)
>>
>> I'm on Win7 x64, go 1.2.1 and setting TMPDIR didn't help.
>> `go test -work` still displays the standard windows system temp dir and creates temporary test executable there.
>
> Right, TMPDIR is not a thing on Windows.

i think it's TEMP on windows.

Chris Hines

unread,
Apr 30, 2014, 3:40:34 PM4/30/14
to golan...@googlegroups.com, Andrew Gerrand, Zura Khetsuriani
On Windows os.TempDir() eventually calls syscall.GetTempPath() which calls kernel32.dll:GetTempPath() documented at:


The relevant extract from that page is:

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path specified by the USERPROFILE environment variable.
  4. The Windows directory.

Note that the function does not verify that the path exists, nor does it test to see if the current process has any kind of access rights to the path.

Zura Khetsuriani

unread,
Apr 30, 2014, 3:55:28 PM4/30/14
to Chris Hines, golan...@googlegroups.com, Andrew Gerrand
Right, it is for setting a system temporary directory on Windows. But,
what I originally meant is that to be able to specify some custom
directory to `go test` in particular, independently from system temp dir.

On 30.04.2014 23:40, Chris Hines wrote:
> On Windows os.TempDir() eventually calls syscall.GetTempPath() which
> calls kernel32.dll:GetTempPath() documented at:
>
> http://msdn.microsoft.com/en-us/library/windows/desktop/aa364992(v=vs.85).aspx
>
> The relevant extract from that page is:
>
> The *GetTempPath* function checks for the existence of environment
> variables in the following order and uses the first path found:
>
> 1. The path specified by the TMP environment variable.
> 2. The path specified by the TEMP environment variable.
> 3. The path specified by the USERPROFILE environment variable.
> 4. The Windows directory.
>
> Note that the function does not verify that the path exists, nor does it
> test to see if the current process has any kind of access rights to the
> path.
>
>
> On Wednesday, April 30, 2014 1:06:11 PM UTC-4, minux wrote:
>
>
> On Apr 30, 2014 12:52 PM, "Andrew Gerrand" <a...@golang.org
> <javascript:>> wrote:
> >
> > +golang-dev (keep it on list because I know nothing about windows)
> >
> > On 30 April 2014 12:47, Zura Khetsuriani <zura.khe...@gmail.com

Chris Hines

unread,
May 1, 2014, 11:01:24 AM5/1/14
to golan...@googlegroups.com, Chris Hines, Andrew Gerrand, zura.khe...@gmail.com
On Wednesday, April 30, 2014 3:55:28 PM UTC-4, Zura Khetsuriani wrote:
Right, it is for setting a system temporary directory on Windows. But,
what I originally meant is that to be able to specify some custom
directory to `go test` in particular, independently from system temp dir.

I think what you want is possible given the information in this thread, but maybe I have misunderstood so let's walk through an example and see if it is good enough.

C:\Workspaces\Go\src\foo>type *.go

foo.go


package foo

func Hello() string {
        return "Hello, world!"
}

foo_test.go


package foo_test

import "testing"
import "foo"

func TestHello(t *testing.T) {
        const exp = "Hello, world!"
        if act := foo.Hello(); act != exp {
                t.Fatalf(`expected %s, got %s`, act, exp)
        }
}

C:\Workspaces\Go\src\foo>set TMP
TMP=C:\Users\chines\AppData\Local\Temp

C:\Workspaces\Go\src\foo>go version
go version go1.2 windows/amd64

C:\Workspaces\Go\src\foo>go test -x
WORK=C:\Users\chines\AppData\Local\Temp\go-build100984599
mkdir -p $WORK\foo\_test\
....
$WORK\foo\_test\foo.test.exe
PASS
ok      foo     0.208s

C:\Workspaces\Go\src\foo>set TMP=C:\temp

C:\Workspaces\Go\src\foo>go test -x
WORK=C:\temp\go-build852349539
mkdir -p $WORK\foo\_test\
....
$WORK\foo\_test\foo.test.exe
PASS
ok      foo     0.221s


So that seems to do what was requested. Also note that TMP was only set for the current CMD shell. Opening a new CMD shell we can see:

C:\Users\chines>set TMP
TMP=C:\Users\chines\AppData\Local\Temp

So there is no need to change the system wide TMP.

eliv...@gmail.com

unread,
May 15, 2018, 8:17:49 AM5/15/18
to golang-dev
Just updating this thread. This has an official workaround now via the GOTMPDIR variable in Go v1.10:

seb5...@gmail.com

unread,
May 16, 2018, 7:46:40 AM5/16/18
to golang-dev
Hi,

I also have a workaround working for me on Windows, you have to set the "DOS" %temp% var to the directory you like go test work
Of course this will be system wide, you may see on windows cmd how it is currently set

echo %temp%
C
:\Users\xxxyyy\AppData\Local\Temp

Seb

Wade Jr Graunke

unread,
Jan 25, 2022, 12:03:21 PM1/25/22
to golang-dev

Kasperky was giving me problems until I set GOTMPDIR to a directory that could be included as an exception. The problem is that if GOTMPDIR is not set, golang will sometimes build in that generic \Temp directory tied to your users profile that is dangerous to allow as a generic exception to Kaspersky. And for me, Kaspersky doesn't allow wildcards in the directory exceptions like Avast can.

Assuming you've already added your GOPATH as an exception in your antivirus (for example, "C:\Users<user>\go"...what worked for me was to...

  1. add a new directory (I named mine "tmp" but you could probably name it a number of things) within that path, i.e., "C:\Users<user>\go\tmp"

  2. Set the golang environment variable GOTMPDIR to it...

> go env -w GOTMPDIR="C:\Users<user>\go\tmp"

And the issue goes away. But note that I already added "C:\Users<user>\go" as an exception to my antivirus.

Window 10, GO 1.17, and Kaspersky

Reply all
Reply to author
Forward
0 new messages