I'm developing a vscode extension that should move some files inside of a working directory. And sometimes an output file is a blank file, the content is getting lost somewhere...
I tried await fs.rename(oldPath, newPath) and
the result is ~ the same, the second way seems to be more stable and blank files appear more rarely, but still they do. Adding a 1 second sleep like this
await fs.rename(oldPath, newPath)
await new Promise((r) => setTimeout(r, 1000));
seems to help, idk if it fixes the issue 100% but several test shows that it does. But, it's not an option, because it drastically reduces the 'perfomance' of the extension.
So, why does it happen? And what's the right way to fix it. Thanks
Node.js version
v18.17.0
Example code
const scopeFileContent = await readFile(scopeFiles[0].fsPath, 'utf8');
const lines = scopeFileContent.split('\n');
for await (let line of lines) {
if (line.replace('\r', '').length === 0) {
continue;
}
const scopeFileName = path.basename(line.replace('\r', ''));
let oldPath = line.toString()[0] === '/' ? thePath + line.replace('\r', '') : thePath + '/' + line.replace('\r', '');
const newPath = thePath + '/src/scope/' + scopeFileName;
await new Promise(async (resolve) => {
let success = false;
while (!success) {
try {
// console.log('CURRENT FILE', path.basename(line));
await copyFile(oldPath, newPath);
await unlink(oldPath);
// await rename(oldPath, newPath);
success = true;
} catch (error: any) {
if (error.message.includes('ENOENT')) {
console.log('Wrong path, searching for the file');
const scopeFiles = await workspace.findFiles(`**/${scopeFileName}`, `{${excludePattern.join(',')}}`);
if (scopeFiles.length === 0) {
throw Error('File from the scope not found, aborting...');
} else if (scopeFiles.length > 1) {
throw Error('Duplicate from the scope found, aborting...');
}
oldPath = scopeFiles[0].fsPath;
} else {
// if error is about file busy or sth
console.log(error);
await new Promise((r) => setTimeout(r, 1000));
}
}
}
resolve(true);
});
}
Details
I'm developing a vscode extension that should move some files inside of a working directory. And sometimes an output file is a blank file, the content is getting lost somewhere... I tried
await fs.rename(oldPath, newPath)
andthe result is ~ the same, the second way seems to be more stable and blank files appear more rarely, but still they do. Adding a 1 second sleep like this
seems to help, idk if it fixes the issue 100% but several test shows that it does. But, it's not an option, because it drastically reduces the 'perfomance' of the extension.
So, why does it happen? And what's the right way to fix it. Thanks
Node.js version
v18.17.0
Example code
Operating system
Microsoft Windows [Version 10.0.22631.2506]
Scope
moving files from to
Module and version
fs