From 8fbd7ebb877da2698d70db7411ae2dcf0ddcdcbf Mon Sep 17 00:00:00 2001 From: Mathias Lang Date: Fri, 3 Jan 2025 15:26:28 +0100 Subject: [PATCH] fix: Ensure nameless FSEntry is not and cannot be constructed We currently do not expect to have nameless FSEntry save for the root, so add an assertiong that would catch such a case. If a use case arise, this can always be removed. --- source/dub/internal/io/mockfs.d | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/source/dub/internal/io/mockfs.d b/source/dub/internal/io/mockfs.d index ade5adec9..db9a1395c 100644 --- a/source/dub/internal/io/mockfs.d +++ b/source/dub/internal/io/mockfs.d @@ -47,8 +47,12 @@ public final class MockFS : Filesystem { import std.algorithm.iteration : reduce; const abs = path.absolute(); + auto segments = path.bySegment; + // `library-nonet` (using vibe.d) has an empty front for absolute path, + // while our built-in module (in vibecompat) does not. + if (abs && segments.front.name.length == 0) segments.popFront(); reduce!((FSEntry dir, segment) => dir.mkdir(segment.name))( - (abs ? this.root : this.cwd), path.bySegment); + (abs ? this.root : this.cwd), segments); } /// Ditto @@ -276,11 +280,15 @@ public final class MockFS : Filesystem { import std.algorithm.iteration : reduce; const abs = path.absolute(); + auto segments = path.bySegment; + // `library-nonet` (using vibe.d) has an empty front for absolute path, + // while our built-in module (in vibecompat) does not. + if (abs && segments.front.name.length == 0) segments.popFront(); // Casting away constness because no good way to do this with `inout`, // but `FSEntry.lookup` is `inout` too. return cast(inout(FSEntry)) reduce!( (FSEntry dir, segment) => dir ? dir.lookup(segment.name) : null) - (cast() (abs ? this.root : this.cwd), path.bySegment); + (cast() (abs ? this.root : this.cwd), segments); } } @@ -333,6 +341,10 @@ public class FSEntry import std.datetime.date; SysTime DefaultTime = SysTime(DateTime(2020, 01, 01)); + assert(n.length > 0, + "FSentry.this(%s, %s, %s) called with empty name" + .format(p.path(), t, n)); + this.attributes.type = t; this.parent = p; this.name = n;