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: document that OpenFile Create do not create file if directory do not exist #69836

Closed
r4f4ss opened this issue Oct 10, 2024 · 4 comments
Closed
Labels
Documentation Issues describing a change to documentation. help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@r4f4ss
Copy link

r4f4ss commented Oct 10, 2024

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

$ go version
go version go1.23.1 linux/amd64

Does this issue reproduce with the latest release?

I do not know

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

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='HOME/.cache/go-build'
GOENV='HOME/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='HOME/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='HOME/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.1'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='HOME/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='HOME/geth/goBug/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build44258856=/tmp/go-build -gno-record-gcc-switches'
uname -sr: Linux 6.8.0-45-generic
Distributor ID:	Linuxmint
Description:	Linux Mint 22
Release:	22
Codename:	wilma
/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Ubuntu GLIBC 2.39-0ubuntu8.3) stable release version 2.39.
gdb --version: GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git

What did you do?

Run the followeing two codes (./bugDir directory must do not exist):

$go run main.go
package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	dir := "./bugDir"
	fileName := "bugFile"
	fullPath := filepath.Join(dir, fileName)

	_, err := os.OpenFile(fullPath, os.O_CREATE|os.O_WRONLY, 0600)
	if err != nil {
		fmt.Println(err)
	}
}
package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	dir := "./bugDir"
	fileName := "bugFile"
	fullPath := filepath.Join(dir, fileName)

	_, err := os.Create(fullPath)
	if err != nil {
		fmt.Println(err)
	}
}

What did you expect to see?

The creation of the file bugFile, if the directory do not exist it should be created. This is my understanding of documentation described behavior (1,2), otherwise should be stated clearly in docs that the path must exist before file creation.

Note that the use of the flag O_CREATE suggest that the file will be created eitherways:

If the file does not exist, and the O_CREATE flag is passed, it is created with mode perm (before umask)

workarround: create the directory with os.MkdirAll(dir, os.ModePerm)

What did you see instead?

Error:

open bugDir/bugFile: no such file or directory
@ianlancetaylor
Copy link
Member

This is how it is supposed to work. It corresponds to how the create-file functions work in other languages.

I'm not opposed to a brief doc note.

@ianlancetaylor ianlancetaylor changed the title os.OpenFile and os.Create do not create file if directory do not exist os: document that OpenFile Create do not create file if directory do not exist Oct 10, 2024
@ianlancetaylor ianlancetaylor added Documentation Issues describing a change to documentation. help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 10, 2024
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Oct 10, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/619316 mentions this issue: os: add precondition doc for Create and OpenFile

@xieyuschen
Copy link
Contributor

This is how it is supposed to work. It corresponds to how the create-file functions work in other languages.

I'm not opposed to a brief doc note.

@ianlancetaylor Send you CL 619316. I have encountered this problem before and solved it by using MkdirAll to create the directories first. I think it's nice to link the MkdirAll and Mkdir as well.

cc: @r4f4ss

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 18, 2024
@dmitshur dmitshur modified the milestones: Backlog, Go1.24 Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues describing a change to documentation. help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants