Skip to content

Commit 3afbf62

Browse files
Add a new diagnostic source for Alire issues
This source publishes diagnostics on the alire.toml file in case of errors found while trying to setup an Alire project (e.g: an alire.toml file is present but 'alr' is not available in the user's PATH). Add a test for this. For eng/ide/ada_language_server#1533
1 parent c7123df commit 3afbf62

15 files changed

+357
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ section below it for the last release. -->
1111
* Fix cross-references (navigation, call hierarchy, finding all references) when the project is an Extending project.
1212
* [GNATformat](https://github.com/AdaCore/gnatformat) is now the default back-end for LSP formatting requests
1313
* Add tasks and a CodeLens to run a given main with GNATemulator on non-native projects
14+
* Diagnostics are now emitted for issues encountered when trying to load an Alire crate
1415

1516
## 26.0.202412190
1617

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2018-2023, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
with LSP.Structures;
19+
with LSP.Enumerations;
20+
21+
package body LSP.Ada_Handlers.Alire_Diagnostics is
22+
23+
---------------------
24+
-- Get_Diagnostics --
25+
---------------------
26+
27+
overriding
28+
procedure Get_Diagnostics
29+
(Self : in out Diagnostic_Source;
30+
Diagnostics : out LSP.Structures.Diagnostic_Vector;
31+
Target_File : out GNATCOLL.VFS.Virtual_File)
32+
is
33+
Diag : LSP.Structures.Diagnostic;
34+
begin
35+
for Msg of Self.Handler.Project_Status.Get_Alire_Messages loop
36+
Diag.message := Msg;
37+
Diag.a_range := LSP.Structures.A_Range'((0, 0), (0, 0));
38+
Diag.severity := (True, LSP.Enumerations.Warning);
39+
Diag.source := Alire_Diagnostics_Source_ID;
40+
Diagnostics.Append (Diag);
41+
end loop;
42+
43+
-- Emit Alire-related diagnostics directly on the 'alire.toml' file
44+
Target_File :=
45+
GNATCOLL.VFS.Create_From_Base
46+
(Base_Name => "alire.toml",
47+
Base_Dir => Self.Handler.Client.Root_Directory.Full_Name);
48+
end Get_Diagnostics;
49+
50+
------------------------
51+
-- Has_New_Diagnostic --
52+
------------------------
53+
54+
overriding
55+
function Has_New_Diagnostic (Self : in out Diagnostic_Source) return Boolean
56+
is
57+
begin
58+
return not Self.Handler.Project_Status.Get_Alire_Messages.Is_Empty;
59+
end Has_New_Diagnostic;
60+
61+
end LSP.Ada_Handlers.Alire_Diagnostics;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
------------------------------------------------------------------------------
2+
-- Language Server Protocol --
3+
-- --
4+
-- Copyright (C) 2018-2023, AdaCore --
5+
-- --
6+
-- This is free software; you can redistribute it and/or modify it under --
7+
-- terms of the GNU General Public License as published by the Free Soft- --
8+
-- ware Foundation; either version 3, or (at your option) any later ver- --
9+
-- sion. This software is distributed in the hope that it will be useful, --
10+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
11+
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
12+
-- License for more details. You should have received a copy of the GNU --
13+
-- General Public License distributed with this software; see file --
14+
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
15+
-- of the license. --
16+
------------------------------------------------------------------------------
17+
18+
with GNATCOLL.VFS;
19+
with LSP.Diagnostic_Sources;
20+
21+
package LSP.Ada_Handlers.Alire_Diagnostics is
22+
23+
Alire_Diagnostics_Source_ID : constant VSS.Strings.Virtual_String :=
24+
"ada.alire";
25+
type Diagnostic_Source
26+
(Handler : not null access LSP.Ada_Handlers.Message_Handler'Class)
27+
is limited new LSP.Diagnostic_Sources.Workspace_Diagnostic_Source with private;
28+
29+
overriding procedure Get_Diagnostics
30+
(Self : in out Diagnostic_Source;
31+
Diagnostics : out LSP.Structures.Diagnostic_Vector;
32+
Target_File : out GNATCOLL.VFS.Virtual_File);
33+
-- Fill Alire diagnostics for the given document.
34+
35+
overriding
36+
function Has_New_Diagnostic
37+
(Self : in out Diagnostic_Source) return Boolean;
38+
39+
private
40+
41+
type Diagnostic_Source
42+
(Handler : not null access LSP.Ada_Handlers.Message_Handler'Class)
43+
is limited new LSP.Diagnostic_Sources.Workspace_Diagnostic_Source with null record;
44+
45+
end LSP.Ada_Handlers.Alire_Diagnostics;

source/ada/lsp-ada_handlers-project_loading.adb

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,14 @@ package body LSP.Ada_Handlers.Project_Loading is
119119
procedure Ensure_Project_Loaded (Self : in out Message_Handler'Class) is
120120
use type VSS.Strings.Virtual_String;
121121

122-
GPRs_Found : Natural := 0;
123-
Project_File : VSS.Strings.Virtual_String :=
122+
GPRs_Found : Natural := 0;
123+
Project_File : VSS.Strings.Virtual_String :=
124124
Self.Configuration.Project_File;
125125
GPR_Configuration_File : VSS.Strings.Virtual_String :=
126126
Self.Configuration.GPR_Configuration_File;
127-
Is_Alire_Crate : constant Boolean := Alire.Is_Alire_Crate (Self.Client);
128-
Has_Alire : Boolean;
129-
Alire_Errors : VSS.Strings.Virtual_String;
130-
127+
Is_Alire_Crate : constant Boolean :=
128+
Alire.Is_Alire_Crate (Self.Client);
129+
Alire_Error : VSS.Strings.Virtual_String;
131130
begin
132131
if not Self.Contexts.Is_Empty then
133132
-- Rely on the fact that there is at least one context initialized
@@ -157,22 +156,17 @@ package body LSP.Ada_Handlers.Project_Loading is
157156

158157
LSP.Alire.Determine_Alire_Project
159158
(Root => Self.Client.Root_Directory.Display_Full_Name,
160-
Has_Alire => Has_Alire,
161-
Error => Alire_Errors,
159+
Error => Alire_Error,
162160
Project => Project_File);
163161

164-
if not Has_Alire then
165-
Tracer.Trace
166-
("'alr' is not available on PATH, cannot determine project from Alire."
167-
& " Falling back to other methods for finding a project file.");
168-
-- TODO consider reporting this situation to the client as a
169-
-- diagnostic instead of just a log trace
170-
elsif not Alire_Errors.Is_Empty then
171-
Tracer.Trace_Text ("Encountered errors: " & Alire_Errors);
162+
if not Alire_Error.Is_Empty then
163+
Tracer.Trace_Text ("Encountered errors: " & Alire_Error);
164+
Self.Project_Status.Set_Alire_Messages ([Alire_Error]);
172165
else
173166
-- Report how we found the project
174167
Self.Project_Status.Set_Project_Type
175168
(LSP.Ada_Project_Loading.Alire_Project);
169+
Self.Project_Status.Set_Alire_Messages ([]);
176170
end if;
177171
end if;
178172

@@ -225,22 +219,24 @@ package body LSP.Ada_Handlers.Project_Loading is
225219
else
226220
"iso-8859-1");
227221

228-
Errors : VSS.Strings.Virtual_String;
229222
begin
230-
if Is_Alire_Crate then
223+
-- We have an Alire crate and we did not encounter any issue
224+
-- when trying to determine the project file to load via
225+
-- 'alr show': try to setup the needed environment through Alire.
226+
if Is_Alire_Crate and then Alire_Error.Is_Empty then
231227
if LSP.Alire.Should_Setup_Alire_Env (Self.Client) then
232228
Tracer.Trace ("Setting environment from 'alr printenv'");
233229

234230
LSP.Alire.Setup_Alire_Env
235231
(Root =>
236232
Self.Client.Root_Directory.Display_Full_Name,
237-
Has_Alire => Has_Alire,
238-
Error => Errors,
233+
Error => Alire_Error,
239234
Environment => Environment);
240235

241-
if not Errors.Is_Empty then
236+
if not Alire_Error.Is_Empty then
242237
Tracer.Trace_Text
243-
("Encountered errors with Alire:" & LF & Errors);
238+
("Encountered errors with Alire:" & LF & Alire_Error);
239+
Self.Project_Status.Set_Alire_Messages ([Alire_Error]);
244240
end if;
245241
else
246242
Tracer.Trace
@@ -726,6 +722,7 @@ package body LSP.Ada_Handlers.Project_Loading is
726722
-- in all contexts would cost too much memory
727723
if Runtime_Indexing.Is_Active
728724
and then Self.Project_Tree.Is_Defined
725+
and then Self.Project_Tree.Root_Project.Is_Defined
729726
and then Self.Project_Tree.Root_Project.Kind not in GPR2.Aggregate_Kind
730727
then
731728
-- Mark all the predefined sources too (runtime)

source/ada/lsp-ada_handlers.adb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ with LSP.Ada_Completions.Use_Clauses;
5858
with LSP.Ada_Completions;
5959
with LSP.Ada_Documentation;
6060
with LSP.Ada_Empty_Handlers;
61+
with LSP.Ada_Handlers.Alire_Diagnostics;
6162
with LSP.Ada_Handlers.Call_Hierarchy;
6263
with LSP.Ada_Handlers.Formatting;
6364
with LSP.Ada_Handlers.Invisibles;
@@ -429,8 +430,12 @@ package body LSP.Ada_Handlers is
429430
Self.File_Monitor :=
430431
new LSP.Servers.FS_Watch.FS_Watch_Monitor (Self.Server);
431432
Self.Workspace_Diagnostic_Sources :=
432-
[new LSP.Ada_Handlers.Project_Diagnostics.Diagnostic_Source
433-
(Self'Unchecked_Access)];
433+
[
434+
new LSP.Ada_Handlers.Project_Diagnostics.Diagnostic_Source
435+
(Self'Unchecked_Access),
436+
437+
new LSP.Ada_Handlers.Alire_Diagnostics.Diagnostic_Source
438+
(Self'Unchecked_Access)];
434439

435440
Self.Load_Config_Files (CLI_Config_File);
436441
end Initialize;

source/ada/lsp-ada_project_loading.adb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ package body LSP.Ada_Project_Loading is
293293
when Single_Project_Found .. Implicit_Project =>
294294
-- Even if the project is valid this is not an expected one
295295
return True;
296+
296297
when others =>
297-
return Project.Status /= Valid_Project
298+
return
299+
Project.Status /= Valid_Project
298300
or else Has_Pertinent_GPR2_Messages (Project);
299301
end case;
300302
end Has_Diagnostics;
@@ -361,8 +363,7 @@ package body LSP.Ada_Project_Loading is
361363
-----------------------
362364

363365
procedure Set_GPR2_Messages
364-
(Project : in out Project_Status_Type;
365-
GPR2_Messages : GPR2.Log.Object) is
366+
(Project : in out Project_Status_Type; GPR2_Messages : GPR2.Log.Object) is
366367
begin
367368
Project.GPR2_Messages := GPR2_Messages;
368369

@@ -372,16 +373,33 @@ package body LSP.Ada_Project_Loading is
372373
-- Valid_Project_With_Warning.
373374
if Project.Status = Valid_Project then
374375
if Project.GPR2_Messages.Has_Element
375-
(Hint => False,
376-
Warning => True,
377-
Error => True,
378-
Lint => False)
376+
(Hint => False, Warning => True, Error => True, Lint => False)
379377
then
380378
Project.Status := Valid_Project_With_Warning;
381379
end if;
382380
end if;
383381
end Set_GPR2_Messages;
384382

383+
------------------------
384+
-- Set_Alire_Messages --
385+
------------------------
386+
387+
procedure Set_Alire_Messages
388+
(Project : in out Project_Status_Type;
389+
Alire_Messages : VSS.String_Vectors.Virtual_String_Vector) is
390+
begin
391+
Project.Alire_Messages := Alire_Messages;
392+
end Set_Alire_Messages;
393+
394+
------------------------
395+
-- Get_Alire_Messages --
396+
------------------------
397+
398+
function Get_Alire_Messages
399+
(Project : Project_Status_Type)
400+
return VSS.String_Vectors.Virtual_String_Vector
401+
is (Project.Alire_Messages);
402+
385403
----------------------
386404
-- Set_Project_File --
387405
----------------------

source/ada/lsp-ada_project_loading.ads

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
with GNATCOLL.VFS;
1919
with GPR2.Log;
2020
with LSP.Structures;
21+
with VSS.String_Vectors;
2122

2223
package LSP.Ada_Project_Loading is
2324

@@ -69,55 +70,57 @@ package LSP.Ada_Project_Loading is
6970
-- @value Project_Not_Found: the configured project was not found.
7071

7172
procedure Set_Load_Status
72-
(Project : in out Project_Status_Type;
73-
Status : Project_Status);
73+
(Project : in out Project_Status_Type; Status : Project_Status);
7474
-- Set the status of the project
7575

7676
function Get_Load_Status
7777
(Project : Project_Status_Type) return Project_Status;
7878
-- Return the status
7979

8080
procedure Set_Project_Type
81-
(Project : in out Project_Status_Type;
82-
Project_Type : Project_Types);
81+
(Project : in out Project_Status_Type; Project_Type : Project_Types);
8382
-- Set the type of the project.
8483

8584
function Get_Project_Type
86-
(Project : in out Project_Status_Type)
87-
return Project_Types;
85+
(Project : in out Project_Status_Type) return Project_Types;
8886
-- Get the type of the project.
8987

9088
procedure Set_Missing_Ada_Runtime
91-
(Project : in out Project_Status_Type;
92-
Value : Boolean);
89+
(Project : in out Project_Status_Type; Value : Boolean);
9390
-- Should be called when the runtime for Project is found
9491

9592
procedure Set_GPR2_Messages
96-
(Project : in out Project_Status_Type;
97-
GPR2_Messages : GPR2.Log.Object);
93+
(Project : in out Project_Status_Type; GPR2_Messages : GPR2.Log.Object);
9894
-- Set the messages related to GPR2 project loading
9995

96+
procedure Set_Alire_Messages
97+
(Project : in out Project_Status_Type;
98+
Alire_Messages : VSS.String_Vectors.Virtual_String_Vector);
99+
-- Set the messages related to Alire project loading
100+
101+
function Get_Alire_Messages
102+
(Project : Project_Status_Type)
103+
return VSS.String_Vectors.Virtual_String_Vector;
104+
-- Get the messages related to Alire project loading
105+
100106
procedure Set_Project_File
101107
(Project : in out Project_Status_Type;
102108
Project_File : GNATCOLL.VFS.Virtual_File);
103109
-- Set the file we are trying to load
104110

105111
function Get_Project_File
106-
(Project : in out Project_Status_Type)
107-
return GNATCOLL.VFS.Virtual_File;
112+
(Project : in out Project_Status_Type) return GNATCOLL.VFS.Virtual_File;
108113
-- Get the project file that was loaded (or attempted)
109114

110115
function Is_Implicit_Fallback
111116
(Project : Project_Status_Type) return Boolean;
112117
-- Return True if the implicit project has been loaded
113118

114-
function Is_Project_Loaded
115-
(Project : Project_Status_Type) return Boolean;
119+
function Is_Project_Loaded (Project : Project_Status_Type) return Boolean;
116120
-- Return True if the project was loaded
117121

118122
function Has_New_Diagnostics
119-
(Old_Project : Project_Status_Type;
120-
New_Project : Project_Status_Type)
123+
(Old_Project : Project_Status_Type; New_Project : Project_Status_Type)
121124
return Boolean;
122125
-- Return True when the New_Project has a different status or different
123126
-- messages compare to Old_Project
@@ -130,8 +133,7 @@ package LSP.Ada_Project_Loading is
130133
-- Add code actions related to Project in Result
131134

132135
function Get_Diagnostics
133-
(Project : Project_Status_Type)
134-
return LSP.Structures.Diagnostic_Vector;
136+
(Project : Project_Status_Type) return LSP.Structures.Diagnostic_Vector;
135137
-- Compute and return the diagnostics of the project
136138

137139
private
@@ -151,6 +153,10 @@ private
151153

152154
GPR2_Messages : GPR2.Log.Object := GPR2.Log.Undefined;
153155
-- The warning/error messages emitted by GPR2 while loading the project.
156+
157+
Alire_Messages : VSS.String_Vectors.Virtual_String_Vector;
158+
-- The warning/error messages related to Alire while attempting to
159+
-- load a project from a workspace that contains an alire.toml file.
154160
end record;
155161

156162
No_Project_Status : constant Project_Status_Type :=
@@ -159,7 +165,8 @@ private
159165
Status => Valid_Project,
160166
Project_File => GNATCOLL.VFS.No_File,
161167
Missing_Ada_Runtime => False,
162-
GPR2_Messages => <>);
168+
GPR2_Messages => <>,
169+
Alire_Messages => <>);
163170

164171
function Get_Load_Status
165172
(Project : Project_Status_Type) return Project_Status is (Project.Status);

0 commit comments

Comments
 (0)