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

Improved error message for version restrictions #633

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,6 @@ rescue ex : Shards::ParseError
ex.to_s(STDERR)
exit 1
rescue ex : Shards::Error
Shards::Log.error { ex.message }
Shards::Log.error { ex.message.to_s }
exit 1
end
44 changes: 43 additions & 1 deletion src/commands/command.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "../lock"
require "../spec"
require "../override"
require "levenshtein"

module Shards
abstract class Command
Expand Down Expand Up @@ -73,8 +74,49 @@ module Shards
Shards::Lock.write(packages, override_path, LOCK_FILENAME)
end

def handle_resolver_errors(&)
private def log_available_tags(conflicts)
String.build do |str|
shard_source_dependencys = conflicts.flat_map { |k, v| v.requirements.flat_map { |source, deps| deps.map { |dep| {k, source, dep} } } }
if shard_source_dependencys.size > 1
str << "Unable to satisfy the following requirements:\n\n"
shard_source_dependencys.each do |shard, source, dependency|
str << "- `#{shard} (#{dependency.requirement})` required by `#{source}`\n"
end
else
str << "Unable to satisfy the following requirement:\n\n"
shard_source_dependencys.each do |shard, source, dependency|
resolver = dependency.resolver
tags = resolver.available_tags.reverse!.first(5)
releases = resolver.available_releases.map(&.to_s).reverse
req = dependency.requirement

str << "- `#{shard} (#{req})` required by `#{source}`: "
if releases.empty?
str << "It doesn't have any release. "
if tags.empty?
str << "And it doesn't have any tags either."
else
str << "These are the latest tags: #{tags.join(", ")}."
end
elsif req.is_a?(Version) || (req.is_a?(VersionReq) && req.patterns.size == 1 && req.patterns[0] !~ /^(<|>|=)/)
req = req.to_s
found = Levenshtein.find(req, releases, 6) || "none"
info = "These are the latest tags: #{tags.join(", ")}."
str << "The closest available release to #{req} is: #{found}. #{info}"
else
str << "The last available releases are #{releases.first(5).join(", ")}."
end
str << "\n"
end
end
end
end

def handle_resolver_errors(solver, &)
yield
rescue e : Molinillo::VersionConflict(Shards::Dependency, Shards::Spec)
Log.error { log_available_tags(e.conflicts) }
raise Shards::Error.new("Failed to resolve dependencies")
rescue e : Molinillo::ResolverError
Log.error { e.message }
raise Shards::Error.new("Failed to resolve dependencies")
Expand Down
2 changes: 1 addition & 1 deletion src/commands/install.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Shards

solver.prepare(development: Shards.with_development?)

packages = handle_resolver_errors { solver.solve }
packages = handle_resolver_errors(solver) { solver.solve }

if Shards.frozen?
validate(packages)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/lock.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Shards

solver.prepare(development: Shards.with_development?)

packages = handle_resolver_errors { solver.solve }
packages = handle_resolver_errors(solver) { solver.solve }
return if packages.empty?

if print
Expand Down
2 changes: 1 addition & 1 deletion src/commands/outdated.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Shards
solver = MolinilloSolver.new(spec, override, prereleases: @prereleases)
solver.prepare(development: Shards.with_development?)

packages = handle_resolver_errors { solver.solve }
packages = handle_resolver_errors(solver) { solver.solve }
packages.each { |package| analyze(package) }

if @up_to_date
Expand Down
2 changes: 1 addition & 1 deletion src/commands/update.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Shards

solver.prepare(development: Shards.with_development?)

packages = handle_resolver_errors { solver.solve }
packages = handle_resolver_errors(solver) { solver.solve }
install(packages)

if generate_lockfile?(packages)
Expand Down
4 changes: 4 additions & 0 deletions src/resolvers/crystal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Shards
[Version.new Shards.crystal_version]
end

def available_tags : Array(String)
[Shards.crystal_version]
end

def read_spec(version : Version) : String?
nil
end
Expand Down
9 changes: 6 additions & 3 deletions src/resolvers/fossil.cr
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,13 @@ module Shards
end
end

protected def versions_from_tags
def available_tags : Array(String)
capture("fossil tag list -R #{Process.quote(local_fossil_file)}")
.split('\n')
.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }
.lines.reject!(&.empty?)
end

protected def versions_from_tags
available_tags.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }
end

def install_sources(version : Version, install_path : String)
Expand Down
9 changes: 6 additions & 3 deletions src/resolvers/git.cr
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,13 @@ module Shards
end
end

protected def versions_from_tags
def available_tags : Array(String)
capture("git tag --list #{GitResolver.git_column_never}")
.split('\n')
.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }
.lines.reject!(&.empty?)
end

protected def versions_from_tags
available_tags.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }
end

def install_sources(version : Version, install_path : String)
Expand Down
13 changes: 11 additions & 2 deletions src/resolvers/hg.cr
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ module Shards
rescue Error
end

def available_tags : Array(String)
tags = capture("hg tags --template #{Process.quote("{tag}\n")}")
.lines
.sort!

tags.reject!(&.empty?)
end

def available_releases : Array(Version)
update_local_cache
versions_from_tags
Expand Down Expand Up @@ -221,10 +229,11 @@ module Shards
end

protected def versions_from_tags
capture("hg tags --template #{Process.quote("{tag}\n")}")
tags = capture("hg tags --template #{Process.quote("{tag}\n")}")
.lines
.sort!
.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }

tags.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }
end

def install_sources(version : Version, install_path : String)
Expand Down
4 changes: 4 additions & 0 deletions src/resolvers/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module Shards
[spec(nil).version]
end

def available_tags : Array(String)
[spec(nil).version.to_s]
end

def local_path
source
end
Expand Down
2 changes: 2 additions & 0 deletions src/resolvers/resolver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ module Shards
end
end

abstract def available_tags : Array(String)

abstract def available_releases : Array(Version)

def latest_version_for_ref(ref : Ref?) : Version
Expand Down
Loading