This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 8c82d4e0 fix(tmpl): report generated output write errors (#1037)
8c82d4e0 is described below

commit 8c82d4e0ec56f52c5fbdfa4e6b860e5147f8c085
Author: Minh Vu <[email protected]>
AuthorDate: Tue Jul 28 17:07:27 2026 +0200

    fix(tmpl): report generated output write errors (#1037)
    
    ## What changed
    
    Check the final `os.WriteFile` result when generating template output
    and return the failure to `main` for reporting.
    
    ## Why
    
    The generator previously exited successfully when the output could not
    be written, for example when the output path was a directory. That could
    leave stale generated files in place while making the generation step
    appear successful.
    
    The new regression test uses a directory as the output target and
    verifies that `process` returns an error containing the failed path.
    
    ## Validation
    
    `go test -vet=off ./arrow/_tools/tmpl`
---
 arrow/_tools/tmpl/main.go      | 11 ++++++++---
 arrow/_tools/tmpl/main_test.go | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/arrow/_tools/tmpl/main.go b/arrow/_tools/tmpl/main.go
index cad34aec..530accd5 100644
--- a/arrow/_tools/tmpl/main.go
+++ b/arrow/_tools/tmpl/main.go
@@ -114,7 +114,9 @@ func main() {
        }
 
        in.In = readData(*dataArg)
-       process(in, specs)
+       if err := process(in, specs); err != nil {
+               errExit("%s", err)
+       }
 }
 
 func mustReadAll(path string) []byte {
@@ -148,7 +150,7 @@ var funcs = template.FuncMap{
        "upper": strings.ToUpper,
 }
 
-func process(data interface{}, specs []pathSpec) {
+func process(data interface{}, specs []pathSpec) error {
        for _, spec := range specs {
                var (
                        t   *template.Template
@@ -178,8 +180,11 @@ func process(data interface{}, specs []pathSpec) {
                        }
                }
 
-               os.WriteFile(spec.out, generated, fileMode(spec.in))
+               if err := os.WriteFile(spec.out, generated, fileMode(spec.in)); 
err != nil {
+                       return fmt.Errorf("error writing generated output %q: 
%w", spec.out, err)
+               }
        }
+       return nil
 }
 
 var (
diff --git a/arrow/_tools/tmpl/main_test.go b/arrow/_tools/tmpl/main_test.go
index 15fa5b88..948e0260 100644
--- a/arrow/_tools/tmpl/main_test.go
+++ b/arrow/_tools/tmpl/main_test.go
@@ -17,9 +17,28 @@
 package main
 
 import (
+       "os"
+       "path/filepath"
+       "strings"
        "testing"
 )
 
+func TestProcessReturnsOutputWriteError(t *testing.T) {
+       dir := t.TempDir()
+       tmpl := filepath.Join(dir, "input.txt.tmpl")
+       if err := os.WriteFile(tmpl, []byte("generated"), 0o600); err != nil {
+               t.Fatal(err)
+       }
+
+       err := process(nil, []pathSpec{{in: tmpl, out: dir}})
+       if err == nil {
+               t.Fatal("process returned nil for an unwritable output path")
+       }
+       if !strings.Contains(err.Error(), dir) {
+               t.Errorf("error %q does not contain output path %q", err, dir)
+       }
+}
+
 func TestStripComments(t *testing.T) {
        tests := []struct {
                name string

Reply via email to