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

(FACT-3165) Report correct location of where a fact was resolved from #2606

Merged
merged 3 commits into from
Sep 16, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
test_array_fact value is wrong'
)
assert_match(
/custom fact test_array_fact got resolved from.*no_aggregate_block\.rb\", 1\]/,
/custom fact test_array_fact was resolved from.*no_aggregate_block\.rb:1/,
facter_result.stderr.chomp,
'resolution location not found on debug'
)
Expand All @@ -138,7 +138,7 @@
'test_hash_fact value is wrong'
)
assert_match(
/custom fact test_hash_fact got resolved from.*no_aggregate_block\.rb\", 12\]/,
/custom fact test_hash_fact was resolved from.*no_aggregate_block\.rb:12/,
facter_result.stderr.chomp,
'resolution location not found on debug'
)
Expand Down
15 changes: 15 additions & 0 deletions lib/facter/custom_facts/core/aggregate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class Aggregate
# @api private
attr_reader :fact

# @!attribute [r] last_evaluated
#
# @return [String]
#
# @api public
attr_reader :last_evaluated

# Create a new aggregated resolution mechanism.
#
# @param name [String] The name of the resolution.
Expand Down Expand Up @@ -101,7 +108,15 @@ def options(options)
#
# @api private
def evaluate(&block)
if @last_evaluated
msg = "Already evaluated #{@name}"
msg << " at #{@last_evaluated}" if msg.is_a? String
msg << ', reevaluating anyways'
log.warn msg
end
instance_eval(&block)

@last_evaluated = block.source_location.join(':')
end

# Define a new chunk for the given aggregate
Expand Down
3 changes: 1 addition & 2 deletions lib/facter/custom_facts/util/fact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ def find_first_real_value(resolutions)
end

def log_fact_path(resolve)
fact = resolve.fact
log.debug("#{resolve.fact_type} fact #{fact.name} got resolved from: #{fact.location}")
log.debug("#{resolve.fact_type} fact #{resolve.fact.name} was resolved from: #{resolve.last_evaluated}")
joshcooper marked this conversation as resolved.
Show resolved Hide resolved
end

def announce_when_no_suitable_resolution(resolutions)
Expand Down
9 changes: 1 addition & 8 deletions lib/facter/custom_facts/util/resolution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,7 @@ def evaluate(&block)

instance_eval(&block)

# Ruby 1.9+ provides the source location of procs which can provide useful
# debugging information if a resolution is being evaluated twice. Since 1.8
# doesn't support this we opportunistically provide this information.
@last_evaluated = if block.respond_to? :source_location
block.source_location.join(':')
else
true
end
@last_evaluated = block.source_location.join(':')
end

# Sets options for the aggregate fact
Expand Down