aboutsummaryrefslogtreecommitdiff
path: root/src/Module.zig
diff options
context:
space:
mode:
authorRyan Liptak <squeek502@hotmail.com>2023-12-24 04:45:53 -0800
committerVeikka Tuominen <git@vexu.eu>2024-01-04 17:34:34 +0200
commit0527cab71bac6067ceae6a1fb0a0f401a5027607 (patch)
tree025db0799e40c1489ce51f68fc28b60d14c45718 /src/Module.zig
parent9a1608509300fcdf118d82063bbf1481a10ca551 (diff)
downloadzig-0527cab71bac6067ceae6a1fb0a0f401a5027607.tar.gz
zig-0527cab71bac6067ceae6a1fb0a0f401a5027607.zip
Use `std.fs.path.relative` for `@import` and `@embedFile` sub paths
Fixes edge cases where the `startsWith` that was used previously would return a false positive on a resolved path like `foo.zig` when the resolved root was `foo`. Before this commit, such a path would be treated as a sub path of 'foo' with a resolved sub file path of 'zig' (and the `.` would be assumed to be a path separator). After this commit, `foo.zig` will be correctly treated as outside of the root of `foo`. Closes #18355
Diffstat (limited to 'src/Module.zig')
-rw-r--r--src/Module.zig28
1 files changed, 10 insertions, 18 deletions
diff --git a/src/Module.zig b/src/Module.zig
index 9d3810f671..cac4543883 100644
--- a/src/Module.zig
+++ b/src/Module.zig
@@ -3939,15 +3939,11 @@ pub fn importFile(
defer gpa.free(resolved_root_path);
const sub_file_path = p: {
- if (mem.startsWith(u8, resolved_path, resolved_root_path)) {
- // +1 for the directory separator here.
- break :p try gpa.dupe(u8, resolved_path[resolved_root_path.len + 1 ..]);
- }
- if (mem.eql(u8, resolved_root_path, ".") and
- !isUpDir(resolved_path) and
- !std.fs.path.isAbsolute(resolved_path))
- {
- break :p try gpa.dupe(u8, resolved_path);
+ const relative = try std.fs.path.relative(gpa, resolved_root_path, resolved_path);
+ errdefer gpa.free(relative);
+
+ if (!isUpDir(relative) and !std.fs.path.isAbsolute(relative)) {
+ break :p relative;
}
return error.ImportOutsideModulePath;
};
@@ -4038,15 +4034,11 @@ pub fn embedFile(
defer gpa.free(resolved_root_path);
const sub_file_path = p: {
- if (mem.startsWith(u8, resolved_path, resolved_root_path)) {
- // +1 for the directory separator here.
- break :p try gpa.dupe(u8, resolved_path[resolved_root_path.len + 1 ..]);
- }
- if (mem.eql(u8, resolved_root_path, ".") and
- !isUpDir(resolved_path) and
- !std.fs.path.isAbsolute(resolved_path))
- {
- break :p try gpa.dupe(u8, resolved_path);
+ const relative = try std.fs.path.relative(gpa, resolved_root_path, resolved_path);
+ errdefer gpa.free(relative);
+
+ if (!isUpDir(relative) and !std.fs.path.isAbsolute(relative)) {
+ break :p relative;
}
return error.ImportOutsideModulePath;
};