Skip to content

Commit 546d738

Browse files
authored
fmt: always truncate the file when writing format changes (#65)
Signed-off-by: Ryan Cragun <[email protected]>
1 parent 53ae00d commit 546d738

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

internal/server/service_v1_format.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"io"
87
"os"
98
"strings"
109

@@ -69,15 +68,15 @@ func (s *ServiceV1) Format(
6968
}
7069

7170
if file.GetPath() != "STDIN" && req.GetConfig().GetWrite() && !req.GetConfig().GetCheck() {
72-
f, err := os.OpenFile(file.GetPath(), os.O_RDWR, 0o755)
71+
f, err := os.OpenFile(file.GetPath(), os.O_RDWR|os.O_TRUNC, 0o755)
7372
if err != nil {
7473
res.Diagnostics = diagnostics.FromErr(err)
7574
res.Responses = append(res.Responses, r)
7675
continue
7776
}
7877
defer f.Close()
7978

80-
_, err = io.Copy(f, bytes.NewReader(formatted))
79+
_, err = f.Write(formatted)
8180
if err != nil {
8281
res.Diagnostics = diagnostics.FromErr(err)
8382
res.Responses = append(res.Responses, r)

version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
//
1515
// Version must conform to the format expected by github.com/hashicorp/go-version
1616
// for tests to work.
17-
Version = "0.0.9"
17+
Version = "0.0.10"
1818

1919
// VersionPrerelease is a pre-release marker for the version. If this is ""
2020
// (empty string) then it means that it is a final release. Otherwise, this

0 commit comments

Comments
 (0)