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

Adds LazyPath.dirname for getting the containing folder of a LazyPath #16803

Closed
wants to merge 1 commit into from

Conversation

ikskuh
Copy link
Contributor

@ikskuh ikskuh commented Aug 13, 2023

Use cases:

const config_h_step = b.addConfigHeader(
    .{
        .style = .{ .autoconf = .{ .path = "./flex-2.6.4/src/config.h.in" } },
    },
    flex_config,
);
const config_h = config_h_step.getOutput();

exe.defineCMacro("HAVE_CONFIG_H", null);
exe.addIncludePath(config_h.dirname());
const lexer_gen = b.addRunArtifact(flex);
lexer_gen.addArgs(&.{
    "--hex", //  use hexadecimal numbers instead of octal in debug outputs
    "--8bit", // generate 8-bit scanner
    "--batch", // generate batch scanner
    "--yylineno", //  track line count in yylineno
    "--prefix=apigen_parser_", // custom prefix instead of yy
    "--reentrant", // generate a reentrant C scanner
    "--nounistd", //  do not include <unistd.h>
    "--bison-locations", // include yylloc support.
    "--bison-bridge", // scanner for bison pure parser.
});
const lexer_c_source = lexer_gen.addPrefixedOutputFileArg("--outfile=", "lexer.yy.c");
const lexer_h_source = lexer_gen.addPrefixedOutputFileArg("--header-file=", "lexer.yy.h");
lexer_gen.addFileSourceArg(.{ .path = "src/parser/lexer.l" });

exe.addIncludePath(lexer_h_source.dirname());
exe.addCSourceFile(.{ .file = lexer_c_source, .flags = &lax_cflags ++ local_include });

@ikskuh ikskuh marked this pull request as ready for review August 13, 2023 13:23
@kubkon
Copy link
Member

kubkon commented Aug 21, 2023

Nice! This will simplify my zld test harness a fair amount! With this change, I believe this becomes obsolete: https://github.com/kubkon/zld/blob/860135f3e4e1bae21688b09b110711dab799cb34/test/test.zig#L183-L206

}

fn resolve(realpath: []const u8) []const u8 {
return std.fs.path.dirname(realpath) orelse if (std.fs.path.isAbsolute(realpath)) "/" else ".";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On WIndows, we will blindly return / as root for an abs path which is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On WIndows, we will blindly return / as root for an abs path which is wrong.

Actually not quite sure, windows also allows current-drive absolute paths that don't use a drive specifier (\Windows\system32\calc.exe) is valid if your CWD is in C:\. I gotta verify that, will test when i have a windows machine at hand

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, whatever the actual drive or path notation is, we should return that instead of /.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about it for a while, i think the correct solution here is one of:

  1. Panic and let the user fix it
  2. Pass in a builder, resolve the paths to absolute paths and then invoke dirname on those (using . is also a bad idea here)
  3. Rewrite the Build to allow using Build.relPath(…), Build.cwdPath(…) which both resolve to the correct path internally. This is probably the correct solution here, as .path is currently broken-by-design:
    What will happen if a dependency Build will pass a .{ .path = "local" } to a "higher" builder? It will suddenly point to a file in the local folder instead of the one where the dependency was fetched to.

cc @andrewrk

Copy link
Member

@kubkon kubkon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but a little bit more work is required to better support Windows :-)

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with supporting the use case (see #17411) however, I think this is not the best way to go about implementing it. I added a diff to that issue to show my counter-proposal for how to implement it.

One more thing that I want to do here is protect against this dirname function being used to go outside of a root dir. For example, it shouldn't be possible to go from a generated file inside the zig-cache to outside the zig-cache, or from a project-relative file to outside the project. It's less of a security concern issue, and more just that I want to prevent people from putting a bunch of brittle, janky logic in their build scripts.

@ikskuh
Copy link
Contributor Author

ikskuh commented Jan 3, 2024

Superseeded by #18371

@ikskuh ikskuh closed this Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants