25

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.

skaurus
  • 1,581
  • 17
  • 27
  • have you considered Visual Studio Code live share? https://code.visualstudio.com/blogs/2017/11/15/live-share, seems like less trouble – Yahya Hussein Dec 31 '17 at 12:11
  • I have not tried it, but I doubt it will suit me. What if hosting computer goes offline for example? – skaurus Dec 31 '17 at 12:32
  • On Linux the opened file list is probably saved somewhere under `$HOME/.config/Code` (that folder exists at least on my Ubuntu 21.04 laptop). – Håkon Hægland May 28 '21 at 14:15
  • 4
    It is also stored as json in the sqlite db used by vscode. For me, this database was located at `$XDG_DATA_HOME/Code/user-data/User/globalStorage/state.vscdb`. The json object for recently opened files is stored under `history.recentlyOpenedPathsList`. Make sure you properly exit vscode before editing this database, otherwise your changes might not persist – smac89 Jun 22 '22 at 19:33
  • Great find! But most likely having this file in shared folder will lead to trouble, or, in case of OneDrive specifically, to proliferation of duplicates due to conflicts in this file. – skaurus Jun 23 '22 at 17:42

2 Answers2

21

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.)

Jonathan
  • 454
  • 3
  • 5
  • Thanks for the tip that `file__0.localstorage` is an SQLite file! This Friday I've specifically closed my Code at work, copied `AppData\Roaming\Code` to OneDrive and tried to make it work at home - it didn't help. I'll make more experiments though... – skaurus Dec 31 '17 at 14:59
  • I remember about the bounty and going to finish my research before it expires :) So far it seems that open files stored in `file__0.localstorage`. Tomorrow I should make the decisive experiment. – skaurus Jan 06 '18 at 00:48
  • 1
    So, what have I learned. Yesterday my Code at home was opening without any Open Files. I opened two files, closed editor, checked that `file__0.localstorage` is now different from OneDrive copy. Opened Code again - same two Open Files, as expected. Closed it. Renamed `file__0.localstorage` and put instead of it and its journal files from OneDrive. Opened Code again - no Open Files. Closed editor. Returned renamed `file__0.localstorage` back. Opened Code. And there are my two Open Files again. So far so good. – skaurus Jan 06 '18 at 15:40
  • 1
    Today I got to work, opened Code - there was the full list of Opened Files, few dozens of them. That's what I want at home too. Closed editor, copied `AppData\Roaming\Code` to OneDrive. Got home, renamed current `file__0.localstorage`. Copied dir from OneDrive to the local user profile. Opened Code - no Open Files. Returned renamed `file__0.localstorage` back - two Open Files. Conclusion - opened files stored in `file__0.localstorage` indeed, but there are more to that. Maybe some random workspace ids are different or something. Thank you for your answer, I will upvote it, but no bounty. – skaurus Jan 06 '18 at 15:49
  • Does code really sits on top of atom? I thought they just share electron under the hood. – dahrens Jan 06 '18 at 20:19
  • @dahrens [Wikipedia](https://en.wikipedia.org/wiki/Visual_Studio_Code) explicitly states that it does not: `Although it uses the Electron framework, the software does not use Atom and instead employs the same editor component (codenamed "Monaco") used in Visual Studio Team Services`. But I'm not sure anyway. The full story may be more complex. – skaurus Jan 07 '18 at 00:29
  • Apologies, my bad. Electron rather than Atom. A lot shared between them because of this though, so it can still be helpful to look at one or the other when investigating issues. – Jonathan Jan 07 '18 at 20:22
6

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))
kingbase
  • 1,268
  • 14
  • 23
  • Thanks for this, could you extend this to Linux also? That would be great! On my Ubuntu 21.04 laptop, there is no `file__0.localstorage` in `$HOME/.config/Code/Local Storage`. – Håkon Hægland May 28 '21 at 14:21
  • Not finding a `file__0.localstorage` anywhere on my macOS system, VSCode 1.67.1. I have scoured the entire `~/Library/Application Support/Code/User/` dir. Is there a new location for this? – luckman212 May 16 '22 at 18:12
  • 1
    @luckman212 see my [comment](https://stackoverflow.com/questions/48014141/where-vs-code-stores-the-list-of-open-files#comment128450484_48014141) on the question. I believe the file is now called `state.vscdb` – smac89 Jun 22 '22 at 19:40
  • I can confirm what @smac89 said, Open files are stored in state.vscdb stored in (for W10) %APPDATA%\Code\User\workspaceStorage\ – rd51 Oct 28 '22 at 16:44