refactor: simplify resolving js peer versions when installing#1034
refactor: simplify resolving js peer versions when installing#1034justin808 merged 1 commit intoshakacode:mainfrom
Conversation
WalkthroughThe 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 Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Greptile SummaryThis 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 What changed:
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 Confidence Score: 5/5Safe to merge — the simplification is logically equivalent for all version strings in the current 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
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"]
Reviews (1): Last reviewed commit: "refactor: simplify resolving js peer ver..." | Re-trigger Greptile |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/install/template.rb (1)
264-266: Defensively handle empty OR-segments before composingpackage@version.Line 265 can produce an empty version for malformed constraints (for example, trailing
||), which then builds an invalidentry. 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
📒 Files selected for processing (1)
lib/install/template.rb
|
Thanks @G-Rath! |
* 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
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 changesUpdate documentationUpdate CHANGELOG fileOther Information
I came across this while checking out #1035
Summary by CodeRabbit