Repository: ARMmbed/mbed-tools

Issue:
Users are building cli1 and cli2 within the same app
**Describe the bug**
A clear and concise description of what the bug is.

I've received 2 questions with the similar topic within the last 24 hours - "my build is failing with a weird error" - a user first run cli2 and then later another day switched to cli1 and received an error.

A question is: should we fix this and avoid surprises if users use either cli1 or cli2 ? One suggestion that many of us came to was adding .mbedignore and add there `cmake_build` directory.  Is there better way to make this use case working without errors ?

**To Reproduce**
Steps to reproduce the behavior:

Build blinky with cli2 first, then build with cli1.

**Expected behavior**
Both builds would work as they work on the same codebase, in separate build directories. As we support both at the time being, we should allow users to build both or at least warn them if they are doing this (if we do not want to support the use case)


**Mbed (please complete the following information):**
mbed-tools, mbed-cli and Mbed OS - any version

Comments:

Patch:
diff --git a/news/233.bugfix b/news/233.bugfix
new file mode 100644
index 0000000..dc0a11e
--- /dev/null
+++ b/news/233.bugfix
@@ -0,0 +1,1 @@
+Generate .mbedignore in a project's build directory, to prevent Mbed CLI 1 from picking up CMake build files.
diff --git a/src/mbed_tools/build/config.py b/src/mbed_tools/build/config.py
index bea354e..e408564 100644
--- a/src/mbed_tools/build/config.py
+++ b/src/mbed_tools/build/config.py
@@ -16,6 +16,7 @@ from mbed_tools.build._internal.write_files import write_file
 from mbed_tools.build.exceptions import MbedBuildError
 
 CMAKE_CONFIG_FILE = "mbed_config.cmake"
+MBEDIGNORE_FILE = ".mbedignore"
 
 
 def generate_config(target_name: str, toolchain: str, program: MbedProgram) -> Tuple[Config, pathlib.Path]:
@@ -40,6 +41,8 @@ def generate_config(target_name: str, toolchain: str, program: MbedProgram) -> T
     )
     cmake_config_file_path = program.files.cmake_build_dir / CMAKE_CONFIG_FILE
     write_file(cmake_config_file_path, cmake_file_contents)
+    mbedignore_path = program.files.cmake_build_dir / MBEDIGNORE_FILE
+    write_file(mbedignore_path, "*")
     return config, cmake_config_file_path
 
 


Test patch:
diff --git a/tests/build/test_generate_config.py b/tests/build/test_generate_config.py
index e7d7eb7..9e94993 100644
--- a/tests/build/test_generate_config.py
+++ b/tests/build/test_generate_config.py
@@ -4,11 +4,12 @@
 #
 import json
 
+import os
 import pytest
 
 from mbed_tools.project import MbedProgram
 from mbed_tools.build import generate_config
-from mbed_tools.build.config import CMAKE_CONFIG_FILE
+from mbed_tools.build.config import CMAKE_CONFIG_FILE, MBEDIGNORE_FILE
 from mbed_tools.lib.exceptions import ToolsError
 
 
@@ -96,6 +97,17 @@ def matching_target_and_filter(request):
     return request.param
 
 
+def test_mbedignore_generated(program):
+    target = "K64F"
+    toolchain = "GCC_ARM"
+
+    generate_config(target, toolchain, program)
+
+    mbedignore_file = (program.files.cmake_build_dir / MBEDIGNORE_FILE)
+
+    assert os.path.isfile(mbedignore_file)
+
+
 def test_target_and_toolchain_collected(program):
     target = "K64F"
     toolchain = "GCC_ARM"