Skip to content

TempFile fails to find temporary directory #834

@peterGo

Description

@peterGo
hg id e712aba3277f+ tip GOOS windows GOARCH 386

package main

import (
    "fmt"
    "io/ioutil"
)

func main() {
    tf, err := ioutil.TempFile("", "go")
    fmt.Println(tf, err)
}

The program fails with the error: <nil> open /tmp/go116878482: The system
cannot find the path specified.

io/ioutil.TempFile() fails to honor Windows environment variables when the
directory is not specified.

Windows GetTempPath Function

http://msdn.microsoft.com/en-us/library/aa364992%28v=VS.85%29.aspx



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. The GetTempPath function returns the properly formatted string that
specifies the fully-qualified path based on the environment variable search
order as previously specified. The application should verify the existence
of the path and adequate access rights to the path prior to any use for
file I/O operations.



Symbolic link behavior: If the path points to a symbolic link, the temp
path name maintains any symbolic links.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions