Skip to content

Commit c80370b

Browse files
authored
Merge pull request moby#5645 from tonistiigi/syntax-bom
dockerfile: fix syntax forwarding for files with BOM
2 parents ef0f822 + d66fb36 commit c80370b

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

frontend/dockerfile/parser/directives.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func DetectSyntax(dt []byte) (string, string, []Range, bool) {
116116
}
117117

118118
func ParseDirective(key string, dt []byte) (string, string, []Range, bool) {
119+
dt = discardBOM(dt)
119120
dt, hadShebang, err := discardShebang(dt)
120121
if err != nil {
121122
return "", "", nil, false
@@ -171,3 +172,7 @@ func discardShebang(dt []byte) ([]byte, bool, error) {
171172
}
172173
return dt, false, nil
173174
}
175+
176+
func discardBOM(dt []byte) []byte {
177+
return bytes.TrimPrefix(dt, []byte{0xEF, 0xBB, 0xBF})
178+
}

frontend/dockerfile/parser/directives_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ RUN ls
104104
require.False(t, ok)
105105
}
106106

107+
func TestDetectSyntaxBOM(t *testing.T) {
108+
t.Parallel()
109+
110+
dt := append([]byte{0xEF, 0xBB, 0xBF}, []byte(`# syntax = myfrontend
111+
FROM busybox
112+
`)...)
113+
ref, cmdline, loc, ok := DetectSyntax(dt)
114+
require.True(t, ok)
115+
require.Equal(t, "myfrontend", ref)
116+
require.Equal(t, "myfrontend", cmdline)
117+
require.Equal(t, 1, loc[0].Start.Line)
118+
require.Equal(t, 1, loc[0].End.Line)
119+
}
120+
107121
func TestParseDirective(t *testing.T) {
108122
t.Parallel()
109123

frontend/dockerfile/parser/parser.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func Parse(rwc io.Reader) (*Result, error) {
291291
bytesRead := scanner.Bytes()
292292
if currentLine == 0 {
293293
// First line, strip the byte-order-marker if present
294-
bytesRead = bytes.TrimPrefix(bytesRead, utf8bom)
294+
bytesRead = discardBOM(bytesRead)
295295
}
296296
if isComment(bytesRead) {
297297
comment := strings.TrimSpace(string(bytesRead[1:]))
@@ -522,8 +522,6 @@ func isEmptyContinuationLine(line []byte) bool {
522522
return len(trimLeadingWhitespace(trimNewline(line))) == 0
523523
}
524524

525-
var utf8bom = []byte{0xEF, 0xBB, 0xBF}
526-
527525
func trimContinuationCharacter(line []byte, d *directives) ([]byte, bool) {
528526
if d.lineContinuationRegex.Match(line) {
529527
line = d.lineContinuationRegex.ReplaceAll(line, []byte("$1"))

0 commit comments

Comments
 (0)