fix(scripts): regenerate package-lock.json via npm instead of sed#2639
Conversation
Fixes the issue where set-project-version.sh incorrectly replaced library dependency versions in package-lock.json files that happened to match the current project version. Changes: - Remove package-lock.json files from the sed replacement loop - Exclude node_modules from find commands for package.json - Add a new step that runs 'npm install --package-lock-only' in each directory containing a package-lock.json to correctly regenerate the lock file without touching library versions Co-authored-by: kthoms <265597+kthoms@users.noreply.github.com> Agent-Logs-Url: https://github.com/operaton/operaton/sessions/7883b528-117e-4def-b7f1-d49cb000503f
There was a problem hiding this comment.
Pull request overview
This PR updates the release/version-bump automation to avoid corrupting dependency versions in package-lock.json by no longer editing lockfiles with sed, and instead regenerating them through npm.
Changes:
- Update only
package.jsonversions via text replacement (excludepackage-lock.json). - Exclude
node_moduleswhen discoveringpackage.json/package-lock.jsonfiles. - Regenerate each
package-lock.jsonusingnpm install --package-lock-onlyin the corresponding directory.
| PACKAGE_JSON_FILES=($(find . -name package.json -not -path "*/node_modules/*")) | ||
|
|
||
| for PACKAGE_JSON_FILE in "${PACKAGE_JSON_FILES[@]}"; do | ||
| sed -i '' -e "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" $PACKAGE_JSON_FILE | ||
| done | ||
|
|
||
| echo "🔄 Regenerating package-lock.json files via npm install" | ||
| PACKAGE_LOCK_FILES=($(find . -name package-lock.json -not -path "*/node_modules/*")) |
There was a problem hiding this comment.
PACKAGE_JSON_FILES=($(find ...)) relies on word-splitting and will break if any path contains whitespace/newlines. Consider using a NUL-delimited find ... -print0 with mapfile -d '' -t (or a while IFS= read -r -d '' loop) to populate the array safely.
| PACKAGE_JSON_FILES=($(find . -name package.json -not -path "*/node_modules/*")) | |
| for PACKAGE_JSON_FILE in "${PACKAGE_JSON_FILES[@]}"; do | |
| sed -i '' -e "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" $PACKAGE_JSON_FILE | |
| done | |
| echo "🔄 Regenerating package-lock.json files via npm install" | |
| PACKAGE_LOCK_FILES=($(find . -name package-lock.json -not -path "*/node_modules/*")) | |
| PACKAGE_JSON_FILES=() | |
| while IFS= read -r -d '' PACKAGE_JSON_FILE; do | |
| PACKAGE_JSON_FILES+=("$PACKAGE_JSON_FILE") | |
| done < <(find . -name package.json -not -path "*/node_modules/*" -print0) | |
| for PACKAGE_JSON_FILE in "${PACKAGE_JSON_FILES[@]}"; do | |
| sed -i '' -e "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" "$PACKAGE_JSON_FILE" | |
| done | |
| echo "🔄 Regenerating package-lock.json files via npm install" | |
| PACKAGE_LOCK_FILES=() | |
| while IFS= read -r -d '' PACKAGE_LOCK_FILE; do | |
| PACKAGE_LOCK_FILES+=("$PACKAGE_LOCK_FILE") | |
| done < <(find . -name package-lock.json -not -path "*/node_modules/*" -print0) |
| for PACKAGE_JSON_FILE in "${PACKAGE_JSON_FILES[@]}"; do | ||
| sed -i '' -e "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" $PACKAGE_JSON_FILE | ||
| done |
There was a problem hiding this comment.
The in-place edit uses sed -i '', which is BSD/macOS-specific; GNU sed (Ubuntu runners) treats the empty string as a positional arg and fails. Since this script is invoked from GitHub Actions on Ubuntu, consider a portable helper that selects sed -i''/sed -i for GNU sed and sed -i '' for BSD sed.
| echo "🔄 Regenerating package-lock.json files via npm install" | ||
| PACKAGE_LOCK_FILES=($(find . -name package-lock.json -not -path "*/node_modules/*")) | ||
|
|
||
| for PACKAGE_LOCK_FILE in "${PACKAGE_LOCK_FILES[@]}"; do | ||
| PACKAGE_LOCK_DIR=$(dirname "$PACKAGE_LOCK_FILE") | ||
| echo " Running npm install --package-lock-only in $PACKAGE_LOCK_DIR" | ||
| (cd "$PACKAGE_LOCK_DIR" && npm install --package-lock-only) |
There was a problem hiding this comment.
This script now depends on npm being available and on a consistent npm version to avoid lockfile churn. Consider adding an explicit command -v npm check with a clear error, and (for CI) pass --no-audit --no-fund (and optionally --ignore-scripts) to make lock regeneration faster and less noisy.
|


set-project-version.shusedsedto replace"version": "<current>"across allpackage.jsonandpackage-lock.jsonfiles. Sincepackage-lock.jsonstores exact versions for every resolved dependency, any npm package that coincidentally shares the project version gets corrupted — e.g.picocolor@1.1.1becamepicocolor@1.1.2-SNAPSHOTwhen the project was bumped from1.1.1.Changes
package-lock.jsonfrom thesedloop — onlypackage.jsonfiles are updated via text replacementnode_modulesfromfindwhen collectingpackage.jsonfilesnpm install --package-lock-only— runs in each directory that owns apackage-lock.jsonafter itspackage.jsonis updated, letting npm produce a correct lock file without touching dependency versionsOriginal prompt
⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.