Skip to content

fs.rm cannot delete a symlink which is part of a loop #45404

Description

@nathanael-ruf

Version

18.12.1

Platform

Darwin Nathanaels-MBP.fritz.box 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103 arm64

Subsystem

fs

What steps will reproduce the bug?

mkdir repro
cd repro
ln -s a b && ln -s b a
node -e 'fs.rmSync("a")'
cd ..
rm -r repro

How often does it reproduce? Is there a required condition?

Always. No.

What is the expected behavior?

Can delete symlinks that form a loop.

What do you see instead?

This error

Uncaught Error: ELOOP: too many symbolic links encountered, stat 'a'
    at Object.statSync (node:fs:1596:3)
    at __node_internal_ (node:internal/fs/utils:804:8)
    at Object.rmSync (node:fs:1272:13) {
  errno: -62,
  syscall: 'stat',
  code: 'ELOOP',
  path: 'a'
}

Additional information

This stat call should probably be a lstatcall.

lazyLoadFs().stat(path, (err, stats) => {

Activity

  1. nathanael-ruf commented on Nov 10, 2022

    @nathanael-ruf
    ContributorAuthor

    Workaround:

    try {
        await rm(path, {...});
    } catch (e) {
        if ('code' in e && e.code === 'ELOOP') {
             await unlink(p);
         }
    }
  2. aduh95 commented on Nov 10, 2022

    @aduh95
    Contributor

    Thanks for the report. Would you be interested in sending a PR to fix this?

  3. added
    confirmed-bugIssues and PRs for confirmed bugs.
    fsIssues and PRs related to file-system APIs and the fs module.
    on Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions