Skip to content

Commit

Permalink
Fix file name when using the import_fetcher option
Browse files Browse the repository at this point in the history
When using the import_fetcher option, the file name of
fetched content was accidentally set to "from_fetched.proto".

This could cause problems when there were more than one import
statement, because gpb would consider all to originate from the
file "from_fetched.proto", and this would result in an error:

  {multiply_defined_file_or_files,["from_fetched.proto"]}

Fix by using the file name in the import statement.
  • Loading branch information
tomas-abrahamsson committed Mar 17, 2022
1 parent 199340c commit 8719912
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/gpb_compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3841,8 +3841,8 @@ locate_read_input_once(Input, CheckSeen, ImEnv) ->
{error, {locate, Reason}}
end.

parse_one_input(#path{orig=In}, Content, #import_env{opts=Opts}) ->
FName = file_name_from_input(In),
parse_one_input(Path, Content, #import_env{opts=Opts}) ->
FName = path_to_filename(Path, orig),
case scan_and_parse_string(Content, FName, Opts) of
{ok, Defs} ->
Imports = gpb_defs:fetch_imports(Defs),
Expand Down Expand Up @@ -3877,9 +3877,6 @@ add_curr_dir_as_include_if_needed(Opts) ->
false -> Opts ++ [{i,"."}]
end.

file_name_from_input({Mod,_S}) -> lists:concat([Mod, ".proto"]);
file_name_from_input(FName) -> FName.

scan_and_parse_string(S, FName, Opts) ->
case gpb_scan:binary(unicode:characters_to_binary(S)) of
{ok, Tokens, _} ->
Expand Down
20 changes: 20 additions & 0 deletions test/gpb_compile_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ flush_msgs() ->
[]
end.

several_imports_with_fetcher_test() ->
A = <<"import 'b.proto';
import 'c.proto';
message A { optional uint32 a = 1; }">>,
B = <<"message B { optional uint32 b = 2; }">>,
C = <<"message C { optional uint32 c = 3; }">>,
Fetcher = fun("b.proto") -> {ok, binary_to_list(B)};
("c.proto") -> {ok, binary_to_list(C)}
end,
Vsn = gpb_defs:latest_defs_version(),
{ok, [{proto_defs_version, Vsn} | Defs], _Warns=[]} =
gpb_compile:string(a, binary_to_list(A),
[to_proto_defs,
return_errors, return_warnings,
{import_fetcher, Fetcher},
{proto_defs_version, Vsn}]),
["a.proto", "b.proto", "c.proto"] =
[Full || {file, {_BaseSansExt, Full}} <- Defs],
ok.

parses_file_to_binary_test() ->
Contents = <<"message Msg { required uint32 field1 = 1; }\n">>,
{ok, 'X', Code, []} =
Expand Down

0 comments on commit 8719912

Please sign in to comment.