Skip to content

Commit 78ce679

Browse files
authored
Store relative file in module definition (#9099)
Prior to this patch, we would always compute the relative path to the current working directory (CWD), but this meant consolidated protocols would always get a full path since they are always outside of their current working directory. We address this by also storing the relative path in the definition. Closes #9095
1 parent 7e1d165 commit 78ce679

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/elixir/src/elixir_erl.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ spawned_compile(Map, Set, _Bag, TranslatedTypespecs) ->
151151

152152
load_form(Map, Prefix, Forms, TypeSpecs, Chunks).
153153

154-
dynamic_form(#{module := Module, line := Line, file := File, attributes := Attributes,
154+
dynamic_form(#{module := Module, line := Line, relative_file := RelativeFile, attributes := Attributes,
155155
definitions := Definitions, unreachable := Unreachable, compile_opts := Opts} = Map) ->
156156
{Def, Defmacro, Macros, Exports, Functions} =
157157
split_definition(Definitions, Unreachable, [], [], [], [], {[], []}),
158158

159-
Location = {elixir_utils:characters_to_list(elixir_utils:relative_to_cwd(File)), Line},
159+
Location = {elixir_utils:characters_to_list(RelativeFile), Line},
160160
Prefix = [{attribute, Line, file, Location},
161161
{attribute, Line, module, Module},
162162
{attribute, Line, compile, [no_auto_import | Opts]}],

lib/elixir/src/elixir_module.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ compile(Line, Module, Block, Vars, E) ->
109109
module => Module,
110110
line => Line,
111111
file => File,
112+
relative_file => elixir_utils:relative_to_cwd(File),
112113
attributes => Attributes,
113114
definitions => AllDefinitions,
114115
unreachable => Unreachable,

lib/mix/test/mix/tasks/compile.protocols_test.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,34 @@ defmodule Mix.Tasks.Compile.ProtocolsTest do
117117
end)
118118
end
119119

120+
test "consolidated protocols keep relative path to their source" do
121+
Mix.Project.push(MixTest.Case.Sample)
122+
123+
in_fixture("no_mixfile", fn ->
124+
Mix.Tasks.Compile.Elixir.run([])
125+
Mix.Tasks.Compile.Protocols.run([])
126+
127+
# Load consolidated
128+
:code.add_patha('_build/dev/lib/sample/consolidated')
129+
:code.purge(Enumerable)
130+
:code.delete(Enumerable)
131+
132+
try do
133+
Enumerable.impl_for!(:oops)
134+
rescue
135+
Protocol.UndefinedError ->
136+
assert [{_, _, _, [file: 'lib/enum.ex'] ++ _} | _] = __STACKTRACE__
137+
else
138+
_ ->
139+
flunk("Enumerable.impl_for!/1 should have failed")
140+
after
141+
:code.del_path('_build/dev/lib/sample/consolidated')
142+
:code.purge(Enumerable)
143+
:code.delete(Enumerable)
144+
end
145+
end)
146+
end
147+
120148
defp compile_elixir_and_protocols do
121149
Mix.Tasks.Compile.Elixir.run([])
122150
Mix.Tasks.Compile.Protocols.run([])

0 commit comments

Comments
 (0)