copyTo doesn't work

Hello! It’s my first post on the forums.

I’m working on a plugin that should copy several files from the plugin folder to the user-selected folder.
The problem is that I get this error:

Plugin Error: copyEntry: failed to copy file
at Object._copyFileEntry (uxp://uxp-internal/webfs_scripts.js:2:66158)

But… the file is actually copied to the selected location. The problem is that since the error is thrown the plugin doesn’t execute the code that comes after the file copy.

const fs = require("uxp").storage.localFileSystem;

async function testFiles(selection) { 

    const folder = await fs.getFolder();
    const pluginFolder = await fs.getPluginFolder();
    const testFile = await pluginFolder.getEntry('testfile.txt');
   
  	await  testFile.copyTo(folder, {overwrite:true});
  	console.log("complete");
}

module.exports = {
    commands: {
        testFiles: testFiles
    }
};

I’m working on Mac with the latest XD versions installed.
Any help would be much appreciated.

1 Like

try this line without the await and say if it works:

await testFile.copyTo(folder, {overwrite:true});

1 Like

Sorry, but just not awaiting it isn’t really a good solution for this (to clarify: I’m not saying the answer is bad; it just might get interpreted in a way that leads to bad practices :slightly_smiling_face: ). While it might make this work, it causes more harm than good. If a promise like this rejects, this should get handled by the plugin in some way (error message, …). Simply not awaiting the result of the operation is basically like throwing away information.

The curious thing here, of course, is that

Therefore, there seems to be a discrepancy between the result of the Promise (rejection) and what actually happens in the file system (success). To me, it kind of sounds like some file permission issues, but that is up to Adobe to figure out (since this seems to be something that fails internally inside the copyTo function).

If you really want to “throw away” the information the promise gives you, for now (since the result of the promise appears to give you wrong information), wrap the relevant code into a try catch block and handle this error case (which might be an actual error!) inside the catch block (if this is non-critical, you can, of course, ignore the error case, that’s up to you, @piotrD :wink:).


@piotrD Welcome to the community :wave::slightly_smiling_face:


I’m re-categorizing this to API Feedback/Bugs as the error message appearing while the file successfully gets copied clearly means there’s some sort of bug (maybe an edge-case, but still) inside uxp.storage.Entry::copyTo

1 Like

Thanks Guys! I’ll do what @pklaschka suggested.

2 Likes