Skip to content

Commit 0e90b25

Browse files
committed
fix(output): open device and pipe targets without O_TRUNC (#208)
Windows rejects O_TRUNC on the NUL device with ERROR_INVALID_FUNCTION, so -o NUL, -stats NUL and -sarif NUL failed with "open NUL: Incorrect function." and exit 1. Truncating a device or pipe is meaningless, so drop the flag; the direct-write branch already handles non-regular targets. Drops the Windows skip in TestCreateFileWritesNonRegularTargetDirectly: it now exercises the NUL device on Windows too. Signed-off-by: bfs2021 <101915083+bfs2021@users.noreply.github.com>
1 parent 28de702 commit 0e90b25

2 files changed

Lines changed: 4 additions & 5 deletions

File tree

‎internal/output/file.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func CreateFile(path string) (*File, error) {
3636
keep := os.FileMode(0)
3737
if fi, err := os.Stat(path); err == nil {
3838
if !fi.Mode().IsRegular() {
39-
f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0)
39+
// No O_TRUNC: truncating a device or pipe is meaningless, and
40+
// Windows rejects it on the NUL device with ERROR_INVALID_FUNCTION
41+
// (#208), which broke -o NUL.
42+
f, err := os.OpenFile(path, os.O_WRONLY, 0)
4043
if err != nil {
4144
return nil, err
4245
}

‎internal/output/lifecycle_test.go‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"os"
88
"path/filepath"
9-
"runtime"
109
"strings"
1110
"testing"
1211

@@ -97,9 +96,6 @@ func TestCreateFileMissingDirectory(t *testing.T) {
9796
}
9897

9998
func TestCreateFileWritesNonRegularTargetDirectly(t *testing.T) {
100-
if runtime.GOOS == "windows" {
101-
t.Skip("opening NUL with O_TRUNC fails on Windows")
102-
}
10399
fi, err := os.Stat(os.DevNull)
104100
if err != nil || fi.Mode().IsRegular() {
105101
t.Skipf("%s is not a device here", os.DevNull)

0 commit comments

Comments
 (0)