Skip to content

Commit 3732423

Browse files
committed
fix: improve error visibility and add UTF-8 encoding for file reads
- artifact_exporter.py: log at WARNING (not DEBUG) when a child template declares AWS::LanguageExtensions but expansion fails, so users see the error instead of discovering it at deploy time - artifact_exporter.py, package_context.py: add encoding='utf-8' to open() calls to prevent Windows locale encoding issues with non-ASCII templates
1 parent 89d6013 commit 3732423

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

samcli/commands/package/package_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def _export(self, template_path, use_json):
173173
from samcli.lib.cfn_language_extensions.sam_integration import expand_language_extensions
174174

175175
# Read the original template
176-
with open(template_path, "r") as f:
176+
with open(template_path, "r", encoding="utf-8") as f:
177177
original_template_dict = yaml_parse(f.read())
178178

179179
# Build combined parameter values for expand_language_extensions

samcli/lib/package/artifact_exporter.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ def do_export(self, resource_id, resource_dict, parent_dir):
7979
The S3 URIs are then merged back into the original template
8080
(preserving the Fn::ForEach structure) before uploading.
8181
"""
82-
from samcli.lib.cfn_language_extensions.sam_integration import expand_language_extensions
82+
from samcli.lib.cfn_language_extensions.sam_integration import (
83+
check_using_language_extension,
84+
expand_language_extensions,
85+
)
8386
from samcli.lib.intrinsic_resolver.intrinsics_symbol_table import IntrinsicsSymbolTable
8487
from samcli.lib.package.language_extensions_packaging import (
8588
generate_and_apply_artifact_mappings,
@@ -99,7 +102,7 @@ def do_export(self, resource_id, resource_dict, parent_dir):
99102
)
100103

101104
# Read and attempt language extensions expansion on the child template
102-
with open(abs_template_path, "r") as f:
105+
with open(abs_template_path, "r", encoding="utf-8") as f:
103106
child_template_dict = yaml_parse(f.read())
104107

105108
child_template_dir = os.path.dirname(abs_template_path)
@@ -108,8 +111,11 @@ def do_export(self, resource_id, resource_dict, parent_dir):
108111

109112
try:
110113
result = expand_language_extensions(child_template_dict, parameter_values, template_path=abs_template_path)
111-
except InvalidSamDocumentException:
112-
LOG.debug("Language extensions expansion failed for %s, using original template", abs_template_path)
114+
except InvalidSamDocumentException as e:
115+
if check_using_language_extension(child_template_dict):
116+
LOG.warning("Language extensions expansion failed for %s: %s", abs_template_path, e)
117+
else:
118+
LOG.debug("Language extensions expansion failed for %s, using original template", abs_template_path)
113119
result = None
114120

115121
if result and result.had_language_extensions:
@@ -260,7 +266,7 @@ def __init__(
260266
abs_template_path = make_abs_path(parent_dir, template_path)
261267
template_dir = os.path.dirname(abs_template_path)
262268

263-
with open(abs_template_path, "r") as handle:
269+
with open(abs_template_path, "r", encoding="utf-8") as handle:
264270
template_str = handle.read()
265271

266272
self.template_dir = template_dir

0 commit comments

Comments
 (0)