Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sg2342
Copy link

@sg2342 sg2342 commented Jun 16, 2025

fail config validation if:

  • the module referenced in a rule does not exist
  • a module does not export the function referenced in a rule

Description

  • use module_info(exports) (runtime will attempt to load the module) to get the list of exported functions
  • change elvis_SUITE:rock_with_invalid_rules/1

Closes #476

fail config validation if:
- the module referenced in a rule does not exist
- a module does not export the function referenced in a rule
@elbrujohalcon elbrujohalcon changed the title Fix inaka#476 Fix #476: Ensure modules are _load-able_ instead _load-ed_ Jun 16, 2025
Copy link
Member

@elbrujohalcon elbrujohalcon left a comment

Choose a reason for hiding this comment

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

I like it!
Thanks, @sg2342 🫶

@elbrujohalcon elbrujohalcon changed the title Fix #476: Ensure modules are _load-able_ instead _load-ed_ Fix #476: Ensure modules are loadABLE instead loadED Jun 16, 2025
false
ExportedRules =
try
Module:module_info(exports)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Module:module_info(exports)
erlang:get_module_info(Module, exports)

To avoid using dynamic calls.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see… it fails if it's not loaded… 🤦🏻
We need a non-dynamic way of doing what module_info/1 does.

Copy link
Member

@elbrujohalcon elbrujohalcon Jun 16, 2025

Choose a reason for hiding this comment

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

Maybe…

Suggested change
Module:module_info(exports)
{ok, Module} = code:ensure_loaded(Module),
erlang:get_module_info(Module, exports)

…?

Copy link
Author

@sg2342 sg2342 Jun 16, 2025

Choose a reason for hiding this comment

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

erlang:get_module_info/2 is undocumented.

i think without resorting to beam_lib ... the only documented way to get the exported functions of a module is the dynamic call.

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe just change line 130 from {file, _} ?= code:is_loaded(Module), to {ok, Module} ?= code:ensure_loaded(Module),.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ohh, I thought I added this suggestion to that PR I will review how I missed it.

Copy link
Member

Choose a reason for hiding this comment

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

Since the other 2 maintainers are OK with it, let's conclude that we should use:

  1. code:ensure_loaded
  2. dynamic call to get exports (with -elvis attribute to avoid it in elvis_core)

Copy link
Contributor

@kivra-pauoli kivra-pauoli Jun 16, 2025

Choose a reason for hiding this comment

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

I tested this locally and see no big issue in just replacing

- {file, _} ?= code:is_loaded(Module),
+ {module, _} ?= code:ensure_loaded(Module),

and

- false ->
+ {error, _}

Copy link
Contributor

Choose a reason for hiding this comment

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

Ohh, I thought I added this suggestion to that PR I will review how I missed it.

@bormilan, it's not a big issue. I guess we got concentrated on conversation (the resolvable elements in the description) and since this was a "generic comment" it got lost. I should've payed more attention, too...

Copy link
Member

Choose a reason for hiding this comment

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

Nope. And that's why I wrote #479. We can merge that one and generate releases 4.1.1 now…

Comment on lines -138 to -141
elvis_utils:warn_prn(
"Invalid module (~p) specified in elvis.config.~n",
[Module]
),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we avoiding this warning, though?

Copy link
Member

Choose a reason for hiding this comment

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

Because the code (as it is in the PR right now) would not fail in that way… But… since we're going to use code:ensure_loaded/1, we can recover this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gimme a few minutes so I can try to check this better. If you wanna move forward, it's also no biggie.

Copy link
Contributor

Choose a reason for hiding this comment

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

Because the code (as it is in the PR right now) would not fail in that way… But… since we're going to use code:ensure_loaded/1, we can recover this.

I assumed we would, so best to ask 😄

Comment on lines +434 to +436
{invalid_rule, {elvis_style, what_is_this_rule}},
{invalid_rule, {not_existing_module, dont_repeat_yourself}},
{invalid_rule, {not_existing_module, dont_repeat_yourself}}
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@elbrujohalcon
Copy link
Member

Thanks again, @sg2342 !! I'll close this one now and we can move on in #479 with an alternative fix based on the comments from this PR.

@sg2342 sg2342 deleted the fix_476 branch June 16, 2025 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warning: Invalid module (elvis_style) specified in elvis.config.

4 participants