diff --git a/src/Uri.zig b/src/Uri.zig index c4d3d154e..b4fc1785d 100644 --- a/src/Uri.zig +++ b/src/Uri.zig @@ -449,21 +449,23 @@ pub fn resolveImport( if (parsed_uri.port) |port| result.printAssumeCapacity(":{d}", .{port}); } } - var aw: std.Io.Writer.Allocating = .fromArrayList(allocator, &result); + var aw: std.Io.Writer.Allocating = try .initCapacity(allocator, sub_path.len); defer aw.deinit(); + std.Uri.Component.percentEncode(&aw.writer, sub_path, isPathChar) catch unreachable; + const percent_encoded_path = parsed_uri.path.percent_encoded; - const joined_path = try std.fs.path.resolvePosix(allocator, &.{ percent_encoded_path, "..", sub_path }); + const joined_path = try std.fs.path.resolvePosix(allocator, &.{ percent_encoded_path, "..", aw.written() }); defer allocator.free(joined_path); - std.Uri.Component.percentEncode(&aw.writer, joined_path, isPathChar) catch unreachable; + try result.appendSlice(allocator, joined_path); - return .{ .raw = try aw.toOwnedSlice() }; + return .{ .raw = try result.toOwnedSlice(allocator) }; } test "resolve" { - const uri: Uri = try .parseWithOs(std.testing.allocator, "file:///dir/main.zig", false); + const uri: Uri = try .parseWithOs(std.testing.allocator, "file:///foo%20bar/main.zig", false); defer uri.deinit(std.testing.allocator); const parsed_uri = std.Uri.parse(uri.raw) catch unreachable; @@ -471,7 +473,7 @@ test "resolve" { const resolved_uri = try resolveImport(std.testing.allocator, uri, parsed_uri, "foo bar.zig"); defer resolved_uri.deinit(std.testing.allocator); - try std.testing.expectEqualStrings("file:///dir/foo%20bar.zig", resolved_uri.raw); + try std.testing.expectEqualStrings("file:///foo%20bar/foo%20bar.zig", resolved_uri.raw); var round_trip_uri: Uri = try .parseWithOs(std.testing.allocator, resolved_uri.raw, false); defer round_trip_uri.deinit(std.testing.allocator);