Repository: readthedocs/readthedocs.org
Issue: Build: incompatible Mercurial versions from VCS setup than build commands

## Details

* Read the Docs project URL: https://readthedocs.org/projects/taskcluster-taskgraph/
* Build URL (if applicable): https://readthedocs.org/projects/taskcluster-taskgraph/builds/16259866/
* Read the Docs username (if applicable): ahal

## Expected Result

The builds should succeed :)

## Actual Result

Builds fail as seen [here](https://readthedocs.org/projects/taskcluster-taskgraph/builds/16259866/). I don't believe anything changed on the repo side, this was [working a week or two ago](https://readthedocs.org/projects/taskcluster-taskgraph/builds/).

```
hg identify --id
abort: repository requires features unknown to this Mercurial: sparserevlog!
(see https://mercurial-scm.org/wiki/MissingRequirement for more information) 
```


Changed files:
readthedocs/projects/tasks/builds.py, readthedocs/doc_builder/director.py

diff --git a/readthedocs/doc_builder/director.py b/readthedocs/doc_builder/director.py
index dc7646db9..79388cc0f 100644
--- a/readthedocs/doc_builder/director.py
+++ b/readthedocs/doc_builder/director.py
@@ -65,7 +65,30 @@ class BuildDirector:
                 ),
             )
 
-        environment = self.data.environment_class(
+        before_vcs.send(
+            sender=self.data.version,
+            environment=self.vcs_environment,
+        )
+
+        # Create the VCS repository where all the commands are going to be
+        # executed for a particular VCS type
+        self.vcs_repository = self.data.project.vcs_repo(
+            version=self.data.version.slug,
+            environment=self.vcs_environment,
+            verbose_name=self.data.version.verbose_name,
+            version_type=self.data.version.type,
+        )
+
+        self.pre_checkout()
+        self.checkout()
+        self.post_checkout()
+
+        commit = self.data.build_commit or self.vcs_repository.commit
+        if commit:
+            self.data.build["commit"] = commit
+
+    def create_vcs_environment(self):
+        self.vcs_environment = self.data.environment_class(
             project=self.data.project,
             version=self.data.version,
             build=self.data.build,
@@ -74,28 +97,6 @@ class BuildDirector:
             # ca-certificate package which is compatible with Lets Encrypt
             container_image=settings.RTD_DOCKER_BUILD_SETTINGS["os"]["ubuntu-20.04"],
         )
-        with environment:
-            before_vcs.send(
-                sender=self.data.version,
-                environment=environment,
-            )
-
-            # Create the VCS repository where all the commands are going to be
-            # executed for a particular VCS type
-            self.vcs_repository = self.data.project.vcs_repo(
-                version=self.data.version.slug,
-                environment=environment,
-                verbose_name=self.data.version.verbose_name,
-                version_type=self.data.version.type,
-            )
-
-            self.pre_checkout()
-            self.checkout()
-            self.post_checkout()
-
-            commit = self.data.build_commit or self.vcs_repository.commit
-            if commit:
-                self.data.build["commit"] = commit
 
     def create_build_environment(self):
         self.build_environment = self.data.environment_class(
diff --git a/readthedocs/projects/tasks/builds.py b/readthedocs/projects/tasks/builds.py
index cb97fcb6b..24ecbab30 100644
--- a/readthedocs/projects/tasks/builds.py
+++ b/readthedocs/projects/tasks/builds.py
@@ -543,11 +543,20 @@ class UpdateDocsTask(SyncRepositoryMixin, Task):
 
         # Clonning
         self.update_build(state=BUILD_STATE_CLONING)
-        self.data.build_director.setup_vcs()
 
-        # Sync tags/branches from VCS repository into Read the Docs' `Version`
-        # objects in the database
-        self.sync_versions(self.data.build_director.vcs_repository)
+        # TODO: remove the ``create_vcs_environment`` hack. Ideally, this should be
+        # handled inside the ``BuildDirector`` but we can't use ``with
+        # self.vcs_environment`` twice because it kills the container on
+        # ``__exit__``
+        self.data.build_director.create_vcs_environment()
+        with self.data.build_director.vcs_environment:
+            self.data.build_director.setup_vcs()
+
+            # Sync tags/branches from VCS repository into Read the Docs'
+            # `Version` objects in the database. This method runs commands
+            # (e.g. "hg tags") inside the VCS environment, so it requires to be
+            # inside the `with` statement
+            self.sync_versions(self.data.build_director.vcs_repository)
 
         # TODO: remove the ``create_build_environment`` hack. Ideally, this should be
         # handled inside the ``BuildDirector`` but we can't use ``with