Skip to content

Commit

Permalink
Merge pull request #5645 from tonistiigi/syntax-bom
Browse files Browse the repository at this point in the history
dockerfile: fix syntax forwarding for files with BOM
  • Loading branch information
tonistiigi authored Jan 13, 2025
2 parents ef0f822 + d66fb36 commit c80370b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions frontend/dockerfile/parser/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func DetectSyntax(dt []byte) (string, string, []Range, bool) {
}

func ParseDirective(key string, dt []byte) (string, string, []Range, bool) {
dt = discardBOM(dt)
dt, hadShebang, err := discardShebang(dt)
if err != nil {
return "", "", nil, false
Expand Down Expand Up @@ -171,3 +172,7 @@ func discardShebang(dt []byte) ([]byte, bool, error) {
}
return dt, false, nil
}

func discardBOM(dt []byte) []byte {
return bytes.TrimPrefix(dt, []byte{0xEF, 0xBB, 0xBF})
}
14 changes: 14 additions & 0 deletions frontend/dockerfile/parser/directives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ RUN ls
require.False(t, ok)
}

func TestDetectSyntaxBOM(t *testing.T) {
t.Parallel()

dt := append([]byte{0xEF, 0xBB, 0xBF}, []byte(`# syntax = myfrontend
FROM busybox
`)...)
ref, cmdline, loc, ok := DetectSyntax(dt)
require.True(t, ok)
require.Equal(t, "myfrontend", ref)
require.Equal(t, "myfrontend", cmdline)
require.Equal(t, 1, loc[0].Start.Line)
require.Equal(t, 1, loc[0].End.Line)
}

func TestParseDirective(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 1 addition & 3 deletions frontend/dockerfile/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func Parse(rwc io.Reader) (*Result, error) {
bytesRead := scanner.Bytes()
if currentLine == 0 {
// First line, strip the byte-order-marker if present
bytesRead = bytes.TrimPrefix(bytesRead, utf8bom)
bytesRead = discardBOM(bytesRead)
}
if isComment(bytesRead) {
comment := strings.TrimSpace(string(bytesRead[1:]))
Expand Down Expand Up @@ -522,8 +522,6 @@ func isEmptyContinuationLine(line []byte) bool {
return len(trimLeadingWhitespace(trimNewline(line))) == 0
}

var utf8bom = []byte{0xEF, 0xBB, 0xBF}

func trimContinuationCharacter(line []byte, d *directives) ([]byte, bool) {
if d.lineContinuationRegex.Match(line) {
line = d.lineContinuationRegex.ReplaceAll(line, []byte("$1"))
Expand Down

0 comments on commit c80370b

Please sign in to comment.