Skip to content

Commit efa591a

Browse files
authored
docs: debug_info documentation (#223)
1 parent aaf6384 commit efa591a

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

documentation/how_to/use-source-annotations.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ using Erlang's `:erl_anno` module. This provides comprehensive location tracking
55
for sections, options, and entities, enabling better error messages, IDE
66
integration, and debugging capabilities.
77

8+
Source annotations are only enabled when the Elixir compile option `debug_info`
9+
is enabled (`Code.get_compiler_option(:debug_info)` returns true). By default,
10+
debug info is disabled in production and in `.exs` script files, which means
11+
source annotations won't be available in those contexts.
12+
13+
> #### ExUnit Test Cases {: .warning}
14+
>
15+
> If you're defining modules inside ExUnit test cases (which use `.exs` files),
16+
> source annotations won't be available unless you explicitly enable
17+
> `debug_info` in your tests.
18+
>
19+
> ```elixir
20+
> setup do
21+
> debug_info? = Code.get_compiler_option(:debug_info)
22+
> Code.put_compiler_option(:debug_info, true)
23+
> on_exit(fn -> Code.put_compiler_option(:debug_info, debug_info?) end)
24+
> :ok
25+
> end
26+
> ```
27+
828
## What are Source Annotations?
929
1030
Source annotations capture metadata about where DSL elements are defined in your

lib/spark/dsl/extension.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2170,7 +2170,7 @@ defmodule Spark.Dsl.Extension do
21702170

21712171
@spec macro_env_anno(env :: Macro.Env.t(), do_block :: Macro.t()) :: :erl_anno.anno()
21722172
def macro_env_anno(env, do_block) do
2173-
if Code.compiler_options()[:debug_info] do
2173+
if Code.get_compiler_option(:debug_info) do
21742174
anno = :erl_anno.new(env.line)
21752175
anno = :erl_anno.set_file(String.to_charlist(env.file), anno)
21762176
maybe_set_end_location(anno, do_block)

test/dsl_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ defmodule Spark.DslTest do
55
import Spark.CodeHelpers
66

77
setup do
8-
compiler_options = Code.compiler_options()
9-
Code.compiler_options(debug_info: true)
10-
on_exit(fn -> Code.compiler_options(compiler_options) end)
8+
debug_info? = Code.get_compiler_option(:debug_info)
9+
Code.put_compiler_option(:debug_info, true)
10+
on_exit(fn -> Code.put_compiler_option(:debug_info, debug_info?) end)
1111
:ok
1212
end
1313

0 commit comments

Comments
 (0)