Drone 1.9 broke jsonnet rendering (with string variables)

Hi all,

It seems Drone 1.9 and up is not able to render JSONNET anymore, when running the below jsonnet file against drone 1.8 it works. But on 1.9 it barfs with:

RUNTIME ERROR: Unexpected type string, expected number .drone.jsonnet:11:24-31 object <anonymous> During manifestation 

File in question:

local Converge(distro) = {
  name: "Converge - "+distro,
  image: "registry.element-networks.nl/tools/molecule",
  commands: [
    "molecule destroy",
    "molecule converge",
    "molecule idempotence",
    "molecule verify",
    "molecule destroy",
  ],
  environment:
    { MOLECULE_DISTRO: +distro, },
  privileged: true,
  volumes: [
    { name: "docker", path: "/var/run/docker.sock" },
  ],
};

[
  {
    name: "Lint",
    kind: "pipeline",
    steps: [
      {
        name: "Lint code",
        image: "registry.element-networks.nl/tools/molecule",
        commands: [
          "molecule lint",
          "molecule syntax"
        ],
        privileged: true,
        volumes: [
          { name: "docker", path: "/var/run/docker.sock" },
        ],
      }
    ],
    volumes: [
      { name: "docker",
        host: { path: "/var/run/docker.sock" }
      },
    ],
  },
  {
    kind: "pipeline",
    name: "Test",
    steps: [
      Converge("debian9"),
      Converge("debian10"),
      Converge("ubuntu1804"),
      Converge("centos7"),
    ],
    volumes: [
      { name: "docker",
        host: { path: "/var/run/docker.sock" }
      },
    ],

    depends_on: [
      "Lint",
    ],
  },
  {
    name: "Publish",
    kind: "pipeline",
    clone:
      { disable: true },
    steps: [
      {
        name: "Ansible Galaxy",
        image: "registry.element-networks.nl/tools/molecule",
        commands: [
          "ansible-galaxy login --github-token $$GITHUB_TOKEN",
          "ansible-galaxy import Thulium-Drake ansible-role-sshd --role-name=sshd",
        ],
        environment:
          { GITHUB_TOKEN: { from_secret: "github_token" } },
      },
    ],
    depends_on: [
      "Test",
    ],
  },
]

I pasted your jsonnet file into the editor a jsonnet.org and it also threw a parsing error:

RUNTIME ERROR: unary operator + does not operate on type string
	example1.jsonnet:12:24-31	object <anonymous>
	example1.jsonnet:12:5-34	object <anonymous>
	example1.jsonnet:47:7-26	thunk <array_element>
	example1.jsonnet:(46:12)-(51:6)	object <anonymous>
	example1.jsonnet:(43:3)-(61:4)	thunk <array_element>
	During manifestation	

It looks like this is a syntax error at line 12 in your jsonnet snippet which can be fixed with the following change:

  environment:
-    { MOLECULE_DISTRO: +distro, },
+    { MOLECULE_DISTRO: distro, },

Hmm… That’s odd, before posting I tried validating it with the drone CLI, to make sure I didn’t forget anything. And it worked just fine…

$ drone --version 
drone version 1.2.2

$ drone jsonnet --stream

$ cat .drone.yml 
---
kind: pipeline
name: Lint

platform:
  os: linux
  arch: amd64

steps:
- name: Lint code
  image: registry.element-networks.nl/tools/molecule
  commands:
  - molecule lint
  - molecule syntax
  privileged: true
  volumes:
  - name: docker
    path: /var/run/docker.sock

volumes:
- name: docker
  host:
    path: /var/run/docker.sock

---
kind: pipeline
name: Test

platform:
  os: linux
  arch: amd64

steps:
- name: Converge - debian9
  image: registry.element-networks.nl/tools/molecule
  commands:
  - molecule destroy
  - molecule converge
  - molecule idempotence
  - molecule verify
  - molecule destroy
  environment:
    MOLECULE_DISTRO: debian9
  privileged: true
  volumes:
  - name: docker
    path: /var/run/docker.sock

- name: Converge - debian10
  image: registry.element-networks.nl/tools/molecule
  commands:
  - molecule destroy
  - molecule converge
  - molecule idempotence
  - molecule verify
  - molecule destroy
  environment:
    MOLECULE_DISTRO: debian10
  privileged: true
  volumes:
  - name: docker
    path: /var/run/docker.sock

- name: Converge - ubuntu1804
  image: registry.element-networks.nl/tools/molecule
  commands:
  - molecule destroy
  - molecule converge
  - molecule idempotence
  - molecule verify
  - molecule destroy
  environment:
    MOLECULE_DISTRO: ubuntu1804
  privileged: true
  volumes:
  - name: docker
    path: /var/run/docker.sock

- name: Converge - centos7
  image: registry.element-networks.nl/tools/molecule
  commands:
  - molecule destroy
  - molecule converge
  - molecule idempotence
  - molecule verify
  - molecule destroy
  environment:
    MOLECULE_DISTRO: centos7
  privileged: true
  volumes:
  - name: docker
    path: /var/run/docker.sock

volumes:
- name: docker
  host:
    path: /var/run/docker.sock

depends_on:
- Lint

---
kind: pipeline
name: Publish

platform:
  os: linux
  arch: amd64

clone:
  disable: true

steps:
- name: Ansible Galaxy
  image: registry.element-networks.nl/tools/molecule
  commands:
  - ansible-galaxy login --github-token $$GITHUB_TOKEN
  - ansible-galaxy import Thulium-Drake ansible-role-sshd --role-name=sshd
  environment:
    GITHUB_TOKEN:
      from_secret: github_token

depends_on:
- Test

...

I’ll put in the changes and give it a spin!

the Drone CLI uses an older version of the libjsonnet compiler. It is possible the older version of the compiler does not catch the error, or it is possible the compiler has become more strict over time. I am not an expert on the jsonnet spec, but if you feel this should not be an error and is a regression in the latest version of jsonnet, you may want to get in touch with their development team at https://github.com/google/go-jsonnet

Ah, that makes sense.

Well I am no jsonnet expert either, so if they feel it is wrong, that’s OK with me.

But it was weird that the CLI (which I use to validate the code is compatible with Drone) didn’t report the same error. :slight_smile:

Will the CLI be updated as well?

Will the CLI be updated as well?

yes, we recently received a pr to upgrade, however, it has not been reviewed or merged yet.

Awesome! In the meantime, I updated my jsonnet files with the change you suggested and they work like a charm again! :smiley: