Skip to content

Commit eaff116

Browse files
committed
Show the package name for which the error associated
In certain cases, it's unclear which package causes an error during shard installation. These changes improves error and log messages by adding explicit package context. When an error occurs, the log will now indicate which package triggered the failure.
1 parent e74e050 commit eaff116

File tree

6 files changed

+48
-16
lines changed

6 files changed

+48
-16
lines changed

spec/integration/install_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ describe "install" do
855855
with_shard(metadata) do
856856
ex = expect_raises(FailedCommand) { run "shards install --no-color" }
857857
ex.stdout.should contain <<-ERROR
858-
E: Could not find executable "nonexistent"
858+
E: Failed to install `executable_missing`: Could not find executable "nonexistent"
859859
ERROR
860860
end
861861
end

src/cli.cr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,19 @@ end
176176
begin
177177
Shards.run
178178
rescue ex : OptionParser::InvalidOption
179-
Shards::Log.fatal { ex.message }
179+
Shards::Log.fatal(exception: ex) { ex.message }
180+
Shards::Log.trace { ex.inspect_with_backtrace }
180181
exit 1
181182
rescue ex : Shards::ParseError
182183
ex.to_s(STDERR)
183184
exit 1
185+
rescue ex : Shards::Package::Error
186+
package = ex.package
187+
Shards::Log.error(exception: ex) { "Failed to install `#{package.name}`: #{ex.message}" }
188+
Shards::Log.trace { ex.inspect_with_backtrace }
189+
exit 1
184190
rescue ex : Shards::Error
185-
Shards::Log.error { ex.message }
191+
Shards::Log.error(exception: ex) { ex.message }
192+
Shards::Log.trace { ex.inspect_with_backtrace }
186193
exit 1
187194
end

src/logger.cr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ module Shards
3030

3131
FORMATTER = ::Log::Formatter.new do |entry, io|
3232
message = entry.message
33-
33+
package_name = entry.context[:package]?
3434
if @@colors
35+
io << "[" << package_name.colorize(:blue).to_s << "] " if package_name && entry.severity <= ::Log::Severity::Debug
3536
io << if color = LOGGER_COLORS[entry.severity]?
3637
if idx = message.index(' ')
3738
message[0...idx].colorize(color).to_s + message[idx..-1]
@@ -42,7 +43,9 @@ module Shards
4243
message
4344
end
4445
else
45-
io << entry.severity.label[0] << ": " << message
46+
io << entry.severity.label[0] << ": "
47+
io << "[" << package_name << "] " if package_name && entry.severity <= ::Log::Severity::Debug
48+
io << message
4649
end
4750
end
4851
end

src/molinillo_solver.cr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ module Shards
6161
end
6262
spawn do
6363
begin
64-
dep.resolver.update_local_cache if dep.resolver.is_a? GitResolver
64+
Log.with_context do
65+
Log.context.set package: dep.name
66+
dep.resolver.update_local_cache if dep.resolver.is_a? GitResolver
67+
end
6568
ch.send(nil)
6669
rescue ex : Exception
6770
ch.send(ex)

src/package.cr

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ require "./helpers"
33

44
module Shards
55
class Package
6+
class Error < Shards::Error
7+
getter package
8+
9+
def initialize(message, @package : Package)
10+
super message
11+
end
12+
end
13+
614
getter name : String
715
getter resolver : Resolver
816
getter version : Version
@@ -58,15 +66,20 @@ module Shards
5866
end
5967

6068
def install
61-
cleanup_install_directory
69+
Log.with_context do
70+
Log.context.set package: name
71+
Log.context.set version: report_version
72+
73+
cleanup_install_directory
6274

63-
# install the shard:
64-
resolver.install_sources(version, install_path)
75+
# install the shard:
76+
resolver.install_sources(version, install_path)
6577

66-
# link the project's lib path as the shard's lib path, so the dependency
67-
# can access transitive dependencies:
68-
unless resolver.is_a?(PathResolver)
69-
install_lib_path
78+
# link the project's lib path as the shard's lib path, so the dependency
79+
# can access transitive dependencies:
80+
unless resolver.is_a?(PathResolver)
81+
install_lib_path
82+
end
7083
end
7184

7285
Shards.info.installed[name] = self
@@ -114,7 +127,7 @@ module Shards
114127
spec.executables.each do |name|
115128
exe_name = find_executable_file(Path[install_path], name)
116129
unless exe_name
117-
raise Shards::Error.new("Could not find executable #{name.inspect}")
130+
raise Shards::Package::Error.new("Could not find executable #{name.inspect}", package: self)
118131
end
119132
Log.debug { "Install #{exe_name}" }
120133
source = File.join(install_path, exe_name)

src/resolvers/git.cr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,10 @@ module Shards
363363

364364
private def valid_repository?
365365
command = "git config --get remote.origin.mirror"
366-
Log.debug { command }
366+
Log.with_context do
367+
Log.context.set package: name
368+
Log.debug { command }
369+
end
367370

368371
output = Process.run(command, shell: true, output: :pipe, chdir: local_path) do |process|
369372
process.output.gets_to_end
@@ -437,7 +440,10 @@ module Shards
437440
raise Error.new("Error missing git command line tool. Please install Git first!")
438441
end
439442

440-
Log.debug { command }
443+
Log.with_context do
444+
Log.context.set package: name
445+
Log.debug { command }
446+
end
441447

442448
output = capture ? IO::Memory.new : Process::Redirect::Close
443449
error = IO::Memory.new

0 commit comments

Comments
 (0)