Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support relative paths in package manager #14603

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions lib/std/Uri.zig
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort };
/// original `text`. Each component that is provided, will be non-`null`.
pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
var reader = SliceReader{ .slice = text };

var uri = Uri{
.scheme = "",
.user = null,
Expand All @@ -145,13 +146,14 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
.fragment = null,
};

if (reader.peekPrefix("//")) { // authority part
if (reader.peekPrefix("//")) a: { // authority part
std.debug.assert(reader.get().? == '/');
std.debug.assert(reader.get().? == '/');

const authority = reader.readUntil(isAuthoritySeparator);
if (authority.len == 0)
return error.InvalidFormat;
if (authority.len == 0) {
if (reader.peekPrefix("/")) break :a else return error.InvalidFormat;
}

var start_of_host: usize = 0;
if (std.mem.indexOf(u8, authority, "@")) |index| {
Expand Down Expand Up @@ -224,7 +226,6 @@ pub fn format(
try writer.writeAll(":");
if (uri.host) |host| {
try writer.writeAll("//");

if (uri.user) |user| {
try writer.writeAll(user);
if (uri.password) |password| {
Expand Down Expand Up @@ -486,6 +487,23 @@ test "should fail gracefully" {
try std.testing.expectEqual(@as(ParseError!Uri, error.InvalidFormat), parse("foobar://"));
}

test "file" {
const parsed = try parse("file:///");
try std.testing.expectEqualSlices(u8, "file", parsed.scheme);
try std.testing.expectEqual(@as(?[]const u8, null), parsed.host);
try std.testing.expectEqualSlices(u8, "/", parsed.path);

const parsed2 = try parse("file:///an/absolute/path/to/something");
try std.testing.expectEqualSlices(u8, "file", parsed2.scheme);
try std.testing.expectEqual(@as(?[]const u8, null), parsed2.host);
try std.testing.expectEqualSlices(u8, "/an/absolute/path/to/something", parsed2.path);

const parsed3 = try parse("file://localhost/an/absolute/path/to/another/thing/");
try std.testing.expectEqualSlices(u8, "file", parsed3.scheme);
try std.testing.expectEqualSlices(u8, "localhost", parsed3.host.?);
try std.testing.expectEqualSlices(u8, "/an/absolute/path/to/another/thing/", parsed3.path);
}

test "scheme" {
try std.testing.expectEqualSlices(u8, "http", (try parse("http:_")).scheme);
try std.testing.expectEqualSlices(u8, "scheme-mee", (try parse("scheme-mee:_")).scheme);
Expand Down Expand Up @@ -695,3 +713,20 @@ test "URI query escaping" {
defer std.testing.allocator.free(formatted_uri);
try std.testing.expectEqualStrings("/?response-content-type=application%2Foctet-stream", formatted_uri);
}

test "format" {
const uri = Uri{
.scheme = "file",
.user = null,
.password = null,
.host = null,
.port = null,
.path = "/foo/bar/baz",
.query = null,
.fragment = null,
};
var buf = std.ArrayList(u8).init(std.testing.allocator);
defer buf.deinit();
try uri.format("+/", .{}, buf.writer());
try std.testing.expectEqualSlices(u8, "file:/foo/bar/baz", buf.items);
}
44 changes: 32 additions & 12 deletions src/Manifest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ pub const basename = "build.zig.zon";
pub const Hash = std.crypto.hash.sha2.Sha256;

pub const Dependency = struct {
url: []const u8,
url_tok: Ast.TokenIndex,
location: union(enum) {
url: []const u8,
path: []const u8,
},
location_tok: Ast.TokenIndex,
hash: ?[]const u8,
hash_tok: Ast.TokenIndex,
};
Expand Down Expand Up @@ -218,12 +221,12 @@ const Parse = struct {
};

var dep: Dependency = .{
.url = undefined,
.url_tok = undefined,
.location = undefined,
.location_tok = undefined,
.hash = null,
.hash_tok = undefined,
};
var have_url = false;
var has_location = false;

for (struct_init.ast.fields) |field_init| {
const name_token = ast.firstToken(field_init) - 2;
Expand All @@ -232,12 +235,29 @@ const Parse = struct {
// things manually provides an opportunity to do any additional verification
// that is desirable on a per-field basis.
if (mem.eql(u8, field_name, "url")) {
dep.url = parseString(p, field_init) catch |err| switch (err) {
error.ParseFailure => continue,
else => |e| return e,
if (has_location) {
return fail(p, main_tokens[field_init], "dependency should specify only one of 'url' and 'path' fields.", .{});
}
dep.location = .{
.url = parseString(p, field_init) catch |err| switch (err) {
error.ParseFailure => continue,
else => |e| return e,
},
};
has_location = true;
dep.location_tok = main_tokens[field_init];
} else if (mem.eql(u8, field_name, "path")) {
if (has_location) {
return fail(p, main_tokens[field_init], "dependency should specify only one of 'url' and 'path' fields.", .{});
}
dep.location = .{
.path = parseString(p, field_init) catch |err| switch (err) {
error.ParseFailure => continue,
else => |e| return e,
},
};
dep.url_tok = main_tokens[field_init];
have_url = true;
has_location = true;
dep.location_tok = main_tokens[field_init];
} else if (mem.eql(u8, field_name, "hash")) {
dep.hash = parseHash(p, field_init) catch |err| switch (err) {
error.ParseFailure => continue,
Expand All @@ -250,8 +270,8 @@ const Parse = struct {
}
}

if (!have_url) {
try appendError(p, main_tokens[node], "dependency is missing 'url' field", .{});
if (!has_location) {
try appendError(p, main_tokens[node], "dependency requires location field, one of 'url' or 'path'.", .{});
}

return dep;
Expand Down
Loading