Repository: conan-io/conan

Issue:
Wrong option value for local flow install + build 
Conan 1.2

```python
options = {
        "foo": [True, False],
    }
    default_options = (
        "foo=True",
    )
    def configure(self):
        if self.settings.os == "Windows" and "foo" in self.options:
            self.output.info("REMOVING foo")
            del self.options.foo

    def build(self):
        self.output.info("OPTION: %s" % self.options.foo)
```

Local flow:
```bash
conan install ..
conan build ..
```
Then in prints ``OPTION: True``. This needs to be fixed.




Discussion:

Resolving patch:
diff --git a/conans/client/loader.py b/conans/client/loader.py
--- a/conans/client/loader.py
+++ b/conans/client/loader.py
@@ -59,7 +59,7 @@ def load_conan(self, conanfile_path, output, consumer=False, reference=None, loc
 
             if consumer:
                 self._user_options.descope_options(result.name)
-                result.options.initialize_upstream(self._user_options)
+                result.options.initialize_upstream(self._user_options, local=local)
                 self._user_options.clear_unscoped_options()
             else:
                 result.in_local_cache = True
diff --git a/conans/model/options.py b/conans/model/options.py
--- a/conans/model/options.py
+++ b/conans/model/options.py
@@ -427,6 +427,14 @@ def values(self, vals):
             self._ensure_exists(name)
             self._data[name].value = value
 
+    def set_local(self, values):
+        # For local commands, to restore state from conaninfo it is necessary to remove
+        for k in list(self._data):
+            try:
+                self._data[k].value = values._dict[k]
+            except KeyError:
+                self._data.pop(k)
+
     def propagate_upstream(self, package_values, down_ref, own_ref, pattern_options):
         """
         :param: package_values: PackageOptionValues({"shared": "True"}
@@ -547,13 +555,16 @@ def propagate_upstream(self, down_package_values, down_ref, own_ref):
                 pkg_values = self._deps_package_values.setdefault(name, PackageOptionValues())
                 pkg_values.propagate_upstream(option_values, down_ref, own_ref, name)
 
-    def initialize_upstream(self, user_values):
+    def initialize_upstream(self, user_values, local=False):
         """ used to propagate from downstream the options to the upper requirements
         """
         if user_values is not None:
             assert isinstance(user_values, OptionsValues)
             # This values setter implements an update, not an overwrite
-            self._package_options.values = user_values._package_values
+            if local:
+                self._package_options.set_local(user_values._package_values)
+            else:
+                self._package_options.values = user_values._package_values
             for package_name, package_values in user_values._reqs_options.items():
                 pkg_values = self._deps_package_values.setdefault(package_name, PackageOptionValues())
                 pkg_values.update(package_values)


Test patch:
diff --git a/conans/test/remove_subsetting_test.py b/conans/test/remove_subsetting_test.py
--- a/conans/test/remove_subsetting_test.py
+++ b/conans/test/remove_subsetting_test.py
@@ -8,6 +8,7 @@ class RemoveSubsettingTest(unittest.TestCase):
 
     def remove_options_test(self):
         # https://github.com/conan-io/conan/issues/2327
+        # https://github.com/conan-io/conan/issues/2781
         client = TestClient()
         conanfile = """from conans import ConanFile
 class Pkg(ConanFile):
@@ -15,13 +16,19 @@ class Pkg(ConanFile):
     default_options = "opt1=True", "opt2=False"
     def config_options(self):
         del self.options.opt2
+    def build(self):
+        assert "opt2" not in self.options
+        self.options.opt2
 """
         client.save({"conanfile.py": conanfile})
         build_folder = os.path.join(client.current_folder, "build")
         mkdir(build_folder)
         client.current_folder = build_folder
         client.run("install ..")
-        client.run("build ..")
+        error = client.run("build ..", ignore_error=True)
+        self.assertTrue(error)
+        self.assertIn("ConanException: 'options.opt2' doesn't exist", client.out)
+        self.assertIn("Possible options are ['opt1']", client.out)
 
     def remove_setting_test(self):
         # https://github.com/conan-io/conan/issues/2327