Skip to content

Commit

Permalink
Don't attempt to parse custom syntax (#346)
Browse files Browse the repository at this point in the history
The provider currently attempts to parse your Dockerfile in case there
are any obviously broken syntax errors.

However, Dockerfiles can use custom syntaxes which are implemented as
their own images. Buildkit detects `# syntax=` directives, pulls the
image, and uses it to parse.

It doesn't make sense for us to attempt to replicate that behavior, so
when we detect a custom syntax we will simply disable this validation.
If there are syntax errors the user will discover them after the build
is attempted.

Fixes #300
  • Loading branch information
blampe authored Dec 9, 2024
1 parent 0a045d1 commit 2907567
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

- Upgraded buildx from 0.16.0 to 0.18.0.

### Fixed
- Custom `# syntax=` directives no longer cause validation errors. (https://github.com/pulumi/pulumi-docker-build/issues/300)

## 0.0.7 (2024-10-16)

### Fixed
Expand Down
18 changes: 16 additions & 2 deletions provider/internal/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package internal

import (
"bytes"
"errors"
"io"
"os"
Expand Down Expand Up @@ -96,13 +97,26 @@ func (d *Dockerfile) validate(preview bool, c *Context) error {
}

func parseDockerfile(r io.Reader) error {
parsed, err := parser.Parse(r)
df, _ := io.ReadAll(r)
syntax, _, _, _ := parser.DetectSyntax(df)
if syntax == "" {
syntax = os.Getenv("BUILDKIT_SYNTAX")
}

// Disable validation if this uses a custom syntax.
if syntax != "" && syntax != "docker/dockerfile:1" {
return nil
}

parsed, err := parser.Parse(bytes.NewReader(df))
if err != nil {
return newCheckFailure(err, "dockerfile")
}

_, _, err = instructions.Parse(parsed.AST, nil)
if err != nil {
return err
return newCheckFailure(err, "dockerfile")
}

return nil
}
19 changes: 19 additions & 0 deletions provider/internal/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,31 @@ func TestValidateDockerfile(t *testing.T) {
},
wantErr: "unknown instruction: RUNN",
},
{
name: "invalid syntax inline with default syntax directive",
d: Dockerfile{
Inline: `# syntax=docker/dockerfile:1
RUNN it`,
},
wantErr: "unknown instruction: RUNN",
},
{
name: "valid syntax inline",
d: Dockerfile{
Inline: "FROM scratch",
},
},
{
name: "valid custom syntax inline",
d: Dockerfile{
Inline: `# syntax=docker.io/docker/dockerfile:1.7-labs
FROM public.ecr.aws/docker/library/node:22-alpine AS base
WORKDIR /app
COPY --parents ./package.json ./package-lock.json ./apps/*/package.json ./packages/*/package.json ./
`,
},
},
{
name: "unset",
d: Dockerfile{},
Expand Down

0 comments on commit 2907567

Please sign in to comment.