camdencheek / tree-sitter-dockerfile

A tree-sitter grammar for Dockerfile
MIT License
71 stars 20 forks source link

Support single-quoted strings #36

Closed mjambon closed 1 year ago

mjambon commented 1 year ago

They're apparently allowed and prevent variable expansion as in sh or bash.

Source:

Note

Be sure to use double quotes and not single quotes. Particularly when you are using string interpolation (e.g. LABEL example="foo-$ENV_VAR"), single quotes will take the string as is without unpacking the variable’s value.

(https://docs.docker.com/engine/reference/builder/)

Docker's behavior:

$ cat Dockerfile
FROM alpine
ENV A=hello
ENV LITERAL='$A world'
ENV EXPANDED="$A world"
RUN echo "literal: $LITERAL"
RUN echo "expanded: $EXPANDED"

$ docker build .
[+] Building 0.1s (7/7) FINISHED                                                
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 170B                                       0.0s
 => [internal] load metadata for docker.io/library/alpine:latest           0.0s
 => [1/3] FROM docker.io/library/alpine                                    0.0s
 => CACHED [2/3] RUN echo "literal: $A world"                              0.0s
 => CACHED [3/3] RUN echo "expanded: hello world"                          0.0s
 => exporting to image                                                     0.0s
 => => exporting layers                                                    0.0s
 => => writing image sha256:6066bbb97b9d14f0109a8c5d94ff84ddea6f5151874b1  0.0s

Minimal test case showing the failure to parse single-quoted strings:

$ cat debug
FROM alpine
ENV A='a b'

$ tree-sitter parse debug
(source_file [0, 0] - [2, 0]
  (from_instruction [0, 0] - [0, 11]
    (image_spec [0, 5] - [0, 11]
      name: (image_name [0, 5] - [0, 11])))
  (ERROR [1, 0] - [1, 11]
    (env_pair [1, 4] - [1, 8]
      name: (unquoted_string [1, 4] - [1, 5])
      value: (unquoted_string [1, 6] - [1, 8]))
    (unquoted_string [1, 9] - [1, 10])))
debug   0 ms    (ERROR [1, 0] - [1, 11])