Repository: conan-io/conan

Issue:
[bug] SCM and not using "--ignore_dirty" copies wrong folder to source / build folder
### Environment Details (include every applicable attribute)
  * Operating System+version: Win10
  * Compiler+version: VC15
  * Conan version: 1.20.3 vs 1.19.2
  * Python version: 3.7

### Steps to reproduce (Include if Applicable)
I have a `conanfile.py` located in a subfolder `test/main` of my git repository. The repository is in `dirty` state.

When I use 'conan create ....` with changes in my repository with Conan 1.19.2 all files (starting from repository root) are copied to the source / build folder of the local cache. **Ok**

When I use 'conan create .... --ignore_dirty` with changes in my repository with Conan 1.20.3 all files (starting from repository root) are copied to the source / build folder of the local cache. **Ok**

When I use 'conan create .... ` with changes in my repository with Conan 1.20.3 only files loacated in 'test/main' are copied to the source / build folder of the local cache. The following build process fails, because the files are in other location. **Failed**

Should conan try to copy the files from the same root as the git repository, when `--ignore_dirty` is not selected?




Discussion:
Thanks a lot for submitting the issue, indeed it looks like a bug. We'll try to fix it asap.

Without `--ignore-dirty` only files located in the `conanfile.py` directory are copied to the `scm_source` folder in the cache.


Resolving patch:
diff --git a/conans/client/cmd/export.py b/conans/client/cmd/export.py
--- a/conans/client/cmd/export.py
+++ b/conans/client/cmd/export.py
@@ -220,8 +220,8 @@ def _capture_scm_auto_fields(conanfile, conanfile_dir, destination_folder, outpu
                     "'scm.revision' auto fields. Use --ignore-dirty to force it. The 'conan "
                     "upload' command will prevent uploading recipes with 'auto' values in these "
                     "fields.")
-        local_src_path = scm.get_local_path_to_url(scm_data.url) \
-            if scm_data.url != "auto" else conanfile_dir
+        origin = scm.get_qualified_remote_url(remove_credentials=True)
+        local_src_path = scm.get_local_path_to_url(origin)
         return scm_data, local_src_path
 
     if scm_data.url == "auto":


Test patch:
diff --git a/conans/test/functional/scm/scm_test.py b/conans/test/functional/scm/scm_test.py
--- a/conans/test/functional/scm/scm_test.py
+++ b/conans/test/functional/scm/scm_test.py
@@ -1,4 +1,5 @@
 import os
+import textwrap
 import unittest
 from collections import namedtuple
 
@@ -171,6 +172,36 @@ def test_auto_subfolder(self):
         self.assertTrue(os.path.exists(os.path.join(folder, "mysub", "myfile.txt")))
         self.assertFalse(os.path.exists(os.path.join(folder, "mysub", "conanfile.py")))
 
+    def test_ignore_dirty_subfolder(self):
+        # https://github.com/conan-io/conan/issues/6070
+        conanfile = textwrap.dedent("""
+            import os
+            from conans import ConanFile, tools
+
+            class ConanLib(ConanFile):
+                name = "lib"
+                version = "0.1"
+                short_paths = True
+                scm = {
+                    "type": "git",
+                    "url": "auto",
+                    "revision": "auto",
+                }
+
+                def build(self):
+                    path = os.path.join("base_file.txt")
+                    assert os.path.exists(path)
+        """)
+        self.client.save({"test/main/conanfile.py": conanfile, "base_file.txt": "foo"})
+        create_local_git_repo(folder=self.client.current_folder)
+        self.client.run_command('git remote add origin https://myrepo.com.git')
+
+        # Introduce changes
+        self.client.save({"dirty_file.txt": "foo"})
+        # The build() method will verify that the files from the repository are copied ok
+        self.client.run("create test/main/conanfile.py user/channel")
+        self.assertIn("Package '{}' created".format(NO_SETTINGS_PACKAGE_ID), self.client.out)
+
     def test_auto_conanfile_no_root(self):
         """
         Conanfile is not in the root of the repo: https://github.com/conan-io/conan/issues/3465