Skip to content
26 changes: 26 additions & 0 deletions samcli/commands/_utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
RemoteInvokeBotoApiParameterType,
SigningProfilesOptionType,
)
from samcli.commands._utils.click_mutex import ClickMutex
from samcli.commands._utils.constants import (
DEFAULT_BUILD_DIR,
DEFAULT_BUILT_TEMPLATE_PATH,
Expand All @@ -33,8 +34,17 @@
from samcli.lib.hook.hook_wrapper import get_available_hook_packages_ids
from samcli.lib.observability.util import OutputOption
from samcli.lib.utils.packagetype import IMAGE, ZIP
from samcli.local.docker.lambda_image import Runtime

_TEMPLATE_OPTION_DEFAULT_VALUE = "template.[yaml|yml|json]"
SUPPORTED_BUILD_IN_SOURCE_WORKFLOWS = [
Runtime.nodejs12x.value,
Runtime.nodejs14x.value,
Runtime.nodejs16x.value,
Runtime.nodejs18x.value,
"Makefile",
"esbuild",
]

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -906,3 +916,19 @@ def generate_next_command_recommendation(command_tuples: List[Tuple[str, str]])
"""
command_list_txt = "\n".join(f"[*] {description}: {command}" for description, command in command_tuples)
return template.format(command_list_txt)


def build_in_source_click_option():
return click.option(
"--build-in-source/--no-build-in-source",
required=False,
is_flag=True,
help="Opts in to build project in the source folder. The following workflows support "
f"building in source: {SUPPORTED_BUILD_IN_SOURCE_WORKFLOWS}",
cls=ClickMutex,
incompatible_params=["use_container", "hook_name"],
)


def build_in_source_option(f):
return build_in_source_click_option()(f)
11 changes: 7 additions & 4 deletions samcli/commands/build/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from samcli.cli.context import Context
from samcli.commands._utils.options import (
build_in_source_option,
skip_prepare_infra_option,
template_option_without_build,
docker_common_options,
Expand Down Expand Up @@ -74,10 +75,11 @@
@terraform_project_root_path_option
@hook_name_click_option(
force_prepare=True,
invalid_coexist_options=["t", "template-file", "template", "parameter-overrides"],
invalid_coexist_options=["t", "template-file", "template", "parameter-overrides", "build-in-source"],
)
@skip_prepare_infra_option
@use_container_build_option
@build_in_source_option
@click.option(
"--container-env-var",
"-e",
Expand Down Expand Up @@ -158,8 +160,9 @@ def cli(
save_params: bool,
hook_name: Optional[str],
skip_prepare_infra: bool,
mount_with,
mount_with: str,
terraform_project_root_path: Optional[str],
build_in_source: Optional[bool],
) -> None:
"""
`sam build` command entry point
Expand Down Expand Up @@ -189,7 +192,7 @@ def cli(
build_image,
exclude,
hook_name,
None, # TODO: replace with build_in_source once it's added as a click option
build_in_source,
mount_with,
) # pragma: no cover

Expand All @@ -216,7 +219,7 @@ def do_cli( # pylint: disable=too-many-locals, too-many-statements
exclude: Optional[Tuple[str, ...]],
hook_name: Optional[str],
build_in_source: Optional[bool],
mount_with,
mount_with: str,
) -> None:
"""
Implementation of the ``cli`` method
Expand Down
2 changes: 1 addition & 1 deletion samcli/commands/build/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

EXTENSION_OPTIONS: List[str] = ["hook_name", "skip_prepare_infra"]

BUILD_STRATEGY_OPTIONS: List[str] = ["parallel", "exclude", "manifest", "cached"]
BUILD_STRATEGY_OPTIONS: List[str] = ["parallel", "exclude", "manifest", "cached", "build_in_source"]

ARTIFACT_LOCATION_OPTIONS: List[str] = [
"build_dir",
Expand Down
5 changes: 4 additions & 1 deletion samcli/commands/sync/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from samcli.commands._utils.options import (
base_dir_option,
build_image_option,
build_in_source_option,
capabilities_option,
image_repositories_option,
image_repository_option,
Expand Down Expand Up @@ -158,6 +159,7 @@
@stack_name_option(required=True) # pylint: disable=E1120
@base_dir_option
@use_container_build_option
@build_in_source_option
@build_image_option(cls=ContainerOptions)
@image_repository_option
@image_repositories_option
Expand Down Expand Up @@ -209,6 +211,7 @@ def cli(
config_file: str,
config_env: str,
build_image: Optional[Tuple[str]],
build_in_source: Optional[bool],
) -> None:
"""
`sam sync` command entry point
Expand Down Expand Up @@ -244,7 +247,7 @@ def cli(
build_image,
config_file,
config_env,
None, # TODO: replace with build_in_source once it's added as a click option
build_in_source,
) # pragma: no cover


Expand Down
1 change: 1 addition & 0 deletions samcli/commands/sync/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"resource_id",
"resource",
"base_dir",
"build_in_source",
]
OTHER_OPTIONS: List[str] = ["debug", "help"]

Expand Down
14 changes: 12 additions & 2 deletions schema/samcli.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_invalid_coexist_parameters(self):
process_stderr = stderr.strip()
self.assertRegex(
process_stderr.decode("utf-8"),
"Error: Invalid value: Parameters hook-name, and t,template-file,template,parameter-overrides cannot "
"Error: Invalid value: Parameters hook-name, and t,template-file,template,parameter-overrides,build-in-source cannot "
"be used together",
)
self.assertNotEqual(return_code, 0)
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/commands/samconfig/test_samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_build(self, do_cli_mock):
("",),
("",),
None,
None,
False,
"READ",
)

Expand Down Expand Up @@ -217,7 +217,7 @@ def test_build_with_no_cached_override(self, do_cli_mock):
("",),
("",),
None,
None,
False,
"READ",
)

Expand Down Expand Up @@ -274,7 +274,7 @@ def test_build_with_container_env_vars(self, do_cli_mock):
(),
(),
None,
None,
False,
"READ",
)

Expand Down Expand Up @@ -330,7 +330,7 @@ def test_build_with_build_images(self, do_cli_mock):
("Function1=image_1", "image_2"),
(),
None,
None,
False,
"READ",
)

Expand Down Expand Up @@ -1010,7 +1010,7 @@ def test_sync(
(),
"samconfig.toml",
"default",
None,
False,
)


Expand Down