Compare commits

...
Author SHA1 Message Date
Saad Farooq f58bdf7646 fix(cli): decline root copy on patterns Docker reads differently
Docker treats `[!...]` and inner `**` differently from pathspec, and
tarfile skips codecs Python was built without. Decline on those patterns,
detect compressed archives by magic bytes, and pin the saved-Dockerfile
and compose paths to per-entry copies.
2026-11-02 19:15:10 -05:00
Saad Farooq cfb0fcc435 fix(cli): only collapse the uv root copy for immediate builds
A saved Dockerfile can be built after the tree changes, and ADD . would
then pick up entries the generation-time check never saw (.venv, .git,
symlinks). Gate the single copy behind single_root_copy, which only
build_docker_image sets.
2026-11-02 18:51:49 -05:00
Saad Farooq 777df4d7da refactor(cli): trim root-copy guard and its tests 2026-11-02 18:24:40 -05:00
Saad Farooq 2d4b6a6ce7 perf(cli): copy uv project root in a single layer when equivalent
In uv.lock mode the root package was copied with one ADD per top-level
entry. Emit a single ADD . when that provably yields the same files, and
keep the per-entry walk otherwise.
2026-11-02 18:13:47 -05:00
5 changed files with 264 additions and 2 deletions

No files matched your search

+4
View File
@@ -1400,6 +1400,7 @@ def python_config_to_docker(
api_version: str | None = None,
*,
escape_variables: bool = False,
single_root_copy: bool = False,
) -> tuple[str, dict[str, str]]:
"""Generate a Dockerfile from the configuration."""
source_kind = _get_source_kind(config)
@@ -1412,6 +1413,7 @@ def python_config_to_docker(
base_image,
api_version=api_version,
build_tools_to_uninstall=build_tools_to_uninstall,
single_root_copy=single_root_copy,
)
_validate_local_dependency_files(config_path, config)
if pip_installer == "auto":
@@ -1772,6 +1774,7 @@ def config_to_docker(
build_command: str | None = None,
build_context: str | None = None,
escape_variables: bool = False,
single_root_copy: bool = False,
) -> tuple[str, dict[str, str]]:
base_image = base_image or default_base_image(config)
@@ -1792,6 +1795,7 @@ def config_to_docker(
base_image=base_image,
api_version=api_version,
escape_variables=escape_variables,
single_root_copy=single_root_copy,
)
+2
View File
@@ -387,6 +387,8 @@ def build_docker_image(
install_command=install_command,
build_command=build_command,
build_context=build_context,
# Safe because this Dockerfile is built immediately, not saved for later.
single_root_copy=True,
)
# add additional_contexts
if additional_contexts:
+63 -2
View File
@@ -2,6 +2,7 @@ import os
import pathlib
import re
import shlex
import tarfile
from dataclasses import dataclass
try:
@@ -13,6 +14,7 @@ import click
import pathspec
from langgraph_cli._ignore import (
_ALWAYS_EXCLUDE_NAMES,
_build_dockerignore_negation_hints,
_build_ignore_spec,
_is_always_excluded,
@@ -517,6 +519,56 @@ def _uv_lock_package_copy_items(
return iter_entries(plan.project_root)
_COMPRESSION_MAGIC = (b"\x1f\x8b", b"BZh", b"\xfd7zXZ\x00", b"\x28\xb5\x2f\xfd")
def _is_local_archive(path: pathlib.Path) -> bool:
"""Whether Docker's `ADD` would auto-extract this local file."""
with path.open("rb") as f:
# Match gzip/bzip2/xz/zstd by magic, since tarfile skips codecs Python lacks.
if f.read(6).startswith(_COMPRESSION_MAGIC):
return True
f.seek(0)
return tarfile.is_tarfile(f)
def _root_copy_is_equivalent(
plan: UvLockPlan,
config_root: pathlib.Path,
config: Config,
copy_items: tuple[tuple[pathlib.PurePosixPath, pathlib.PurePosixPath], ...],
) -> bool:
"""Whether one `ADD .` produces the same files as the per-entry ADDs."""
root = plan.project_root
# Only the build context's .dockerignore applies, and nested members need the walk.
if root != config_root or any(root in m.parents for m in plan.all_workspace_roots):
return False
ignore_file = root / ".dockerignore"
text = ignore_file.read_text(encoding="utf-8") if ignore_file.is_file() else ""
patterns = {s for line in text.splitlines() if (s := line.strip()) and s[0] != "#"}
# Negations need the walk; Docker reads `[...]` and inner `**` differently.
if any(
p.startswith("!") or "[" in p or "**" in p.removeprefix("**/") for p in patterns
):
return False
literals = {p.lstrip("/").removeprefix("./").rstrip("/") for p in patterns}
# Docker doesn't apply the CLI's built-in exclusions, so .dockerignore must.
if any(
c.name in _ALWAYS_EXCLUDE_NAMES
and c.is_dir()
and not {c.name, f"**/{c.name}"} & literals
for c in root.iterdir()
):
return False
# Per-entry ADD follows symlinks and extracts archives; `ADD .` does neither.
paths = [root / source for source, _ in copy_items]
if any(p.is_symlink() or (p.is_file() and _is_local_archive(p)) for p in paths):
return False
# Custom lines that touch /deps could make the destination merge differ.
return not any("/deps" in line for line in config["dockerfile_lines"])
def _resolve_uv_lock_container_path(
host_path: pathlib.Path, plan: UvLockPlan
) -> pathlib.PurePosixPath | None:
@@ -872,6 +924,7 @@ def python_config_to_docker_uv_lock(
api_version: str | None = None,
*,
build_tools_to_uninstall: tuple[str, ...] | None,
single_root_copy: bool = False,
) -> tuple[str, dict[str, str]]:
from langgraph_cli.config import (
DEFAULT_NODE_VERSION,
@@ -1023,9 +1076,17 @@ 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)
# Only for immediate builds: `ADD .` copies the tree as it is at build time.
if (
single_root_copy
and package.root == plan.project_root
and _root_copy_is_equivalent(plan, config_root, config, 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()
+171
View File
@@ -2,13 +2,16 @@ import copy
import json
import os
import pathlib
import tarfile
import tempfile
import textwrap
from unittest.mock import patch
import click
import pytest
from click.testing import CliRunner
from langgraph_cli.cli import cli
from langgraph_cli.config import (
_BUILD_TOOLS,
_get_pip_cleanup_lines,
@@ -2360,6 +2363,174 @@ def test_config_to_docker_uv_lock_does_not_apply_gitignore():
assert "ADD README.md /deps/workspace/README.md" in docker
def _write_single_uv_project(project_root: pathlib.Path) -> None:
project_root.mkdir(parents=True)
(project_root / "uv.lock").write_text("# uv lock file\n")
(project_root / "pyproject.toml").write_text(
'[project]\nname = "single-app"\nversion = "0.1.0"\n'
'dependencies = ["httpx>=0.28"]\n'
)
(project_root / "langgraph.json").write_text("{}\n")
(project_root / "src").mkdir()
(project_root / "src" / "agent.py").write_text("graph = object()\n")
(project_root / "README.md").write_text("# hi\n")
def _single_uv_docker(
config_path: pathlib.Path, single_root_copy: bool = True, **overrides
) -> str:
config = validate_config(
{
"python_version": "3.11",
"graphs": {"agent": "./src/agent.py:graph"},
"source": {"kind": "uv"},
**overrides,
}
)
docker, _ = config_to_docker(
config_path,
config,
base_image="langchain/langgraph-api:0.2.47",
single_root_copy=single_root_copy,
)
return docker
def test_config_to_docker_uv_lock_copies_safe_root_in_one_layer():
with tempfile.TemporaryDirectory() as tmpdir:
project_root = pathlib.Path(tmpdir) / "single"
_write_single_uv_project(project_root)
(project_root / "docs").mkdir()
(project_root / "docs" / "notes.md").write_text("notes\n")
# Built-in exclusions are fine when Docker excludes them too.
(project_root / ".venv").mkdir()
(project_root / "__pycache__").mkdir()
(project_root / ".dockerignore").write_text("/.venv/\n**/__pycache__\n")
docker = _single_uv_docker(project_root / "langgraph.json")
source_copies = [
line
for line in docker.splitlines()
if line.startswith("ADD ") and "/deps/workspace" in line
]
assert source_copies == ["ADD . /deps/workspace"]
# Dependency metadata is still copied separately ahead of uv export.
assert "ADD pyproject.toml /tmp/uv_export/project/pyproject.toml" in docker
assert "ADD uv.lock /tmp/uv_export/project/uv.lock" in docker
# A saved Dockerfile may be built after the tree changes, so keep the list.
saved = _single_uv_docker(
project_root / "langgraph.json", single_root_copy=False
)
assert "ADD . /deps/workspace" not in saved
assert "ADD src /deps/workspace/src" in saved
def _write_tar(root: pathlib.Path) -> None:
# No extension, so detection must go by content.
with tarfile.open(root / "bundle.bin", "w") as tar:
tar.add(root / "README.md", arcname="README.md")
def _add_unrelated_member(root: pathlib.Path) -> None:
with (root / "pyproject.toml").open("a") as f:
f.write('\n[tool.uv.workspace]\nmembers = ["libs/*"]\n')
(root / "libs" / "extra").mkdir(parents=True)
(root / "libs" / "extra" / "pyproject.toml").write_text(
'[project]\nname = "extra"\nversion = "0.1.0"\n'
)
def _venv_ignored_by(patterns: str):
def setup(root: pathlib.Path) -> None:
(root / ".venv").mkdir()
(root / ".dockerignore").write_text(patterns)
return setup
@pytest.mark.parametrize(
("setup", "overrides"),
[
pytest.param(lambda r: (r / ".venv").mkdir(), {}, id="unexcluded_venv"),
pytest.param(
_venv_ignored_by(".venv-old\nsrc/.venv\n"), {}, id="near_miss_literal"
),
pytest.param(_venv_ignored_by(".venv\n[!t]*.log\n"), {}, id="char_class"),
pytest.param(_venv_ignored_by(".venv\ndata/**\n"), {}, id="inner_globstar"),
pytest.param(_add_unrelated_member, {}, id="workspace_member"),
pytest.param(
lambda r: (r / ".dockerignore").write_text("*.log\n!keep.log\n"),
{},
id="negation",
),
pytest.param(lambda r: (r / "linked").symlink_to("src"), {}, id="symlink"),
pytest.param(_write_tar, {}, id="tar"),
pytest.param(
lambda r: (r / "data.zst").write_bytes(b"\x28\xb5\x2f\xfd" + bytes(16)),
{},
id="zstd",
),
pytest.param(
lambda r: None,
{"dockerfile_lines": ["RUN mkdir -p /deps/workspace/cache"]},
id="deps_dockerfile_line",
),
],
)
def test_config_to_docker_uv_lock_keeps_per_entry_copies_when_unsafe(setup, overrides):
with tempfile.TemporaryDirectory() as tmpdir:
project_root = pathlib.Path(tmpdir) / "single"
_write_single_uv_project(project_root)
setup(project_root)
docker = _single_uv_docker(project_root / "langgraph.json", **overrides)
assert "ADD . /deps/workspace" not in docker
assert "ADD src /deps/workspace/src" in docker
assert "ADD README.md /deps/workspace/README.md" in docker
def test_saved_and_compose_dockerfiles_keep_per_entry_copies():
with tempfile.TemporaryDirectory() as tmpdir:
project_root = pathlib.Path(tmpdir) / "single"
_write_single_uv_project(project_root)
config_path = project_root / "langgraph.json"
raw = {"graphs": {"agent": "./src/agent.py:graph"}, "source": {"kind": "uv"}}
config_path.write_text(json.dumps({"python_version": "3.11", **raw}))
compose = config_to_compose(
config_path, validate_config_file(config_path), "langchain/langgraph-api"
)
saved = project_root / "Dockerfile"
result = CliRunner().invoke(
cli, ["dockerfile", str(saved), "--config", str(config_path)]
)
assert result.exit_code == 0, result.output
for dockerfile in (compose, saved.read_text()):
assert "ADD . /deps/workspace" not in dockerfile
assert "ADD src /deps/workspace/src" in dockerfile
def test_config_to_docker_uv_lock_keeps_per_entry_copies_outside_build_context():
with tempfile.TemporaryDirectory() as tmpdir:
project_root = pathlib.Path(tmpdir) / "single"
_write_single_uv_project(project_root)
(project_root / "deploy").mkdir()
(project_root / "deploy" / "langgraph.json").write_text("{}\n")
docker = _single_uv_docker(
project_root / "deploy" / "langgraph.json",
graphs={"agent": "../src/agent.py:graph"},
source={"kind": "uv", "root": ".."},
)
assert "COPY --from=uv-workspace-root . /deps/workspace" not in docker
assert "COPY --from=uv-workspace-root src /deps/workspace/src" in docker
def test_config_to_docker_uv_lock_skips_dockerignore_entries_in_workspace():
"""Multi-member workspace: ignore patterns must filter root-level entries
AND entries encountered while recursing into directories that contain
+24
View File
@@ -1,3 +1,6 @@
import pathlib
from unittest.mock import MagicMock, patch
import pytest
from langgraph_cli.docker import (
@@ -5,6 +8,7 @@ from langgraph_cli.docker import (
DockerCapabilities,
Version,
_parse_version,
build_docker_image,
compose,
)
from langgraph_cli.util import clean_empty_lines
@@ -440,3 +444,23 @@ def test_compose_combined_mode_has_no_n_jobs():
)
def test_parse_version_w_edge_cases(input_str, expected):
assert _parse_version(input_str) == expected
def test_build_docker_image_opts_into_single_root_copy():
with (
patch(
"langgraph_cli.config.config_to_docker", return_value=("", {})
) as to_docker,
patch("langgraph_cli.docker.subp_exec"),
):
build_docker_image(
MagicMock(),
lambda _: None,
pathlib.Path("/tmp/langgraph.json"),
{"python_version": "3.11"},
base_image=None,
api_version=None,
pull=False,
tag="probe",
)
assert to_docker.call_args.kwargs["single_root_copy"] is True