Skip to content

Commit b36b87a

Browse files
committed
fix(builder): strip UTF-8 BOM from .ino sources before preprocessing
When a sketch .ino is saved as UTF-8 *with BOM*, the BOM bytes (EF BB BF) reach the compiler and cause: stray '\357' in program stray '\273' in program stray '\277' in program This strips the BOM at read-time so the merged .cpp and copied sources are clean. Refs: arduino/arduino-ide#2752
1 parent b6ddb5a commit b36b87a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

internal/arduino/builder/sketch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ var (
3737
includesArduinoH = regexp.MustCompile(`(?m)^\s*#\s*include\s*[<\"]Arduino\.h[>\"]`)
3838
)
3939

40+
// stripUTF8BOM removes a UTF-8 BOM if present at the start of the file.
41+
func stripUTF8BOM(b []byte) []byte {
42+
if len(b) >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF {
43+
return b[3:]
44+
}
45+
return b
46+
}
47+
4048
// prepareSketchBuildPath copies the sketch source files in the build path.
4149
// The .ino files are merged together to create a .cpp file (by the way, the
4250
// .cpp file still needs to be Arduino-preprocessed to compile).
@@ -82,6 +90,7 @@ func (b *Builder) sketchMergeSources(overrides map[string]string) (int, string,
8290
if err != nil {
8391
return "", errors.New(i18n.Tr("reading file %[1]s: %[2]s", f, err))
8492
}
93+
data = stripUTF8BOM(data)
8594
return string(data), nil
8695
}
8796

@@ -136,7 +145,9 @@ func (b *Builder) sketchCopyAdditionalFiles(buildPath *paths.Path, overrides map
136145
if err != nil {
137146
return fmt.Errorf("%s: %w", i18n.Tr("unable to read contents of the source item"), err)
138147
}
148+
s = stripUTF8BOM(s)
139149
sourceBytes = s
150+
140151
}
141152

142153
// tag each additional file with the filename of the source it was copied from

0 commit comments

Comments
 (0)