-
Notifications
You must be signed in to change notification settings - Fork 325
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
Add --warnings-as-errors flag for non-zero exit code (continued) #1564
Closed
eksperimental
wants to merge
5
commits into
elixir-lang:main
from
eksperimental-forks:warnings_as_errors
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df1758b
Add --warnings-as-errors flag for non-zero exit code
kdawgwilk 050916a
Merge branch 'master' into warnings_as_errors
eksperimental bb43d07
Unify warnings under ExDoc.Utils.warn/1
eksperimental c8c738c
Isolate warning counts per calling process
eksperimental a0e2944
Add WarningCounter tests for HTML and Epub fixture tests
eksperimental File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
defmodule ExDoc.WarningCounter do | ||
@moduledoc false | ||
|
||
use GenServer | ||
|
||
@type count :: non_neg_integer() | ||
@type counters_ref :: :counters.counters_ref() | ||
@typep caller :: pid() | :default | ||
@typep state :: %{:default => counters_ref(), caller() => counters_ref()} | ||
|
||
defguardp is_caller(term) when is_pid(term) or term == :default | ||
|
||
########################### | ||
# Callback implementations | ||
|
||
@spec start_link(any()) :: GenServer.on_start() | ||
def start_link(arg) do | ||
GenServer.start_link(__MODULE__, arg, name: __MODULE__) | ||
end | ||
|
||
@impl GenServer | ||
@spec init(any()) :: {:ok, state} | ||
def init(_args) do | ||
counter_ref = new_counter() | ||
|
||
state = %{ | ||
self() => counter_ref, | ||
default: counter_ref | ||
} | ||
|
||
{:ok, state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_call({:count, caller}, _from, state) when is_caller(caller) do | ||
counter_ref = get_counter_ref(state, caller) | ||
count = :counters.get(counter_ref, 1) | ||
|
||
{:reply, count, state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({:increment, caller}, state) when is_caller(caller) do | ||
counter_ref = get_counter_ref(state, caller) | ||
:counters.add(counter_ref, 1, 1) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
def handle_info({:register_caller, caller}, state) when is_caller(caller) do | ||
counter_ref = new_counter() | ||
state = Map.put(state, caller, counter_ref) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
def handle_info({:unregister_caller, caller}, state) when is_caller(caller) do | ||
state = Map.delete(state, caller) | ||
|
||
{:noreply, state} | ||
end | ||
|
||
############# | ||
# Public API | ||
|
||
@spec count(caller()) :: count() | ||
def count(caller \\ self()) do | ||
GenServer.call(__MODULE__, {:count, caller}) | ||
end | ||
|
||
@spec increment() :: :ok | ||
def increment() do | ||
caller = self() | ||
send(__MODULE__, {:increment, caller}) | ||
|
||
:ok | ||
end | ||
|
||
@spec register_caller(caller()) :: :ok | ||
def register_caller(caller) when is_caller(caller) do | ||
send(__MODULE__, {:register_caller, caller}) | ||
end | ||
|
||
@spec unregister_caller(caller()) :: :ok | ||
def unregister_caller(caller) when is_caller(caller) do | ||
send(__MODULE__, {:unregister_caller, caller}) | ||
end | ||
|
||
################## | ||
# Private helpers | ||
|
||
defp new_counter() do | ||
:counters.new(1, [:atomics]) | ||
end | ||
|
||
defp get_counter_ref(state, caller) | ||
when is_map(state) and is_caller(caller) and is_map_key(state, caller) do | ||
Map.fetch!(state, caller) | ||
end | ||
|
||
defp get_counter_ref(%{default: default_counter_ref} = state, caller) | ||
when is_map(state) and is_caller(caller) do | ||
callers = callers(caller) | ||
|
||
Enum.find_value(callers, default_counter_ref, fn the_caller -> | ||
Map.get(state, the_caller) | ||
end) | ||
end | ||
|
||
defp callers(pid) when is_pid(pid) do | ||
case Process.info(pid, :dictionary) do | ||
{:dictionary, dictionary} -> | ||
Keyword.get(dictionary, :"$callers", []) | ||
|
||
nil -> | ||
[] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of introducing some global state, I wonder if you could create a counter and store it in the
config
. And then you callwarning(config, ...)
which will bump the counter. This means it is naturally async and you don't need the additional process. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the same vein, perhaps
mix docs
would return https://hexdocs.pm/mix/1.15/Mix.Task.Compiler.Diagnostic.html structs? Could be useful for IDEs? Or since ExDoc is not a compiler it’s a bad idea?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I don't see how they could be used if it is not a compiler :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing config simplifies a lot the implementation.
The problem I am facing now is that there are functions that generate a warning and have no notion of config,
such as
ExDoc.Markdown.Earmark.to_ast/2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would need to either:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d do the latter, return
{ast, %{warnings: warnings}}
or something like that.I think such APIs that return instead of emit warnings are nice because we can test without capture_io and thus async. In this particular case since it’s not useful to return these warnings from
mix docs
, what we can also do is push all ExDoc warnings via some ExDoc.Utils.warn, inside call Application.put_env(:ex_doc, :warnings_emitted, true), check for this app env at the end of the run and call it a day. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The counter will tell us if warnings were emitted or not. So having a config plus function return should be enough!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will work on this solution and let you know when it is done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you ❤️