@@ -41,6 +41,7 @@ import { getErrorMessage } from "../common/helpers-pure";
4141import { createTimeoutSignal } from "../common/fetch-stream" ;
4242import { unzipToDirectoryConcurrently } from "../common/unzip-concurrently" ;
4343import { reportUnzipProgress } from "../common/vscode/unzip-progress" ;
44+ import { getDirectoryNamesInsidePath } from "../common/files" ;
4445
4546// Limit to three repos when generating autofixes so not sending
4647// too many requests to autofix. Since we only need to validate
@@ -422,10 +423,21 @@ async function downloadPublicCommitSource(
422423
423424 // Check if directory already exists to avoid re-downloading
424425 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+ }
429441 }
430442
431443 void extLogger . log ( `Fetching source of repository ${ nwo } at ${ sha } ...` ) ;
@@ -547,14 +559,23 @@ async function downloadPublicCommitSource(
547559 // Extract the downloaded zip file
548560 await unzipToDirectoryConcurrently (
549561 archivePath ,
550- outputPath ,
562+ checkoutDir ,
551563 progressCallback
552564 ? reportUnzipProgress ( `Unzipping source root...` , progressCallback )
553565 : undefined ,
554566 ) ;
555567 await remove ( archivePath ) ;
556568
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+ }
558579 } catch ( error ) {
559580 throw new Error (
560581 `Failed to download ${ nwo } at ${ sha } :. Reason: ${ getErrorMessage ( error ) } ` ,
0 commit comments