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

avoid overwrite user CMakePresets #15058

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions conan/tools/cmake/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def generate(conanfile, toolchain_file, generator, cache_variables, preset_prefi

preset_path = os.path.join(conanfile.generators_folder, "CMakePresets.json")
multiconfig = is_multi_configuration(generator)
if os.path.exists(preset_path):
data = json.loads(load(preset_path))
if "conan" not in data.get("vendor", {}):
# The file is not ours, we cannot overwrite it
raise ConanException("Existing CMakePresets.json not generated by Conan cannot be "
"overwritten.\nUse --output-folder or define a 'layout' to "
"avoid collision with your CMakePresets.json")
if os.path.exists(preset_path) and multiconfig:
data = json.loads(load(preset_path))
build_preset = _CMakePresets._build_and_test_preset_fields(conanfile, multiconfig,
Expand Down
10 changes: 10 additions & 0 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,3 +1176,13 @@ def generate(self):
assert 'string(APPEND CONAN_C_FLAGS " extra_cflags cflags")' in toolchain
assert 'string(APPEND CONAN_SHARED_LINKER_FLAGS " extra_sharedlinkflags sharedlinkflags")' in toolchain
assert 'string(APPEND CONAN_EXE_LINKER_FLAGS " extra_exelinkflags exelinkflags")' in toolchain


def test_avoid_ovewrite_user_cmakepresets():
# https://github.com/conan-io/conan/issues/15052
c = TestClient()
c.save({"conanfile.txt": "",
"CMakePresets.json": "{}"})
c.run('install . -g CMakeToolchain', assert_error=True)
assert "Error in generator 'CMakeToolchain': Existing CMakePresets.json not generated" in c.out
assert "Use --output-folder or define a 'layout' to avoid collision" in c.out