in samcli/commands/build/build_context.py [0:0]
def run(self) -> None:
"""Runs the building process by creating an ApplicationBuilder."""
if self._is_sam_template():
SamApiProvider.check_implicit_api_resource_ids(self.stacks)
self._stacks = self._handle_build_pre_processing()
caught_exception: Optional[Exception] = None
try:
# boolean value indicates if mount with write or not, defaults to READ ONLY
mount_with_write = False
if self._use_container:
if self._mount_with == MountMode.WRITE:
mount_with_write = True
else:
# if self._mount_with is NOT WRITE
# check the need of mounting with write permissions and prompt user to enable it if needed
mount_with_write = prompt_user_to_enable_mount_with_write_if_needed(
self.get_resources_to_build(),
self.base_dir,
)
builder = ApplicationBuilder(
self.get_resources_to_build(),
self.build_dir,
self.base_dir,
self.cache_dir,
self.cached,
self.is_building_specific_resource,
manifest_path_override=self.manifest_path_override,
container_manager=self.container_manager,
mode=self.mode,
parallel=self._parallel,
container_env_var=self._container_env_var,
container_env_var_file=self._container_env_var_file,
build_images=self._build_images,
combine_dependencies=not self._create_auto_dependency_layer,
build_in_source=self._build_in_source,
mount_with_write=mount_with_write,
mount_symlinks=self._mount_symlinks,
)
self._check_exclude_warning()
self._check_rust_cargo_experimental_flag()
for f in self.get_resources_to_build().functions:
EventTracker.track_event(EventName.BUILD_FUNCTION_RUNTIME.value, f.runtime)
self._build_result = builder.build()
self._handle_build_post_processing(builder, self._build_result)
click.secho("\nBuild Succeeded", fg="green")
# try to use relpath so the command is easier to understand, however,
# under Windows, when SAM and (build_dir or output_template_path) are
# on different drive, relpath() fails.
root_stack = SamLocalStackProvider.find_root_stack(self.stacks)
out_template_path = root_stack.get_output_template_path(self.build_dir)
try:
build_dir_in_success_message = os.path.relpath(self.build_dir)
output_template_path_in_success_message = os.path.relpath(out_template_path)
except ValueError:
LOG.debug("Failed to retrieve relpath - using the specified path as-is instead")
build_dir_in_success_message = self.build_dir
output_template_path_in_success_message = out_template_path
if self._print_success_message:
msg = self._gen_success_msg(
build_dir_in_success_message,
output_template_path_in_success_message,
os.path.abspath(self.build_dir) == os.path.abspath(DEFAULT_BUILD_DIR),
)
click.secho(msg, fg="yellow")
except FunctionNotFound as function_not_found_ex:
caught_exception = function_not_found_ex
raise UserException(
str(function_not_found_ex), wrapped_from=function_not_found_ex.__class__.__name__
) from function_not_found_ex
except (
UnsupportedRuntimeException,
BuildError,
BuildInsideContainerError,
UnsupportedBuilderLibraryVersionError,
InvalidBuildGraphException,
ResourceNotFound,
) as ex:
caught_exception = ex
click.secho("\nBuild Failed", fg="red")
# Some Exceptions have a deeper wrapped exception that needs to be surfaced
# from deeper than just one level down.
deep_wrap = getattr(ex, "wrapped_from", None)
wrapped_from = deep_wrap if deep_wrap else ex.__class__.__name__
raise UserException(str(ex), wrapped_from=wrapped_from) from ex
finally:
if self.build_in_source:
exception_name = type(caught_exception).__name__ if caught_exception else None
EventTracker.track_event(
EventName.USED_FEATURE.value, UsedFeature.BUILD_IN_SOURCE.value, exception_name
)