-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
"Experimental" fs.cp(), fs.cpSync(), fsPromises.cp() methods don't handle recursion when there is a filter #49092
Comments
Related: #44598 |
@nodejs/fs |
Currently, await cp('src', 'lib', {
recursive: true,
- filter: (source, _destination) => {
+ filter: async (source, _destination) => {
console.log(source, _destination, source.endsWith('.css'));
- return source.endsWith('.css');
+ return (await lstat(source)).isDirectory() || source.endsWith('.css');
}
}); If this is a very common usecase, we might consider adding some sort of |
I'd be okay with the current function — if the documentation made that clear. Right now it only says "Function to filter copied files/directories. Return true to copy the item, false to ignore it.", and while technically not traversing can be seen as ignoring, I think it should be stated explicitly. |
Would you like to send a PR to update the docs? |
PR-URL: nodejs#49289 Fixes: nodejs#49092 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
PR-URL: nodejs/node#49289 Fixes: nodejs/node#49092 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
PR-URL: nodejs/node#49289 Fixes: nodejs/node#49092 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
Version
18.17.0
Platform
Windows
Subsystem
No response
What steps will reproduce the bug?
Any simple recursive call to
cp
to recursively copy a director, when there is a filter that does not always returntrue
I used
to copy only
.css
files after creating "lib/" from "src/" withtsc
in a React JSX library project, and it stopped right at the top level directory.I also checked non-promise
cp()
andcpSync()
.How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
Expected: Recurse over all files and subdirectories no matter what the filter function returns
What do you see instead?
Recursion stops completely when the filter function returns
false
. The rest of the not yet visited files and sub directories are ignored and not handled.Additional information
Note: All directories are definitely present since my example use case copies CSS files from src/ into lib/ after compiling the project with tsc. However, this leaves open the question of what would happen if the destination directory does not exist? Feature question: When using a filter it's possible to skip directories. Of course, one could ask the programmer to handle directories and return
true
in such cases? Or would it also be okay to add a feature to create any missing directories? Not sure how it behaves right now. In any case, that's only an additional consideration and not the issue of this... issue.The text was updated successfully, but these errors were encountered: