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

fs.cpSync() not able to copy directory if new directory contains the name of the original directory #55267

Closed
k-capehart opened this issue Oct 4, 2024 · 2 comments
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs.

Comments

@k-capehart
Copy link

k-capehart commented Oct 4, 2024

Version

22.9.0

Platform

Microsoft Windows NT 10.0.22631.0 x64
Ubuntu 22.04.5

Subsystem

No response

What steps will reproduce the bug?

This does not occur on any previous version of Node that I have tried (22.2.0, 21.7.3, 20.18.).

This is an excerpt of the code that I was working with. I was writing a unit test to test some code that parsed XML files and updated them. As part of this test, I copied a directory of example XML files to a backup directory, and then restored them in a afterEach hook. This worked until I ran it in CI and came across the issue with the current node version.

import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import * as fs from 'node:fs';
import { ux } from '@oclif/core';

describe('update xml files', () => {
  const filename = fileURLToPath(import.meta.url);
  const fileDirName = dirname(filename);
  const xmlFilesPath = join(fileDirName, '../../xml');
  const xmlBackupPath = join(fileDirName, '../../xmlBackup');

  beforeEach(async () => {
    try {
      fs.cpSync(xmlFilesPath, xmlBackupPath, { recursive: true });
      if (!fs.existsSync(xmlBackupPath)) {
        throw new Error(`Backup XML directory does not exist: ${xmlFilesPath}`);
      }
    } catch (error: unknown) {
      ux.error(error as Error);
    }
  });
});

The error received was: Error: Cannot copy \\?\C:\Users\kc_ra\Documents\repos\kc-sf-plugin\test\xml to a subdirectory of self \\?\C:\Users\kc_ra\Documents\repos\kc-sf-plugin\test\xmlBackup

This was weird because it's not a subdirectory. However, I figured out that to fix it, I had to change the name of the backup directory from xmlBackup to just backup. This resolves the issue.

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

It happens every time you try to copy a directory into a sibling directory, in which the new directory's name contains the original directory's name.

Example:

const originalDir = join(fileDirName, '../../xml');
const newDir = join(fileDirName, '../../xmlBackup');
fs.cpSync(originalDir , newDir , { recursive: true });
const originalDir = join(fileDirName, '../../test');
const newDir = join(fileDirName, '../../testing');
fs.cpSync(originalDir , newDir , { recursive: true });

What is the expected behavior? Why is that the expected behavior?

The expected behavior would be for this to only occur when it is an actual subdirectory, not just have similar names.

What do you see instead?

Error: Cannot copy \\?\C:\Users\kc_ra\Documents\repos\kc-sf-plugin\test\test to a subdirectory of self \\?\C:\Users\kc_ra\Documents\repos\kc-sf-plugin\test\testing

Additional information

No response

@k-capehart
Copy link
Author

This actually looks like it was already handled here: #54285

But just making sure it didn't resurface. I'm assuming the fixed code just hasn't gone out yet.

@avivkeller avivkeller closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2024
@avivkeller avivkeller added the duplicate Issues and PRs that are duplicates of other issues or PRs. label Oct 4, 2024
@avivkeller
Copy link
Member

This is a duplicate of #54285. This fix has already been applied to main, however it has not landed on any release lines yet. I assume it'll release with v22.10.0, whever that releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs.
Projects
None yet
Development

No branches or pull requests

2 participants