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

**URL:** <https://forum.makecode.com/t/lost-main-ts-code-is-there-a-way-to-recover/6281>\
**Category:** Help\
**Created:** [February 19, 2021, 7:34pm UTC](https://forum.makecode.com/t/lost-main-ts-code-is-there-a-way-to-recover/6281 "2021-02-19T19:34:13Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![kwx](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kwx/32/1545_2.png) [@kwx](https://forum.makecode.com/u/kwx)\
**Post date:** [February 19, 2021, 8:09pm UTC](https://forum.makecode.com/t/lost-main-ts-code-is-there-a-way-to-recover/6281/2 "2021-02-19T20:09:40Z")

</div>

Edit: See UnsignedArduino’s response, simply drag&drop the UF2 file into [arcade.makecode.com](http://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 🙂

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](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…

---

_[View the full topic](https://forum.makecode.com/t/lost-main-ts-code-is-there-a-way-to-recover/6281)._
