Skip to content

Bug in fs.cpSync handling of the dereference option #58939

Closed

Description

@jasnell

Setup:

$ cd ~/tmp
$ touch a
$ mkdir b
$ ln -s b c

Then in Node.js:

> fs.cpSync('a', 'c', { dereference: false })
Uncaught Error: Cannot overwrite directory c with non-directory a
    at cpSyncFn (node:internal/fs/cp/cp-sync:56:13)
    at Object.cpSync (node:fs:3128:3) {
  code: 'ERR_FS_CP_NON_DIR_TO_DIR'
}
> fs.cp('a', 'c', { dereference: false }, console.log)
undefined
> null undefined

The fs.cpSync call should not be throwing in this case and does not do so in Node.js v20.x. It throws in v22.x, v24.x, and main.

/cc @anonrig @nodejs/fs

Activity

  1. added
    confirmed-bug
    fs
    on Jul 3, 2025
  2. juanarbol commented on Jul 3, 2025

    @juanarbol
    Member

    Maybe introduced by 88027e8?

  3. jasnell commented on Jul 3, 2025

    @jasnell
    MemberAuthor

    Almost certainly is that commit.

  4. jasnell commented on Jul 3, 2025

    @jasnell
    MemberAuthor

    @anonrig ... another inconsistency I spotted here:

    > fs.cpSync('b', 'd', { dereference: false  })
    Uncaught Error: Cannot overwrite non-directory b with directory d
        at cpSyncFn (node:internal/fs/cp/cp-sync:56:13)
        at Object.cpSync (node:fs:3111:3) {
      code: 'ERR_FS_CP_DIR_TO_NON_DIR'
    }
    > fs.cp('b', 'd', { dereference: false  }, console.log)
    undefined
    > SystemError [ERR_FS_CP_DIR_TO_NON_DIR]: Cannot overwrite non-directory with directory: cp returned EISDIR (cannot overwrite non-directory d with directory b) d
        at checkPaths (node:internal/fs/cp/cp:89:13)
        at async cpFn (node:internal/fs/cp/cp:66:17) {
      code: 'ERR_FS_CP_DIR_TO_NON_DIR',
      info: {
        message: 'cannot overwrite non-directory d with directory b',
        path: 'd',
        syscall: 'cp',
        errno: 21,
        code: 'EISDIR'
      },
      errno: [Getter/Setter: 21],
      syscall: [Getter/Setter: 'cp'],
      path: [Getter/Setter: 'd']
    }

    In this example, 'b' is a directory, and 'd' is a symlink to a file. Note the differences in the error messages. The cpSync error message has the ordering of the src and dest reversed

  5. jasnell commented on Jul 3, 2025

    @jasnell
    MemberAuthor

    And another...

    -rw-rw-r--  1 jsnell jsnell    0 Jul  3 08:57 a
    lrwxrwxrwx  1 jsnell jsnell    1 Jul  3 09:27 b -> a
    lrwxrwxrwx  1 jsnell jsnell   18 Jul  3 09:25 c -> /home/jsnell/tmp/a
    lrwxrwxrwx  1 jsnell jsnell   18 Jul  3 09:26 d -> /home/jsnell/tmp/a
    jsnell@james-cloudflare-build:~/tmp$ node
    Welcome to Node.js v24.3.0.
    Type ".help" for more information.
    > fs.cpSync('b', 'e', { dereference: true })
    Uncaught Error: Recursive option not enabled, cannot copy a directory: b/
        at cpSyncFn (node:internal/fs/cp/cp-sync:56:13)
        at Object.cpSync (node:fs:3111:3) {
      code: 'ERR_FS_EISDIR'
    }

    'b' is being misinterpreted as a directory when it is a symlink to a file.

  6. added
    regression
    on Jul 22, 2025
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-bugfsregression

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions