go-graphviz
go-graphviz copied to clipboard
RenderFilename() does not work as expected
I had some code that used
if err := g.RenderFilename(mycontext, graph, graphviz.PNG, imagepath); err != nil {
log.Fatal(err)
}
This used to work, generating an image in imagepath as expected.
However re-running this code for the first time for a while (and after an update to the latest version) the code no longer works. There is no error - but no file is created.
If I replace with
fl, err := os.Create(imagepath)
if err != nil {
log.Fatal(err)
}
defer fl.Close()
if err := g.Render(mycontext, graph, graphviz.PNG, fl); err != nil {
log.Fatal(err)
}
Then the code works as expected - an image file is created.
My expectation is that RenderFilename should render to a file and leave that file available.
go version go1.23.1 linux/amd64
github.com/goccy/go-graphviz v0.2.1
@GavinB-hpe Thank you for your reporting.
But RenderFilename function works on my side, so could you provide a full reproducible code example ?
I see this as well on MacOS. the RenderFilename function works if the file already exists. It fails silently otherwise. I can touch the file and then see the contents updated.
package main
import (
"context"
"github.com/goccy/go-graphviz"
"log"
)
func main() {
ctx := context.Background()
g, err := graphviz.New(ctx)
// Assume this is a valid graphviz file:
graph, err := graphviz.ParseFile("MathSci.dot")
checkErr(err)
// Works:
//f, err := os.Create("out.png")
//checkErr(err)
//defer f.Close()
//err = g.Render(ctx, graph, graphviz.PNG, f)
//checkErr(err)
// Does not work unless the file already exists:
err = g.RenderFilename(ctx, graph, graphviz.PNG, "out.png")
checkErr(err) // no error will be reported, but no file will exist
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
Running the program:
$ go version
go version go1.23.1 darwin/arm64
$ ls -l out.png
ls: out.png: No such file or directory
$ go run main.go
$ ls -l out.png
ls: out.png: No such file or directory
$ touch out.png
$ ls -l out.png
-rw-r--r-- 1 cws staff 0 Oct 25 10:04 out.png
$ go run main.go
$ ls -l out.png
-rw-r--r-- 1 cws staff 72624 Oct 25 10:04 out.png
I've fixed this problem with v0.2.8 ! Thank you for your reporting.