# Buggy code
def __init__(self, filename=None, sessionid=None, buffersize=100, gc=True, **meta):
    """Represents a xonsh session's history as an in-memory buffer that is
    periodically flushed to disk.

    Parameters
    ----------
    filename : str, optional
        Location of history file, defaults to
        ``$XONSH_DATA_DIR/xonsh-{sessionid}.json``.
    sessionid : int, uuid, str, optional
        Current session identifier, will generate a new sessionid if not
        set.
    buffersize : int, optional
        Maximum buffersize in memory.
    meta : optional
        Top-level metadata to store along with the history. The kwargs
        'cmds' and 'sessionid' are not allowed and will be overwritten.
    gc : bool, optional
        Run garbage collector flag.
    """
    self.sessionid = sid = uuid.uuid4() if sessionid is None else sessionid
    if filename is None:
        # pylint: disable=no-member
        data_dir = builtins.__xonsh_env__.get("XONSH_DATA_DIR")
        self.filename = os.path.join(data_dir, "xonsh-{0}.json".format(sid))
    else:
        self.filename = filename
    self.buffer = []
    self.buffersize = buffersize
    self._queue = deque()
    self._cond = Condition()
    self._len = 0
    self.last_cmd_out = None
    self.last_cmd_rtn = None
    meta["cmds"] = []
    meta["sessionid"] = str(sid)
    with open(self.filename, "w", newline="\n") as f:
        lazyjson.dump(meta, f, sort_keys=True)
    self.gc = HistoryGC() if gc else None
    # command fields that are known
    self.tss = CommandField("ts", self)
    self.inps = CommandField("inp", self)
    self.outs = CommandField("out", self)
    self.rtns = CommandField("rtn", self)


# Traceback (FileNotFoundError)
$ xonsh
Traceback (most recent call last):
File "/usr/local/bin/xonsh", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python3.5/dist-packages/xonsh/main.py", line 180, in main
args = premain(argv)
File "/usr/local/lib/python3.5/dist-packages/xonsh/main.py", line 165, in premain
shell = builtins.__xonsh_shell__ = Shell(**shell_kwargs)
File "/usr/local/lib/python3.5/dist-packages/xonsh/shell.py", line 76, in __init__
self._init_environ(ctx, config, rc)
File "/usr/local/lib/python3.5/dist-packages/xonsh/shell.py", line 116, in _init_environ
self.execer = Execer(config=config)
File "/usr/local/lib/python3.5/dist-packages/xonsh/execer.py", line 40, in __init__
load_builtins(execer=self, config=config)
File "/usr/local/lib/python3.5/dist-packages/xonsh/built_ins.py", line 780, in load_builtins
ts=[time.time(), None], locked=True)
File "/usr/local/lib/python3.5/dist-packages/xonsh/history.py", line 268, in __init__
with open(self.filename, 'w', newline='\n') as f:
FileNotFoundError: [Errno 2] No such file or directory: '~/.local/share/xonsh/xonsh-328aa1da-e158-4be1-af29-1f90be87a349.json'

# Fixed code
def __init__(self, filename=None, sessionid=None, buffersize=100, gc=True, **meta):
    """Represents a xonsh session's history as an in-memory buffer that is
    periodically flushed to disk.

    Parameters
    ----------
    filename : str, optional
        Location of history file, defaults to
        ``$XONSH_DATA_DIR/xonsh-{sessionid}.json``.
    sessionid : int, uuid, str, optional
        Current session identifier, will generate a new sessionid if not
        set.
    buffersize : int, optional
        Maximum buffersize in memory.
    meta : optional
        Top-level metadata to store along with the history. The kwargs
        'cmds' and 'sessionid' are not allowed and will be overwritten.
    gc : bool, optional
        Run garbage collector flag.
    """
    self.sessionid = sid = uuid.uuid4() if sessionid is None else sessionid
    if filename is None:
        # pylint: disable=no-member
        data_dir = builtins.__xonsh_env__.get("XONSH_DATA_DIR")
        data_dir = os.path.expanduser(data_dir)
        self.filename = os.path.join(data_dir, "xonsh-{0}.json".format(sid))
    else:
        self.filename = filename
    self.buffer = []
    self.buffersize = buffersize
    self._queue = deque()
    self._cond = Condition()
    self._len = 0
    self.last_cmd_out = None
    self.last_cmd_rtn = None
    meta["cmds"] = []
    meta["sessionid"] = str(sid)
    with open(self.filename, "w", newline="\n") as f:
        lazyjson.dump(meta, f, sort_keys=True)
    self.gc = HistoryGC() if gc else None
    # command fields that are known
    self.tss = CommandField("ts", self)
    self.inps = CommandField("inp", self)
    self.outs = CommandField("out", self)
    self.rtns = CommandField("rtn", self)