skaurus
skaurus

Reputation: 1709

Where VS Code stores the list of open files?

I'm trying to have synced VS Code instances at work and at home, including the list of open files. I know there are extensions to sync settings, but they do not cover Open Files AFAIK. I do not expect live syncing, under running instance, but if I, say, reboot both machines and start Code on them, I want them identical.

Currently, I have a portable installation of Code on my OneDrive, and I've tried to move AppData\Roaming\Code to OneDrive as well, replacing the actual directories with the symbolic links to this copy.

But still, when I open editor at home it has its own set of Open Files.

I tried to use ProcMon to get an idea where it comes from, I tried to read sources a bit. It seems asking may be easier :-)

Btw, I'm opening in Code git folder of my project. And this folder is located at the same path on both PCs.

Upvotes: 26

Views: 11816

Answers (3)

Stefan Musarra
Stefan Musarra

Reputation: 1501

You might also want to read this very interesting and descriptive blog post. Kudos to Alfie Champion

Discover a user’s recent Code files and access unsaved edits

In this short blog we’ve seen how several VSCode configuration files can be used to gain an understanding of the recent files and folders a user has accessed. This is particularly useful as the JSON and sqlite files in question fall outside of TCC’s protection.

Having identified recent access, we’ve also looked at how the mechanism by which VSCode ‘backs up’ unsaved edits can be exploited to view file content which could otherwise be protected by TCC

Upvotes: 1

kingbase
kingbase

Reputation: 1446

I'm totally agree with @Jonathan and want to add a bit more.

For the question, the opened files was stored in file__0.localstorage which is a sqlite file. And I just write a bit of code for extract the list of opened files. Hope it helps.

import sqlite3
import pandas as pd
import json

fn = r"C:\Users\HelloWorld\AppData\Roaming\Code\Local Storage\file__0.localstorage"
conn = sqlite3.connect(fn)
df = pd.read_sql("select key, value as _value from ItemTable", conn)
df["v"] = df.pop("_value").map(lambda x: x.decode("utf-16"))

# should be choosen carefully
known_file_opened = "numpy"
known_file_closed = "aws"

df_check = df[
    df.v.str.contains(known_file_opened)
    &(~df.v.str.contains(known_file_closed))
]
assert len(df_check) == 1

js = json.loads(df_check.v.iloc[0])
editors = js["groups"][0]["editors"]
print("Found %d editors" %(len(editors)))

paths = []
for editor in editors:
    js_ = json.loads(editor["value"])
    path = js_["resourceJSON"]["fsPath"]
    paths.append(path)

print("Found opened file list:\n%s" %(paths))

Upvotes: 6

Jonathan
Jonathan

Reputation: 454

I'm pretty sure you're right with AppData\Roaming\Code being the location in question. Specifically:

  • AppData\Roaming\Code\storage.json and in that the windowsState section.
  • AppData\Roaming\Code\Backups\workspaces.json

These files (or at least storage.json) do not get updated until you exit Code (File > Exit). If you are leaving Code open on your work machine and not seeing the changes when you get to your home machine, that might be why you're not seeing the expected.

Code / Atom also stores state information in sqlite3 databases and lots of state information is stored in there:

  • AppData\Roaming\Code\Local Storage\file__0.localstorage

Use an SQLite browser tool such as http://sqlitebrowser.org/ to open it up. You'll see lots of familiar path references in the table ItemTable. The column value shows as "BLOB" (binary) but you can click on any row and export the data to a file. Do this and open up it up in a text editor (e.g. Code! :)) and you'll see it's just a JSON string.

(As VS Code is based on GitHub's Atom editor, searching for issues using "Atom" rather than "Code" often digs up information you might not otherwise find.)

Upvotes: 21

Related Questions