mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.Build.Step.Run: convert relative paths to be relative to child cwd
Because any `LazyPath` might be resolved to a relative path, it's incorrect to pass that directly to a child process whose cwd might differ. Instead, if the child has an overriden cwd, we need to convert such paths to be relative to the child cwd using `std.fs.path.relative`.
This commit is contained in:
parent
5c8b92db7f
commit
14e033ed95
1 changed files with 46 additions and 22 deletions
|
|
@ -622,6 +622,18 @@ fn checksContainStderr(checks: []const StdIo.Check) bool {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If `path` is cwd-relative, make it relative to the cwd of the child instead.
|
||||||
|
///
|
||||||
|
/// Whenever a path is included in the argv of a child, it should be put through this function first
|
||||||
|
/// to make sure the child doesn't see paths relative to a cwd other than its own.
|
||||||
|
fn convertPathArg(run: *Run, path: Build.Cache.Path) []const u8 {
|
||||||
|
const b = run.step.owner;
|
||||||
|
const path_str = path.toString(b.graph.arena) catch @panic("OOM");
|
||||||
|
const child_lazy_cwd = run.cwd orelse return path_str;
|
||||||
|
const child_cwd = child_lazy_cwd.getPath3(b, &run.step).toString(b.graph.arena) catch @panic("OOM");
|
||||||
|
return std.fs.path.relative(b.graph.arena, child_cwd, path_str) catch @panic("OOM");
|
||||||
|
}
|
||||||
|
|
||||||
const IndexedOutput = struct {
|
const IndexedOutput = struct {
|
||||||
index: usize,
|
index: usize,
|
||||||
tag: @typeInfo(Arg).@"union".tag_type.?,
|
tag: @typeInfo(Arg).@"union".tag_type.?,
|
||||||
|
|
@ -676,14 +688,14 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
|
||||||
man.hash.addBytes(bytes);
|
man.hash.addBytes(bytes);
|
||||||
},
|
},
|
||||||
.lazy_path => |file| {
|
.lazy_path => |file| {
|
||||||
const file_path = file.lazy_path.getPath2(b, step);
|
const file_path = file.lazy_path.getPath3(b, step);
|
||||||
try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));
|
try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, run.convertPathArg(file_path) }));
|
||||||
man.hash.addBytes(file.prefix);
|
man.hash.addBytes(file.prefix);
|
||||||
_ = try man.addFile(file_path, null);
|
_ = try man.addFilePath(file_path, null);
|
||||||
},
|
},
|
||||||
.decorated_directory => |dd| {
|
.decorated_directory => |dd| {
|
||||||
const file_path = dd.lazy_path.getPath3(b, step);
|
const file_path = dd.lazy_path.getPath3(b, step);
|
||||||
const resolved_arg = b.fmt("{s}{}{s}", .{ dd.prefix, file_path, dd.suffix });
|
const resolved_arg = b.fmt("{s}{s}{s}", .{ dd.prefix, run.convertPathArg(file_path), dd.suffix });
|
||||||
try argv_list.append(resolved_arg);
|
try argv_list.append(resolved_arg);
|
||||||
man.hash.addBytes(resolved_arg);
|
man.hash.addBytes(resolved_arg);
|
||||||
},
|
},
|
||||||
|
|
@ -696,7 +708,10 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
|
||||||
}
|
}
|
||||||
const file_path = artifact.installed_path orelse artifact.generated_bin.?.path.?;
|
const file_path = artifact.installed_path orelse artifact.generated_bin.?.path.?;
|
||||||
|
|
||||||
try argv_list.append(b.fmt("{s}{s}", .{ pa.prefix, file_path }));
|
try argv_list.append(b.fmt("{s}{s}", .{
|
||||||
|
pa.prefix,
|
||||||
|
run.convertPathArg(.{ .root_dir = .cwd(), .sub_path = file_path }),
|
||||||
|
}));
|
||||||
|
|
||||||
_ = try man.addFile(file_path, null);
|
_ = try man.addFile(file_path, null);
|
||||||
},
|
},
|
||||||
|
|
@ -787,11 +802,14 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
|
||||||
b.cache_root, output_sub_dir_path, @errorName(err),
|
b.cache_root, output_sub_dir_path, @errorName(err),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const output_path = placeholder.output.generated_file.path.?;
|
const arg_output_path = run.convertPathArg(.{
|
||||||
|
.root_dir = .cwd(),
|
||||||
|
.sub_path = placeholder.output.generated_file.getPath(),
|
||||||
|
});
|
||||||
argv_list.items[placeholder.index] = if (placeholder.output.prefix.len == 0)
|
argv_list.items[placeholder.index] = if (placeholder.output.prefix.len == 0)
|
||||||
output_path
|
arg_output_path
|
||||||
else
|
else
|
||||||
b.fmt("{s}{s}", .{ placeholder.output.prefix, output_path });
|
b.fmt("{s}{s}", .{ placeholder.output.prefix, arg_output_path });
|
||||||
}
|
}
|
||||||
|
|
||||||
try runCommand(run, argv_list.items, has_side_effects, output_dir_path, prog_node, null);
|
try runCommand(run, argv_list.items, has_side_effects, output_dir_path, prog_node, null);
|
||||||
|
|
@ -816,12 +834,15 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
|
||||||
b.cache_root, output_sub_dir_path, @errorName(err),
|
b.cache_root, output_sub_dir_path, @errorName(err),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const output_path = try b.cache_root.join(arena, &output_components);
|
const raw_output_path: Build.Cache.Path = .{
|
||||||
placeholder.output.generated_file.path = output_path;
|
.root_dir = b.cache_root,
|
||||||
argv_list.items[placeholder.index] = if (placeholder.output.prefix.len == 0)
|
.sub_path = b.pathJoin(&output_components),
|
||||||
output_path
|
};
|
||||||
else
|
placeholder.output.generated_file.path = raw_output_path.toString(b.graph.arena) catch @panic("OOM");
|
||||||
b.fmt("{s}{s}", .{ placeholder.output.prefix, output_path });
|
argv_list.items[placeholder.index] = b.fmt("{s}{s}", .{
|
||||||
|
placeholder.output.prefix,
|
||||||
|
run.convertPathArg(raw_output_path),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try runCommand(run, argv_list.items, has_side_effects, tmp_dir_path, prog_node, null);
|
try runCommand(run, argv_list.items, has_side_effects, tmp_dir_path, prog_node, null);
|
||||||
|
|
@ -899,20 +920,23 @@ pub fn rerunInFuzzMode(
|
||||||
try argv_list.append(arena, bytes);
|
try argv_list.append(arena, bytes);
|
||||||
},
|
},
|
||||||
.lazy_path => |file| {
|
.lazy_path => |file| {
|
||||||
const file_path = file.lazy_path.getPath2(b, step);
|
const file_path = file.lazy_path.getPath3(b, step);
|
||||||
try argv_list.append(arena, b.fmt("{s}{s}", .{ file.prefix, file_path }));
|
try argv_list.append(arena, b.fmt("{s}{s}", .{ file.prefix, run.convertPathArg(file_path) }));
|
||||||
},
|
},
|
||||||
.decorated_directory => |dd| {
|
.decorated_directory => |dd| {
|
||||||
const file_path = dd.lazy_path.getPath3(b, step);
|
const file_path = dd.lazy_path.getPath3(b, step);
|
||||||
try argv_list.append(arena, b.fmt("{s}{}{s}", .{ dd.prefix, file_path, dd.suffix }));
|
try argv_list.append(arena, b.fmt("{s}{s}{s}", .{ dd.prefix, run.convertPathArg(file_path), dd.suffix }));
|
||||||
},
|
},
|
||||||
.artifact => |pa| {
|
.artifact => |pa| {
|
||||||
const artifact = pa.artifact;
|
const artifact = pa.artifact;
|
||||||
const file_path = if (artifact == run.producer.?)
|
const file_path: []const u8 = p: {
|
||||||
b.fmt("{}", .{run.rebuilt_executable.?})
|
if (artifact == run.producer.?) break :p b.fmt("{}", .{run.rebuilt_executable.?});
|
||||||
else
|
break :p artifact.installed_path orelse artifact.generated_bin.?.path.?;
|
||||||
(artifact.installed_path orelse artifact.generated_bin.?.path.?);
|
};
|
||||||
try argv_list.append(arena, b.fmt("{s}{s}", .{ pa.prefix, file_path }));
|
try argv_list.append(arena, b.fmt("{s}{s}", .{
|
||||||
|
pa.prefix,
|
||||||
|
run.convertPathArg(.{ .root_dir = .cwd(), .sub_path = file_path }),
|
||||||
|
}));
|
||||||
},
|
},
|
||||||
.output_file, .output_directory => unreachable,
|
.output_file, .output_directory => unreachable,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue