Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into cmikolajczak/move_…
Browse files Browse the repository at this point in the history
…bump_into_publish
  • Loading branch information
NiklasRosenstein committed Feb 27, 2024
2 parents 1cc060a + 89d1cfc commit 576297e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[[entries]]
id = "67c15e74-a142-482f-9d2e-972a50161129"
type = "feature"
description = "Make Rust builds respect Cargo.lock when present"
author = "[email protected]"

[[entries]]
id = "db65bd12-a9d1-472c-a38b-6f889cdda683"
type = "improvement"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/on-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@v4
- uses: NiklasRosenstein/slap@gha/install/v1

- run: pipx install kraken-wrapper==0.34.0 && krakenw config --installer=UV
- run: pipx install kraken-wrapper==0.34.1 && krakenw config --installer=UV
- run: pipx install mksync

- name: Restore Kraken build cache
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
- uses: NiklasRosenstein/slap@gha/install/v1
- uses: actions/setup-python@v5
with: { python-version: "${{ matrix.python-version }}" }
- run: pip install pipx && pipx install poetry && pipx install pdm && pipx install kraken-wrapper==0.34.0 && krakenw config --installer=UV
- run: pip install pipx && pipx install poetry && pipx install pdm && pipx install kraken-wrapper==0.34.1 && krakenw config --installer=UV
- run: rustup update

- name: Restore Kraken build cache
Expand Down
16 changes: 15 additions & 1 deletion kraken-build/src/kraken/std/cargo/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class CargoMetadata:
target_directory: Path

@classmethod
def read(cls, project_dir: Path, from_project_dir: bool = False) -> CargoMetadata:
def read(cls, project_dir: Path, from_project_dir: bool = False, locked: bool | None = None) -> CargoMetadata:
cmd = [
"cargo",
"metadata",
Expand All @@ -80,14 +80,28 @@ def read(cls, project_dir: Path, from_project_dir: bool = False) -> CargoMetadat
"--manifest-path",
str(project_dir / "Cargo.toml"),
]

# the .cargo/config.toml in user/foo/bar is not picked up when running this command from
# user/foo for example. This flag executes the subprocess from the project dir
if from_project_dir:
cwd = project_dir
else:
cwd = Path(os.getcwd())

if locked is None:
# Append `--locked` if a Cargo.lock file is found in the project directory or any of its parents.
project_dir = project_dir.absolute()
for parent in [project_dir, *project_dir.parents]:
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 a Cargo.lock file.
cmd.append("--locked")

result = subprocess.run(cmd, stdout=subprocess.PIPE, cwd=cwd)

if result.returncode != 0:
logger.error("Stderr: %s", result.stderr)
logger.error(f"Could not execute `{' '.join(cmd)}`, and thus can't read the cargo metadata.")
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 @@ -61,11 +67,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 @@ -44,6 +44,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()

0 comments on commit 576297e

Please sign in to comment.