Skip to content

Commit

Permalink
Properly Close some files, which also helps address some Windows test…
Browse files Browse the repository at this point in the history
… failures.

We may want to also revisit some of the other defers in iohelpers/ in a future change, to set the returned error if appropriate.

PiperOrigin-RevId: 646604591
  • Loading branch information
suyashkumar authored and copybara-github committed Jun 25, 2024
1 parent d8a423a commit da5fcd6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/iohelpers/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func filesWithSuffixGCS(ctx context.Context, gcsPath, suffix string, cfg IOConfi

// ReadFile reads the contents of a file at the given path.
// If the path is a GCS it will attempt to read the file from GCS.
func ReadFile(ctx context.Context, filePath string, cfg *IOConfig) ([]byte, error) {
func ReadFile(ctx context.Context, filePath string, cfg *IOConfig) (contents []byte, funcErr error) {
if strings.HasPrefix(filePath, "gs://") {
if cfg == nil {
return nil, fmt.Errorf("ReadFile() IOConfig cannot be nil for GCS paths, but was nil. path: %s", filePath)
Expand All @@ -103,6 +103,15 @@ func ReadFile(ctx context.Context, filePath string, cfg *IOConfig) ([]byte, erro
if err != nil {
return nil, err
}

defer func() {
if err = f.Close(); err != nil {
if funcErr == nil {
funcErr = err
}
}
}()

return io.ReadAll(f)
}

Expand Down
2 changes: 2 additions & 0 deletions terminology/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"os"

"path/filepath"
"strings"

Expand Down Expand Up @@ -69,6 +70,7 @@ func NewLocalFHIRProvider(dir string) (*LocalFHIRProvider, error) {
if err != nil {
return nil, err
}
defer f.Close() // Possible returned error on read is ignored, since it'll be superseeded by the decodeFHIRResource error.
fr, err := decodeFHIRResource(f)
if err != nil {
return nil, err
Expand Down

0 comments on commit da5fcd6

Please sign in to comment.