Don't attempt to parse custom syntax (#346)

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 https://github.com/pulumi/pulumi-docker-build/issues/300
This commit is contained in:
Bryce Lampe
2024-12-09 15:41:58 -08:00
committed by GitHub
parent 0a045d12bf
commit 2907567484
3 changed files with 38 additions and 2 deletions

View File

@@ -4,6 +4,9 @@
- Upgraded buildx from 0.16.0 to 0.18.0. - 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) ## 0.0.7 (2024-10-16)
### Fixed ### Fixed

View File

@@ -15,6 +15,7 @@
package internal package internal
import ( import (
"bytes"
"errors" "errors"
"io" "io"
"os" "os"
@@ -96,13 +97,26 @@ func (d *Dockerfile) validate(preview bool, c *Context) error {
} }
func parseDockerfile(r io.Reader) 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 { if err != nil {
return newCheckFailure(err, "dockerfile") return newCheckFailure(err, "dockerfile")
} }
_, _, err = instructions.Parse(parsed.AST, nil) _, _, err = instructions.Parse(parsed.AST, nil)
if err != nil { if err != nil {
return err return newCheckFailure(err, "dockerfile")
} }
return nil return nil
} }

View File

@@ -57,12 +57,31 @@ func TestValidateDockerfile(t *testing.T) {
}, },
wantErr: "unknown instruction: RUNN", 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", name: "valid syntax inline",
d: Dockerfile{ d: Dockerfile{
Inline: "FROM scratch", 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", name: "unset",
d: Dockerfile{}, d: Dockerfile{},