-
-
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
@src().file no longer includes directory #20864
Comments
Intentional change to avoid different output based on weird things like the compiler's cwd. That said, if you have a use case which is impacted, please explain in a comment and we can figure out how to address it. |
$ echo 'const std = @import("std");
test "where am I" {
const loc = @src();
std.debug.print("location: {s}\\n", .{loc.file});
}
' > file.zig
$ zig test file.zig
$ echo 'const std = @import("std");
test "where am I" {
const loc = @src();
std.debug.print("location: {s}\\n", .{loc.file});
}
' > src/file.zig
$ zig test src/file.zig These produce identical output. |
This breaks the ohsnap module, which relies on being able to find a file in order to update the snapshot. It will also break the TigerBeetle snapshot testing which it is directly based upon. A reasonable solution would be to include the relative-path part as its own field of the |
This comment was marked as outdated.
This comment was marked as outdated.
@mnemnion Within a single compilation, files are disambiguated. If you have a file comptime {
_ = @import("a/f.zig");
_ = @import("b/f.zig");
} and Alternatively you could navigate into the file's directory - that way the file should always be accessible without any path prefix. |
If you run the shell commands I sketched out above, you'll see that the current Also, in the const file_as_slice = try std.fs.cwd().readAlloc(alloc, source_location.file, 65526); Which would be quite a bit more involved in a situation where the user code needs to guess which directory the file actually lives in. I think the change is reasonable for what the build system is doing, I'm sure it wasn't done capriciously, but in the service of the task at hand. I see the point in making the But $ echo 'const std = @import("std");
test "where am I" {
const loc = @src();
std.debug.print("location: {s}\\n", .{loc.file});
}
' > test/file.zig What does this print? A reasonable fix, since the path string is going to be in These share a common suffix, so they can be two slices into the same string. That way, the build system gets a relocatable path, without having to do any fancy path handling, and users of It's one more field of |
@mlugg I've done so, I'm not sure if you see comments in closed issues or not, which is why I'm tagging you here. It would be good to see a final form for this change which continues to support this use case for |
Having two versions of the What about adding a The outcome I care about is having something I can combine with the |
Note that in this example, src/main.zig is the root source file, making src/ the root directory of the module. The expression I think this use case will be solved by adding a field for the module. It still won't show the cwd but it will provide the uniqueness property that is needed. Modules have canonical names that are decided by the CLI invocation. |
Great, any solution which allows code to go from |
Zig Version
0.14.0-dev.658+4a77c7f25
Steps to Reproduce and Observed Behavior
Sometime between 0.14.0-dev.544+7aaebd177 and 0.14.0-dev.611+f2bf6c1b1 this changed
Not sure if this is an intentional breaking change or not
Expected Behavior
The text was updated successfully, but these errors were encountered: