fix: order the resources to delete

longest path comes first to remove to most nested resources, then their
container if any.

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta 2024-02-22 09:38:01 +01:00
parent 478c36c5bd
commit 67e863d9ea
2 changed files with 11 additions and 10 deletions

View File

@ -104,9 +104,9 @@ export class CreateFsProvider
let errToRethrow = err; let errToRethrow = err;
// Not Found (Create API) errors must be remapped to VS Code filesystem provider specific errors // Not Found (Create API) errors must be remapped to VS Code filesystem provider specific errors
// https://code.visualstudio.com/api/references/vscode-api#FileSystemError // https://code.visualstudio.com/api/references/vscode-api#FileSystemError
if (isNotFound(err)) { if (isNotFound(errToRethrow)) {
errToRethrow = createFileSystemProviderError( errToRethrow = createFileSystemProviderError(
err, errToRethrow,
FileSystemProviderErrorCode.FileNotFound FileSystemProviderErrorCode.FileNotFound
); );
} }

View File

@ -391,9 +391,10 @@ export class CloudSketchbookTree extends SketchbookTree {
const { filesToWrite, filesToDelete } = await this.treeDiff(source, dest); const { filesToWrite, filesToDelete } = await this.treeDiff(source, dest);
// Sort by the URIs. The shortest comes first. It's to ensure creating the parent folder for nested resources, for example. // Sort by the URIs. The shortest comes first. It's to ensure creating the parent folder for nested resources, for example.
// When sorting the URIs, it does not matter whether on source or dest, only the URI path and its length matters; they're the same for a source+dest pair // When sorting the URIs, it does not matter whether on source or dest, only the URI path and its length matters; they're the same for a source+dest pair
filesToWrite.sort( const uriPathLengthComparator = (left: URI, right: URI) =>
(left, right) => left.path.toString().length - right.path.toString().length;
left.source.path.toString().length - right.source.path.toString().length filesToWrite.sort((left, right) =>
uriPathLengthComparator(left.source, right.source)
); );
for (const { source, dest } of filesToWrite) { for (const { source, dest } of filesToWrite) {
const stat = await this.fileService.resolve(source); const stat = await this.fileService.resolve(source);
@ -405,11 +406,11 @@ export class CloudSketchbookTree extends SketchbookTree {
} }
} }
await Promise.all( // Longes URI paths come first to delete the most nested ones first.
filesToDelete.map((file) => filesToDelete.sort(uriPathLengthComparator).reverse();
this.fileService.delete(file, { recursive: true }) for (const resource of filesToDelete) {
) await this.fileService.delete(resource, { recursive: true });
); }
} }
override async resolveChildren( override async resolveChildren(