Compare commits

...
Author SHA1 Message Date
Sera f3579c53a4 chore(cli): trim docstrings 2026-11-15 21:02:40 -05:00
Sera d19a9b2987 fix(cli): copy a uv project root in one ADD when it matches the build context
source.kind "uv" copied the project root as one ADD per top-level entry.
Each ADD is a layer, and on remote BuildKit layer snapshots and export
made a 37-entry repo build 3-6x slower than a single ADD.

Emit a single ADD . when the project root is the build context, has no
other workspace members, and every top-level entry the walk skips is
also dropped by the context's .dockerignore. Otherwise keep the walk,
so .git, .venv and node_modules never reach the image.
2026-11-15 21:01:34 -05:00
3 changed files with 90 additions and 9 deletions

No files matched your search

+9 -6
View File
@@ -36,16 +36,19 @@ class _NegatedDockerignoreHints:
def _build_ignore_spec(
directory: pathlib.Path, *, include_gitignore: bool = True
directory: pathlib.Path,
*,
include_gitignore: bool = True,
include_always_excluded: bool = True,
) -> pathspec.PathSpec:
"""Build a PathSpec combining built-in exclusions with ignore files.
Always excludes common non-source directories (`_ALWAYS_EXCLUDE`). On top
of that, patterns from `.dockerignore` are merged in. `.gitignore` patterns
are optional because some callers need Docker build-context semantics,
while archive creation wants both files.
Excludes common non-source directories (`_ALWAYS_EXCLUDE`) by default. On
top of that, patterns from `.dockerignore` are merged in. `.gitignore`
patterns are optional because some callers need Docker build-context
semantics, while archive creation wants both files.
"""
lines: list[str] = list(_ALWAYS_EXCLUDE)
lines: list[str] = list(_ALWAYS_EXCLUDE) if include_always_excluded else []
ignore_files = [".dockerignore"]
if include_gitignore:
ignore_files.append(".gitignore")
+31 -2
View File
@@ -517,6 +517,30 @@ def _uv_lock_package_copy_items(
return iter_entries(plan.project_root)
def _uv_lock_root_copy_is_whole_context(
plan: UvLockPlan,
config_root: pathlib.Path,
copy_items: tuple[tuple[pathlib.PurePosixPath, pathlib.PurePosixPath], ...],
) -> bool:
"""Each ADD is a layer, and layer count dominates remote BuildKit build time.
One ADD is only safe when everything the walk skipped is also outside the build context."""
if plan.project_root.resolve() != config_root or (
plan.all_workspace_roots - {plan.project_root}
):
return False
if not copy_items or any(len(source.parts) != 1 for source, _ in copy_items):
return False
copied = {source.as_posix() for source, _ in copy_items}
context_ignore = _build_ignore_spec(
plan.project_root, include_gitignore=False, include_always_excluded=False
)
return all(
context_ignore.match_file(f"{child.name}/" if child.is_dir() else child.name)
for child in plan.project_root.iterdir()
if child.name not in copied
)
def _resolve_uv_lock_container_path(
host_path: pathlib.Path, plan: UvLockPlan
) -> pathlib.PurePosixPath | None:
@@ -1023,9 +1047,14 @@ def python_config_to_docker_uv_lock(
for package in plan.install_order:
package_label = package.root.relative_to(plan.project_root).as_posix() or "."
docker_plan.add_raw(f"# -- Adding workspace package {package_label} --")
for source, destination in _uv_lock_package_copy_items(
package, plan, ignore_spec
copy_items = _uv_lock_package_copy_items(package, plan, ignore_spec)
if package.root == plan.project_root and _uv_lock_root_copy_is_whole_context(
plan, config_root, copy_items
):
copy_items = (
(pathlib.PurePosixPath("."), plan.container_roots[package.root]),
)
for source, destination in copy_items:
docker_plan.add_raw(copy_from_project_root(source, destination.as_posix()))
docker_plan.add_instruction(
"WORKDIR", plan.container_roots[package.root].as_posix()
+50 -1
View File
@@ -2357,7 +2357,56 @@ def test_config_to_docker_uv_lock_does_not_apply_gitignore():
base_image="langchain/langgraph-api:0.2.47",
)
assert "ADD README.md /deps/workspace/README.md" in docker
assert "ADD . /deps/workspace\n" in docker
def test_config_to_docker_uv_lock_copies_root_once_when_dockerignore_drops_builtins():
with tempfile.TemporaryDirectory() as tmpdir:
project_root = pathlib.Path(tmpdir) / "single"
project_root.mkdir()
(project_root / "uv.lock").write_text("# uv lock file\n")
(project_root / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "single-app"
version = "0.1.0"
dependencies = ["httpx>=0.28"]
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
"""
).strip()
+ "\n"
)
(project_root / "langgraph.json").write_text("{}\n")
(project_root / "src").mkdir()
(project_root / "src" / "agent.py").write_text("graph = object()\n")
(project_root / "docs").mkdir()
(project_root / "docs" / "index.md").write_text("# docs\n")
(project_root / ".git").mkdir()
(project_root / ".git" / "HEAD").write_text("ref: refs/heads/main\n")
(project_root / ".venv").mkdir()
(project_root / ".venv" / "pyvenv.cfg").write_text("home = /usr\n")
(project_root / ".dockerignore").write_text(".git\n.venv\n")
config = validate_config(
{
"python_version": "3.11",
"graphs": {"agent": "./src/agent.py:graph"},
"source": {"kind": "uv"},
}
)
docker, _ = config_to_docker(
project_root / "langgraph.json",
config,
base_image="langchain/langgraph-api:0.2.47",
)
assert "ADD . /deps/workspace\n" in docker
assert "ADD src " not in docker
assert "ADD docs " not in docker
def test_config_to_docker_uv_lock_skips_dockerignore_entries_in_workspace():