Improve resolution of relative includes #980

Merged
zxq9 merged 6 commits from fix_relative_includes into master 2023-09-14 22:00:30 +09:00
Showing only changes of commit dee5dd027b - Show all commits

View File

@ -757,7 +757,9 @@ stdlib_options() ->
end. end.
get_include_code(File, Ann, Opts) -> get_include_code(File, Ann, Opts) ->
case {read_file(File, Opts), read_file(File, stdlib_options())} of %% Temporarily extend include paths with the directory of the current file
Opts1 = include_current_file_dir(Opts, Ann),
case {read_file(File, Opts1), read_file(File, stdlib_options())} of
{{ok, Bin}, {ok, _}} -> {{ok, Bin}, {ok, _}} ->
case filename:basename(File) == File of case filename:basename(File) == File of
true -> { error true -> { error
@ -774,6 +776,19 @@ get_include_code(File, Ann, Opts) ->
{error, {ann_pos(Ann), include_error, File}} {error, {ann_pos(Ann), include_error, File}}
end. end.
include_current_file_dir(Opts, Ann) ->
case {proplists:get_value(file, Ann, undefined),
proplists:get_value(include, Opts, undefined)} of
{undefined, _} -> Opts;
{FromFile, {file_system, Paths}} ->
BaseDir = aeso_utils:canonical_dir(filename:dirname(FromFile)),
case lists:member(BaseDir, Paths) of
false -> [{include, {file_system, [BaseDir | Paths]}} | Opts];
true -> Opts
end;
{_, _} -> Opts
end.
-spec hash_include(string() | binary(), string()) -> include_hash(). -spec hash_include(string() | binary(), string()) -> include_hash().
hash_include(File, Code) when is_binary(File) -> hash_include(File, Code) when is_binary(File) ->
hash_include(binary_to_list(File), Code); hash_include(binary_to_list(File), Code);