@@ -57,19 +57,17 @@ func PreprocessSketchWithCtags(
5757
5858 // Check if the preprocessed file is already up-to-date
5959 unpreprocessedSourceFile := buildPath .Join ("sketch" , sketch .MainFile .Base ()+ ".cpp.merged" )
60- sourceFile := buildPath .Join ("sketch" , sketch .MainFile .Base ()+ ".cpp" )
60+ preprocessedSourceFile := buildPath .Join ("sketch" , sketch .MainFile .Base ()+ ".cpp" )
6161 if unpreprocessedStat , err := unpreprocessedSourceFile .Stat (); err != nil {
6262 return nil , fmt .Errorf ("%s: %w" , i18n .Tr ("unable to open unpreprocessed source file" ), err )
63- } else if sourceStat , err := sourceFile .Stat (); err != nil {
64- return nil , fmt .Errorf ("%s: %w" , i18n .Tr ("unable to open source file" ), err )
65- } else if unpreprocessedStat .ModTime ().Before (sourceStat .ModTime ()) {
63+ } else if sourceStat , err := preprocessedSourceFile .Stat (); err == nil && unpreprocessedStat .ModTime ().Before (sourceStat .ModTime ()) {
6664 fmt .Fprintln (stdout , i18n .Tr ("Sketch is unchanged, skipping preprocessing." ))
6765 res := & runner.Result {Stdout : stdout .Bytes (), Stderr : stderr .Bytes ()}
6866 return res , nil
6967 }
7068
7169 // Run GCC preprocessor
72- result := GCC (sourceFile , ctagsTarget , includes , buildProperties ).Run (ctx )
70+ result := GCC (unpreprocessedSourceFile , ctagsTarget , includes , buildProperties ).Run (ctx )
7371 stdout .Write (result .Stdout )
7472 stderr .Write (result .Stderr )
7573 if err := result .Error ; err != nil {
@@ -81,7 +79,7 @@ func PreprocessSketchWithCtags(
8179 fmt .Fprintf (stderr , "%s: %s" ,
8280 i18n .Tr ("An error occurred adding prototypes" ),
8381 i18n .Tr ("the compilation database may be incomplete or inaccurate" ))
84- if err := sourceFile .CopyTo (ctagsTarget ); err != nil {
82+ if err := unpreprocessedSourceFile .CopyTo (ctagsTarget ); err != nil {
8583 return & runner.Result {Args : result .Args , Stdout : stdout .Bytes (), Stderr : stderr .Bytes ()}, err
8684 }
8785 }
@@ -114,7 +112,7 @@ func PreprocessSketchWithCtags(
114112
115113 // Add prototypes to the original sketch source
116114 var source string
117- if sourceData , err := sourceFile .ReadFile (); err == nil {
115+ if sourceData , err := unpreprocessedSourceFile .ReadFile (); err == nil {
118116 source = string (sourceData )
119117 } else {
120118 return & runner.Result {Args : result .Args , Stdout : stdout .Bytes (), Stderr : stderr .Bytes ()}, err
@@ -148,7 +146,7 @@ func PreprocessSketchWithCtags(
148146 }
149147
150148 // Write back arduino-preprocess output to the sourceFile
151- err = sourceFile .WriteFile ([]byte (preprocessedSource ))
149+ err = preprocessedSourceFile .WriteFile ([]byte (preprocessedSource ))
152150 return & runner.Result {Args : result .Args , Stdout : stdout .Bytes (), Stderr : stderr .Bytes ()}, err
153151}
154152
0 commit comments