From 1121a334ff8a75da36d45f6714f0dab61b2547fd Mon Sep 17 00:00:00 2001 From: "S.Sandhu" <167903774+sachin-sandhu@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:47:36 -0400 Subject: [PATCH] Fixes unhandled couldn't find any versions for package exception (#10491) * fixes instance of package version not found error --- npm_and_yarn/lib/dependabot/npm_and_yarn.rb | 15 +++++++++++++++ .../npm_and_yarn/yarn_error_handler_spec.rb | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/npm_and_yarn/lib/dependabot/npm_and_yarn.rb b/npm_and_yarn/lib/dependabot/npm_and_yarn.rb index ef45014bf7..c6ffd04914 100644 --- a/npm_and_yarn/lib/dependabot/npm_and_yarn.rb +++ b/npm_and_yarn/lib/dependabot/npm_and_yarn.rb @@ -126,6 +126,9 @@ module NpmAndYarn YARNRC_ENOENT = /Internal Error: ENOENT/ YARNRC_ENOENT_REGEX = /Internal Error: ENOENT: no such file or directory, stat '(?.*?)'/ + # if not package found with specified version + YARN_PACKAGE_NOT_FOUND = /MessageError: Couldn't find any versions for "(?.*?)" that matches "(?.*?)"/ + YN0001_FILE_NOT_RESOLVED_CODES = T.let({ FIND_PACKAGE_LOCATION: /YN0001: UsageError: Couldn't find the (?.*) state file/, NO_CANDIDATE_FOUND: /YN0001: Error: (?.*): No candidates found/, @@ -477,6 +480,18 @@ def self.sanitize_resolvability_message(error_message, dependencies, yarn_lock) }, in_usage: false, matchfn: nil + }, + { + patterns: [YARN_PACKAGE_NOT_FOUND], + handler: lambda { |message, _error, _params| + package_name = message.match(YARN_PACKAGE_NOT_FOUND).named_captures["pkg"] + version = message.match(YARN_PACKAGE_NOT_FOUND).named_captures["ver"] + + Dependabot::InconsistentRegistryResponse.new("Couldn't find any versions for \"#{package_name}\" that " \ + "matches \"#{version}\"") + }, + in_usage: false, + matchfn: nil } ].freeze, T::Array[{ diff --git a/npm_and_yarn/spec/dependabot/npm_and_yarn/yarn_error_handler_spec.rb b/npm_and_yarn/spec/dependabot/npm_and_yarn/yarn_error_handler_spec.rb index abb24d1b73..24d994f2b7 100644 --- a/npm_and_yarn/spec/dependabot/npm_and_yarn/yarn_error_handler_spec.rb +++ b/npm_and_yarn/spec/dependabot/npm_and_yarn/yarn_error_handler_spec.rb @@ -314,6 +314,21 @@ end end + context "when the error message contains no specified version for package error" do + let(:error_message) do + "MessageError: Couldn't find any versions for \"@types/react-test-renderer\" that matches \"~18.2.0\"" \ + "at /opt/npm_and_yarn/node_modules/@dependabot/yarn-lib/lib/resolvers/registries/npm-resolver.js:120:13 + at Generator.next ()" + end + + it "raises a DependencyFileNotResolvable error with the correct message" do + expect { error_handler.handle_error(error, { yarn_lock: yarn_lock }) } + .to raise_error(Dependabot::InconsistentRegistryResponse, + "Couldn't find any versions for \"@types/react-test-renderer\" that " \ + "matches \"~18.2.0\"") + end + end + context "when the error message contains YN0001 response (could not read Username)" do let(:error_message) do "➤ YN0000: ┌ Resolution step