Skip to content

Bug 1446236 - Add & use simpler method to check if an extension is present#35

Merged
dylanwh merged 6 commits into
bugzilla:unstablefrom
CyberShadow:have-extension
Apr 8, 2018
Merged

Bug 1446236 - Add & use simpler method to check if an extension is present#35
dylanwh merged 6 commits into
bugzilla:unstablefrom
CyberShadow:have-extension

Conversation

@CyberShadow

Copy link
Copy Markdown
Member

Ideally these should probably all be refactored into extension-agnostic hooks, but for the time being simplify the code a little.

This adds extensions_hash as a separate variable to request_cache. An alternative would be to change $cache->{extensions} to a hash, however as hashes are unsorted, I was worried that it could introduce unwanted effects due to the iteration order being changed.

Add a new method to check if an extension is present and loaded.
Transition from previous explicit searches in the extension list to
invoking the method added in the previous commit.
Comment thread Bugzilla.pm
}
return exists $cache->{extensions_hash}{$name};
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is something wrong with this implementation, but I'm not sure yet what. After the hash is built, it contains only two elements on successive requests.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That suggests to me there should be a test to capture correctness in extensions, which I gather this change would fail (and prompt an investigation of why).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I figured out the problem, it was caused by a change in another pull request of mine.

Comment thread Bugzilla.pm Outdated
return $cache->{extensions};
}

sub have_extension {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think has_extension() is a better name, as it maps to has_feature(). Not sure if it's grammatically correct or not though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Renamed.

@dylanwh dylanwh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested changes to make it work.

Comment thread Bugzilla.pm Outdated
my ($class, $name) = @_;
my $cache = $class->request_cache;
if (!$cache->{extensions_hash}) {
my %extensions;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

my %extensions = map { $_->NAME => 1 } @{ Bugzilla->extensions };

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same problem with that code... but it is shorter.

Also using () does something really weird here. I thought it evaluates to undef, which should be slightly more efficient than using 1 or any other value.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Found the problem: Bugzilla->extensions was being invoked recursively (as it initializes extensions as it populates the list), so the truncated list I was seeing was from the incomplete invocation higher in the stack trace.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed

There is no simple and obvious way to allow extensions to query the
extension list as it is being constructed, so explicitly forbid this
behavior.

The trap will help avoid weird bugs caused by halfway-populated and
cached extension lists.
Suggested-by: Dylan William Hardison <dylan@hardison.net>
Suggested-by: Dylan William Hardison <dylan@hardison.net>
@CyberShadow

Copy link
Copy Markdown
Member Author

Will update the other PRs to rename have_extension when this is merged.

Comment thread Bugzilla.pm
}
$cache->{extensions} = \@extensions;
}
$recursive = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't understand why you are guarding against a recursive call. That at least needs a comment.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There's some explanation in the commit message. Essentially it's an extension checking Bugzilla->extensions during its initialization, which is invalid as the extension list is still being constructed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a comment.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great! Looking at it now, my gut says it might not work completely right. I'd have thought it was more idiomatic to use not a state var, but a my var outside the sub, and use local to set the guard variable straight after the check, in order to use dynamic scoping rather than lexical.

Comment thread Bugzilla.pm
}
return exists $cache->{extensions_hash}{$name};
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That suggests to me there should be a test to capture correctness in extensions, which I gather this change would fail (and prompt an investigation of why).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants