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

stop calling realpath in the compiler #16571

Open
andrewrk opened this issue Jul 26, 2023 · 2 comments
Open

stop calling realpath in the compiler #16571

andrewrk opened this issue Jul 26, 2023 · 2 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Jul 26, 2023

realpath is problematic for several reasons:

  1. it is not available for many targets, including WASI and BSDs
  2. it just straight up doesn't work for long file paths, if you have /long1/long2/foo for example where long1 and long2 together exceed MAX_PATH, then the OS will give an error for that path when asked for the realpath.
  3. it fails to respect the lazy nature of symlinks
  4. race conditions with moving files around

Robust software avoids realpath.

/home/andy/dev/zig/lib/std/os.zig:5079:15: error: realpath is forbidden
    if (true) @compileError("realpath is forbidden");
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    init: /home/andy/dev/zig/src/codegen/llvm.zig:802:38
    create: /home/andy/dev/zig/src/codegen/llvm.zig:754:27
    createEmpty: /home/andy/dev/zig/src/link/NvPtx.zig:41:39
    openPath: /home/andy/dev/zig/src/link/NvPtx.zig:63:12
    openPath: /home/andy/dev/zig/src/link.zig:363:41
    update: /home/andy/dev/zig/src/Compilation.zig:2003:38
    updateModule: /home/andy/dev/zig/src/main.zig:3864:17
    cmdBuild: /home/andy/dev/zig/src/main.zig:4510:9
    mainArgs: /home/andy/dev/zig/src/main.zig:285:16
    main: /home/andy/dev/zig/src/main.zig:213:12

Looks like this regressed in #13843.

Maybe we can even add a std lib option to disallow realpath on a per-application basis and then opt into that restriction in the compiler.

Related:

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior backend-llvm The LLVM backend outputs an LLVM IR Module. labels Jul 26, 2023
@andrewrk andrewrk added this to the 0.12.0 milestone Jul 26, 2023
@semarie
Copy link
Contributor

semarie commented Aug 3, 2023

Regarding availability of realpath on BSDs, the problem isn't really std.os.realpath() function (it is POSIX and wildly defined), but std.fs.Dir.realpath() counterpart which use a directory descriptor (no POSIX equivalent) and is implemented using std.os.getFdPath() (which isn't available for many BSDs).

@andrewrk
Copy link
Member Author

There can be exactly one call to realpath in the compiler, in the CLI. It can set up the Package.Path corresponding to the current working directory. Depending on a few things, probably including user configuration, it may call realpath to populate the string for that path object, or it might leave it as relative to the cwd. Then, the rest of the codebase must never call realpath() and instead respect that cwd Path object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants