Copilot Coding

(Last update: )

The last two days I've been trying to put together an extension for the League/CommonMarkdown converter, which would automatically lookup the dimensions of local images included in the markdown file and add the height and width attributes to the img tag when the markdown is converted to HTML. Clearly not the most efficient thing to be doing, but then this is a hobby project of mine and speed is not a primary concern for my homebrew static site generator.

I decided to use Copilot to help me with the task.

AI - trust it at your own risk

I started using Copilot outside of Visual Studio Code and asked it how to proceed. After explaining my needs it gave me some code examples. When I then asked it to give a complete solution, it duly complied. I thought: great, add this in to my code and I am ready to go!

Unfortunately the proposed solution, whilst generally quite good had some major mistakes which were hard to track down. They needed a lot of extra interaction with Copilot.

First there was the case of mixed up classes being referenced. In one file the code was referencing League\CommonMark\Extension\CommonMark\Node\Inline\Image in the other it was League\CommonMark\Node\Inline\Image. This is a pretty basic mistake once you understand that the former is the current, v2, method and the latter is the outdated, v1, method. Why Copilot spat out code using an outdated method I have no idea.

However adding this fix still didn't help. Whilst my extension was being registered, it was not being used. It turns out it needed to be prioritised properly. This is where it got really interesting, and embarrassing for Mr Copilot. Well, I hope he should be embarrassed but not sure AI has that capability yet.

We figured out the issue was prioritisation and that the lower the priority number (eg 0) for my extension the higher its priority. So Copilot told me to put 0. Which I did and it didn't help. Then it actually contradicted itself and told me to add a higher number to get a higher priority. I corrected Copilot and it even apologised for its mistake!

Next it told me to enter -1 as the priority, as that is lower than 0. That threw an error if I remember correctly. So I tried entering 1 and that worked! Copilot basically had no idea what value to choose and was making it up based on common logic, rather than knowledge of the actual Commonmark extension system.

Not great, but better than nothing

So am I disappointed in Copilot? No. Whilst not being a professional, I have enough coding experience to vaguely understand what it is spitting out. This is a requirement to be able to make use of it. You cannot use it with no coding experience.

I don't have enough experience to write this extension myself, so Copilot is defininetly a huge help. But it cannot be blindly trusted. The information it provides you with needs to be understood and checked. It contains errors which are sometimes not obvious, especially to the inexperienced eye. It certainly can't be trusted in mission critical situations or where human lives are affected, such as in the legal system or policing. Never mind when lives might be at risk.

Copiloting Context

Interestingly the version of Copilot that can be added to Visual Studio Code was simply not fit for purpose. Its answers were not clear and often unhelpful. I don't know why this is. No idea if that is to do with my VSC set up or something else, but it was very poor.

Job Done

In any case, the extension now works and the job is done. All local images I link in my markdown files will now have the exact height and width of the image added to the img tag in the HTML generated from the markdown.

Tagged: