Skip to content

os: Truncate() on a non-existent file will create a file on Windows #58977

@jfantinhardesty

Description

@jfantinhardesty

What version of Go are you using (go version)?

$ go version
go version go1.20.2 windows/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\jfan\AppData\Local\go-build
set GOENV=C:\Users\jfan\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\jfan\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\jfan\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\jfan\code\lyvecloudfuse\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\jfan\AppData\Local\Temp\go-build1326796536=/tmp/go-build -gno-record-gcc-switches

What did you do?

If you truncate a non-existent file with os.Truncate on Linux a no such file or directory error is given. On Windows, however, a file will be created and the error will be nil.

Here is a link to an application https://go.dev/play/p/SoUjXmpP0Ii that behaves as expected on Linux. It correctly gives a no such file or directory error. However, if you run it on Windows, you will not see any errors and a file will be created. Here is the test code to run.

package main

import (
	"fmt"
	"os"
)

func main() {
	err := os.Truncate("./testfile", 1)
	fmt.Println("Expect error to be no such file or directory and error is :", err)

	_, err = os.ReadFile("./testfile")
	fmt.Println("Expect error to be no such file or directory and error is :", err)
}

What did you expect to see?

The behavior on Windows should be the same as it is on Linux. Since no file exists, it should give a no such file or directory error.

What did you see instead?

On Windows, no error is given and a file is created.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.OS-Windows

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions