Repository: conan-io/conan

Issue:
local cache inconsistent after enabling short_paths in a recipe
To help us debug your issue please explain:

- [x] I've read the [CONTRIBUTING guide](https://raw.githubusercontent.com/conan-io/conan/develop/.github/CONTRIBUTING.md).
- [x] I've specified the Conan version, operating system version and any tool that can be relevant.
- [x] I've explained the steps to reproduce the error or the motivation/use case of the question/suggestion.

Conan Version 1.3.3
Windows 10

With a package in local cache whose recipe does NOT have `short_paths=True`, modify in normal development folder the recipe and set `short_paths=True` and run conan create.

Folders in local cache become inconsistent showing both folders from previous conan create run and .conan_link files pointing to the short paths folders.

This seems no not affect conan tool behavior when running commands and works well if `short_paths` is removed once again.




Discussion:

Resolving patch:
diff --git a/conans/util/windows.py b/conans/util/windows.py
--- a/conans/util/windows.py
+++ b/conans/util/windows.py
@@ -45,6 +45,9 @@ def path_shortener(path, short_paths):
     elif short_paths is None:
         return path
 
+    if os.path.exists(path):
+        rmdir(path)
+
     short_home = os.getenv("CONAN_USER_HOME_SHORT")
     if not short_home:
         drive = os.path.splitdrive(path)[0]


Test patch:
diff --git a/conans/test/functional/short_paths_test.py b/conans/test/functional/short_paths_test.py
new file mode 100644
--- /dev/null
+++ b/conans/test/functional/short_paths_test.py
@@ -0,0 +1,67 @@
+import os
+import platform
+import unittest
+
+from conans.model.ref import ConanFileReference
+from conans.test.utils.tools import TestClient
+
+
+class ShortPathsTest(unittest.TestCase):
+
+    @unittest.skipUnless(platform.system() == "Windows", "Requires Windows")
+    def inconsistent_cache_test(self):
+        conanfile = """
+import os
+from conans import ConanFile, tools
+
+
+class TestConan(ConanFile):
+    name = "test"
+    version = "1.0"
+    short_paths = {0}
+    exports_sources = "source_file.cpp"
+    
+    def source(self):
+        for item in os.listdir(self.source_folder):
+            self.output.info("SOURCE: " + str(item))
+    def build(self):
+        tools.save(os.path.join(self.build_folder, "artifact"), "")
+        for item in os.listdir(self.build_folder):
+            self.output.info("BUILD: " + str(item))
+    def package(self):
+        self.copy("source_file.cpp")
+        self.copy("artifact")
+        for item in os.listdir(self.build_folder):
+            self.output.info("PACKAGE: " + str(item))
+"""
+
+        client = TestClient()
+        client.save({"conanfile.py": conanfile.format("False"),
+                     "source_file.cpp": ""})
+        client.run("create . danimtb/testing")
+        conan_ref = ConanFileReference("test", "1.0", "danimtb", "testing")
+        source_folder = os.path.join(client.client_cache.conan(conan_ref), "source")
+        build_folder = os.path.join(client.client_cache.conan(conan_ref), "build",
+                                    "5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9")
+        package_folder = os.path.join(client.client_cache.conan(conan_ref), "package",
+                                      "5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9")
+        self.assertIn("SOURCE: source_file.cpp", client.out)
+        self.assertEqual(["source_file.cpp"], os.listdir(source_folder))
+        self.assertIn("BUILD: source_file.cpp", client.out)
+        self.assertIn("BUILD: artifact", client.out)
+        self.assertEqual(["artifact", "conanbuildinfo.txt", "conaninfo.txt", "source_file.cpp"],
+                         os.listdir(build_folder))
+        self.assertIn("PACKAGE: source_file.cpp", client.out)
+        self.assertIn("PACKAGE: artifact", client.out)
+        self.assertEqual(["artifact", "conaninfo.txt", "conanmanifest.txt", "source_file.cpp"],
+                         os.listdir(package_folder))
+        client.save({"conanfile.py": conanfile.format("True")})
+        client.run("create . danimtb/testing")
+        self.assertIn("SOURCE: source_file.cpp", client.out)
+        self.assertEqual([".conan_link"], os.listdir(source_folder))
+        self.assertIn("BUILD: source_file.cpp", client.out)
+        self.assertIn("BUILD: artifact", client.out)
+        self.assertEqual([".conan_link"], os.listdir(build_folder))
+        self.assertIn("PACKAGE: source_file.cpp", client.out)
+        self.assertIn("PACKAGE: artifact", client.out)
+        self.assertEqual([".conan_link"], os.listdir(package_folder))