Lost main.ts code, is there a way to recover?

Edit: See UnsignedArduino’s response, simply drag&drop the UF2 file into arcade.makecode.com, same as loading a saved image. I’m keeping the unnecessarily complicated method below for curiosity’s sake.

The UF2 file does indeed contain a copy of the source :slight_smile:

There’s likely a cleaner way to do that, but here’s how I got the data out using command line tools.

First confirm that the “source” magic number is present - this part isn’t part of recovering, but I wanted to check if it has source at all:

$ fgrep -ba $'\x41\x14\x0E\x2F\xB8\x2F\xA2\xBB' FILE.uf2
495620:[binary stuff]{"compression":"LZMA"}

Use the uf2conv tool to get a binary file: https://github.com/microsoft/uf2/blob/master/utils/uf2conv.md

$./uf2conv.py ~/FILE.uf2 --convert --output FILE.bin

Load the file into a binary-clean editor, search for “LZMA”, delete everything up to and including the final } of the JSON header, and save to FILE_SOURCE.lzma. In my case, the first bytes of the compressed data was ]^@^@ or 5d 00 00 80 00 in hex.

Then use the lzma and jq tools to get the source back:

$ lzma -d < FILE_SOURCE.lzma > source.json
$ jq '.["main.ts"]' < source.json > recovered-main.ts

(The source actually contains an extra JSON header, but the extraction worked anyway.)

It should be possible to wrap this in a recovery tool, but I wanted to save the steps in case this helps someone else, or future me if I get bitten by such a bug again…

3 Likes