Skip to content

Commit b9787ed

Browse files
committed
Conan/Cmake: Handle Spaces in Binary Path
1 parent 63abec1 commit b9787ed

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

cppython/plugins/conan/builder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Construction of Conan data"""
22

33
import shutil
4-
from pathlib import Path
4+
from pathlib import Path, PurePosixPath
55

66
from pydantic import DirectoryPath
77

@@ -48,11 +48,11 @@ def _create_base_conanfile(
4848

4949
# Generate configure method content for cmake_program if specified
5050
if cmake_binary:
51-
# Use forward slashes for cross-platform compatibility in Conan
52-
cmake_path_str = str(cmake_binary.resolve()).replace('\\', '/')
51+
# Use PurePosixPath for conan compatibility and space handling
52+
cmake_path_str = f'"{PurePosixPath(cmake_binary.resolve())}"'
5353
configure_content = f''' def configure(self):
5454
"""CPPython managed configuration."""
55-
self.conf.define("tools.cmake:cmake_program", "{cmake_path_str}")'''
55+
self.conf.define("tools.cmake:cmake_program", {repr(cmake_path_str)})'''
5656
else:
5757
configure_content = ''
5858

tests/unit/plugins/conan/test_builder.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ def test_cmake_binary_configure(self, builder: Builder, tmp_path: Path) -> None:
185185
assert 'C:/Program Files/CMake/bin/cmake.exe' in content
186186
assert '\\' not in content.split('tools.cmake:cmake_program')[1].split('"')[1]
187187

188+
def test_cmake_binary_path_with_spaces(self, builder: Builder, tmp_path: Path) -> None:
189+
"""Test that cmake_binary paths with spaces are quoted to prevent shell parsing errors."""
190+
base_file = tmp_path / 'conanfile_base.py'
191+
cmake_path = Path('C:/Program Files/CMake/bin/cmake.exe')
192+
193+
builder._create_base_conanfile(base_file, [], {}, cmake_binary=cmake_path)
194+
195+
content = base_file.read_text(encoding='utf-8')
196+
# The path must be wrapped in quotes so Conan passes it as a single argument
197+
assert '"C:/Program Files/CMake/bin/cmake.exe"' in content
198+
188199
def test_no_cmake_binary(self, builder: Builder, tmp_path: Path) -> None:
189200
"""Test that no cmake_binary means no configure() method."""
190201
base_file = tmp_path / 'conanfile_base.py'

0 commit comments

Comments
 (0)