Skip to content

The agent that grows with you

fix(update): preserve root lockfile for dirty workspace manifests

joaomarcosc115c55aa027
diff --git a/hermes_cli/update_cmd_git.py b/hermes_cli/update_cmd_git.py
index b479cb8373f..37bdb8d7ad6 100644
--- a/hermes_cli/update_cmd_git.py
+++ b/hermes_cli/update_cmd_git.py
@@ -421,17 +421,44 @@ def _ensure_non_trampoline_git(git_cmd: list) -> list:
return [str(real_git)] + list(git_cmd[1:])
+def _npm_lockfile_owners(repo_root: Path) -> set[Path]:
+ """Return manifest directories covered by the root npm lockfile."""
+ owners = {Path(".")}
+ try:
+ import json
+ package = json.loads((repo_root / "package.json").read_text(encoding="utf-8"))
+ workspaces = package.get("workspaces", [])
+ if isinstance(workspaces, dict):
+ workspaces = workspaces.get("packages", [])
+ for pattern in workspaces:
+ for directory in repo_root.glob(str(pattern)):
+ if (directory / "package.json").is_file():
+ owners.add(directory.relative_to(repo_root))
+ except (OSError, ValueError, TypeError):
+ pass
+ return owners
+
+
def _discard_lockfile_churn(git_cmd, repo_root):
- """Restore ``package-lock.json`` files npm rewrote non-deterministically, so the update sees a clean tree
- instead of autostashing every run. Only touches lockfiles whose package.json is NOT also dirty. Best-effort."""
+ """Restore npm lockfile churn unless its owning manifest is dirty."""
from hermes_cli.update_cmd import _git_run
with suppress(Exception):
diff = _git_run(git_cmd, ["diff", "--name-only"], repo_root)
if diff.returncode != 0:
return
changed = [line.strip() for line in diff.stdout.splitlines()]
- dirty_package_dirs = {Path(p).parent for p in changed if p.endswith("package.json")}
- dirty = [p for p in changed if p.endswith("package-lock.json") and Path(p).parent not in dirty_package_dirs]
+ dirty_manifests = {Path(p).parent for p in changed if p.endswith("package.json")}
+ root_owners = _npm_lockfile_owners(Path(repo_root))
+ dirty = []
+ for path in changed:
+ if not path.endswith("package-lock.json"):
+ continue
+ lock_dir = Path(path).parent
+ protected = (lock_dir == Path(".") and bool(dirty_manifests & root_owners)) or (
+ lock_dir != Path(".") and lock_dir in dirty_manifests
+ )
+ if not protected:
+ dirty.append(path)
if not dirty:
return
_git_run(git_cmd, ["checkout", "--", *dirty], repo_root)
diff --git a/tests/hermes_cli/test_discard_lockfile_churn.py b/tests/hermes_cli/test_discard_lockfile_churn.py
new file mode 100644
index 00000000000..89b87d2612d
--- /dev/null
+++ b/tests/hermes_cli/test_discard_lockfile_churn.py
@@ -0,0 +1,77 @@
+"""Behavioral tests for npm lockfile ownership cleanup (#112378)."""
+
+import json
+import subprocess
+from pathlib import Path
+
+from hermes_cli.update_cmd_git import _discard_lockfile_churn
+
+
+def _repo(tmp_path):
+ subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)
+ (tmp_path / "package.json").write_text(json.dumps({"workspaces": ["apps/*", "ui-tui", "web"]}))
+ (tmp_path / "package-lock.json").write_text("root clean\n")
+ (tmp_path / "apps" / "desktop").mkdir(parents=True)
+ (tmp_path / "apps" / "desktop" / "package.json").write_text("desktop clean\n")
+ (tmp_path / "ui-tui").mkdir()
+ (tmp_path / "ui-tui" / "package.json").write_text("tui clean\n")
+ subprocess.run(["git", "add", "."], cwd=tmp_path, check=True)
+ subprocess.run(["git", "commit", "-qm", "initial"], cwd=tmp_path, check=True)
+ return tmp_path
+
+
+def _dirty(path, text="dirty\n"):
+ path.write_text(text)
+
+
+def _changed(repo):
+ return subprocess.run(["git", "diff", "--name-only"], cwd=repo, text=True, capture_output=True, check=True).stdout.splitlines()
+
+
+def test_dirty_workspace_manifest_preserves_root_lock(tmp_path):
+ repo = _repo(tmp_path)
+ _dirty(repo / "apps/desktop/package.json")
+ _dirty(repo / "package-lock.json")
+ _discard_lockfile_churn(["git"], repo)
+ assert "package-lock.json" in _changed(repo)
+
+
+def test_lock_only_churn_is_discarded(tmp_path):
+ repo = _repo(tmp_path)
+ _dirty(repo / "package-lock.json")
+ _discard_lockfile_churn(["git"], repo)
+ assert "package-lock.json" not in _changed(repo)
+
+
+def test_dirty_root_manifest_preserves_root_lock(tmp_path):
+ repo = _repo(tmp_path)
+ _dirty(repo / "package.json")
+ _dirty(repo / "package-lock.json")
+ _discard_lockfile_churn(["git"], repo)
+ assert set(_changed(repo)) == {"package-lock.json", "package.json"}
+
+
+def test_sibling_workspace_manifest_preserves_root_lock(tmp_path):
+ repo = _repo(tmp_path)
+ _dirty(repo / "ui-tui/package.json")
+ _dirty(repo / "package-lock.json")
+ _discard_lockfile_churn(["git"], repo)
+ assert "package-lock.json" in _changed(repo)
+
+
+def test_unrelated_nested_lock_is_discarded(tmp_path):
+ repo = _repo(tmp_path)
+ nested = repo / "ui-tui/package-lock.json"
+ _dirty(nested)
+ _discard_lockfile_churn(["git"], repo)
+ assert "ui-tui/package-lock.json" not in _changed(repo)
+
+
+def test_non_workspace_manifest_does_not_protect_root_lock(tmp_path):
+ repo = _repo(tmp_path)
+ vendor = repo / "vendor/foo"
+ vendor.mkdir(parents=True)
+ _dirty(vendor / "package.json")
+ _dirty(repo / "package-lock.json")
+ _discard_lockfile_churn(["git"], repo)
+ assert "package-lock.json" not in _changed(repo)