# Buggy code
def _default_target_from_env(self):
    """Set and return the default CrayPE target loaded in a clean login
    session.

    A bash subshell is launched with a wiped environment and the list of
    loaded modules is parsed for the first acceptable CrayPE target.
    """
    # env -i /bin/bash -lc echo $CRAY_CPU_TARGET 2> /dev/null
    if getattr(self, "default", None) is None:
        output = Executable("/bin/bash")(
            "-lc",
            "echo $CRAY_CPU_TARGET",
            env={"TERM": os.environ["TERM"]},
            output=str,
            error=os.devnull,
        )
        output = "".join(output.split())  # remove all whitespace
        if output:
            self.default = output
            tty.debug("Found default module:%s" % self.default)
    return self.default


# Traceback (KeyError)
Traceback (most recent call last):
File "/u/sciteam/stewart1/spack/bin/spack", line 64, in <module>
sys.exit(spack.main.main())
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/main.py", line 770, in main
if spack.config.get('config:debug'):
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/config.py", line 671, in get
return config.get(path, default, scope)
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/llnl/util/lang.py", line 552, in __getattr__
return getattr(self.instance, name)
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/llnl/util/lang.py", line 548, in instance
self._instance = self.factory()
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/config.py", line 653, in _config
_add_platform_scope(cfg, ConfigScope, name, path)
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/config.py", line 606, in _add_platform_scope
platform = spack.architecture.platform().name
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/llnl/util/lang.py", line 178, in _memoized_function
func.cache[args] = func(*args)
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/architecture.py", line 516, in platform
return platform_cls()
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/platforms/cray.py", line 63, in __init__
_target = self._default_target_from_env()
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/platforms/cray.py", line 121, in _default_target_from_env
env={'TERM': os.environ['TERM']},
File "/usr/lib64/python2.6/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: 'TERM'

# Fixed code
def _default_target_from_env(self):
    """Set and return the default CrayPE target loaded in a clean login
    session.

    A bash subshell is launched with a wiped environment and the list of
    loaded modules is parsed for the first acceptable CrayPE target.
    """
    # env -i /bin/bash -lc echo $CRAY_CPU_TARGET 2> /dev/null
    if getattr(self, "default", None) is None:
        bash = Executable("/bin/bash")
        output = bash(
            "-lc",
            "echo $CRAY_CPU_TARGET",
            env={"TERM": os.environ.get("TERM", "")},
            output=str,
            error=os.devnull,
        )
        output = "".join(output.split())  # remove all whitespace
        if output:
            self.default = output
            tty.debug("Found default module:%s" % self.default)
    return self.default