Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #966 by using fixed folders #1362

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
34 changes: 24 additions & 10 deletions apps/vscode/command_client/command_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import time
from dataclasses import dataclass
from pathlib import Path
from tempfile import gettempdir
from typing import Any
from uuid import uuid4

from talon import Context, Module, actions, speech_system
from talon import Context, Module, actions, app, speech_system
from talon_init import TALON_HOME

# How old a request file needs to be before we declare it stale and are willing
# to remove it
Expand Down Expand Up @@ -197,20 +197,34 @@ def run_command(
return decoded_contents["returnValue"]


command_server_directory = None


# NB: See https://github.com/talonhub/community/issues/966 for why we do OS-specific temp dirs
def get_platform_specific_communication_dir_path():
home_dir = Path(os.path.expanduser("~"))
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
if app.platform == "linux" or app.platform == "mac":
return (
Path(home_dir) / f".talon/.comms/{actions.user.command_server_directory()}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought accessing a hidden folder was also a problem for snaps, no? Cc/ @auscompgeek

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true for most snaps by default iirc, but afaik vscode uses something called classic confinement, which has unrestricted access to files. This makes sense since it's an IDE and users would expected to be able to access anything as their user anyway. So I think we should be okay here (though I haven't tested with the snap yet).

fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
)
elif app.platform == "windows":
# subprocess.run(["attrib","+H","myfile.txt"],check=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it may have been some testing code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this in for someone with access to Windows in case they want to test using a hidden folder, but didn't uncomment it because I can't test if it works.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knausj85 any thoughts about that part? I'm happy to just delete the commented out bit if windows users don't care to add it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no concerns with leaving this file unhidden. Thoughts on this @AndreasArvidsson ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi I'm not sure I want plugins making top-level directories under .talon, hidden or not

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Should we continue that discussion in the issue or here?

Personally I would prefer to keep it in the temp directory and agree with @rntz arguments in the issue that the extension should respect TMPDIR.

If that doesn't work for some technical reason we should probably just create a new directory in user home. eg ~/.rpc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe TMPDIR doesn't work as a solution because it can be different path in talon vs vscode. This can definitely be the case on nix, as noted here #966 (comment) and also unrelated to that plugin which I've run into (which I already forget where as I've been using this PR for ~ 1 year). I originally hardcoded /tmp in the PR, but then eventually we opted for a /home-based path, which unfortunately I now can't find the reason/comment for it seems.

But I think because of all the potential snap/flatpak/nix related issues mentioned it's likely just best to avoid hoping one of those tmpdir/runtime path solutions work everywhere? Pretty sure atm we don't have the capacity to actually test it.

Something like ~/.rpc is fine with me if that's what people like (I just want it fixed at this point :D), but imo it does seem a bit opaque and potentially collision prone vs some name that associates it with talon, which was why I chose to drop the folder in ~/.talon/... ~/.talon-rpc or similar maybe makes more sense?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone on purpose has set a different environment variable in Talon and vscode they kind off asking for trouble. As long as we check the sources in the same order we should get the same result.

Year that is a real problem because we don't want to break it for existing users.

~/.talon-rpc would be fine with me, but I'm not sure the home directory is any more stable than the temp directory since the home directory can also be moved with environment variables.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I agree if it were actually the user just wantonly changing environment variables, it shouldn't something that needs catering to. However, this isn't a case of a user choosing to change TMPDIR, it's being done somewhere behind the scenes, so it's not something they can just choose not to do.

In addition to the original bug filer, there were numerous (#966 (comment)) cases of it differing in practice, as well as in my systems and for some other Nix users. When it started breaking for me (which prompted finally creating the PR) it's certainly not from me explicitly changing TMPDIR myself. This comment #966 (comment) also implies this type of thing happens on windows.

In light of that, I don't see how keeping the order checking consistent is enough, unless you mean you have some other mechanism to ensure the paths they are checking in the same order are actually matching? Maybe I'm missing something.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can't make the temp directory work reliably then we can't of course use that. I assume the home directory is more reliable?

return Path(
home_dir
/ f"\\AppData\\Roaming\\talon\\{actions.user.command_server_directory()}"
)


def get_communication_dir_path():
"""Returns directory that is used by command-server for communication

Returns:
Path: The path to the communication dir
"""
suffix = ""

# NB: We don't suffix on Windows, because the temp dir is user-specific
# anyways
if hasattr(os, "getuid"):
suffix = f"-{os.getuid()}"

return Path(gettempdir()) / f"{actions.user.command_server_directory()}{suffix}"
global command_server_directory
if command_server_directory is None:
command_server_directory = get_platform_specific_communication_dir_path()
return command_server_directory


def robust_unlink(path: Path):
Expand Down