Fixes #662 - Set a current working directory when running apps#803
Conversation
I think unexpectedly running something in the home folder could still really confuse a user. And it's not unusual for someone to drop a file in their home directory and then forget about it for months. Ideally we would simply tell Python not to add the current directory to sys.path. Unfortunately, it looks like there isn't an option for this until the upcoming Python 3.11, which adds a However, we could still achieve the desired effect by changing the Python command line from this: To this: If that works for Of course, for |
|
I completely agree that However, I think you're right that the I think it's still worth retaining the |
Codecov Report
|
Good point: I've seen from the Chaquopy issue tracker that a lot of beginner programmers don't understand the concept of a working directory at all. This should help catch that kind of bug earlier. |
By default, the Python interpreter will include the current working directory in the PYTHONPATH. As a result, if you create an app named
helloworld, and create ahelloworld.pyin the root directory of the project,briefcase devwould pick up thehelloworld.pyin the project root, not the actual app code.This would also affect Linux AppImage apps. As Linux AppImages are an embedded Python interpreter, the folder where you invoke the AppImage would implicitly be on the PYTHONPATH. If you gave a
helloworldAppImage to an end-user who hashelloworld.pyin their local folder, the end-user'shelloworld.pywould be executed.macOS and Windows aren't affected by this problem, as they use an embedded interpreter that doesn't add CWD to the PythonPath. However it doesn't hurt to set the CWD for those run calls.
iOS and Android aren't affected because they're running on device in a sandbox, so they can't be affected by local code.
Linux Flatpaks are affected by this in a slightly different way, as they run with a CWD of
~; so ahelloworld.pyin the user's home folder would get picked up. Adopting #628 (implemented in beeware/briefcase-linux-flatpak-template#3) would address this, as it would make Flatpaks isolated in the same way as Windows/macOS apps.This PR addresses the problem by setting the CWD to the user's home folder when running the app, or when in dev mode. This doesn't provide perfect isolation in dev mode, but it's significantly better than the status quo, and leads to easier-to-diagnose end-user questions of "Why is Briefcase finding code in my home folder?", rather than "Why isn't briefcase finding the code in my project?" or "Why is briefcase picking up a stale version of my app?". Running apps aren't affected by this decision (other than the Flatpak leakage mentioned above, which is fixed in the linked template PR).
By way of debugging assistance, this PR also adds the CWD into the logged subprocess output.
beeware/briefcase-linux-appimage-template#17 includes a fix on the AppImage binary to force better isolation of the `AppImage itself.
PR Checklist: