@@ -282,12 +282,12 @@ export class WorkbenchDB {
282282 fileList . forEach ( ( file ) => {
283283 const fileParentPath = file . getDataValue ( "parent" ) ;
284284 const fileNode = pathToNodeMap . get ( file . getDataValue ( "path" ) ) ;
285- if ( Number ( file . getDataValue ( "id" ) ) !== 0 ) {
285+ if ( file . getDataValue ( "parent" ) === "#" ) {
286+ roots . push ( fileNode ) ;
287+ } else {
286288 if ( pathToNodeMap . has ( fileParentPath ) ) {
287289 pathToNodeMap . get ( fileParentPath ) . children ?. push ( fileNode ) ;
288290 }
289- } else {
290- roots . push ( fileNode ) ;
291291 }
292292 } ) ;
293293
@@ -401,6 +401,7 @@ export class WorkbenchDB {
401401 this . pause ( ) ;
402402
403403 promiseChain = promiseChain
404+ . then ( ( ) => this . _imputeMissingIntermediateDirectories ( files ) )
404405 . then ( ( ) => primaryPromise . _batchCreateFiles ( files ) )
405406 . then ( ( ) => {
406407 const currentProgress = Math . round (
@@ -427,7 +428,8 @@ export class WorkbenchDB {
427428 // See https://github.com/nexB/scancode-toolkit/issues/543
428429 promiseChain
429430 . then ( ( ) => {
430- if ( rootPath && ! hasRootPath ) {
431+ if ( ! hasRootPath ) {
432+ rootPath = rootPath || "no-files" ;
431433 files . push ( {
432434 path : rootPath ,
433435 name : rootPath ,
@@ -436,6 +438,7 @@ export class WorkbenchDB {
436438 } ) ;
437439 }
438440 } )
441+ . then ( ( ) => this . _imputeIntermediateDirectories ( files ) )
439442 . then ( ( ) => this . _batchCreateFiles ( files ) )
440443 . then ( ( ) => this . db . Header . create ( TopLevelData . parsedHeader ) )
441444 . then ( ( ) => {
@@ -600,7 +603,7 @@ export class WorkbenchDB {
600603 duration : header . duration ,
601604 options : header ?. options || { } ,
602605 input,
603- files_count : header . extra_data ?. files_count ,
606+ files_count : header . extra_data ?. files_count || 0 ,
604607 output_format_version : header . output_format_version ,
605608 spdx_license_list_version : header . extra_data ?. spdx_license_list_version ,
606609 operating_system : header . extra_data ?. system_environment ?. operating_system ,
@@ -880,6 +883,37 @@ export class WorkbenchDB {
880883 } ) ;
881884 }
882885
886+ // Adds & modifies files array in place, adding missing intermediate directories
887+ _imputeIntermediateDirectories ( files : Resource [ ] ) {
888+ const availableFiles = new Set ( files . map ( ( file ) => file . path ) ) ;
889+ const intermediateDirectories : Resource [ ] = [ ] ;
890+
891+ files . forEach ( ( file ) => {
892+ file . parent = parentPath ( file . path ) ;
893+
894+ // Add intermediate directories if parent not available in files
895+ if ( ! availableFiles . has ( file . parent ) ) {
896+ for (
897+ let currentDir = file . parent ;
898+ currentDir !== parentPath ( currentDir ) &&
899+ ! availableFiles . has ( currentDir ) ;
900+ currentDir = parentPath ( currentDir )
901+ ) {
902+ availableFiles . add ( currentDir ) ;
903+ intermediateDirectories . push ( {
904+ path : currentDir ,
905+ parent : parentPath ( currentDir ) ,
906+ name : path . basename ( currentDir ) ,
907+ type : "directory" ,
908+ files_count : 0 ,
909+ } ) ;
910+ }
911+ availableFiles . add ( file . parent ) ;
912+ }
913+ } ) ;
914+ files . push ( ...intermediateDirectories ) ;
915+ }
916+
883917 _batchCreateFiles ( files : Resource [ ] ) {
884918 // Add batched files to the DB
885919 return this . _addFlattenedFiles ( files ) . then ( ( ) => this . _addFiles ( files ) ) ;
0 commit comments