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

Make Rust builds respect Cargo.lock when present #202

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[entries]]
id = "67c15e74-a142-482f-9d2e-972a50161129"
type = "feature"
description = "Make Rust builds respect Cargo.lock when present"
author = "[email protected]"
11 changes: 10 additions & 1 deletion kraken-build/src/kraken/std/cargo/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class CargoMetadata:
target_directory: Path

@classmethod
def read(cls, project_dir: Path) -> CargoMetadata:
def read(cls, project_dir: Path, locked: bool | None = None) -> CargoMetadata:
cmd = [
"cargo",
"metadata",
Expand All @@ -79,6 +79,15 @@ def read(cls, project_dir: Path) -> CargoMetadata:
"--manifest-path",
str(project_dir / "Cargo.toml"),
]
if locked is None:
for parent in (project_dir / "Cargo.toml").absolute().parents:
NiklasRosenstein marked this conversation as resolved.
Show resolved Hide resolved
if (parent / "Cargo.lock").exists():
cmd.append("--locked")
break
elif locked:
# if locked is True, we should *always* pass --locked.
# the expectation is that the command will fail w/o Cargo.lock.
cmd.append("--locked")
result = subprocess.run(cmd, stdout=subprocess.PIPE)
if result.returncode != 0:
logger.error("Stderr: %s", result.stderr)
Expand Down
28 changes: 27 additions & 1 deletion kraken-build/src/kraken/std/cargo/tasks/cargo_build_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess as sp
import time
from dataclasses import dataclass
from pathlib import Path

from kraken.core import Project, Property, Task, TaskStatus
from kraken.std.cargo.manifest import ArtifactKind, CargoMetadata
Expand Down Expand Up @@ -35,6 +36,11 @@ class CargoBuildTask(Task):
#: Whether to build incrementally or not.
incremental: Property[bool | None] = Property.default(None)

#: Whether to pass --locked to cargo or not.
#:
#: When set to None, --locked is passed if Cargo.lock exists.
locked: Property[bool | None] = Property.default(None)

#: Environment variables for the Cargo command.
env: Property[dict[str, str]] = Property.default_factory(dict)

Expand All @@ -58,11 +64,31 @@ def get_description(self) -> str | None:
def get_cargo_command_additional_flags(self) -> list[str]:
return shlex.split(os.environ.get("KRAKEN_CARGO_BUILD_FLAGS", ""))

def should_add_locked_flag(self) -> bool:
locked = self.locked.get()
if locked is None:
# pass --locked if we have a lock file
# since we may be in a workspace member, we need to search up!
for parent in (Path.cwd() / "Cargo.toml").parents:
if (parent / "Cargo.lock").exists():
return True
elif locked:
# if locked is True, we should *always* pass --locked.
# the expectation is that the command will fail w/o Cargo.lock.
return True
return False

def get_additional_args(self) -> list[str]:
args = self.additional_args.get()
if "--locked" not in args and self.should_add_locked_flag():
args = ["--locked", *args]
return args

def get_cargo_command(self, env: dict[str, str]) -> list[str]:
incremental = self.incremental.get()
if incremental is not None:
env["CARGO_INCREMENTAL"] = "1" if incremental else "0"
return ["cargo", "build"] + self.additional_args.get()
return ["cargo", "build"] + self.get_additional_args()

def make_safe(self, args: list[str], env: dict[str, str]) -> None:
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def get_cargo_command(self, env: dict[str, str]) -> list[str]:
raise ValueError(f'registry {registry.alias!r} missing a "publish_token"')
command = (
["cargo", "publish"]
+ (["--locked"] if self.should_add_locked_flag() else [])
+ self.additional_args.get()
+ ["--registry", registry.alias, "--token", registry.publish_token]
+ ([] if self.verify.get() else ["--no-verify"])
Expand Down
2 changes: 1 addition & 1 deletion kraken-build/src/kraken/std/cargo/tasks/cargo_test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ class CargoTestTask(CargoBuildTask):

def get_cargo_command(self, env: dict[str, str]) -> list[str]:
super().get_cargo_command(env)
return ["cargo", "test"] + self.additional_args.get()
return ["cargo", "test"] + self.get_additional_args()
Loading