Skip to content

Commit 2b3d114

Browse files
committed
Add --cwd-app flag
Add `--cwd-app` flag to `protontricks` and `protontricks-launch` to set the working directory to the game's installation directory. Also document how to maintain previous behavior in the changelog. This also makes it easier to maintain backwards compatibility with scripts that assume installation directory as the working directory. Scripts relying on the working directory should be infrequent based on searching GitHub repositories for `protontricks` usage, however. Fixes #287
1 parent 5d18e37 commit 2b3d114

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## [Unreleased]
8+
### Added
9+
- `--cwd-app` flag to set working directory to the game's installation directory
10+
811
### Changed
9-
- `-c` command now uses the current working directory instead of the game's installation directory
12+
- `-c` command now uses the current working directory instead of the game's installation directory. `--cwd-app` can be used to restore old behavior. Scripts can also `$STEAM_APP_PATH` environment variable to determine the app's installation directory; this has been supported (albeit undocumented) since 1.8.0.
1013

1114
## [1.11.1] - 2024-02-20
1215
### Fixed

src/protontricks/cli/launch.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ def main(args=None):
9292
parser.add_argument(
9393
"--appid", type=int, nargs="?", default=None
9494
)
95+
parser.add_argument(
96+
"--cwd-app",
97+
dest="cwd_app",
98+
default=False,
99+
action="store_true",
100+
help=(
101+
"Set the working directory of launched executable to the Steam "
102+
"app's installation directory."
103+
)
104+
)
95105
parser.add_argument("executable", type=str)
96106
parser.add_argument("exec_args", nargs=argparse.REMAINDER)
97107
parser.set_defaults(background_wineserver=False)
@@ -180,6 +190,9 @@ def exit_(error):
180190
+ exec_args
181191
)
182192

193+
if args.cwd_app:
194+
cli_args += ["--cwd-app"]
195+
183196
cli_args += [
184197
"-c", inner_args, str(appid)
185198
]

src/protontricks/cli/main.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ def main(args=None, steam_path=None, steam_root=None):
135135
"command startup time."
136136
)
137137
)
138+
parser.add_argument(
139+
"--cwd-app",
140+
dest="cwd_app",
141+
default=False,
142+
action="store_true",
143+
help=(
144+
"Set the working directory of launched command to the Steam app's "
145+
"installation directory."
146+
)
147+
)
138148
parser.set_defaults(background_wineserver=False)
139149

140150
parser.add_argument("appid", type=int, nargs="?", default=None)
@@ -276,6 +286,8 @@ def exit_(error):
276286
"YAD or Zenity is not installed. Either executable is required for the "
277287
"Protontricks GUI."
278288
)
289+
290+
cwd = str(steam_app.install_path) if args.cwd_app else None
279291

280292
# 6. Find Proton version of selected app
281293
proton_app = find_proton_app(
@@ -291,6 +303,7 @@ def exit_(error):
291303
"installation?"
292304
)
293305

306+
294307
run_command(
295308
winetricks_path=winetricks_path,
296309
proton_app=proton_app,
@@ -299,7 +312,8 @@ def exit_(error):
299312
legacy_steam_runtime_path=legacy_steam_runtime_path,
300313
command=[str(winetricks_path), "--gui"],
301314
use_bwrap=use_bwrap,
302-
start_wineserver=start_background_wineserver
315+
start_wineserver=start_background_wineserver,
316+
cwd=cwd
303317
)
304318

305319
return
@@ -370,6 +384,8 @@ def exit_(error):
370384
"$ protontricks -s <GAME NAME>"
371385
)
372386

387+
cwd = str(steam_app.install_path) if args.cwd_app else None
388+
373389
if args.winetricks_command:
374390
returncode = run_command(
375391
winetricks_path=winetricks_path,
@@ -379,7 +395,8 @@ def exit_(error):
379395
legacy_steam_runtime_path=legacy_steam_runtime_path,
380396
use_bwrap=use_bwrap,
381397
start_wineserver=start_background_wineserver,
382-
command=[str(winetricks_path)] + args.winetricks_command
398+
command=[str(winetricks_path)] + args.winetricks_command,
399+
cwd=cwd
383400
)
384401
elif args.command:
385402
returncode = run_command(
@@ -393,7 +410,8 @@ def exit_(error):
393410
start_wineserver=start_background_wineserver,
394411
# Pass the command directly into the shell *without*
395412
# escaping it
396-
shell=True
413+
shell=True,
414+
cwd=cwd,
397415
)
398416

399417
logger.info("Command returned %d", returncode)

tests/cli/test_launch.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ def _set_launch_args(*args, **kwargs):
113113
steam_app_factory(name="Fake game", appid=10)
114114

115115
launch_cli([
116-
"--verbose", "--no-bwrap", "--no-runtime", "--no-term", "--appid",
117-
"10", "test.exe"
116+
"--verbose", "--no-bwrap", "--no-runtime", "--no-term",
117+
"--cwd-app", "--appid", "10", "test.exe"
118118
])
119119

120120
# CLI flags are passed through to the main CLI entrypoint
121-
assert cli_args[0:6] == [
121+
assert cli_args[0:7] == [
122122
"-v", "--no-runtime", "--no-bwrap",
123-
"--no-background-wineserver", "--no-term", "-c"
123+
"--no-background-wineserver", "--no-term", "--cwd-app", "-c"
124124
]
125-
assert cli_args[6].startswith("wine ")
126-
assert cli_args[6].endswith("test.exe")
127-
assert cli_args[7] == "10"
125+
assert cli_args[7].startswith("wine ")
126+
assert cli_args[7].endswith("test.exe")
127+
assert cli_args[8] == "10"
128128

129129
# Steam installation was provided to the main entrypoint
130130
assert str(cli_kwargs["steam_path"]) == str(steam_dir)
@@ -231,5 +231,3 @@ def test_steam_installation_not_selected(self, launch_cli, gui_provider):
231231
result = launch_cli(["test.exe"], expect_returncode=1)
232232

233233
assert "No Steam installation was selected" in result
234-
235-

tests/cli/test_main.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ def test_run_command(
797797

798798
# The command is just 'bash'
799799
assert command.args == "bash"
800-
800+
assert command.cwd is None
801801
assert command.shell is True
802802

803803
# Correct environment vars were set
@@ -816,6 +816,22 @@ def test_run_command(
816816
str(proton_install_path / "dist" / "lib" / "wine")
817817
)
818818

819+
@pytest.mark.usefixtures("default_proton")
820+
def test_run_command_cwd_app(self, cli, steam_app_factory, command_mock):
821+
"""
822+
Run a shell command for a given game using `--cwd-app` flag and
823+
ensure the working directory was set to the game's installation
824+
directory
825+
"""
826+
steam_app = steam_app_factory(name="Fake game", appid=10)
827+
828+
cli(["--cwd-app", "-c", "bash", "10"])
829+
830+
command = command_mock.commands[-1]
831+
832+
assert command.args == "bash"
833+
assert command.cwd == str(steam_app.install_path)
834+
819835

820836
class TestCLISearch:
821837
def test_search_case_insensitive(self, cli, steam_app_factory):

0 commit comments

Comments
 (0)