Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle String response for "find latest version" #246

Conversation

vietqhoang
Copy link
Contributor

@vietqhoang vietqhoang commented Feb 14, 2024

Closes #245

Problem

It is expected NPM to return a Hash for the response.

latest_version = response.dig('dist-tags', 'latest')

But for whatever reason the response can now also return a String, which is not accounted for in npm.rb.

See the referenced issue for further context.

Expectation

The importmap outdated command should handle the problem more gracefully. In this case, a reasonable expectation is to surface the String response.

Solution

How to handle is up for discussion.

Just to get the fix process started the propose change is to surface the String in the response as latest_version since it isn't clear what the contents of the String could be.

Using the fork with change on our production app which has the problematic pinned dependency, the following outdated is now returned.

| Package                | Current | Latest                 |
|------------------------|---------|------------------------|
| @floating-ui/utils/dom | 0.2.1   | version not found: dom |
  1 outdated package found

See rails#245 for context.

The TL;DR is the response sometimes returns a String instead of Hash object. This should be handled properly.

The way to handle this is up for discussion. To start addressing the issue the String response will just be surfaced as `latest_version`.
@vietqhoang
Copy link
Contributor Author

Not exactly sure why the CI checks are failing, but it seems to have nothing to do with the proposed changes.

We only care to assert on the `latest_version` since this is what we care about. Can revert the change if it is desired to assert the rest.
@vietqhoang vietqhoang changed the title Handle string response for "find latest version" Handle String response for "find latest version" Feb 14, 2024
@@ -86,7 +86,7 @@ def get_json(uri)
end

def find_latest_version(response)
latest_version = response.dig('dist-tags', 'latest')
latest_version = response.is_a?(String) ? response : response.dig('dist-tags', 'latest')
Copy link
Contributor Author

@vietqhoang vietqhoang Feb 15, 2024

Choose a reason for hiding this comment

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

The response may be a blank String. One reported issue (#245 (comment)) made me question this possibility.

If this is the case then a fallback may be worthwhile. I am writing it with available Rails methods, but can change it to use Ruby-only accessible ones.

Suggested change
latest_version = response.is_a?(String) ? response : response.dig('dist-tags', 'latest')
latest_version = response.is_a?(String) ? (response.presence || 'unknown') : response.dig('dist-tags', 'latest')

@rbclark
Copy link

rbclark commented Apr 1, 2024

Any chance this fix could be merged or at least considered? Without this it's making it very hard for us to figure out which JS dependencies are out of date since we were relying heavily on the importmap outdated command to list them out for us.

@rafaelfranca rafaelfranca merged commit 1d93e41 into rails:main Apr 19, 2024
0 of 21 checks passed
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 outdated command fails with undefined method `dig' for an instance of String
3 participants