Skip to content

Commit

Permalink
Raise user error when Yarn is misconfigured
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Nov 29, 2023
1 parent 6491e2e commit 7214306
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bin/dry-run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ def handle_dependabot_error(error:)
"error-type": "git_dependencies_not_reachable",
"error-detail": { "dependency-urls": error.dependency_urls }
}
when Dependabot::MisconfiguredTooling
{
"error-type": "misconfigured_tooling",
"error-detail": { "tool-name": error.tool_name, "message": error.message }
}
when Dependabot::GitDependencyReferenceNotFound
{
"error-type": "git_dependency_reference_not_found",
Expand Down
21 changes: 21 additions & 0 deletions common/lib/dependabot/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ def initialize(source, msg = nil)
# File level errors #
#####################

class MisconfiguredTooling < DependabotError
extend T::Sig

sig { returns(String) }
attr_reader :tool_name

sig do
params(
tool_name: String,
msg: String
).void
end
def initialize(tool_name, msg)
@tool_name = tool_name

msg = "Dependabot detected that #{tool_name} is misconfigured in this repository. " \
"Running `#{tool_name.downcase}` results in the following error: #{msg}"
super(msg)
end
end

class ToolVersionNotSupported < DependabotError
extend T::Sig

Expand Down
7 changes: 7 additions & 0 deletions npm_and_yarn/lib/dependabot/npm_and_yarn/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
module Dependabot
module NpmAndYarn
module Helpers
YARN_PATH_NOT_FOUND = /^.*(?<error>The "yarn-path" option has been set.*)/

def self.npm_version(lockfile_content)
"npm#{npm_version_numeric(lockfile_content)}"
end
Expand Down Expand Up @@ -72,6 +74,11 @@ def self.yarn_major_version
retry
end

if YARN_PATH_NOT_FOUND.match?(message)
error = T.must(T.must(YARN_PATH_NOT_FOUND.match(message))[:error]).gsub(Dir.pwd, ".")
raise MisconfiguredTooling.new("Yarn", error)
end

raise
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@
it { is_expected.to eq(latest_allowable_version) }
end

context "with a misconfigured yarn-berry project" do
let(:project_name) { "yarn_berry/misconfigured" }
let(:latest_allowable_version) { Gem::Version.new("1.3.0") }
let(:dependency) do
Dependabot::Dependency.new(
name: "left-pad",
version: "1.0.1",
requirements: [{
file: "package.json",
requirement: "^1.0.1",
groups: ["dependencies"],
source: { type: "registry", url: "https://registry.npmjs.org" }
}],
package_manager: "npm_and_yarn"
)
end

it "raises a Dependabot::MisconfiguredTooling error" do
expect { subject }.to raise_error(Dependabot::MisconfiguredTooling)
end
end

context "with an npm 8 package-lock.json using the v3 lockfile format" do
context "updating a dependency without peer dependency issues" do
let(:project_name) { "npm8/package-lock-v3" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-3.5.1.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "original",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"is-positive": "3.1.0",
"left-pad": "^1.0.1"
},
"packageManager": "yarn@3.5.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 6
cacheKey: 8

"is-positive@npm:3.1.0":
version: 3.1.0
resolution: "is-positive@npm:3.1.0"
checksum: 3675229110735f470860b5fe0740dae1baa9d6f7c6a69f25116aa1888cbc3d092f068f6f94219c290022d0e334f1c82db3041286236ae8a8e132146fd7bcf99d
languageName: node
linkType: hard

"left-pad@npm:^1.0.1":
version: 1.3.0
resolution: "left-pad@npm:1.3.0"
checksum: 13fa96e17b70a54836490de22d4bab706e2ed508338bbabecfac72ecce445a74139c5b009a8112252cab8fc4ab7ac4ebd870e5b35bd236b443b12be96f8745ac
languageName: node
linkType: hard

"original@workspace:.":
version: 0.0.0-use.local
resolution: "original@workspace:."
dependencies:
is-positive: 3.1.0
left-pad: ^1.0.1
languageName: unknown
linkType: soft
5 changes: 5 additions & 0 deletions updater/lib/dependabot/update_files_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def handle_parser_error(error)
"error-type": "git_dependencies_not_reachable",
"error-detail": { "dependency-urls": error.dependency_urls }
}
when Dependabot::MisconfiguredTooling
{
"error-type": "misconfigured_tooling",
"error-detail": { "tool-name": error.tool_name, message: error.message }
}
when Dependabot::NotImplemented
{
"error-type": "not_implemented",
Expand Down

0 comments on commit 7214306

Please sign in to comment.