Repository: ESMCI/cime

Issue:
Some cime_developer tests fail during phase SHAREDLIB_BUILD
This issue is reproducible on both anlworkstation and summit.
It is caused by PR #3449: Improve case.setup clean (happens for --clean or --reset)

Steps to reproduce:
```
git clone https://github.com/ESMCI/cime.git
cd cime/scripts
./create_test DAE.ww3a.ADWAV --no-run
```
Errors are:
```
        ...
        WARNING: Test case setup failed. Case2 has been removed, but the main case may be in an inconsistent state. If you want to rerun this test, you should create a new test rather than trying to rerun this one.
        ERROR: Cannot move past core phase 'SETUP', it didn't pass:
```

CaseStatus shows:
```
case.setup error 
ERROR: FakeCase does not support getting value of 'SMP_PRESENT'
```
Another way to reproduce this issue:
```
git clone https://github.com/ESMCI/cime.git
cd cime/scripts
./create_test DAE.ww3a.ADWAV --no-build
cd to Case dir
./case.setup --reset
```
Output messages:
```
Successfully cleaned .case.test
Successfully cleaned env_mach_specific.xml
Successfully cleaned Macros.make
Successfully cleaned Macros.cmake
ERROR: FakeCase does not support getting value of 'SMP_PRESENT'
```



Discussion:
Not reproducible on Cori.

Resolving patch:
diff --git a/scripts/lib/CIME/BuildTools/configure.py b/scripts/lib/CIME/BuildTools/configure.py
--- a/scripts/lib/CIME/BuildTools/configure.py
+++ b/scripts/lib/CIME/BuildTools/configure.py
@@ -23,7 +23,7 @@
 logger = logging.getLogger(__name__)
 
 def configure(machobj, output_dir, macros_format, compiler, mpilib, debug,
-              comp_interface, sysos, unit_testing=False):
+              comp_interface, sysos, unit_testing=False, noenv=False):
     """Add Macros, Depends, and env_mach_specific files to a directory.
 
     Arguments:
@@ -46,7 +46,7 @@ def configure(machobj, output_dir, macros_format, compiler, mpilib, debug,
 
     _copy_depends_files(machobj.get_machine_name(), machobj.machines_dir, output_dir, compiler)
     _generate_env_mach_specific(output_dir, machobj, compiler, mpilib,
-                                debug, comp_interface, sysos, unit_testing)
+                                debug, comp_interface, sysos, unit_testing, noenv=noenv)
 
 def _copy_depends_files(machine_name, machines_dir, output_dir, compiler):
     """
@@ -84,7 +84,7 @@ def get_value(self, attrib):
         return self._vals[attrib]
 
 def _generate_env_mach_specific(output_dir, machobj, compiler, mpilib, debug,
-                                comp_interface, sysos, unit_testing):
+                                comp_interface, sysos, unit_testing, noenv=False):
     """
     env_mach_specific generation.
     """
@@ -92,9 +92,14 @@ def _generate_env_mach_specific(output_dir, machobj, compiler, mpilib, debug,
     if os.path.exists(ems_path):
         logger.warning("{} already exists, delete to replace".format(ems_path))
         return
+
     ems_file = EnvMachSpecific(output_dir, unit_testing=unit_testing)
     ems_file.populate(machobj)
     ems_file.write()
+
+    if noenv:
+        return
+
     fake_case = FakeCase(compiler, mpilib, debug, comp_interface)
     ems_file.load_env(fake_case)
     for shell in ('sh', 'csh'):
diff --git a/scripts/lib/CIME/case/case_setup.py b/scripts/lib/CIME/case/case_setup.py
--- a/scripts/lib/CIME/case/case_setup.py
+++ b/scripts/lib/CIME/case/case_setup.py
@@ -110,11 +110,16 @@ def _case_setup_impl(case, caseroot, clean=False, test_mode=False, reset=False,
         # creates the Macros.make, Depends.compiler, Depends.machine, Depends.machine.compiler
         # and env_mach_specific.xml if they don't already exist.
         if not os.path.isfile("Macros.make") or not os.path.isfile("env_mach_specific.xml"):
-            configure(Machines(machine=mach), caseroot, ["Makefile"], compiler, mpilib, debug, comp_interface, sysos)
+            reread = not os.path.isfile("env_mach_specific.xml")
+            if reread:
+                case.flush()
+            configure(Machines(machine=mach), caseroot, ["Makefile"], compiler, mpilib, debug, comp_interface, sysos, noenv=True)
+            if reread:
+                case.read_xml()
 
         # Also write out Cmake macro file
         if not os.path.isfile("Macros.cmake"):
-            configure(Machines(machine=mach), caseroot, ["CMake"], compiler, mpilib, debug, comp_interface, sysos)
+            configure(Machines(machine=mach), caseroot, ["CMake"], compiler, mpilib, debug, comp_interface, sysos, noenv=True)
 
         # Set tasks to 1 if mpi-serial library
         if mpilib == "mpi-serial":


Test patch: