From 640f1c844b5cd935e809b0195df06f1639eb9979 Mon Sep 17 00:00:00 2001 From: Marek Fiala Date: Fri, 23 Sep 2022 15:13:50 +0200 Subject: [PATCH] Tools: bugfix wrong format of idf-env.json, KeyError: 'idfSelectedId' Closes https://github.com/espressif/esp-idf/issues/9837 --- tools/idf_tools.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tools/idf_tools.py b/tools/idf_tools.py index 32afa57b40..d568b91ff1 100755 --- a/tools/idf_tools.py +++ b/tools/idf_tools.py @@ -1077,7 +1077,10 @@ def get_idf_env(): # type: () -> Any try: idf_env_file_path = os.path.join(global_idf_tools_path, IDF_ENV_FILE) # type: ignore with open(idf_env_file_path, 'r') as idf_env_file: - return json.load(idf_env_file) + idf_env_json = json.load(idf_env_file) + if 'sha' not in idf_env_json['idfInstalled']: + idf_env_json['idfInstalled']['sha'] = {'targets': []} + return idf_env_json except (IOError, OSError): if not os.path.exists(idf_env_file_path): warn('File {} was not found. '.format(idf_env_file_path)) @@ -1087,17 +1090,14 @@ def get_idf_env(): # type: () -> Any os.rename(idf_env_file_path, os.path.join(os.path.dirname(idf_env_file_path), (filename + '_failed' + ending))) info('Creating {}' .format(idf_env_file_path)) - return {'idfSelectedId': 'sha', 'idfInstalled': {'sha': {'targets': {}}}} + return {'idfInstalled': {'sha': {'targets': []}}} def export_targets_to_idf_env_json(targets): # type: (list[str]) -> None idf_env_json = get_idf_env() targets = list(set(targets + get_user_defined_targets())) - for env in idf_env_json['idfInstalled']: - if env == idf_env_json['idfSelectedId']: - idf_env_json['idfInstalled'][env]['targets'] = targets - break + idf_env_json['idfInstalled']['sha']['targets'] = targets try: if global_idf_tools_path: # mypy fix for Optional[str] in the next call @@ -1132,16 +1132,13 @@ def get_user_defined_targets(): # type: () -> list[str] try: with open(os.path.join(global_idf_tools_path, IDF_ENV_FILE), 'r') as idf_env_file: # type: ignore idf_env_json = json.load(idf_env_file) + if 'sha' not in idf_env_json['idfInstalled']: + idf_env_json['idfInstalled']['sha'] = {'targets': []} except OSError: # warn('File {} was not found. Installing tools for all esp targets.'.format(os.path.join(global_idf_tools_path, IDF_ENV_FILE))) # type: ignore return [] - targets = [] - for env in idf_env_json['idfInstalled']: - if env == idf_env_json['idfSelectedId']: - targets = idf_env_json['idfInstalled'][env]['targets'] - break - return targets + return idf_env_json['idfInstalled']['sha']['targets'] # type: ignore def get_all_targets_from_tools_json(): # type: () -> list[str]