Skip to content

Commit

Permalink
fix: set CARGO_TARGET_DIR env var to support cargo workspace setup fo…
Browse files Browse the repository at this point in the history
…r sam build (#649)
  • Loading branch information
hawflau authored May 13, 2024
1 parent c5e7d3e commit 0c61738
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
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

0 comments on commit 0c61738

Please sign in to comment.