Skip to content

refactor: simplify resolving js peer versions when installing#1034

Merged
justin808 merged 1 commit intoshakacode:mainfrom
G-Rath:patch-2
Mar 30, 2026
Merged

refactor: simplify resolving js peer versions when installing#1034
justin808 merged 1 commit intoshakacode:mainfrom
G-Rath:patch-2

Conversation

@G-Rath
Copy link
Copy Markdown
Contributor

@G-Rath G-Rath commented Mar 30, 2026

Summary

The current logic ends up passing the full constraint range which TIL is actually valid, but still confusing and ultimately this can be simplified.

Pull Request checklist

  • Add/update test to cover these changes
  • Update documentation
  • Update CHANGELOG file

Other Information

I came across this while checking out #1035

Summary by CodeRabbit

  • Bug Fixes
    • Improved how dependency version constraints are processed during package installation, ensuring more consistent handling of complex version specifications.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 30, 2026

Walkthrough

The template's peer dependency version normalization logic is simplified. Instead of conditionally preserving version prefixes and extracting major versions, the new approach extracts the substring after the last || separator and treats it as the installable version, changing which dependencies are categorized as regular vs. dev dependencies.

Changes

Cohort / File(s) Summary
Peer Dependency Version Normalization
lib/install/template.rb
Replaced complex version prefix handling logic with simplified substring extraction after `

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A version string once tangled tight,
With carets, tildes—quite a sight,
Now split at || with cleaner grace,
Much simpler rules win the race!
Dependencies sorted, logic refined—
A tidier template we now find. 🎀

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'refactor: simplify resolving js peer versions when installing' directly and clearly describes the main change in the changeset: simplifying the logic for resolving JavaScript peer dependency versions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 30, 2026

Greptile Summary

This PR simplifies the peer dependency version-resolution logic inside the install template. The original code conditionally branched on version string format — preserving the full constraint as-is for ^/~-prefixed versions, or extracting a bare major version for everything else. The new code unconditionally splits on || and takes the last (latest) constraint, stripped of whitespace.

What changed:

  • The 9-line conditional block in peers.each is replaced with two lines: version.split(\"|\").last.strip and the entry assignment.
  • For single-constraint versions (e.g. \"^5.76.0\"), behaviour is identical — split(\"||\") returns a one-element array so last returns the original string.
  • For multi-constraint versions (e.g. \"^4.9.2 || ^5.0.0 || ^6.0.0\" for webpack-cli), the old code passed the full || range to the package manager; the new code passes only the latest constraint (^6.0.0). Both approaches result in installing the highest compatible release on a fresh install.
  • Every version string present in lib/install/package.json is handled correctly by the new logic, including the no-space variant \"^9.0.0 || ^10.0.0|| ^11.0.0\" for compression-webpack-plugin (.strip handles the surrounding whitespace after splitting).

Minor note: The PR description ends mid-sentence ("I came across this while checking out bug"), and the CHANGELOG checklist item is left unchecked rather than crossed out — though per project guidelines refactors do not require changelog entries, so it should be struck through with ~…~ to match the other checklist items.

Confidence Score: 5/5

Safe to merge — the simplification is logically equivalent for all version strings in the current lib/install/package.json.

No P0/P1 issues found. The new two-line implementation correctly handles every version format present in the codebase. The only remaining remarks are a cosmetic PR-description nit and a checklist housekeeping item, both P2 at most.

No files require special attention.

Important Files Changed

Filename Overview
lib/install/template.rb Simplifies peer dependency version resolution by always taking the last `

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["peers.each — package + version string"] --> B["version.split('||').last.strip"]
    B --> C{Single constraint?}
    C -- "e.g. '^5.76.0'" --> D["Returns full string unchanged\ne.g. '^5.76.0'"]
    C -- "e.g. '^4.9.2 || ^5.0.0 || ^6.0.0'" --> E["Returns last constraint stripped\ne.g. '^6.0.0'"]
    D --> F["entry = 'package@version'"]
    E --> F
    F --> G{Dev dependency?}
    G -- Yes --> H["dev_dependencies_to_add"]
    G -- No --> I["dependencies_to_add"]
    H --> J["manager.add! type: :dev"]
    I --> K["manager.add! type: :production"]
Loading

Reviews (1): Last reviewed commit: "refactor: simplify resolving js peer ver..." | Re-trigger Greptile

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/install/template.rb (1)

264-266: Defensively handle empty OR-segments before composing package@version.

Line 265 can produce an empty version for malformed constraints (for example, trailing ||), which then builds an invalid entry. A tiny guard keeps installer failures explicit and easier to diagnose.

Proposed hardening
-    latest_version = version.split("||").last.strip
-    entry = "#{package}@#{latest_version}"
+    alternatives = version.to_s.split("||").map(&:strip).reject(&:empty?)
+    latest_version = alternatives.last
+    raise ArgumentError, "Invalid peer dependency constraint for #{package}: #{version.inspect}" if latest_version.nil?
+    entry = "#{package}@#{latest_version}"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/install/template.rb` around lines 264 - 266, The code composes entry =
"#{package}@#{latest_version}" using latest_version =
version.split("||").last.strip which can be empty for malformed constraints
(e.g., trailing "||"); add a defensive guard in the same scope to detect an
empty latest_version (or blank after strip) and raise or return a clear error
(or skip composing entry) so the installer fails explicitly instead of producing
an invalid package@version string; update the logic around latest_version and
entry in template.rb to validate version, handling the empty OR-segment case
before creating entry.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@lib/install/template.rb`:
- Around line 264-266: The code composes entry = "#{package}@#{latest_version}"
using latest_version = version.split("||").last.strip which can be empty for
malformed constraints (e.g., trailing "||"); add a defensive guard in the same
scope to detect an empty latest_version (or blank after strip) and raise or
return a clear error (or skip composing entry) so the installer fails explicitly
instead of producing an invalid package@version string; update the logic around
latest_version and entry in template.rb to validate version, handling the empty
OR-segment case before creating entry.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7776d00d-38ad-4634-830a-9375bfb9d6c2

📥 Commits

Reviewing files that changed from the base of the PR and between aacbb99 and e76567e.

📒 Files selected for processing (1)
  • lib/install/template.rb

@justin808 justin808 merged commit bfc28c5 into shakacode:main Mar 30, 2026
53 checks passed
@justin808
Copy link
Copy Markdown
Member

Thanks @G-Rath!

@G-Rath G-Rath deleted the patch-2 branch March 30, 2026 22:05
justin808 added a commit that referenced this pull request Apr 30, 2026
* origin/main: (22 commits)
  docs: add Dependabot configuration guide (#1094)
  Sync address-review prompt with upstream PR #16 (#1098)
  Supersede #910: entry shape test with lint unblock (#919)
  fix: align rspack v2 peer deps and installer defaults (#1091)
  docs: update README and guides for Shakapacker v10 (#1092)
  Release 10.0.0
  Update CHANGELOG.md for v10.0.0 (#1089)
  Release 10.0.0-rc.1
  Update CHANGELOG.md for v10.0.0-rc.1 (#1087)
  Supersede #961 by using pack-config-diff (#973)
  Add final summary output to rake release (#1041)
  Add bin/setup to install development deps (#1039)
  Release 10.0.0-rc.0
  Use npx release-it to avoid mise shim failures (#1040)
  Fix Nokogiri build failure on Ruby 3.4.6 (#1038)
  Update CHANGELOG.md for v10.0.0-rc.0 (#1037)
  Update rspack dev deps to 2.0.0-rc.0 (#1036)
  Fix stale and broken documentation across Shakapacker guides (#1023)
  Allow webpack-cli v7 in peer dependencies (#1021)
  refactor: simplify resolving js peer versions when installing (#1034)
  ...

# Conflicts:
#	package.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants