@@ -30,6 +30,7 @@ import (
3030 "github.com/arduino/arduino-cli/legacy/builder/utils"
3131 "github.com/arduino/go-paths-helper"
3232 "github.com/arduino/go-properties-orderedmap"
33+ "github.com/pkg/errors"
3334)
3435
3536func PrintProgressIfProgressEnabledAndMachineLogger (ctx * types.Context ) {
@@ -48,18 +49,18 @@ func PrintProgressIfProgressEnabledAndMachineLogger(ctx *types.Context) {
4849func CompileFilesRecursive (ctx * types.Context , sourcePath * paths.Path , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
4950 objectFiles , err := CompileFiles (ctx , sourcePath , false , buildPath , buildProperties , includes )
5051 if err != nil {
51- return nil , i18n . WrapError (err )
52+ return nil , errors . WithStack (err )
5253 }
5354
5455 folders , err := utils .ReadDirFiltered (sourcePath .String (), utils .FilterDirs )
5556 if err != nil {
56- return nil , i18n . WrapError (err )
57+ return nil , errors . WithStack (err )
5758 }
5859
5960 for _ , folder := range folders {
6061 subFolderObjectFiles , err := CompileFilesRecursive (ctx , sourcePath .Join (folder .Name ()), buildPath .Join (folder .Name ()), buildProperties , includes )
6162 if err != nil {
62- return nil , i18n . WrapError (err )
63+ return nil , errors . WithStack (err )
6364 }
6465 objectFiles .AddAll (subFolderObjectFiles )
6566 }
@@ -70,15 +71,15 @@ func CompileFilesRecursive(ctx *types.Context, sourcePath *paths.Path, buildPath
7071func CompileFiles (ctx * types.Context , sourcePath * paths.Path , recurse bool , buildPath * paths.Path , buildProperties * properties.Map , includes []string ) (paths.PathList , error ) {
7172 sObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".S" , constants .RECIPE_S_PATTERN )
7273 if err != nil {
73- return nil , i18n . WrapError (err )
74+ return nil , errors . WithStack (err )
7475 }
7576 cObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".c" , constants .RECIPE_C_PATTERN )
7677 if err != nil {
77- return nil , i18n . WrapError (err )
78+ return nil , errors . WithStack (err )
7879 }
7980 cppObjectFiles , err := compileFilesWithExtensionWithRecipe (ctx , sourcePath , recurse , buildPath , buildProperties , includes , ".cpp" , constants .RECIPE_CPP_PATTERN )
8081 if err != nil {
81- return nil , i18n . WrapError (err )
82+ return nil , errors . WithStack (err )
8283 }
8384 objectFiles := paths .NewPathList ()
8485 objectFiles .AddAll (sObjectFiles )
@@ -90,15 +91,15 @@ func CompileFiles(ctx *types.Context, sourcePath *paths.Path, recurse bool, buil
9091func compileFilesWithExtensionWithRecipe (ctx * types.Context , sourcePath * paths.Path , recurse bool , buildPath * paths.Path , buildProperties * properties.Map , includes []string , extension string , recipe string ) (paths.PathList , error ) {
9192 sources , err := findFilesInFolder (sourcePath , extension , recurse )
9293 if err != nil {
93- return nil , i18n . WrapError (err )
94+ return nil , errors . WithStack (err )
9495 }
9596 return compileFilesWithRecipe (ctx , sourcePath , sources , buildPath , buildProperties , includes , recipe )
9697}
9798
9899func findFilesInFolder (sourcePath * paths.Path , extension string , recurse bool ) (paths.PathList , error ) {
99100 files , err := utils .ReadDirFiltered (sourcePath .String (), utils .FilterFilesWithExtensions (extension ))
100101 if err != nil {
101- return nil , i18n . WrapError (err )
102+ return nil , errors . WithStack (err )
102103 }
103104 var sources paths.PathList
104105 for _ , file := range files {
@@ -108,13 +109,13 @@ func findFilesInFolder(sourcePath *paths.Path, extension string, recurse bool) (
108109 if recurse {
109110 folders , err := utils .ReadDirFiltered (sourcePath .String (), utils .FilterDirs )
110111 if err != nil {
111- return nil , i18n . WrapError (err )
112+ return nil , errors . WithStack (err )
112113 }
113114
114115 for _ , folder := range folders {
115116 otherSources , err := findFilesInFolder (sourcePath .Join (folder .Name ()), extension , recurse )
116117 if err != nil {
117- return nil , i18n . WrapError (err )
118+ return nil , errors . WithStack (err )
118119 }
119120 sources = append (sources , otherSources ... )
120121 }
@@ -126,7 +127,7 @@ func findFilesInFolder(sourcePath *paths.Path, extension string, recurse bool) (
126127func findAllFilesInFolder (sourcePath string , recurse bool ) ([]string , error ) {
127128 files , err := utils .ReadDirFiltered (sourcePath , utils .FilterFiles ())
128129 if err != nil {
129- return nil , i18n . WrapError (err )
130+ return nil , errors . WithStack (err )
130131 }
131132 var sources []string
132133 for _ , file := range files {
@@ -136,15 +137,15 @@ func findAllFilesInFolder(sourcePath string, recurse bool) ([]string, error) {
136137 if recurse {
137138 folders , err := utils .ReadDirFiltered (sourcePath , utils .FilterDirs )
138139 if err != nil {
139- return nil , i18n . WrapError (err )
140+ return nil , errors . WithStack (err )
140141 }
141142
142143 for _ , folder := range folders {
143144 if ! utils .IsSCCSOrHiddenFile (folder ) {
144145 // Skip SCCS directories as they do not influence the build and can be very large
145146 otherSources , err := findAllFilesInFolder (filepath .Join (sourcePath , folder .Name ()), recurse )
146147 if err != nil {
147- return nil , i18n . WrapError (err )
148+ return nil , errors . WithStack (err )
148149 }
149150 sources = append (sources , otherSources ... )
150151 }
@@ -160,7 +161,7 @@ func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources
160161 return objectFiles , nil
161162 }
162163 var objectFilesMux sync.Mutex
163- var errors []error
164+ var errorsList []error
164165 var errorsMux sync.Mutex
165166
166167 ctx .Progress .Steps = ctx .Progress .Steps / float64 (len (sources ))
@@ -171,7 +172,7 @@ func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources
171172 objectFile , err := compileFileWithRecipe (ctx , sourcePath , source , buildPath , buildProperties , includes , recipe )
172173 if err != nil {
173174 errorsMux .Lock ()
174- errors = append (errors , err )
175+ errorsList = append (errorsList , err )
175176 errorsMux .Unlock ()
176177 } else {
177178 objectFilesMux .Lock ()
@@ -199,7 +200,7 @@ func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources
199200 // Feed jobs until error or done
200201 for _ , source := range sources {
201202 errorsMux .Lock ()
202- gotError := len (errors ) > 0
203+ gotError := len (errorsList ) > 0
203204 errorsMux .Unlock ()
204205 if gotError {
205206 break
@@ -208,9 +209,9 @@ func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources
208209 }
209210 close (queue )
210211 wg .Wait ()
211- if len (errors ) > 0 {
212+ if len (errorsList ) > 0 {
212213 // output the first error
213- return nil , i18n . WrapError ( errors [0 ])
214+ return nil , errors . WithStack ( errorsList [0 ])
214215 }
215216 objectFiles .Sort ()
216217 return objectFiles , nil
@@ -224,25 +225,25 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
224225 properties .SetPath (constants .BUILD_PROPERTIES_SOURCE_FILE , source )
225226 relativeSource , err := sourcePath .RelTo (source )
226227 if err != nil {
227- return nil , i18n . WrapError (err )
228+ return nil , errors . WithStack (err )
228229 }
229230 depsFile := buildPath .Join (relativeSource .String () + ".d" )
230231 objectFile := buildPath .Join (relativeSource .String () + ".o" )
231232
232233 properties .SetPath (constants .BUILD_PROPERTIES_OBJECT_FILE , objectFile )
233234 err = objectFile .Parent ().MkdirAll ()
234235 if err != nil {
235- return nil , i18n . WrapError (err )
236+ return nil , errors . WithStack (err )
236237 }
237238
238239 objIsUpToDate , err := ObjFileIsUpToDate (ctx , source , objectFile , depsFile )
239240 if err != nil {
240- return nil , i18n . WrapError (err )
241+ return nil , errors . WithStack (err )
241242 }
242243 if ! objIsUpToDate {
243244 _ , _ , err = ExecRecipe (ctx , properties , recipe , false /* stdout */ , utils .ShowIfVerbose /* stderr */ , utils .Show )
244245 if err != nil {
245- return nil , i18n . WrapError (err )
246+ return nil , errors . WithStack (err )
246247 }
247248 } else if ctx .Verbose {
248249 logger .Println (constants .LOG_LEVEL_INFO , constants .MSG_USING_PREVIOUS_COMPILED_FILE , objectFile )
@@ -267,7 +268,7 @@ func ObjFileIsUpToDate(ctx *types.Context, sourceFile, objectFile, dependencyFil
267268 sourceFile = sourceFile .Clean ()
268269 sourceFileStat , err := sourceFile .Stat ()
269270 if err != nil {
270- return false , i18n . WrapError (err )
271+ return false , errors . WithStack (err )
271272 }
272273
273274 objectFile = objectFile .Clean ()
@@ -279,7 +280,7 @@ func ObjFileIsUpToDate(ctx *types.Context, sourceFile, objectFile, dependencyFil
279280 }
280281 return false , nil
281282 } else {
282- return false , i18n . WrapError (err )
283+ return false , errors . WithStack (err )
283284 }
284285 }
285286
@@ -292,7 +293,7 @@ func ObjFileIsUpToDate(ctx *types.Context, sourceFile, objectFile, dependencyFil
292293 }
293294 return false , nil
294295 } else {
295- return false , i18n . WrapError (err )
296+ return false , errors . WithStack (err )
296297 }
297298 }
298299
@@ -311,7 +312,7 @@ func ObjFileIsUpToDate(ctx *types.Context, sourceFile, objectFile, dependencyFil
311312
312313 rows , err := dependencyFile .ReadFileAsLines ()
313314 if err != nil {
314- return false , i18n . WrapError (err )
315+ return false , errors . WithStack (err )
315316 }
316317
317318 rows = utils .Map (rows , removeEndingBackSlash )
@@ -346,7 +347,7 @@ func ObjFileIsUpToDate(ctx *types.Context, sourceFile, objectFile, dependencyFil
346347 // Ignore the error and trigger a full rebuild anyway
347348 if debugLevel >= 20 {
348349 logger .Fprintln (os .Stdout , constants .LOG_LEVEL_DEBUG , "Failed to read: {0}" , row )
349- logger .Fprintln (os .Stdout , constants .LOG_LEVEL_DEBUG , i18n . WrapError ( err ) .Error ())
350+ logger .Fprintln (os .Stdout , constants .LOG_LEVEL_DEBUG , err .Error ())
350351 }
351352 return false , nil
352353 }
@@ -454,9 +455,8 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
454455
455456 // something changed, rebuild the core archive
456457 if rebuildArchive {
457- err = archiveFilePath .Remove ()
458- if err != nil {
459- return nil , i18n .WrapError (err )
458+ if err := archiveFilePath .Remove (); err != nil {
459+ return nil , errors .WithStack (err )
460460 }
461461 } else {
462462 if ctx .Verbose {
@@ -472,9 +472,8 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
472472 properties .SetPath (constants .BUILD_PROPERTIES_ARCHIVE_FILE_PATH , archiveFilePath )
473473 properties .SetPath (constants .BUILD_PROPERTIES_OBJECT_FILE , objectFile )
474474
475- _ , _ , err := ExecRecipe (ctx , properties , constants .RECIPE_AR_PATTERN , false /* stdout */ , utils .ShowIfVerbose /* stderr */ , utils .Show )
476- if err != nil {
477- return nil , i18n .WrapError (err )
475+ if _ , _ , err := ExecRecipe (ctx , properties , constants .RECIPE_AR_PATTERN , false /* stdout */ , utils .ShowIfVerbose /* stderr */ , utils .Show ); err != nil {
476+ return nil , errors .WithStack (err )
478477 }
479478 }
480479
@@ -485,7 +484,7 @@ func ExecRecipe(ctx *types.Context, buildProperties *properties.Map, recipe stri
485484 // See util.ExecCommand for stdout/stderr arguments
486485 command , err := PrepareCommandForRecipe (ctx , buildProperties , recipe , removeUnsetProperties )
487486 if err != nil {
488- return nil , nil , i18n . WrapError (err )
487+ return nil , nil , errors . WithStack (err )
489488 }
490489
491490 return utils .ExecCommand (ctx , command , stdout , stderr )
@@ -514,7 +513,7 @@ func PrepareCommandForRecipe(ctx *types.Context, buildProperties *properties.Map
514513
515514 command , err := utils .PrepareCommand (commandLine , logger , relativePath )
516515 if err != nil {
517- return nil , i18n . WrapError (err )
516+ return nil , errors . WithStack (err )
518517 }
519518
520519 return command , nil
0 commit comments