Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jfantinhardesty opened this issue Mar 10, 2023 · 9 comments
Closed
Labels
NeedsFix The path to resolution is known, but the work has not been done. OS-Windows
Milestone

Comments

@jfantinhardesty
Copy link

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.

@seankhliao seankhliao added OS-Windows NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Mar 11, 2023
@bcmills
Copy link
Contributor

bcmills commented Mar 11, 2023

(CC @golang/windows)

@alexbrainman
Copy link
Member

The behavior on Windows should be the same as it is on Linux.

Why should Windows program behave the same as Linux?

Alex

@icholy
Copy link

icholy commented Mar 12, 2023

@alexbrainman that's kind of the point of the os package.

@alexbrainman
Copy link
Member

@alexbrainman that's kind of the point of the os package.

@icholy

If you read os.Truncate documentation, it does not say that the function will fail, if the file does not exists.

I suspect that authors of the os.Truncate code decided that this behaviour is not important enough to implement / document.

Alex

@icholy
Copy link

icholy commented Mar 13, 2023

@alexbrainman luckily, we're not bound by the underspecified documentation.

@bcmills
Copy link
Contributor

bcmills commented Mar 13, 2023

Why should Windows program behave the same as Linux?

The behavior should be consistent across platforms unless there is a well-defined platform-specific behavior that would lead a Go user to expect otherwise.

In this case, the difference in behavior is because of explicit flags passed to OpenFile in Go, not because of some subtle difference between SetEndOfFile and truncate(2).

It seems to me that the behavior should be made consistent, although it may need a GODEBUG to provide a transition for existing Windows-only programs.

@bcmills bcmills added the NeedsFix The path to resolution is known, but the work has not been done. label Mar 13, 2023
@bcmills bcmills added this to the Backlog milestone Mar 13, 2023
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 13, 2023
@qmuntal
Copy link
Contributor

qmuntal commented Mar 13, 2023

The behavior should be consistent across platforms unless there is a well-defined platform-specific behavior that would lead a Go user to expect otherwise.

AFAIK, the only well-defined Windows api that truncate files referenced by file name and not by handler is fsutil file seteof <filename> <length> (docs). It returns an error when the file name is not found:

$ fsutil file seteof foo.txt 2
Error:  The system cannot find the file specified.

@gopherbot
Copy link

Change https://go.dev/cl/477215 mentions this issue: os: truncate on Windows will not create a new file

@gopherbot
Copy link

Change https://go.dev/cl/498600 mentions this issue: doc/go1.21: mention os package changes

gopherbot pushed a commit that referenced this issue May 26, 2023
Also mention WTF-8 support in the syscall package.

For #32558
For #58977
For #59971

Change-Id: Id1627889b5e498add498748d9bfc69fb58030b35
Reviewed-on: https://go-review.googlesource.com/c/go/+/498600
Reviewed-by: Eli Bendersky <eliben@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done. OS-Windows
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants