Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set CARGO_TARGET_DIR env var to support cargo workspace setup for sam build #649

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions aws_lambda_builders/workflows/rust_cargo/cargo_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ def run(self, command, cwd):
if "RUST_LOG" not in os.environ:
os.environ["RUST_LOG"] = "debug"
LOG.debug("RUST_LOG environment variable set to `%s`", os.environ.get("RUST_LOG"))

if not os.getenv("CARGO_TARGET_DIR"):
# This results in the "target" dir being created under the member dir of a cargo workspace
# This is for supporting sam build for a Cargo Workspace project
os.environ["CARGO_TARGET_DIR"] = "target"

cargo_process = self._osutils.popen(
command,
stderr=subprocess.PIPE,
Expand Down
40 changes: 30 additions & 10 deletions tests/integration/workflows/rust_cargo/test_rust_cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from aws_lambda_builders.workflows.rust_cargo.feature_flag import EXPERIMENTAL_FLAG_CARGO_LAMBDA


def rm_target_lambda(base):
shutil.rmtree(os.path.join(base, "target", "lambda"), ignore_errors=True)
def rm_target(base):
shutil.rmtree(os.path.join(base, "target"), ignore_errors=True)


class TestRustCargo(TestCase):
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_failed_build_project(self):

def test_builds_hello_project(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "hello")
rm_target_lambda(source_dir)
rm_target(source_dir)

self.builder.build(
source_dir,
Expand All @@ -70,7 +70,7 @@ def test_builds_hello_project(self):

def test_builds_hello_project_with_artifact_name(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "hello")
rm_target_lambda(source_dir)
rm_target(source_dir)

self.builder.build(
source_dir,
Expand All @@ -89,7 +89,7 @@ def test_builds_hello_project_with_artifact_name(self):

def test_builds_hello_project_for_arm64(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "hello")
rm_target_lambda(source_dir)
rm_target(source_dir)

self.builder.build(
source_dir,
Expand All @@ -109,10 +109,10 @@ def test_builds_hello_project_for_arm64(self):

def test_builds_workspaces_project_with_bin_name(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "workspaces")
rm_target_lambda(source_dir)
rm_target(source_dir)

self.builder.build(
source_dir,
f"{source_dir}",
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "Cargo.toml"),
Expand All @@ -125,10 +125,30 @@ def test_builds_workspaces_project_with_bin_name(self):
output_files = set(os.listdir(self.artifacts_dir))

self.assertEqual(expected_files, output_files)
self.assertIn("foo", os.listdir(os.path.join(source_dir, "target", "lambda")))

def test_builds_workspace_member(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "workspaces")
rm_target(source_dir)

self.builder.build(
f"{source_dir}/bar",
self.artifacts_dir,
self.scratch_dir,
os.path.join(source_dir, "Cargo.toml"),
runtime=self.runtime,
experimental_flags=[EXPERIMENTAL_FLAG_CARGO_LAMBDA],
)

expected_files = {"bootstrap"}
output_files = set(os.listdir(self.artifacts_dir))

self.assertEqual(expected_files, output_files)
self.assertIn("bar", os.path.join(source_dir, "bar", "target", "lambda"))

def test_builds_workspaces_project_with_package_option(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "workspaces")
rm_target_lambda(source_dir)
rm_target(source_dir)

self.builder.build(
source_dir,
Expand All @@ -147,7 +167,7 @@ def test_builds_workspaces_project_with_package_option(self):

def test_builds_multi_function_project_with_function_a(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "multi-binary")
rm_target_lambda(source_dir)
rm_target(source_dir)

self.builder.build(
source_dir,
Expand All @@ -166,7 +186,7 @@ def test_builds_multi_function_project_with_function_a(self):

def test_builds_multi_function_project_with_function_b(self):
source_dir = os.path.join(self.TEST_DATA_FOLDER, "multi-binary")
rm_target_lambda(source_dir)
rm_target(source_dir)

self.builder.build(
source_dir,
Expand Down
Loading