Skip to content

Commit caf0e3b

Browse files
committed
Fixed a lot of i18n.Tr formatting errors
This commit fixes invalid calls to i18n.Tr. 1. Missing positional arguments, for example: return fmt.Errorf(i18n.Tr("installing %[1]s tool: %[2]s"), tool, err) in the above case the positional arguments must be part of the Tr call: - return fmt.Errorf(i18n.Tr("installing %[1]s tool: %[2]s"), tool, err) + return fmt.Errorf(i18n.Tr("installing %[1]s tool: %[2]s", tool, err)) 2. This also makes the fmt.Errorf call useless and it could be replaced by the less expensive errors.New: - return fmt.Errorf(i18n.Tr("installing %[1]s tool: %[2]s", tool, err)) + return errors.New(i18n.Tr("installing %[1]s tool: %[2]s", tool, err)) but we have cases of useless calls even when the string is a constant, for example: - err := fmt.Errorf(i18n.Tr("no instance specified")) + err := errors.New(i18n.Tr("no instance specified")) Unfortunately, this imperfection is not detected by the linter. 3. The "%w" directive is not supported directly in i18n.Tr, so we have to wrap it around another fmt.Errorf: - return nil, fmt.Errorf(i18n.Tr("reading library headers: %w"), err) + return nil, fmt.Errorf("%s: %w", i18n.Tr("reading library headers"), err)
1 parent 77a9203 commit caf0e3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+219
-209
lines changed

commands/instances.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package commands
1717

1818
import (
1919
"context"
20+
"errors"
2021
"fmt"
2122
"net/url"
2223
"path/filepath"
@@ -51,11 +52,11 @@ func installTool(ctx context.Context, pm *packagemanager.PackageManager, tool *c
5152

5253
taskCB(&rpc.TaskProgress{Name: i18n.Tr("Downloading missing tool %s", tool)})
5354
if err := pme.DownloadToolRelease(ctx, tool, downloadCB); err != nil {
54-
return fmt.Errorf(i18n.Tr("downloading %[1]s tool: %[2]s"), tool, err)
55+
return errors.New(i18n.Tr("downloading %[1]s tool: %[2]s", tool, err))
5556
}
5657
taskCB(&rpc.TaskProgress{Completed: true})
5758
if err := pme.InstallTool(tool, taskCB, true); err != nil {
58-
return fmt.Errorf(i18n.Tr("installing %[1]s tool: %[2]s"), tool, err)
59+
return errors.New(i18n.Tr("installing %[1]s tool: %[2]s", tool, err))
5960
}
6061
return nil
6162
}
@@ -182,7 +183,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
182183
if err != nil {
183184
e := &cmderrors.InitFailedError{
184185
Code: codes.InvalidArgument,
185-
Cause: fmt.Errorf(i18n.Tr("Invalid additional URL: %v", err)),
186+
Cause: errors.New(i18n.Tr("Invalid additional URL: %v", err)),
186187
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INVALID_INDEX_URL,
187188
}
188189
responseError(e.GRPCStatus())
@@ -220,7 +221,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
220221
if err != nil {
221222
e := &cmderrors.InitFailedError{
222223
Code: codes.FailedPrecondition,
223-
Cause: fmt.Errorf(i18n.Tr("Loading index file: %v", err)),
224+
Cause: errors.New(i18n.Tr("Loading index file: %v", err)),
224225
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
225226
}
226227
responseError(e.GRPCStatus())
@@ -231,7 +232,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
231232
if err := pmb.LoadPackageIndex(URL); err != nil {
232233
e := &cmderrors.InitFailedError{
233234
Code: codes.FailedPrecondition,
234-
Cause: fmt.Errorf(i18n.Tr("Loading index file: %v", err)),
235+
Cause: errors.New(i18n.Tr("Loading index file: %v", err)),
235236
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_LOAD_ERROR,
236237
}
237238
responseError(e.GRPCStatus())
@@ -271,7 +272,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
271272
if latest == nil {
272273
e := &cmderrors.InitFailedError{
273274
Code: codes.Internal,
274-
Cause: fmt.Errorf(i18n.Tr("can't find latest release of tool %s", name)),
275+
Cause: errors.New(i18n.Tr("can't find latest release of tool %s", name)),
275276
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR,
276277
}
277278
responseError(e.GRPCStatus())
@@ -341,7 +342,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
341342
logrus.WithField("index", indexFile).Info("Loading libraries index file")
342343
li, err := librariesindex.LoadIndex(indexFile)
343344
if err != nil {
344-
s := status.Newf(codes.FailedPrecondition, i18n.Tr("Loading index file: %v"), err)
345+
s := status.Newf(codes.FailedPrecondition, i18n.Tr("Loading index file: %v", err))
345346
responseError(s)
346347
li = librariesindex.EmptyIndex
347348
}

commands/service_board_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func BoardListWatchProxyToChan(ctx context.Context) (rpc.ArduinoCoreService_Boar
270270
func (s *arduinoCoreServerImpl) BoardListWatch(req *rpc.BoardListWatchRequest, stream rpc.ArduinoCoreService_BoardListWatchServer) error {
271271
syncSend := NewSynchronizedSend(stream.Send)
272272
if req.GetInstance() == nil {
273-
err := fmt.Errorf(i18n.Tr("no instance specified"))
273+
err := errors.New(i18n.Tr("no instance specified"))
274274
syncSend.Send(&rpc.BoardListWatchResponse{
275275
EventType: "error",
276276
Error: err.Error(),

commands/service_compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
118118
if targetPlatform == nil {
119119
return &cmderrors.PlatformNotFoundError{
120120
Platform: fmt.Sprintf("%s:%s", fqbn.Package, fqbn.PlatformArch),
121-
Cause: fmt.Errorf(i18n.Tr("platform not installed")),
121+
Cause: errors.New(i18n.Tr("platform not installed")),
122122
}
123123
}
124124
return &cmderrors.InvalidFQBNError{Cause: err}
@@ -152,7 +152,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
152152
encryptProp := boardBuildProperties.ContainsKey("build.keys.encrypt_key")
153153
// we verify that all the properties for the secure boot keys are defined or none of them is defined.
154154
if !(keychainProp == signProp && signProp == encryptProp) {
155-
return fmt.Errorf(i18n.Tr("Firmware encryption/signing requires all the following properties to be defined: %s", "build.keys.keychain, build.keys.sign_key, build.keys.encrypt_key"))
155+
return errors.New(i18n.Tr("Firmware encryption/signing requires all the following properties to be defined: %s", "build.keys.keychain, build.keys.sign_key, build.keys.encrypt_key"))
156156
}
157157

158158
// Generate or retrieve build path

commands/service_library_install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, s
138138

139139
if req.GetNoOverwrite() {
140140
if installTask.ReplacedLib != nil {
141-
return fmt.Errorf(i18n.Tr("Library %[1]s is already installed, but with a different version: %[2]s", libRelease, installTask.ReplacedLib))
141+
return errors.New(i18n.Tr("Library %[1]s is already installed, but with a different version: %[2]s", libRelease, installTask.ReplacedLib))
142142
}
143143
}
144144
libReleasesToInstall[libRelease] = installTask

commands/service_upload.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -694,18 +694,18 @@ func detectUploadPort(
694694
func runTool(recipeID string, props *properties.Map, outStream, errStream io.Writer, verbose bool, dryRun bool, toolEnv []string) error {
695695
recipe, ok := props.GetOk(recipeID)
696696
if !ok {
697-
return fmt.Errorf(i18n.Tr("recipe not found '%s'"), recipeID)
697+
return errors.New(i18n.Tr("recipe not found '%s'", recipeID))
698698
}
699699
if strings.TrimSpace(recipe) == "" {
700700
return nil // Nothing to run
701701
}
702702
if props.IsPropertyMissingInExpandPropsInString("serial.port", recipe) || props.IsPropertyMissingInExpandPropsInString("serial.port.file", recipe) {
703-
return fmt.Errorf(i18n.Tr("no upload port provided"))
703+
return errors.New(i18n.Tr("no upload port provided"))
704704
}
705705
cmdLine := props.ExpandPropsInString(recipe)
706706
cmdArgs, err := properties.SplitQuotedString(cmdLine, `"'`, false)
707707
if err != nil {
708-
return fmt.Errorf(i18n.Tr("invalid recipe '%[1]s': %[2]s"), recipe, err)
708+
return errors.New(i18n.Tr("invalid recipe '%[1]s': %[2]s", recipe, err))
709709
}
710710

711711
// Run Tool
@@ -718,18 +718,18 @@ func runTool(recipeID string, props *properties.Map, outStream, errStream io.Wri
718718
}
719719
cmd, err := paths.NewProcess(toolEnv, cmdArgs...)
720720
if err != nil {
721-
return fmt.Errorf(i18n.Tr("cannot execute upload tool: %s"), err)
721+
return errors.New(i18n.Tr("cannot execute upload tool: %s", err))
722722
}
723723

724724
cmd.RedirectStdoutTo(outStream)
725725
cmd.RedirectStderrTo(errStream)
726726

727727
if err := cmd.Start(); err != nil {
728-
return fmt.Errorf(i18n.Tr("cannot execute upload tool: %s"), err)
728+
return errors.New(i18n.Tr("cannot execute upload tool: %s", err))
729729
}
730730

731731
if err := cmd.Wait(); err != nil {
732-
return fmt.Errorf(i18n.Tr("uploading error: %s"), err)
732+
return errors.New(i18n.Tr("uploading error: %s", err))
733733
}
734734

735735
return nil
@@ -751,7 +751,7 @@ func determineBuildPathAndSketchName(importFile, importDir string, sk *sketch.Sk
751751
// Case 1: importFile flag has been specified
752752
if importFile != "" {
753753
if importDir != "" {
754-
return nil, "", fmt.Errorf(i18n.Tr("%s and %s cannot be used together", "importFile", "importDir"))
754+
return nil, "", errors.New(i18n.Tr("%s and %s cannot be used together", "importFile", "importDir"))
755755
}
756756

757757
// We have a path like "path/to/my/build/SketchName.ino.bin". We are going to
@@ -761,7 +761,7 @@ func determineBuildPathAndSketchName(importFile, importDir string, sk *sketch.Sk
761761

762762
importFilePath := paths.New(importFile)
763763
if !importFilePath.Exist() {
764-
return nil, "", fmt.Errorf(i18n.Tr("binary file not found in %s"), importFilePath)
764+
return nil, "", errors.New(i18n.Tr("binary file not found in %s", importFilePath))
765765
}
766766
return importFilePath.Parent(), strings.TrimSuffix(importFilePath.Base(), importFilePath.Ext()), nil
767767
}

internal/arduino/builder/builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ func (b *Builder) build() error {
482482
func (b *Builder) prepareCommandForRecipe(buildProperties *properties.Map, recipe string, removeUnsetProperties bool) (*paths.Process, error) {
483483
pattern := buildProperties.Get(recipe)
484484
if pattern == "" {
485-
return nil, fmt.Errorf(i18n.Tr("%[1]s pattern is missing"), recipe)
485+
return nil, errors.New(i18n.Tr("%[1]s pattern is missing", recipe))
486486
}
487487

488488
commandLine := buildProperties.ExpandPropsInString(pattern)

internal/arduino/builder/core.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"crypto/md5"
2020
"encoding/hex"
2121
"errors"
22-
"fmt"
2322
"os"
2423
"strings"
2524

@@ -93,7 +92,7 @@ func (b *Builder) compileCore() (*paths.Path, paths.PathList, error) {
9392
targetArchivedCore = b.coreBuildCachePath.Join(archivedCoreName, "core.a")
9493

9594
if _, err := buildcache.New(b.coreBuildCachePath).GetOrCreate(archivedCoreName); errors.Is(err, buildcache.CreateDirErr) {
96-
return nil, nil, fmt.Errorf(i18n.Tr("creating core cache folder: %s", err))
95+
return nil, nil, errors.New(i18n.Tr("creating core cache folder: %s", err))
9796
}
9897

9998
var canUseArchivedCore bool

internal/arduino/builder/sizer.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (b *Builder) checkSize() (ExecutablesFileSections, error) {
212212
func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, dataSize int, eepromSize int, resErr error) {
213213
command, err := b.prepareCommandForRecipe(properties, "recipe.size.pattern", false)
214214
if err != nil {
215-
resErr = fmt.Errorf(i18n.Tr("Error while determining sketch size: %s"), err)
215+
resErr = errors.New(i18n.Tr("Error while determining sketch size: %s", err))
216216
return
217217
}
218218
if b.logger.Verbose() {
@@ -222,11 +222,11 @@ func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, data
222222
command.RedirectStdoutTo(commandStdout)
223223
command.RedirectStderrTo(b.logger.Stderr())
224224
if err := command.Start(); err != nil {
225-
resErr = fmt.Errorf(i18n.Tr("Error while determining sketch size: %s"), err)
225+
resErr = errors.New(i18n.Tr("Error while determining sketch size: %s", err))
226226
return
227227
}
228228
if err := command.Wait(); err != nil {
229-
resErr = fmt.Errorf(i18n.Tr("Error while determining sketch size: %s"), err)
229+
resErr = errors.New(i18n.Tr("Error while determining sketch size: %s", err))
230230
return
231231
}
232232

@@ -237,7 +237,7 @@ func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, data
237237

238238
textSize, err = computeSize(properties.Get("recipe.size.regex"), out)
239239
if err != nil {
240-
resErr = fmt.Errorf(i18n.Tr("Invalid size regexp: %s"), err)
240+
resErr = errors.New(i18n.Tr("Invalid size regexp: %s", err))
241241
return
242242
}
243243
if textSize == -1 {
@@ -247,13 +247,13 @@ func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, data
247247

248248
dataSize, err = computeSize(properties.Get("recipe.size.regex.data"), out)
249249
if err != nil {
250-
resErr = fmt.Errorf(i18n.Tr("Invalid data size regexp: %s"), err)
250+
resErr = errors.New(i18n.Tr("Invalid data size regexp: %s", err))
251251
return
252252
}
253253

254254
eepromSize, err = computeSize(properties.Get("recipe.size.regex.eeprom"), out)
255255
if err != nil {
256-
resErr = fmt.Errorf(i18n.Tr("Invalid eeprom size regexp: %s"), err)
256+
resErr = errors.New(i18n.Tr("Invalid eeprom size regexp: %s", err))
257257
return
258258
}
259259

internal/arduino/builder/sketch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (b *Builder) sketchMergeSources(overrides map[string]string) (int, string,
7878
}
7979
data, err := f.ReadFile()
8080
if err != nil {
81-
return "", fmt.Errorf(i18n.Tr("reading file %[1]s: %[2]s"), f, err)
81+
return "", errors.New(i18n.Tr("reading file %[1]s: %[2]s", f, err))
8282
}
8383
return string(data), nil
8484
}

internal/arduino/cores/board.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package cores
1717

1818
import (
19-
"fmt"
19+
"errors"
2020
"strings"
2121
"sync"
2222

@@ -140,14 +140,14 @@ func (b *Board) GetBuildProperties(fqbn *FQBN) (*properties.Map, error) {
140140
// Check for residual invalid options...
141141
for option, value := range config.AsMap() {
142142
if option == "" {
143-
return nil, fmt.Errorf(i18n.Tr("invalid empty option found"))
143+
return nil, errors.New(i18n.Tr("invalid empty option found"))
144144
}
145145
if _, ok := b.configOptions.GetOk(option); !ok {
146-
return nil, fmt.Errorf(i18n.Tr("invalid option '%s'"), option)
146+
return nil, errors.New(i18n.Tr("invalid option '%s'", option))
147147
}
148148
optionsConf, ok := b.configOptionProperties[option+"="+value]
149149
if !ok {
150-
return nil, fmt.Errorf(i18n.Tr("invalid value '%[1]s' for option '%[2]s'"), value, option)
150+
return nil, errors.New(i18n.Tr("invalid value '%[1]s' for option '%[2]s'", value, option))
151151
}
152152
buildProperties.Merge(optionsConf)
153153
}
@@ -163,7 +163,7 @@ func (b *Board) GetBuildProperties(fqbn *FQBN) (*properties.Map, error) {
163163
func (b *Board) GeneratePropertiesForConfiguration(config string) (*properties.Map, error) {
164164
fqbn, err := ParseFQBN(b.String() + ":" + config)
165165
if err != nil {
166-
return nil, fmt.Errorf(i18n.Tr("parsing fqbn: %s"), err)
166+
return nil, errors.New(i18n.Tr("parsing fqbn: %s", err))
167167
}
168168
return b.GetBuildProperties(fqbn)
169169
}

internal/arduino/cores/fqbn.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package cores
1717

1818
import (
19-
"fmt"
19+
"errors"
2020
"regexp"
2121
"strings"
2222

@@ -47,7 +47,7 @@ func ParseFQBN(fqbnIn string) (*FQBN, error) {
4747
// Split fqbn
4848
fqbnParts := strings.Split(fqbnIn, ":")
4949
if len(fqbnParts) < 3 || len(fqbnParts) > 4 {
50-
return nil, fmt.Errorf("not an FQBN: %s", fqbnIn)
50+
return nil, errors.New(i18n.Tr("not an FQBN: %s", fqbnIn))
5151
}
5252

5353
fqbn := &FQBN{
@@ -57,33 +57,33 @@ func ParseFQBN(fqbnIn string) (*FQBN, error) {
5757
Configs: properties.NewMap(),
5858
}
5959
if fqbn.BoardID == "" {
60-
return nil, fmt.Errorf(i18n.Tr("empty board identifier"))
60+
return nil, errors.New(i18n.Tr("empty board identifier"))
6161
}
6262
// Check if the fqbn contains invalid characters
6363
fqbnValidationRegex := regexp.MustCompile(`^[a-zA-Z0-9_.-]*$`)
6464
for i := 0; i < 3; i++ {
6565
if !fqbnValidationRegex.MatchString(fqbnParts[i]) {
66-
return nil, fmt.Errorf(i18n.Tr("fqbn's field %s contains an invalid character"), fqbnParts[i])
66+
return nil, errors.New(i18n.Tr("fqbn's field %s contains an invalid character", fqbnParts[i]))
6767
}
6868
}
6969
if len(fqbnParts) > 3 {
7070
for _, pair := range strings.Split(fqbnParts[3], ",") {
7171
parts := strings.SplitN(pair, "=", 2)
7272
if len(parts) != 2 {
73-
return nil, fmt.Errorf(i18n.Tr("invalid config option: %s"), pair)
73+
return nil, errors.New(i18n.Tr("invalid config option: %s", pair))
7474
}
7575
k := strings.TrimSpace(parts[0])
7676
v := strings.TrimSpace(parts[1])
7777
if k == "" {
78-
return nil, fmt.Errorf(i18n.Tr("invalid config option: %s"), pair)
78+
return nil, errors.New(i18n.Tr("invalid config option: %s", pair))
7979
}
8080
if !fqbnValidationRegex.MatchString(k) {
81-
return nil, fmt.Errorf(i18n.Tr("config key %s contains an invalid character"), k)
81+
return nil, errors.New(i18n.Tr("config key %s contains an invalid character", k))
8282
}
8383
// The config value can also contain the = symbol
8484
valueValidationRegex := regexp.MustCompile(`^[a-zA-Z0-9=_.-]*$`)
8585
if !valueValidationRegex.MatchString(v) {
86-
return nil, fmt.Errorf(i18n.Tr("config value %s contains an invalid character"), v)
86+
return nil, errors.New(i18n.Tr("config value %s contains an invalid character", v))
8787
}
8888
fqbn.Configs.Set(k, v)
8989
}

internal/arduino/cores/packageindex/index.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package packageindex
1717

1818
import (
1919
"encoding/json"
20+
"errors"
2021
"fmt"
2122

2223
"github.com/arduino/arduino-cli/internal/arduino/cores"
@@ -273,7 +274,7 @@ func (inPlatformRelease indexPlatformRelease) extractPlatformIn(outPackage *core
273274

274275
size, err := inPlatformRelease.Size.Int64()
275276
if err != nil {
276-
return fmt.Errorf(i18n.Tr("invalid platform archive size: %s"), err)
277+
return errors.New(i18n.Tr("invalid platform archive size: %s", err))
277278
}
278279
outPlatformRelease := outPlatform.GetOrCreateRelease(inPlatformRelease.Version)
279280
outPlatformRelease.Name = inPlatformRelease.Name

0 commit comments

Comments
 (0)