Commit 4d09fb491f

Krzysztof Wolicki <der.teufel.mail@gmail.com>
2024-09-24 22:38:13
fetch: fix mutating unrelated fields when saving (#19816)
closes #19725
1 parent 5e4da1f
Changed files (2)
src/Package/Manifest.zig
@@ -18,8 +18,10 @@ pub const MultiHashHexDigest = [multihash_hex_digest_len]u8;
 pub const Dependency = struct {
     location: Location,
     location_tok: Ast.TokenIndex,
+    location_node: Ast.Node.Index,
     hash: ?[]const u8,
     hash_tok: Ast.TokenIndex,
+    hash_node: Ast.Node.Index,
     node: Ast.Node.Index,
     name_tok: Ast.TokenIndex,
     lazy: bool,
@@ -293,8 +295,10 @@ const Parse = struct {
         var dep: Dependency = .{
             .location = undefined,
             .location_tok = 0,
+            .location_node = undefined,
             .hash = null,
             .hash_tok = 0,
+            .hash_node = undefined,
             .node = node,
             .name_tok = 0,
             .lazy = false,
@@ -320,6 +324,7 @@ const Parse = struct {
                 };
                 has_location = true;
                 dep.location_tok = main_tokens[field_init];
+                dep.location_node = 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.", .{});
@@ -332,12 +337,14 @@ const Parse = struct {
                 };
                 has_location = true;
                 dep.location_tok = main_tokens[field_init];
+                dep.location_node = field_init;
             } else if (mem.eql(u8, field_name, "hash")) {
                 dep.hash = parseHash(p, field_init) catch |err| switch (err) {
                     error.ParseFailure => continue,
                     else => |e| return e,
                 };
                 dep.hash_tok = main_tokens[field_init];
+                dep.hash_node = field_init;
             } else if (mem.eql(u8, field_name, "lazy")) {
                 dep.lazy = parseBool(p, field_init) catch |err| switch (err) {
                     error.ParseFailure => continue,
src/main.zig
@@ -7214,8 +7214,21 @@ fn cmdFetch(
                 .path => {},
             }
         }
+
+        const location_replace = try std.fmt.allocPrint(
+            arena,
+            "\"{}\"",
+            .{std.zig.fmtEscapes(path_or_url)},
+        );
+        const hash_replace = try std.fmt.allocPrint(
+            arena,
+            "\"{}\"",
+            .{std.zig.fmtEscapes(&hex_digest)},
+        );
+
         warn("overwriting existing dependency named '{s}'", .{name});
-        try fixups.replace_nodes_with_string.put(gpa, dep.node, new_node_init);
+        try fixups.replace_nodes_with_string.put(gpa, dep.location_node, location_replace);
+        try fixups.replace_nodes_with_string.put(gpa, dep.hash_node, hash_replace);
     } else if (manifest.dependencies.count() > 0) {
         // Add fixup for adding another dependency.
         const deps = manifest.dependencies.values();