Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Dockerfile ARG parsing #103

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions devcontainer/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-git/go-billy/v5"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/moby/buildkit/frontend/dockerfile/shell"
"github.com/tailscale/hujson"
)

Expand Down Expand Up @@ -317,30 +318,43 @@ func UserFromDockerfile(dockerfileContent string) string {
// ImageFromDockerfile inspects the contents of a provided Dockerfile
// and returns the image that will be used to run the container.
func ImageFromDockerfile(dockerfileContent string) (name.Reference, error) {
args := map[string]string{}
lexer := shell.NewLex('\\')
var args []string
var imageRef string
lines := strings.Split(dockerfileContent, "\n")
// Iterate over lines in reverse
for i := len(lines) - 1; i >= 0; i-- {
line := lines[i]
if strings.HasPrefix(line, "ARG ") {
arg := strings.TrimSpace(strings.TrimPrefix(line, "ARG "))
if arg, ok := strings.CutPrefix(line, "ARG "); ok {
arg = strings.TrimSpace(arg)
if strings.Contains(arg, "=") {
parts := strings.SplitN(arg, "=", 2)
args[parts[0]] = parts[1]
key, err := lexer.ProcessWord(parts[0], args)
if err != nil {
return nil, fmt.Errorf("processing %q: %w", line, err)
}
val, err := lexer.ProcessWord(parts[1], args)
if err != nil {
return nil, fmt.Errorf("processing %q: %w", line, err)
}
args = append(args, key+"="+val)
}
continue
}
if imageRef == "" && strings.HasPrefix(line, "FROM ") {
imageRef = strings.TrimPrefix(line, "FROM ")
if imageRef == "" {
if fromArgs, ok := strings.CutPrefix(line, "FROM "); ok {
imageRef = fromArgs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we strings.TrimSpace(fromArgs), I saw another issue with FROM image<space> being mis-parsed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, n/m I see it in your tests, looks good

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's trimmed on this line below:

	image, err := name.ParseReference(strings.TrimSpace(imageRef))

I also added a unit test case for this.

}
}
}
if imageRef == "" {
return nil, fmt.Errorf("no FROM directive found")
}
image, err := name.ParseReference(os.Expand(imageRef, func(s string) string {
return args[s]
}))
imageRef, err := lexer.ProcessWord(imageRef, args)
if err != nil {
return nil, fmt.Errorf("processing %q: %w", imageRef, err)
}
image, err := name.ParseReference(strings.TrimSpace(imageRef))
if err != nil {
return nil, fmt.Errorf("parse image ref %q: %w", imageRef, err)
}
Expand Down
10 changes: 8 additions & 2 deletions devcontainer/devcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,14 @@ func TestImageFromDockerfile(t *testing.T) {
content: "FROM ubuntu",
image: "index.docker.io/library/ubuntu:latest",
}, {
content: "ARG VARIANT=ionic\nFROM ubuntu:$VARIANT",
image: "index.docker.io/library/ubuntu:ionic",
content: "ARG VARIANT=bionic\nFROM ubuntu:$VARIANT",
image: "index.docker.io/library/ubuntu:bionic",
}, {
content: "ARG VARIANT=\"3.10\"\nFROM mcr.microsoft.com/devcontainers/python:0-${VARIANT}",
image: "mcr.microsoft.com/devcontainers/python:0-3.10",
}, {
content: "ARG VARIANT=\"3.10\"\nFROM mcr.microsoft.com/devcontainers/python:0-$VARIANT ",
image: "mcr.microsoft.com/devcontainers/python:0-3.10",
}} {
tc := tc
t.Run(tc.image, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/google/go-containerregistry v0.15.2
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mattn/go-isatty v0.0.20
github.com/moby/buildkit v0.11.6
github.com/otiai10/copy v1.14.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
Expand Down Expand Up @@ -184,7 +185,6 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/buildkit v0.11.6 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/swarmkit/v2 v2.0.0-20230315203717-e28e8ba9bc83 // indirect
Expand Down
Loading