Skip to content

Don't crash on bin/importmap pin with no arguments#326

Open
ajaynomics wants to merge 1 commit into
rails:mainfrom
ajaynomics:fix/317
Open

Don't crash on bin/importmap pin with no arguments#326
ajaynomics wants to merge 1 commit into
rails:mainfrom
ajaynomics:fix/317

Conversation

@ajaynomics

Copy link
Copy Markdown

What

bin/importmap pin with no package arguments raised NoMethodError: undefined method 'each' for nil instead of printing a friendly message.

Why

On a pin with no packages, the JSPM service returns a 200 whose body has no map.imports. Importmap::Packager#extract_parsed_response builds { imports: parsed.dig("map", "imports") }, so the result is a truthy hash whose :imports value is nil. Importmap::Commands#for_each_import only guarded against a nil response, so the truthy hash sailed past the if response check straight into nil.each and crashed.

How

Tighten the guard from if response to if response && response[:imports].present?. An empty/missing-imports result now routes through the existing handle_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 nil from extract_parsed_response when imports is nil (pushing the guard down to where the { imports: nil } shape is constructed). I kept the guard at the call site since for_each_import is 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/importmap subprocess against live jspm.io, matching the md5@2.2.0/pristine tests) asserting that both bin/importmap pin and bin/importmap unpin with no arguments print Couldn't find any packages instead of raising. Both fail on main with the NoMethodError and pass with this patch — the same for_each_import guard protects both commands.

Fixes #317

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
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.

bin/importmap pin without arguments is raising an exception

1 participant