Skip to content

Commit

Permalink
[#1046,#1064] Follow symlinks in els_utils:resolve_paths/2 (#1394)
Browse files Browse the repository at this point in the history
#346 discarded any paths containing symlinks since
they broke assumptions about there being a single URI for each module
in calls to `els_utils:find_module/1`.

#648 added prioritization for cases when there are
multiple URIs in `els_utils:find_module/1`, so discarding paths with
symlinks is now no longer necessary.

Following symlinks is necessary when using rebar3 checkout dependencies
(#1046) or when using Bazel
(#1064) since Bazel places dependencies and compiled
files under its cache directory and symlinks the relevant directory in
the cache to the workspace directory.

This change removes the behavior of `els_utils:resolve_paths/2` that
discards paths containing symlinks.
  • Loading branch information
the-mikedavis committed Nov 8, 2022
1 parent 68bb338 commit 8e4dcf8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
6 changes: 2 additions & 4 deletions apps/els_core/src/els_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ do_initialize(RootUri, Capabilities, InitOptions, {ConfigPath, Config}) ->
Lenses = maps:get("lenses", Config, #{}),
Diagnostics = maps:get("diagnostics", Config, #{}),
ExcludePathsSpecs = [[OtpPath, "lib", P ++ "*"] || P <- OtpAppsExclude],
ExcludePaths = els_utils:resolve_paths(ExcludePathsSpecs, RootPath, true),
ExcludePaths = els_utils:resolve_paths(ExcludePathsSpecs, true),
?LOG_INFO("Excluded OTP Applications: ~p", [OtpAppsExclude]),
CodeReload = maps:get("code_reload", Config, disabled),
Runtime = maps:get("runtime", Config, #{}),
Expand Down Expand Up @@ -375,7 +375,7 @@ report_missing_config() ->
-spec include_paths(path(), string(), boolean()) -> [string()].
include_paths(RootPath, IncludeDirs, Recursive) ->
Paths = [
els_utils:resolve_paths([[RootPath, Dir]], RootPath, Recursive)
els_utils:resolve_paths([[RootPath, Dir]], Recursive)
|| Dir <- IncludeDirs
],
lists:append(Paths).
Expand All @@ -389,7 +389,6 @@ project_paths(RootPath, Dirs, Recursive) ->
[RootPath, Dir, "test"],
[RootPath, Dir, "include"]
],
RootPath,
Recursive
)
|| Dir <- Dirs
Expand All @@ -411,7 +410,6 @@ otp_paths(OtpPath, Recursive) ->
[OtpPath, "lib", "*", "src"],
[OtpPath, "lib", "*", "include"]
],
OtpPath,
Recursive
).

Expand Down
35 changes: 9 additions & 26 deletions apps/els_core/src/els_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
include_lib_id/1,
macro_string_to_term/1,
project_relative/1,
resolve_paths/3,
resolve_paths/2,
to_binary/1,
to_list/1,
compose_node_name/2,
Expand Down Expand Up @@ -215,12 +215,11 @@ fold_files(F, Filter, Dir, Acc) ->
%% @doc Resolve paths based on path specs
%%
%% Gets a list of path specs and returns the expanded list of paths.
%% Path specs can contains glob expressions. Resolved paths that contain
%% symlinks will be ignored.
-spec resolve_paths([[path()]], path(), boolean()) -> [[path()]].
resolve_paths(PathSpecs, RootPath, Recursive) ->
%% Path specs can contains glob expressions.
-spec resolve_paths([[path()]], boolean()) -> [[path()]].
resolve_paths(PathSpecs, Recursive) ->
lists:append([
resolve_path(PathSpec, RootPath, Recursive)
resolve_path(PathSpec, Recursive)
|| PathSpec <- PathSpecs
]).

Expand Down Expand Up @@ -320,19 +319,19 @@ is_symlink(Path) ->

%% @doc Resolve paths recursively

-spec resolve_path([path()], path(), boolean()) -> [path()].
resolve_path(PathSpec, RootPath, Recursive) ->
-spec resolve_path([path()], boolean()) -> [path()].
resolve_path(PathSpec, Recursive) ->
Path = filename:join(PathSpec),
Paths = filelib:wildcard(Path),

case Recursive of
true ->
lists:append([
[make_normalized_path(P) | subdirs(P)]
|| P <- Paths, not contains_symlink(P, RootPath)
|| P <- Paths
]);
false ->
[make_normalized_path(P) || P <- Paths, not contains_symlink(P, RootPath)]
[make_normalized_path(P) || P <- Paths]
end.

%% Returns all subdirectories for the provided path
Expand Down Expand Up @@ -361,22 +360,6 @@ subdirs_(Path, Files, Subdirs) ->
end,
lists:foldl(Fold, Subdirs, Files).

-spec contains_symlink(path(), path()) -> boolean().
contains_symlink(RootPath, RootPath) ->
false;
contains_symlink([], _RootPath) ->
false;
contains_symlink(Path, RootPath) ->
Parts = filename:split(Path),
case lists:droplast(Parts) of
[] ->
false;
ParentParts ->
Parent = filename:join(ParentParts),
((not (Parent == RootPath)) and (is_symlink(Parent))) orelse
contains_symlink(Parent, RootPath)
end.

-spec cmd_receive(port()) -> integer().
cmd_receive(Port) ->
receive
Expand Down

0 comments on commit 8e4dcf8

Please sign in to comment.