Skip to content

The agent that grows with you

fix(install): installers keep the root lockfile when a workspace manifest is dirty

teknium1ba153d696928
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 6e64bc79c94..2f3a865ed32 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -513,15 +513,38 @@ function Discard-LockfileChurn {
)
foreach ($path in $diff) {
if ($path -like "*package.json") {
- $null = $dirtyPackageDirs.Add((Split-Path $path -Parent))
+ $null = $dirtyPackageDirs.Add(((Split-Path $path -Parent) -replace '\\', '/'))
+ }
+ }
+
+ # The single root lockfile records every workspace's specs (root package.json
+ # "workspaces" globs), so a dirty workspace manifest such as
+ # apps/desktop/package.json protects it; reverting it there desyncs spec and
+ # lock and every later npm ci fails (#112378). A manifest outside the graph
+ # (website/) has its own lockfile and does not protect the root one.
+ $rootLockProtected = $dirtyPackageDirs.Contains("")
+ if (-not $rootLockProtected -and $dirtyPackageDirs.Count -gt 0) {
+ $workspaceGlobs = @()
+ try {
+ $rootPkg = Get-Content (Join-Path $Repo "package.json") -Raw | ConvertFrom-Json
+ $ws = $rootPkg.workspaces
+ if ($ws -and $ws.PSObject.Properties["packages"]) { $ws = $ws.packages }
+ $workspaceGlobs = @($ws | Where-Object { $_ })
+ } catch { }
+ foreach ($dir in $dirtyPackageDirs) {
+ foreach ($glob in $workspaceGlobs) {
+ if ($dir -like ([string]$glob)) { $rootLockProtected = $true }
+ }
}
}
$dirtyLocks = [System.Collections.Generic.List[string]]::new()
foreach ($path in $diff) {
if ($path -notlike "*package-lock.json") { continue }
- $lockDir = Split-Path $path -Parent
- if ($dirtyPackageDirs.Contains($lockDir)) { continue }
+ $lockDir = (Split-Path $path -Parent) -replace '\\', '/'
+ if ($lockDir -eq "") {
+ if ($rootLockProtected) { continue }
+ } elseif ($dirtyPackageDirs.Contains($lockDir)) { continue }
$dirtyLocks.Add($path)
}
diff --git a/scripts/install.sh b/scripts/install.sh
index 1f2635673c1..2b54281bbab 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -281,10 +281,29 @@ discard_update_lockfile_churn() {
[ -n "$dirty_diff" ] || return 0
local dirty_package_dirs=""
+ local root_lock_protected=0
while IFS= read -r path; do
case "$path" in
*package.json)
- dirty_package_dirs="${dirty_package_dirs}$(dirname "$path")"$'\n'
+ local pkg_dir
+ pkg_dir=$(dirname "$path")
+ dirty_package_dirs="${dirty_package_dirs}${pkg_dir}"$'\n'
+ # The single root lockfile records every workspace's specs (root
+ # package.json "workspaces" globs), so a dirty workspace manifest
+ # such as apps/desktop/package.json protects it; reverting it there
+ # desyncs spec and lock and every later npm ci fails (#112378). A
+ # manifest outside the graph (website/) has its own lockfile.
+ if [ "$pkg_dir" = "." ]; then
+ root_lock_protected=1
+ else
+ local ws_glob
+ for ws_glob in $(sed -n '/"workspaces"[[:space:]]*:/,/\]/p' "$repo/package.json" 2>/dev/null \
+ | grep -o '"[^"]*"' | tr -d '"' | grep -v -e '^workspaces$' -e '^packages$'); do
+ case "$pkg_dir" in
+ $ws_glob) root_lock_protected=1 ;;
+ esac
+ done
+ fi
;;
esac
done <<EOF
@@ -298,9 +317,13 @@ EOF
*package-lock.json)
local lock_dir
lock_dir=$(dirname "$path")
- case $'\n'"$dirty_package_dirs" in
- *$'\n'"$lock_dir"$'\n'*) continue ;;
- esac
+ if [ "$lock_dir" = "." ]; then
+ [ "$root_lock_protected" -eq 0 ] || continue
+ else
+ case $'\n'"$dirty_package_dirs" in
+ *$'\n'"$lock_dir"$'\n'*) continue ;;
+ esac
+ fi
dirty_locks="${dirty_locks}${path}"$'\n'
dirty_count=$((dirty_count + 1))
;;
diff --git a/tests/scripts/install/test_install_lockfile_churn.py b/tests/scripts/install/test_install_lockfile_churn.py
index 0c02f636e67..97187add25e 100644
--- a/tests/scripts/install/test_install_lockfile_churn.py
+++ b/tests/scripts/install/test_install_lockfile_churn.py
@@ -91,6 +91,41 @@ def test_install_sh_discards_runtime_lockfile_churn_before_stash(
assert (repo / "package-lock.json").read_text() == '{"lock":"old"}\n'
[email protected]_system_guard_bypass
+def test_install_sh_keeps_root_lockfile_for_dirty_workspace_manifest(
+ tmp_path: Path,
+) -> None:
+ """The root lockfile spans the npm workspace graph: a dirty workspace manifest
+ (apps/desktop/package.json) protects it, a manifest outside the graph does not (#112378)."""
+ repo = tmp_path / "hermes-agent"
+ (repo / "apps" / "desktop").mkdir(parents=True)
+ (repo / "vendor" / "foo").mkdir(parents=True)
+ _git(repo, "init")
+ (repo / "package.json").write_text('{"workspaces": ["apps/*"]}\n')
+ (repo / "package-lock.json").write_text('{"lock":"old"}\n')
+ (repo / "apps" / "desktop" / "package.json").write_text("{}\n")
+ (repo / "vendor" / "foo" / "package.json").write_text("{}\n")
+ _git(repo, "add", ".")
+ _git(repo, "commit", "-m", "init")
+ script = (
+ 'log_info() { echo "INFO: $*"; }\n'
+ 'INSTALL_DIR="$PWD"\n'
+ f"{_extract_install_sh_function('discard_update_lockfile_churn')}\n"
+ 'discard_update_lockfile_churn "$PWD"\n'
+ )
+
+ (repo / "apps" / "desktop" / "package.json").write_text('{"dependencies":{"electron":"41"}}\n')
+ (repo / "package-lock.json").write_text('{"lock":"workspace-bump"}\n')
+ subprocess.run(["bash", "-c", script], cwd=repo, check=True, capture_output=True)
+ assert (repo / "package-lock.json").read_text() == '{"lock":"workspace-bump"}\n'
+
+ _git(repo, "checkout", "--", ".")
+ (repo / "vendor" / "foo" / "package.json").write_text('{"dependencies":{"x":"1"}}\n')
+ (repo / "package-lock.json").write_text('{"lock":"churn"}\n')
+ subprocess.run(["bash", "-c", script], cwd=repo, check=True, capture_output=True)
+ assert (repo / "package-lock.json").read_text() == '{"lock":"old"}\n'
+
+
def test_install_sh_discards_lockfile_churn_before_status_probe() -> None:
text = INSTALL_SH.read_text()
idx_cleanup = text.index('discard_update_lockfile_churn "$INSTALL_DIR"')