@@ -197,9 +197,16 @@ def run_safety_check_in_cb_context(self):
197197
198198 :return: string, A JSON formatted string containing vulnerabilities found in the container
199199 """
200- from dlc .safety_check import SafetyCheck
201-
202- return SafetyCheck ().run_safety_check_on_container (self .docker_exec_cmd )
200+ try :
201+ from dlc .safety_check import SafetyCheck
202+ result = SafetyCheck ().run_safety_check_on_container (self .docker_exec_cmd )
203+ if not result or not result .strip ():
204+ print ("DEBUG: SafetyCheck returned empty result, using fallback" )
205+ return self .run_safety_check_in_non_cb_context ()
206+ return result
207+ except Exception as e :
208+ print (f"DEBUG: SafetyCheck failed: { e } , using fallback" )
209+ return self .run_safety_check_in_non_cb_context ()
203210
204211 def generate (self ):
205212 """
@@ -209,15 +216,28 @@ def generate(self):
209216 :return: list[dict], the output follows the same format as mentioned in the description of the class
210217 """
211218 self .timestamp = datetime .now ().strftime ("%d-%m-%Y" )
219+
220+ safety_version_cmd = f"{ self .docker_exec_cmd } safety --version"
221+ try :
222+ version_output = self .ctx .run (safety_version_cmd , hide = True , warn = True )
223+ print (f"DEBUG: Safety version: { version_output .stdout .strip ()} " )
224+ except :
225+ print ("DEBUG: Failed to get safety version" )
226+
212227 if os .getenv ("IS_CODEBUILD_IMAGE" ) is None :
213228 self .safety_check_output = self .run_safety_check_in_non_cb_context ()
214229 elif os .getenv ("IS_CODEBUILD_IMAGE" ).upper () == "TRUE" :
215230 self .safety_check_output = self .run_safety_check_in_cb_context ()
231+
232+ print (f"DEBUG: safety_check_output is None: { self .safety_check_output is None } " )
233+ print (f"DEBUG: safety_check_output length: { len (self .safety_check_output ) if self .safety_check_output else 0 } " )
234+ print (f"DEBUG: safety_check_output content: { repr (self .safety_check_output )} " )
235+
216236 # In case of errors, json.loads command will fail. We want the failure to occur to ensure that
217237 # build process fails in case the safety report cannot be generated properly.
218238 scanned_vulnerabilities = json .loads (self .safety_check_output )
219239 self .insert_vulnerabilites_into_report (scanned_vulnerabilities )
220240 packages = self .get_package_set_from_container ()
221241 self .insert_safe_packages_into_report (packages )
222242 self .process_report ()
223- return self .vulnerability_list
243+ return self .vulnerability_list
0 commit comments