Skip to content

Commit e0ea9cf

Browse files
committed
Fix a bug where the initialize request would override file-based settings
Closes eng/ide/ada_language_server#1631
1 parent c2d4e28 commit e0ea9cf

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

source/ada/lsp-ada_handlers-project_loading.adb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ with URIs;
5050

5151
with VSS.Characters;
5252
with VSS.Characters.Latin;
53+
with VSS.String_Vectors;
5354
with VSS.Strings;
5455
with VSS.Strings.Conversions;
5556

@@ -118,8 +119,9 @@ package body LSP.Ada_Handlers.Project_Loading is
118119

119120
procedure Ensure_Project_Loaded (Self : in out Message_Handler'Class) is
120121
use type VSS.Strings.Virtual_String;
122+
use VSS.Strings.Conversions;
121123

122-
GPRs_Found : Natural := 0;
124+
Candidates : VSS.String_Vectors.Virtual_String_Vector;
123125
Project_File : VSS.Strings.Virtual_String :=
124126
Self.Configuration.Project_File;
125127
GPR_Configuration_File : VSS.Strings.Virtual_String :=
@@ -188,29 +190,30 @@ package body LSP.Ada_Handlers.Project_Loading is
188190
declare
189191
Files : GNATCOLL.VFS.File_Array_Access :=
190192
Self.Client.Root_Directory.Read_Dir (GNATCOLL.VFS.Files_Only);
191-
Found : GNATCOLL.VFS.Virtual_File;
192193
begin
193194
for X of Files.all loop
194195
if X.Has_Suffix (".gpr") then
195-
GPRs_Found := GPRs_Found + 1;
196-
exit when GPRs_Found > 1;
197-
Found := X;
196+
Candidates.Append (LSP.Utils.To_Virtual_String (X));
198197
end if;
199198
end loop;
200199

201200
GNATCOLL.VFS.Unchecked_Free (Files);
202201

203-
if GPRs_Found = 1 then
204-
Project_File := LSP.Utils.To_Virtual_String (Found);
202+
if Candidates.Length = 1 then
203+
Project_File := Candidates.First_Element;
205204

206205
-- Report how we found the project
207206
Self.Project_Status.Set_Project_Type
208207
(LSP.Ada_Project_Loading.Single_Project_Found);
209208

210209
Tracer.Trace_Text ("Found unique project: " & Project_File);
211210
else
212-
Tracer.Trace
213-
("Found " & GPRs_Found'Image & " projects at the root");
211+
Tracer.Trace_Text
212+
("Found "
213+
& To_Virtual_String (Candidates.Length'Image)
214+
& " projects at the root:"
215+
& VSS.Characters.Latin.Line_Feed
216+
& Candidates.Join (VSS.Characters.Latin.Line_Feed));
214217
end if;
215218
end;
216219
end if;
@@ -276,12 +279,12 @@ package body LSP.Ada_Handlers.Project_Loading is
276279
-- We didn't find a project file. Let's load an implicit project. We
277280
-- reach this point either because there are no GPR projects at the
278281
-- root, or there are more than one.
279-
pragma Assert (GPRs_Found = 0 or GPRs_Found > 1);
282+
pragma Assert (Candidates.Length = 0 or Candidates.Length > 1);
280283

281284
Load_Implicit_Project
282285
(Self,
283-
(if GPRs_Found = 0 then LSP.Ada_Project_Loading.No_Project
284-
elsif GPRs_Found > 1
286+
(if Candidates.Length = 0 then LSP.Ada_Project_Loading.No_Project
287+
elsif Candidates.Length > 1
285288
then LSP.Ada_Project_Loading.Multiple_Projects
286289
else LSP.Ada_Project_Loading.Project_Not_Found));
287290
end if;

source/ada/lsp-ada_handlers.adb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ with LSP.Utils;
9494

9595
package body LSP.Ada_Handlers is
9696

97+
Tracer_Config : constant LSP.GNATCOLL_Tracers.Tracer :=
98+
LSP.GNATCOLL_Tracers.Create ("ALS.CONFIG", GNATCOLL.Traces.On);
99+
97100
pragma Style_Checks ("o"); -- check subprogram bodies in alphabetical ordr
98101

99102
subtype AlsReferenceKind_Array is LSP.Structures.AlsReferenceKind_Set;
@@ -2486,6 +2489,8 @@ package body LSP.Ada_Handlers is
24862489
if not Value.initializationOptions.Is_Empty then
24872490
Self.Tracer.Trace
24882491
("Processing initializationOptions from initialize request");
2492+
Tracer_Config.Trace
2493+
("initializationOptions = " & Value.initializationOptions'Image);
24892494
-- The expected structure is this:
24902495
-- "initializationOptions": {
24912496
-- "ada": {
@@ -2495,7 +2500,12 @@ package body LSP.Ada_Handlers is
24952500
-- }
24962501
-- }
24972502
declare
2498-
New_Configuration : LSP.Ada_Configurations.Configuration;
2503+
New_Configuration : LSP.Ada_Configurations.Configuration :=
2504+
Self.Configuration;
2505+
-- Start from the existing configuration so that settings parsed
2506+
-- from configuration files are preserved, and the settings from
2507+
-- initialize request are applied on top.
2508+
24992509
Messages : VSS.String_Vectors.Virtual_String_Vector;
25002510
begin
25012511
-- Parse the configuration.
@@ -3932,6 +3942,8 @@ package body LSP.Ada_Handlers is
39323942
(Self : in out Message_Handler;
39333943
Value : LSP.Ada_Configurations.Configuration'Class) is
39343944
begin
3945+
Tracer_Config.Trace ("Setting configuration: " & Value'Image);
3946+
39353947
Self.Configuration := LSP.Ada_Configurations.Configuration (Value);
39363948

39373949
-- The base configuration is still the default one, meaning that

testsuite/ada_lsp/config_local/test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ async def func(lsp: ALSLanguageClient) -> None:
1919
response = await lsp.getCurrentProject()
2020
assert response
2121
assertEqual(response, URI("non-root/p2.gpr"))
22+
23+
24+
@test(als_settings={})
25+
async def test_init_override(lsp: ALSLanguageClient) -> None:
26+
# In this test we want to check that settings obtained in the initializationOptions
27+
# of the 'initialize' request are interpreted on top of the file-based
28+
# configuration.
29+
#
30+
# To check that we send an empty dictionary of settings in the initialize request
31+
# (the 'als_settings' argument above), and we check that the applicable project is
32+
# still the one specified in .als.json, i.e. the initialize request did not
33+
# overwrite the file settings.
34+
assertEqual(await lsp.getCurrentProject(), URI("non-root/p2.gpr"))

testsuite/ada_lsp/config_priority/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
The goal of this test is to check that when --config CONFIG_FILE is given, that
3-
configuration is loaded.
2+
The goal of the tests below is to check the priority with which the ALS considers
3+
configuration files and the initialize request.
44
55
To test that we create a unique p1.gpr at the root. If there was no configuration,
66
p1.gpr would be loaded automatically. It's a failure sentinel.

0 commit comments

Comments
 (0)