Skip to content

opendir doesn't work on node 23 for paths ending in a slash #22812

@blexrob

Description

@blexrob

In node 23, the open/openSync calls don't allow opening directories with a path that ends with a slash. The musl opendir function uses open(name, O_RDONLY|O_DIRECTORY|O_CLOEXEC) (in opendir.c), which is routed to openSync in the node fs code. Because of that, it isn't possible to get the directory listing of a path that ends in a slash (which includes the directory root '/').

A PR to revert the change in node is present (nodejs/node#55527), but I'm reporting it here too for awareness.

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.70-git
clang version 20.0.0git
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/bin

Failing command line in full:
If this is compile or link-time failure please include the full failing command
along with its entire output.

Full link command and output with -v appended:

$ emcc -v -s NODERAWFS=1 test.c
 /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=/usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot -DEMSCRIPTEN -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -v test.c -c -o /var/folders/p8/71c14px15wngzhg4qq22x44c0000gn/T/emscripten_temp_mq1ltbwg/test_0.o
clang version 20.0.0git
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/bin
 (in-process)
 "/usr/local/Cellar/emscripten/3.1.70/libexec/llvm/bin/clang-20" -cc1 -triple wasm32-unknown-emscripten -emit-obj -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model static -mframe-pointer=none -ffp-contract=on -fno-rounding-math -mconstructor-aliases -target-cpu generic -fvisibility=hidden -debugger-tuning=gdb -fdebug-compilation-dir=/tmp/_1 -target-linker-version 1115.7.3 -v -fcoverage-compilation-dir=/tmp/_1 -resource-dir /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/lib/clang/20 -D EMSCRIPTEN -isysroot /usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot -internal-isystem /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/lib/clang/20/include -internal-isystem /usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot/include/wasm32-emscripten -internal-isystem /usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot/include -Werror=implicit-function-declaration -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fignore-exceptions -fcolor-diagnostics -iwithsysroot/include/fakesdl -iwithsysroot/include/compat -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -o /var/folders/p8/71c14px15wngzhg4qq22x44c0000gn/T/emscripten_temp_mq1ltbwg/test_0.o -x c test.c
clang -cc1 version 20.0.0git based upon LLVM 20.0.0git default target x86_64-apple-darwin23.6.0
ignoring nonexistent directory "/usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot/include/wasm32-emscripten"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot/include/fakesdl
 /usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot/include/compat
 /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/lib/clang/20/include
 /usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot/include
End of search list.
 /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/bin/clang --version
 /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/bin/wasm-ld -o a.out.wasm -L/usr/local/Cellar/emscripten/3.1.70/libexec/cache/sysroot/lib/wasm32-emscripten /var/folders/p8/71c14px15wngzhg4qq22x44c0000gn/T/emscripten_temp_mq1ltbwg/test_0.o -lGL-getprocaddr -lal -lhtml5 -lstubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-debug-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /var/folders/p8/71c14px15wngzhg4qq22x44c0000gn/T/tmpfbaqf3l6libemscripten_js_symbols.so --strip-debug --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=_emscripten_stack_alloc --export=__get_temp_ret --export=__set_temp_ret --export=__wasm_call_ctors --export=_emscripten_stack_restore --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=__main_argc_argv --export-if-defined=fflush --export-table -z stack-size=65536 --no-growable-memory --initial-heap=16777216 --no-entry --stack-first --table-base=1
 /usr/local/Cellar/emscripten/3.1.70/libexec/llvm/bin/llvm-objcopy a.out.wasm a.out.wasm --remove-section=.debug* --remove-section=producers
 /usr/local/Cellar/emscripten/3.1.70/libexec/binaryen/bin/wasm-emscripten-finalize --dyncalls-i64 --pass-arg=legalize-js-interface-exported-helpers a.out.wasm -o a.out.wasm --detect-features
 /usr/local/opt/node/bin/node /usr/local/Cellar/emscripten/3.1.70/libexec/src/compiler.mjs /var/folders/p8/71c14px15wngzhg4qq22x44c0000gn/T/tmpvni1y0sa.json
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>


int main(int argc,char *argv[])
{
  if (argc != 2) {
    printf("expected arg\n");
    exit(0);
  }
  DIR *dir;
  struct dirent *dent;
  dir = opendir(argv[1]);
  printf("dir ptr: %p\n", dir);

  if (dir!=NULL) {
    while ((dent=readdir(dir))!=NULL) {
      if ((strcmp(dent->d_name,".")==0 || strcmp(dent->d_name,"..")==0 || (*dent->d_name) == '.' )) {
      } else {
        printf("%s\n", dent->d_name);
      }
    }
  }
  closedir(dir);
}

Output:

$ node a.out.js /Users
dir ptr: 0x11268
Shared
rob
$ node a.out.js /Users/
dir ptr: 0x11268

The last invocation should have given back the same list of directories as the first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions