Skip to content

Commit 62e35d7

Browse files
committed
Fix loading of ALIRE project
For eng/ide/ada_language_server#1359
1 parent edabb2a commit 62e35d7

File tree

3 files changed

+52
-62
lines changed

3 files changed

+52
-62
lines changed

source/ada/lsp-ada_handlers-alire.adb

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@ package body LSP.Ada_Handlers.Alire is
7777
-- Run_Alire --
7878
---------------
7979

80-
procedure Run_Alire
80+
procedure Determine_Alire_Project
8181
(Root : String;
8282
Has_Alire : out Boolean;
8383
Error : out VSS.Strings.Virtual_String;
84-
Project : out VSS.Strings.Virtual_String;
85-
Environment : in out GPR2.Environment.Object)
84+
Project : out VSS.Strings.Virtual_String)
8685
is
8786
use type GNAT.OS_Lib.String_Access;
8887

@@ -96,10 +95,6 @@ package body LSP.Ada_Handlers.Alire is
9695
VSS.Regular_Expressions.To_Regular_Expression
9796
(" +Project_File: ([^\n]+)");
9897

99-
Export_Pattern : constant VSS.Regular_Expressions.Regular_Expression :=
100-
VSS.Regular_Expressions.To_Regular_Expression
101-
("export ([^=]+)=""([^\n]+)""");
102-
10398
Lines : VSS.String_Vectors.Virtual_String_Vector;
10499
begin
105100
Project.Clear;
@@ -117,66 +112,64 @@ package body LSP.Ada_Handlers.Alire is
117112
return;
118113
end if;
119114

120-
-- Find project file in `alr show` output
121-
122-
declare
123-
First : constant VSS.Strings.Virtual_String := Lines (1);
124-
-- We should keep copy of regexp subject string while we have a match
125-
Match : constant VSS.Regular_Expressions.Regular_Expression_Match :=
126-
Crate_Pattern.Match (First);
127-
begin
128-
if Match.Has_Match then
129-
Project := Match.Captured (1);
130-
Project.Append (".gpr");
131-
end if;
132-
end;
133-
115+
-- Find project file in `alr show` output. There are several cases to cover.
116+
--
117+
-- If alire.toml contains a "project-files" entry providing a list of
118+
-- projects, `alr show` prints one line per project of the pattern:
119+
-- Project_File: ...
120+
--
121+
-- Otherwise `alr show` doesn't print a Project_File line and we can expect
122+
-- there to be a project named identically to the crate name.
123+
--
124+
-- So the strategy below is to first use the crate name as a project
125+
-- name, and then override it if a Project_File line is found (the first
126+
-- one is taken).
127+
128+
-- When `alr show` is called in a directory where /alire/ and /config/
129+
-- don't exist, the command auto-generates those directories and prints a
130+
-- few lines of output before the actual crate information. So we can't
131+
-- assume that the crate name will be on the first line.
134132
for Line of Lines loop
135133
declare
136-
Match : constant VSS.Regular_Expressions.Regular_Expression_Match
137-
:= Project_Pattern.Match (Line, Anchored);
134+
-- We should keep copy of regexp subject string while we have a match
135+
Match : constant VSS.Regular_Expressions.Regular_Expression_Match :=
136+
Crate_Pattern.Match (Line);
138137
begin
139138
if Match.Has_Match then
140139
Project := Match.Captured (1);
140+
Project.Append (".gpr");
141141
exit;
142142
end if;
143143
end;
144144
end loop;
145145

146-
if Project.Is_Empty then
147-
Error.Append ("No project file is found by alire");
148-
end if;
149-
150-
-- Find variables in `alr printenv` output
151-
152-
Start_Alire
153-
(ALR.all, "--non-interactive", "printenv", Root, Error, Lines);
154-
155-
GNAT.OS_Lib.Free (ALR);
156-
157-
-- Find variables in `alr printenv` output
158-
146+
-- Next check if there is a Project_File line, take the first one.
159147
for Line of Lines loop
160148
declare
161149
Match : constant VSS.Regular_Expressions.Regular_Expression_Match
162-
:= Export_Pattern.Match (Line, Anchored);
150+
:= Project_Pattern.Match (Line, Anchored);
163151
begin
164152
if Match.Has_Match then
165-
Environment.Insert
166-
(Key => VSS.Strings.Conversions.To_UTF_8_String
167-
(Match.Captured (1)),
168-
Value => VSS.Strings.Conversions.To_UTF_8_String
169-
(Match.Captured (2)));
153+
Project := Match.Captured (1);
154+
exit;
170155
end if;
171156
end;
172157
end loop;
173-
end Run_Alire;
158+
159+
if Project.Is_Empty then
160+
Error.Append ("No project file could be determined from the output of `alr show`:");
161+
for Line of Lines loop
162+
Error.Append (Line);
163+
end loop;
164+
end if;
165+
166+
end Determine_Alire_Project;
174167

175168
---------------
176169
-- Run_Alire --
177170
---------------
178171

179-
procedure Run_Alire
172+
procedure Setup_Alire_Env
180173
(Root : String;
181174
Has_Alire : out Boolean;
182175
Error : out VSS.Strings.Virtual_String;
@@ -221,7 +214,7 @@ package body LSP.Ada_Handlers.Alire is
221214
end if;
222215
end;
223216
end loop;
224-
end Run_Alire;
217+
end Setup_Alire_Env;
225218

226219
-----------------
227220
-- Start_Alire --

source/ada/lsp-ada_handlers-alire.ads

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ with VSS.Strings;
2424
private
2525
package LSP.Ada_Handlers.Alire is
2626

27-
procedure Run_Alire
27+
procedure Determine_Alire_Project
2828
(Root : String;
2929
Has_Alire : out Boolean;
3030
Error : out VSS.Strings.Virtual_String;
31-
Project : out VSS.Strings.Virtual_String;
32-
Environment : in out GPR2.Environment.Object);
31+
Project : out VSS.Strings.Virtual_String);
3332
-- if Root directory contains `alire.toml` file, then run
34-
-- `alr printenv` and fetch the first project from `alire.toml`.
33+
-- `alr show` and determine the project from the output.
3534

36-
procedure Run_Alire
35+
procedure Setup_Alire_Env
3736
(Root : String;
3837
Has_Alire : out Boolean;
3938
Error : out VSS.Strings.Virtual_String;
4039
Environment : in out GPR2.Environment.Object);
41-
-- The same as above, but without fetching the project file
40+
-- Run `alr printenv` and set up the obtained environment variables
4241

4342
end LSP.Ada_Handlers.Alire;

source/ada/lsp-ada_handlers-project_loading.adb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,24 +428,22 @@ package body LSP.Ada_Handlers.Project_Loading is
428428

429429
if Project.Is_Empty then
430430

431-
LSP.Ada_Handlers.Alire.Run_Alire
431+
LSP.Ada_Handlers.Alire.Determine_Alire_Project
432432
(Root => Root (Self).Display_Full_Name,
433433
Has_Alire => Has_Alire,
434434
Error => Errors,
435-
Project => Project,
436-
Environment => Environment);
435+
Project => Project);
437436

438437
Status := Alire_Project;
439-
else
438+
end if;
440439

441-
LSP.Ada_Handlers.Alire.Run_Alire
442-
(Root => Root (Self).Display_Full_Name,
443-
Has_Alire => Has_Alire,
444-
Error => Errors,
445-
Environment => Environment);
440+
LSP.Ada_Handlers.Alire.Setup_Alire_Env
441+
(Root => Root (Self).Display_Full_Name,
442+
Has_Alire => Has_Alire,
443+
Error => Errors,
444+
Environment => Environment);
446445

447-
Status := Valid_Project_Configured;
448-
end if;
446+
Status := Valid_Project_Configured;
449447

450448
if Has_Alire and then not Errors.Is_Empty then
451449

0 commit comments

Comments
 (0)