@@ -41,6 +41,7 @@ import { getErrorMessage } from "../common/helpers-pure";
41
41
import { createTimeoutSignal } from "../common/fetch-stream" ;
42
42
import { unzipToDirectoryConcurrently } from "../common/unzip-concurrently" ;
43
43
import { reportUnzipProgress } from "../common/vscode/unzip-progress" ;
44
+ import { getDirectoryNamesInsidePath } from "../common/files" ;
44
45
45
46
// Limit to three repos when generating autofixes so not sending
46
47
// too many requests to autofix. Since we only need to validate
@@ -422,10 +423,21 @@ async function downloadPublicCommitSource(
422
423
423
424
// Check if directory already exists to avoid re-downloading
424
425
if ( await pathExists ( checkoutDir ) ) {
425
- void extLogger . log (
426
- `Source for ${ nwo } at ${ sha } already exists at ${ checkoutDir } .` ,
427
- ) ;
428
- return checkoutDir ;
426
+ const dirNames = await getDirectoryNamesInsidePath ( checkoutDir ) ;
427
+ if ( dirNames . length === 1 ) {
428
+ // The path to the source code should be a single directory inside `checkoutDir`.
429
+ const sourceRootDir = join ( checkoutDir , dirNames [ 0 ] ) ;
430
+ void extLogger . log (
431
+ `Source for ${ nwo } at ${ sha } already exists at ${ sourceRootDir } .` ,
432
+ ) ;
433
+ return sourceRootDir ;
434
+ } else {
435
+ // Remove `checkoutDir` to allow a re-download if the directory structure is unexpected.
436
+ void extLogger . log (
437
+ `Unexpected directory structure. Removing ${ checkoutDir } ` ,
438
+ ) ;
439
+ await remove ( checkoutDir ) ;
440
+ }
429
441
}
430
442
431
443
void extLogger . log ( `Fetching source of repository ${ nwo } at ${ sha } ...` ) ;
@@ -547,14 +559,23 @@ async function downloadPublicCommitSource(
547
559
// Extract the downloaded zip file
548
560
await unzipToDirectoryConcurrently (
549
561
archivePath ,
550
- outputPath ,
562
+ checkoutDir ,
551
563
progressCallback
552
564
? reportUnzipProgress ( `Unzipping source root...` , progressCallback )
553
565
: undefined ,
554
566
) ;
555
567
await remove ( archivePath ) ;
556
568
557
- return checkoutDir ;
569
+ // Since `unzipToDirectoryConcurrently` extracts to a directory within
570
+ // `checkoutDir`, we need to return the path to that extracted directory.
571
+ const dirNames = await getDirectoryNamesInsidePath ( checkoutDir ) ;
572
+ if ( dirNames . length === 1 ) {
573
+ return join ( checkoutDir , dirNames [ 0 ] ) ;
574
+ } else {
575
+ throw new Error (
576
+ `Expected exactly one unzipped source directory for ${ nwo } , but found ${ dirNames . length } .` ,
577
+ ) ;
578
+ }
558
579
} catch ( error ) {
559
580
throw new Error (
560
581
`Failed to download ${ nwo } at ${ sha } :. Reason: ${ getErrorMessage ( error ) } ` ,
0 commit comments