Skip to content

Commit

Permalink
Fix for cache invalidation
Browse files Browse the repository at this point in the history
An unrelated bug is fixed that happened when not using an actual binary crate
for a compiler in the testsuite.
  • Loading branch information
mosteo committed Jan 18, 2024
1 parent e2f122f commit 2d7960d
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/alire/alire-builds-hashes.adb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ package body Alire.Builds.Hashes is
Add ("profile",
Rel.Name.As_String,
Root.Configuration.Build_Profile (Rel.Name)'Image);
exception
when others =>
Trace.Error
("While hashing: missing build profile for "
& Rel.Milestone.TTY_Image);
raise;
end Add_Profile;

------------------
Expand All @@ -123,6 +129,12 @@ package body Alire.Builds.Hashes is
Add ("switches",
Rel.Name.As_String,
Switches.To_Vector.Flatten (","));
exception
when others =>
Trace.Error
("While hashing: missing switches for "
& Rel.Milestone.TTY_Image);
raise;
end Add_Switches;

-------------------
Expand Down
8 changes: 8 additions & 0 deletions src/alire/alire-toolchains-solutions.adb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package body Alire.Toolchains.Solutions is
return Alire.Solutions.Solution
is

Redeployed : Boolean := False;

------------------------
-- Redeploy_If_Needed --
------------------------
Expand All @@ -29,6 +31,7 @@ package body Alire.Toolchains.Solutions is

-- It must be redeployed
Put_Warning ("Tool " & Mil.TTY_Image & " is missing, redeploying...");
Redeployed := True;

Toolchains.Deploy (Index.Find (Mil.Crate, Mil.Version));
end Redeploy_If_Needed;
Expand Down Expand Up @@ -65,6 +68,11 @@ package body Alire.Toolchains.Solutions is
end if;
end loop;

if Redeployed then
Trace.Debug ("Missing tools were redeployed, invalidating cache.");
Invalidate_Available_Cache;
end if;

return Result;
end Add_Toolchain;

Expand Down
22 changes: 16 additions & 6 deletions src/alire/alire-toolchains.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
with AAA.Text_IO;

with Ada.Calendar;
with Ada.Containers.Indefinite_Vectors;
with Ada.Directories;

Expand All @@ -26,8 +27,6 @@ package body Alire.Toolchains is

use Directories.Operators;

procedure Invalidate_Available_Cache;

--------------
-- Any_Tool --
--------------
Expand Down Expand Up @@ -491,7 +490,8 @@ package body Alire.Toolchains is
Invalidate_Available_Cache;
end Unconfigure;

Available_Cached : Releases.Containers.Release_Set;
Available_Cached : Releases.Containers.Release_Set;
Available_Timestamp : Ada.Calendar.Time;

--------------------------------
-- Invalidate_Available_Cache --
Expand Down Expand Up @@ -543,18 +543,27 @@ package body Alire.Toolchains is
end if;
end Detect;

use type Ada.Calendar.Time;

begin
-- Early exit with cached available toolchains. Looking for toolchains
-- on disk is expensive. We invalidate the cache on toolchain changes.
-- on disk is expensive. We rely on folder modification time to
-- re-detect available toolchains.

if not Available_Cached.Is_Empty then
if not Available_Cached.Is_Empty and then
Ada.Directories.Exists (Path) and then
Ada.Directories.Modification_Time (Path) = Available_Timestamp
then
return Available_Cached;
end if;

if Ada.Directories.Exists (Path) then
Directories.Traverse_Tree
(Start => Path,
Doing => Detect'Access);

-- Store last time we went looking for releases on disk
Available_Timestamp := Ada.Directories.Modification_Time (Path);
end if;

-- Include external toolchain members when they are in use
Expand All @@ -573,7 +582,8 @@ package body Alire.Toolchains is
end loop;
end loop;

Available_Cached := Result;
Available_Cached := Result;

return Result;
end Available;

Expand Down
6 changes: 6 additions & 0 deletions src/alire/alire-toolchains.ads
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ package Alire.Toolchains is

private

procedure Invalidate_Available_Cache;
-- This refers to the available toolchains, which are costly to detect
-- but sometimes change during a single run. Manual cache invalidation
-- should not be necessary if the filesystem properly reports directory
-- modification times, but we can leave it as is a fallback.

-----------------------
-- Assistant_Enabled --
-----------------------
Expand Down
Binary file not shown.
Binary file added testsuite/fixtures/crates/gprbuild_mock_1.0.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ licenses = "GPL-3.0-only OR MIT"
maintainers = ["[email protected]"]
maintainers-logins = ["mylogin"]

[origin]
url = "file:."
# Conditional origin so it's considered a binary crate
[origin."case(os)"."linux"]
url = "file:../../../crates/gnat_native_mock_1.0.zip"
hashes = ["sha256:db87865b59d7d733e4104b3c0f29c8bf590d5ff0b3ea2d61bded4e47beabf1a3"]
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ licenses = "GPL-3.0-only OR MIT"
maintainers = ["[email protected]"]
maintainers-logins = ["mylogin"]

[origin]
url = "file:."
# Conditional origin so it's considered a binary crate
[origin."case(os)"."linux"]
url = "file:../../../crates/gprbuild_mock_1.0.zip"
hashes = ["sha256:e3d52b4a441a56ab1c0175ee8ea407864b72879cf73184a9f7d68eef53a87451"]
9 changes: 5 additions & 4 deletions testsuite/tests/toolchain/missing-tool-redeploy/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
import os
from shutil import rmtree
from drivers.alr import init_local_crate, run_alr
from drivers.asserts import assert_match

# We can trigger a buggy situation by configuring a toolchain, removing
# manually one tool (as if we had moved the cache), and running `alr printenv`.
# manually the tool (as if we had moved the cache), and running `alr printenv`.

run_alr("toolchain", "--select", "gprbuild", "gnat_native")
init_local_crate()

# Remove the tool manually through the filesystem
rmtree(os.path.join(os.environ["ALR_CONFIG"], "cache"))

# This should not fail
run_alr("printenv")

# This should not fail. A message should warn of redeployments happening.
p = run_alr("printenv", quiet=False)
assert_match(".*Tool .* is missing, redeploying", p.out)

print("SUCCESS")

0 comments on commit 2d7960d

Please sign in to comment.