Skip to content

Commit ba5a14a

Browse files
committed
Fix vcpkg command paths on Windows
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
1 parent 827c49a commit ba5a14a

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

hatch_cpp/tests/test_vcpkg_ref.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,27 @@ def test_generate_without_ref(self, tmp_path, monkeypatch):
153153
assert not any("checkout" in cmd for cmd in commands)
154154
assert any("git clone" in cmd for cmd in commands)
155155

156+
def test_generate_uses_posix_command_paths(self, tmp_path, monkeypatch):
157+
monkeypatch.chdir(tmp_path)
158+
self._make_vcpkg_env(tmp_path)
159+
160+
cfg = HatchCppVcpkgConfiguration(vcpkg_triplet="x64-linux")
161+
commands = cfg.generate(None)
162+
163+
assert "./vcpkg/bootstrap-vcpkg.sh" in commands
164+
assert "./vcpkg/vcpkg install --triplet x64-linux" in commands
165+
166+
def test_generate_uses_windows_command_paths(self, tmp_path, monkeypatch):
167+
monkeypatch.chdir(tmp_path)
168+
monkeypatch.setattr("hatch_cpp.toolchains.vcpkg.sys_platform", "win32")
169+
self._make_vcpkg_env(tmp_path)
170+
171+
cfg = HatchCppVcpkgConfiguration(vcpkg_triplet="x64-windows-static-md")
172+
commands = cfg.generate(None)
173+
174+
assert r"vcpkg\bootstrap-vcpkg.bat" in commands
175+
assert r"vcpkg\vcpkg.exe install --triplet x64-windows-static-md" in commands
176+
156177
def test_generate_skips_clone_when_vcpkg_root_exists(self, tmp_path, monkeypatch):
157178
monkeypatch.chdir(tmp_path)
158179
self._make_vcpkg_env(tmp_path)

hatch_cpp/toolchains/vcpkg.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ def _vcpkg_executable_path(self) -> Path:
8787
return self.vcpkg_root / "vcpkg.exe"
8888
return self.vcpkg_root / "vcpkg"
8989

90+
def _command_path(self, path: Path) -> str:
91+
if sys_platform == "win32":
92+
return str(path).replace("/", "\\")
93+
return f"./{path}"
94+
9095
def _delete_dir_command(self, path: Path) -> str:
9196
if sys_platform == "win32":
9297
return f'rmdir /s /q "{path}"'
@@ -109,7 +114,7 @@ def _clone_checkout_bootstrap_commands(self) -> list[str]:
109114
if ref is not None:
110115
commands.append(f"git -C {self.vcpkg_root} checkout {ref}")
111116

112-
commands.append(f"./{self._bootstrap_script_path()}")
117+
commands.append(self._command_path(self._bootstrap_script_path()))
113118
return commands
114119

115120
def generate(self, config):
@@ -134,11 +139,11 @@ def generate(self, config):
134139
else:
135140
vcpkg_executable = self._vcpkg_executable_path()
136141
if not vcpkg_executable.exists():
137-
commands.append(f"./{bootstrap_script}")
142+
commands.append(self._command_path(bootstrap_script))
138143
elif not self._is_vcpkg_working():
139144
commands.append(self._delete_dir_command(vcpkg_root))
140145
commands.extend(self._clone_checkout_bootstrap_commands())
141146

142-
commands.append(f"./{self.vcpkg_root / 'vcpkg'} install --triplet {self.vcpkg_triplet}")
147+
commands.append(f"{self._command_path(self._vcpkg_executable_path())} install --triplet {self.vcpkg_triplet}")
143148

144149
return commands

0 commit comments

Comments
 (0)