From 77569c24b98401083f936599e4c94950867a2e22 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 | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/tools/idf_tools.py b/tools/idf_tools.py index 46d1f27568..9b9be35163 100755 --- a/tools/idf_tools.py +++ b/tools/idf_tools.py @@ -924,7 +924,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)) @@ -934,17 +937,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 @@ -979,16 +979,12 @@ 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, IOError): - # 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]