authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 11:55:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
log1fd95fc00519ee44de94fa5b73ca7a3285b92149
treec14672df5650d18990d430b4272c9b1a8ee31c1d
parent29156de12022732cb90b5bc5f5a0e33bf25d894b

Package.Fetch: resolve instead of join relative paths

This prevents bogus "error: file exists in multiple modules" errors due to file paths looking like: ``` note: root of module foo/freetype/ note: root of module foo/fontconfig/../freetype/ ``` It also enables checking for dependency paths outside the root package.

2 files changed, 15 insertions(+), 1 deletions(-)

src/Package.zig+8
......@@ -30,6 +30,14 @@ pub const Path = struct {
3030 };
3131 }
3232
33 pub fn resolvePosix(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path {
34 if (sub_path.len == 0) return p;
35 return .{
36 .root_dir = p.root_dir,
37 .sub_path = try fs.path.resolvePosix(arena, &.{ p.sub_path, sub_path }),
38 };
39 }
40
3341 pub fn joinString(p: Path, allocator: Allocator, sub_path: []const u8) Allocator.Error![]u8 {
3442 const parts: []const []const u8 =
3543 if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
src/Package/Fetch.zig+7-1
......@@ -248,7 +248,13 @@ pub fn run(f: *Fetch) RunError!void {
248248 f.hash_tok,
249249 try eb.addString("path-based dependencies are not hashed"),
250250 );
251 f.package_root = try f.parent_package_root.join(arena, sub_path);
251 f.package_root = try f.parent_package_root.resolvePosix(arena, sub_path);
252 if (std.mem.startsWith(u8, f.package_root.sub_path, "../")) {
253 return f.fail(
254 f.location_tok,
255 try eb.addString("dependency path outside package"),
256 );
257 }
252258 try loadManifest(f, f.package_root);
253259 try checkBuildFileExistence(f);
254260 if (!f.job_queue.recursive) return;