Skip to content

Commit

Permalink
2/n erlang_service: tramp request id through
Browse files Browse the repository at this point in the history
Summary:
When we call back for the include file resolution, we need to pass the original request ID back to ELP, so ELP can route the request to the originating process.

Add the plumbing so the `Id` is available in `elp_lint`.  Does nothing else.

Reviewed By: michalmuskala

Differential Revision: D60450406

fbshipit-source-id: bb26ceec5a9d34368decd9978e69efc7958f2c08
  • Loading branch information
alanz authored and facebook-github-bot committed Aug 1, 2024
1 parent 8c9f07e commit 65b0fdc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 35 deletions.
71 changes: 47 additions & 24 deletions erlang_service/src/elp_epp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

%% An Erlang code preprocessor.

-export([open/1, open/2, open/3, close/1, format_error/1]).
-export([open/1, open/2, open/3, open/4, close/1, format_error/1]).
-export([scan_erl_form/1, parse_erl_form/1, macro_defs/1]).
-export([scan_file/1, scan_file/2, parse_file/1, parse_file/2, parse_file/3]).
-export([scan_file/1, scan_file/3, parse_file/1, parse_file/3, parse_file/4]).
-export([
default_encoding/0,
encoding_to_string/1,
Expand Down Expand Up @@ -92,7 +92,9 @@
pre_opened = false :: boolean(),
in_prefix = true :: boolean(),
fname = [] :: function_name_type(),
scan_opts = [] :: erl_scan:options()
scan_opts = [] :: erl_scan:options(),
% Id required for requests to host ELP for file name resolution
request_id = none
}).

%% open(Options)
Expand All @@ -108,18 +110,18 @@
%% parse_file(FileName, IncludePath, PreDefMacros)
%% macro_defs(Epp)

-spec open(FileName, IncludePath) ->
-spec open(erlang_service_server:id()|none, FileName, IncludePath) ->
{'ok', Epp} | {'error', ErrorDescriptor}
when
FileName :: file:name(),
IncludePath :: [DirectoryName :: file:name()],
Epp :: epp_handle(),
ErrorDescriptor :: term().

open(Name, Path) ->
open(Name, Path, []).
open(Id, Name, Path) ->
open(Id, Name, Path, []).

-spec open(FileName, IncludePath, PredefMacros) ->
-spec open(erlang_service_server:id()|none, FileName, IncludePath, PredefMacros) ->
{'ok', Epp} | {'error', ErrorDescriptor}
when
FileName :: file:name(),
Expand All @@ -128,8 +130,9 @@ when
Epp :: epp_handle(),
ErrorDescriptor :: term().

open(Name, Path, Pdm) ->
open([{name, Name}, {includes, Path}, {macros, Pdm}]).
open(Id, Name, Path, Pdm) ->
open(Id, [{name, Name}, {includes, Path}, {macros, Pdm}]).


-spec open(Options) ->
{'ok', Epp} | {'ok', Epp, Extra} | {'error', ErrorDescriptor}
Expand All @@ -147,13 +150,31 @@ when
Extra :: [{'encoding', source_encoding() | 'none'}],
ErrorDescriptor :: term().

open(Options) ->
open(Options) -> open(none, Options).

-spec open(erlang_service_server:id()|none, Options) ->
{'ok', Epp} | {'ok', Epp, Extra} | {'error', ErrorDescriptor}
when
Options :: [
{'default_encoding', DefEncoding :: source_encoding()}
| {'includes', IncludePath :: [DirectoryName :: file:name()]}
| {'source_name', SourceName :: file:name()}
| {'macros', PredefMacros :: macros()}
| {'name', FileName :: file:name()}
| {'fd', FileDescriptor :: file:io_device()}
| 'extra'
],
Epp :: epp_handle(),
Extra :: [{'encoding', source_encoding() | 'none'}],
ErrorDescriptor :: term().

open(Id, Options) ->
case proplists:get_value(name, Options) of
undefined ->
erlang:error(badarg);
Name ->
Self = self(),
Epp = spawn(fun() -> server(Self, Name, Options) end),
Epp = spawn(fun() -> server(Self, Id, Name, Options) end),
Extra = proplists:get_bool(extra, Options),
case epp_request(Epp) of
{ok, Pid, Encoding} when Extra ->
Expand Down Expand Up @@ -288,7 +309,7 @@ format_error(string_concat) ->
format_error(E) ->
file:format_error(E).

-spec scan_file(FileName, Options) ->
-spec scan_file(erlang_service_server:id(), FileName, Options) ->
{'ok', [Form], Extra} | {error, OpenError}
when
FileName :: file:name(),
Expand All @@ -304,8 +325,8 @@ when
Extra :: [{'encoding', source_encoding() | 'none'}],
OpenError :: file:posix() | badarg | system_limit.

scan_file(Ifile, Options) ->
case open([{name, Ifile}, extra | Options]) of
scan_file(Id, Ifile, Options) ->
case open(Id, [{name, Ifile}, extra | Options]) of
{ok, Epp, Extra} ->
Forms = scan_file(Epp),
close(Epp),
Expand All @@ -324,7 +345,7 @@ scan_file(Epp) ->
[{eof, {Offset, Offset}}]
end.

-spec parse_file(FileName, IncludePath, PredefMacros) ->
-spec parse_file(erlang_service_server:id(), FileName, IncludePath, PredefMacros) ->
{'ok', [Form]} | {error, OpenError}
when
FileName :: file:name(),
Expand All @@ -335,10 +356,10 @@ when
ErrorInfo :: elp_scan:error_info() | elp_parse:error_info(),
OpenError :: file:posix() | badarg | system_limit.

parse_file(Ifile, Path, Predefs) ->
parse_file(Ifile, [{includes, Path}, {macros, Predefs}]).
parse_file(Id, Ifile, Path, Predefs) ->
parse_file(Id, Ifile, [{includes, Path}, {macros, Predefs}]).

-spec parse_file(FileName, Options) ->
-spec parse_file(erlang_service_server:id(), FileName, Options) ->
{'ok', [Form]} | {'ok', [Form], Extra} | {error, OpenError}
when
FileName :: file:name(),
Expand All @@ -358,8 +379,8 @@ when
Extra :: [{'encoding', source_encoding() | 'none'}],
OpenError :: file:posix() | badarg | system_limit.

parse_file(Ifile, Options) ->
case open([{name, Ifile} | Options]) of
parse_file(Id, Ifile, Options) ->
case open(Id, [{name, Ifile} | Options]) of
{ok, Epp} ->
Forms = parse_file(Epp),
close(Epp),
Expand Down Expand Up @@ -663,14 +684,14 @@ restore_typed_record_fields([
restore_typed_record_fields([Form | Forms]) ->
[Form | restore_typed_record_fields(Forms)].

server(Pid, Name, Options) ->
server(Pid, Id, Name, Options) ->
process_flag(trap_exit, true),
St = #epp{},
case proplists:get_value(fd, Options) of
undefined ->
case file:open(Name, [read]) of
{ok, File} ->
init_server(Pid, Name, Options, St#epp{file = File});
init_server(Pid, Name, Options, St#epp{file = File, request_id = Id});
{error, E} ->
epp_reply(Pid, {error, E})
end;
Expand Down Expand Up @@ -837,7 +858,8 @@ enter_file2(NewF, Pname, From, St0) ->
#epp{
include_offset = Offset,
macs = Ms0,
default_encoding = DefEncoding
default_encoding = DefEncoding,
request_id = ReqId
} = St0,
enter_file_reply(From, Pname, 0, Offset),
Ms = Ms0#{'FILE' := {none, [{string, {0, 0}, Pname}]}},
Expand All @@ -857,7 +879,8 @@ enter_file2(NewF, Pname, From, St0) ->
sstk = [St0 | St0#epp.sstk],
path = Path,
macs = Ms,
default_encoding = DefEncoding
default_encoding = DefEncoding,
request_id = ReqId
}.

enter_file_reply(From, Name, AtLine, CurLine) ->
Expand Down
4 changes: 2 additions & 2 deletions erlang_service/src/erlang_service_ct.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
%%% % @format
-module(erlang_service_ct).

-export([run/1]).
-export([run/2]).

run([Module, Filename, CompileOptions, ShouldRequestGroups]) ->
run(_Id, [Module, Filename, CompileOptions, ShouldRequestGroups]) ->
{ok, Module, Binary} = compile:file(Filename, [binary | normalize_compile_options(CompileOptions)]),
code:load_binary(Module, Filename, Binary),
All = eval(lists:flatten(io_lib:format("~p:all().", [Module]))),
Expand Down
4 changes: 2 additions & 2 deletions erlang_service/src/erlang_service_edoc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%%% % @format
-module(erlang_service_edoc).

-export([run/1]).
-export([run/2]).

-define(DICT_KEY, edoc_diagnostics).

Expand All @@ -21,7 +21,7 @@
{Line :: pos_integer(), Message :: binary(), Severity :: severity()}.
-type severity() :: warning | error.

run([FileName, DocOrigin]) ->
run(_Id, [FileName, DocOrigin]) ->
{ok, serialize_docs(get_docs_for_src_file(FileName, DocOrigin))}.

-spec serialize_docs(docs()) -> [{binary(), binary()}].
Expand Down
12 changes: 6 additions & 6 deletions erlang_service/src/erlang_service_lint.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
%%% % @format
-module(erlang_service_lint).

-export([run/1]).
-export([run/2]).

run([FileName, Options0, OverrideOptions, PostProcess, Deterministic]) ->
run(Id, [FileName, Options0, OverrideOptions, PostProcess, Deterministic]) ->
Options1 =
case Deterministic of
true ->
Expand All @@ -30,7 +30,7 @@ run([FileName, Options0, OverrideOptions, PostProcess, Deterministic]) ->
_ ->
Options2
end,
case extract_forms(FileName, Options3) of
case extract_forms(Id, FileName, Options3) of
{ok, Forms0} ->
Transforms0 = proplists:get_value(parse_transforms, Options3, []),
{Transforms, Forms1} = collect_parse_transforms(Forms0, [], Transforms0),
Expand Down Expand Up @@ -249,12 +249,12 @@ inclusion_range(Forms, Path) ->
{1, 1}
end.

extract_forms(FileName, Options) ->
extract_forms(Id, FileName, Options) ->
case filename:extension(FileName) of
".erl" ->
elp_epp:parse_file(FileName, Options);
elp_epp:parse_file(Id, FileName, Options);
".hrl" ->
elp_epp:parse_file(FileName, Options);
elp_epp:parse_file(Id, FileName, Options);
".escript" ->
Forms = elp_escript:extract(FileName),
{ok, Forms};
Expand Down
4 changes: 3 additions & 1 deletion erlang_service/src/erlang_service_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
%% API
-export([process/1]).

-export_type([id/0]).

%%==============================================================================
%% Includes
%%==============================================================================
Expand Down Expand Up @@ -150,7 +152,7 @@ process_request_async(Module, Id, Data, AdditionalParams) ->
fun() ->
try
Params = binary_to_term(Data),
case Module:run(Params ++ AdditionalParams) of
case Module:run(Id, Params ++ AdditionalParams) of
{ok, Result} ->
gen_server:cast(?SERVER, {result, Id, encode_segments(Result)});
{error, Error} ->
Expand Down

0 comments on commit 65b0fdc

Please sign in to comment.