-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Discrepancy between @src()
when invoked via zig build
or in test and project code
#19570
Comments
I think personally my preference would be that |
I agree, I think absolute paths are more distinct. If you have some weird zig project that copies or adds things to the cache and expose as a module and you have similar paths in a different module then it could be difficult to determine where the problems are at. |
If I understand correctly, these paths are baked into the binary. |
@rohlem I'd be fine with that too, so long as there's a builtin for getting the cache path(s) too, so that the developer can construct an absolute path to the source file if they deem it necessary. Otherwise you have a relative path, but as far as I can tell, no method for getting the base path that it is relative to. |
I will second the notion that paths returned by |
Ignore my initial comment, I agree that relative paths should be the preference. I'm trying to think about this from the angle that you might want to use src paths with some external tooling during development, and the external tooling won't necessarily know how to resolve the relative paths if there's no convention or method for determining the base path from which those relative paths were taken. I guess the hardline of this will probably be that tooling shouldn't shape the language, the language should shape the tooling. So maybe the expectation is that the base path needs to be manually specified by the developer, to the external tooling. And that the base path should exist outside of any compiled zig binaries (even debug ones). I'm interested to hear what others think about the above situation/what the expectation should be. |
I've run into a related problem: In Zig Perhaps the |
@mnemnion From quick testing it seems like currently Zig strips only the path prefix of the root source file.
|
This doesn't work, unfortunately, because It's true that a heuristic will handle this in most cases, but a library which alters someone else's source code should be more careful than that. Thanks for the suggestion, I've commented on the issue tracking this, which is a better place for a detailed discussion. |
@mnemnion Please re-read my message. The path prefix of the compilation's (EDIT:module's) root file is stripped from the |
I've explained the several scenarios where this doesn't work in #20864 already, as well as a solution which will include both a |
If you can show me working code which will allow |
@mnemnion Your comment there doesn't reference my reply here, where I outline that the missing path prefix should be exactly the path prefix of the module's root source file.
I'm not responsible for the relevant change, but to me it seems like the intention behind the change was to make a compilation command
I've already outlined a solution above, here is a full example `build.zig` project implementing that solution:
//! build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib_unit_tests = addTestAndRunInSameDirectory(b, b.path("src/root.zig"), target, optimize);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&lib_unit_tests.run_step.step);
}
pub const ArtifactAndRunStep = struct {
artifact: *std.Build.Step.Compile,
run_step: *std.Build.Step.Run,
};
pub fn addTestAndRunInSameDirectory(
b: *std.Build,
root_source_file_path: std.Build.LazyPath,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) ArtifactAndRunStep {
const artifact = b.addTest(.{
.root_source_file = root_source_file_path,
.target = target,
.optimize = optimize,
});
const run_step = b.addRunArtifact(artifact);
run_step.setCwd(root_source_file_path.dirname()); //run the test executable in the directory of the root source file
return .{
.artifact = artifact,
.run_step = run_step,
};
} //! src/root.zig
const std = @import("std");
const testing = std.testing;
test "reading and printing this source file" {
const alloc = testing.allocator;
const contents = try std.fs.cwd().readFileAlloc(alloc, @src().file, 65526);
defer alloc.free(contents);
std.debug.print("{s}", .{contents});
} output from invoking
This is just a simple setup without any external modules, but from how I understand the mechanisms behind For direct command line usage of |
This would impose a substantial and complex build step on any user of the library. It isn't an acceptable workaround.
It's an interesting speculation, I suppose. |
If you want standard |
This issue is solved now that |
Zig Version
0.12.0-dev.3522+b88ae8dbd
Steps to Reproduce and Observed Behavior
Tested on Windows, I've not tested on other platforms.
When the
@src()
builtin is called frombuild.zig
(or any code that runs duringzig build
), in the returnedSourceLocation
the.file
field appears to be always an absolute path, e.g:D:\devspace\myproject\build.zig
But when called in project code, it only returns the name of the file and not a full path:
projectcode.zig
The case where I hit this was writing a test for some of my build utils, inside
build.zig
. The functionality works when callingzig build
but if I callzig test build.zig
then the different value returned by@src()
causes my tests to fail.Expected Behavior
I expect the values returned by
@src()
inbuild.zig
and when runningzig test build.zig
to be consistent with each other.The text was updated successfully, but these errors were encountered: