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 13 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 spec/integration/subcommand_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe "subcommand" do
end
end

private def with_path(path)
private def with_path(path, &)
old_path = ENV["PATH"]
ENV["PATH"] = "#{File.expand_path(path)}#{Process::PATH_DELIMITER}#{ENV["PATH"]}"
yield
Expand Down
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
33 changes: 32 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,38 @@ module Shards
Shards::Lock.write(packages, override_path, LOCK_FILENAME)
end

def handle_resolver_errors(&)
private def log_available_tags(conflicts)
conflicts.join(separator: "\n") do |k, v|
req = v.requirement
resolver = req.resolver
tags = resolver.available_tags.reverse!.first(5)
releases = resolver.available_releases.map(&.to_s).reverse
req = req.requirement

if releases.empty?
if tags.empty?
info = "And it doesn't have any tags either."
else
info = "For information, these are the latest tags: #{tags.join(", ")}."
end
"#{k} doesn't have any release. #{info}"
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)
info = "For information, these are the latest tags: #{tags.join(", ")}."
"For #{k} the closest available release to #{req} is: #{found}. #{info}"
else
"For #{k} the last available releases are #{releases.first(5).join(", ")}."
end
end
end

def handle_resolver_errors(solver, &)
yield
rescue e : Molinillo::VersionConflict(Shards::Dependency, Shards::Spec)
Log.error { e.message }
Log.error { log_available_tags(e.conflicts) }
Copy link
Member

@straight-shoota straight-shoota Nov 4, 2024

Choose a reason for hiding this comment

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

suggestion: This should probably be a single error message. The conflict information is just context for the version conflict error, not itself another error.

Copy link
Member Author

Choose a reason for hiding this comment

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

OTOH if some workflow expects the error output as before, changing that might break it

Copy link
Member

@straight-shoota straight-shoota Nov 4, 2024

Choose a reason for hiding this comment

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

If something depends on the full exact text of the error message, it should be expected to break.
We cannot possibly guarantee stability on that.

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
2 changes: 1 addition & 1 deletion src/molinillo_solver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module Shards
count += 1
active.add(1)
while active.get > Shards.jobs
sleep 0.1
sleep 0.1.seconds
end
spawn do
begin
Expand Down
2 changes: 1 addition & 1 deletion src/package.cr
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ module Shards
end
end

private def each_executable_path(name)
private def each_executable_path(name, &)
exe = Shards::Helpers.exe(name)
yield Path["bin", exe]
yield Path["bin", name] unless name == exe
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

def available_tags : Array(String)
tags = capture("fossil tag list -R #{Process.quote(local_fossil_file)}")
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
.split('\n', remove_empty: true)
end

protected def versions_from_tags
capture("fossil tag list -R #{Process.quote(local_fossil_file)}")
.split('\n')
.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }
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

def available_tags : Array(String)
tags = capture("git tag --list #{GitResolver.git_column_never}")
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
.split('\n', remove_empty: true)
end

protected def versions_from_tags
capture("git tag --list #{GitResolver.git_column_never}")
.split('\n')
.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }
tags = available_tags.compact_map { |tag| Version.new($1) if tag =~ VERSION_TAG }
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
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