Don't crash on bin/importmap pin with no arguments#326
Open
ajaynomics wants to merge 1 commit into
Open
Conversation
When `pin` is called without any packages, the JSPM service responds
with a map that has no imports, so `packager.import` returns
`{ imports: nil }`. `for_each_import` only guarded against a nil
response, so it then called `each` on nil and raised a NoMethodError.
Check that the imports are present before iterating so an empty result
routes to the existing not-found handler, matching the behavior of
`pin` with unknown packages.
Fixes rails#317
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
bin/importmap pinwith no package arguments raisedNoMethodError: undefined method 'each' for nilinstead of printing a friendly message.Why
On a
pinwith no packages, the JSPM service returns a200whose body has nomap.imports.Importmap::Packager#extract_parsed_responsebuilds{ imports: parsed.dig("map", "imports") }, so the result is a truthy hash whose:importsvalue isnil.Importmap::Commands#for_each_importonly guarded against a nilresponse, so the truthy hash sailed past theif responsecheck straight intonil.eachand crashed.How
Tighten the guard from
if responsetoif response && response[:imports].present?. An empty/missing-imports result now routes through the existinghandle_package_not_found, giving the same UX as pinning an unknown package. This is consistent with the.blank?usage already present in that method, and stays a single line.I considered the alternative of returning
nilfromextract_parsed_responsewhenimportsis nil (pushing the guard down to where the{ imports: nil }shape is constructed). I kept the guard at the call site sincefor_each_importis the only consumer and the one-line check reads clearly there, but I'm happy to move it if preferred.Test
Added regression tests in the existing house style (real
bin/importmapsubprocess against live jspm.io, matching themd5@2.2.0/pristinetests) asserting that bothbin/importmap pinandbin/importmap unpinwith no arguments printCouldn't find any packagesinstead of raising. Both fail onmainwith theNoMethodErrorand pass with this patch — the samefor_each_importguard protects both commands.Fixes #317