From 0e90b25bde4ea14b75ba98948444530d2c82b8e9 Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:39:25 +0000 Subject: [PATCH] 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> --- internal/output/file.go | 5 ++++- internal/output/lifecycle_test.go | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/output/file.go b/internal/output/file.go index ec45027..bd5f7e7 100644 --- a/internal/output/file.go +++ b/internal/output/file.go @@ -36,7 +36,10 @@ func CreateFile(path string) (*File, error) { keep := os.FileMode(0) if fi, err := os.Stat(path); err == nil { if !fi.Mode().IsRegular() { - f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0) + // No O_TRUNC: truncating a device or pipe is meaningless, and + // Windows rejects it on the NUL device with ERROR_INVALID_FUNCTION + // (#208), which broke -o NUL. + f, err := os.OpenFile(path, os.O_WRONLY, 0) if err != nil { return nil, err } diff --git a/internal/output/lifecycle_test.go b/internal/output/lifecycle_test.go index 413b881..a2b50d0 100644 --- a/internal/output/lifecycle_test.go +++ b/internal/output/lifecycle_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "os" "path/filepath" - "runtime" "strings" "testing" @@ -97,9 +96,6 @@ func TestCreateFileMissingDirectory(t *testing.T) { } func TestCreateFileWritesNonRegularTargetDirectly(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("opening NUL with O_TRUNC fails on Windows") - } fi, err := os.Stat(os.DevNull) if err != nil || fi.Mode().IsRegular() { t.Skipf("%s is not a device here", os.DevNull)