@@ -90,6 +90,66 @@ let interfaceLoader = {
9090 return pkg ;
9191 } ,
9292
93+ _searchAndGenerateInterface ( packageName , type , messageName , filePath ) {
94+ // Check if it's a valid package
95+ for ( const pkgPath of generator . getInstalledPackagePaths ( ) ) {
96+ if ( pkgPath . includes ( 'ros2-linux' ) ) {
97+ continue ;
98+ }
99+
100+ // Recursively search for files named messageName.* under pkgPath/
101+ if ( fs . existsSync ( pkgPath ) ) {
102+ // Recursive function to search for files
103+ function searchForFile ( dir ) {
104+ try {
105+ const items = fs . readdirSync ( dir , { withFileTypes : true } ) ;
106+ for ( const item of items ) {
107+ const fullPath = path . join ( dir , item . name ) ;
108+
109+ if ( item . isFile ( ) ) {
110+ const baseName = path . parse ( item . name ) . name ;
111+ // Check if the base filename matches messageName
112+ if ( baseName === messageName ) {
113+ return fullPath ;
114+ }
115+ } else if ( item . isDirectory ( ) ) {
116+ // Recursively search subdirectories
117+ const result = searchForFile ( fullPath ) ;
118+ if ( result ) {
119+ return result ;
120+ }
121+ }
122+ }
123+ } catch ( err ) {
124+ // Skip directories we can't read
125+ console . log ( 'Error reading directory:' , dir , err . message ) ;
126+ }
127+ return null ;
128+ }
129+
130+ const foundFilePath = searchForFile (
131+ path . join ( pkgPath , 'share' , packageName )
132+ ) ;
133+ if ( foundFilePath && foundFilePath . length > 0 ) {
134+ // Use worker thread to generate interfaces synchronously
135+ try {
136+ generator . generateInPathSyncWorker ( pkgPath ) ;
137+ // Now try to load the interface again from the generated files
138+ if ( fs . existsSync ( filePath ) ) {
139+ return require ( filePath ) ;
140+ }
141+ } catch ( err ) {
142+ console . error ( 'Error in interface generation:' , err ) ;
143+ }
144+ } else {
145+ throw new Error (
146+ `The message required does not exist: ${ packageName } , ${ type } , ${ messageName } at ${ generator . generatedRoot } `
147+ ) ;
148+ }
149+ }
150+ }
151+ } ,
152+
93153 loadInterface ( packageName , type , messageName ) {
94154 if ( arguments . length === 1 ) {
95155 const type = arguments [ 0 ] ;
@@ -110,6 +170,13 @@ let interfaceLoader = {
110170
111171 if ( fs . existsSync ( filePath ) ) {
112172 return require ( filePath ) ;
173+ } else {
174+ return this . _searchAndGenerateInterface (
175+ packageName ,
176+ type ,
177+ messageName ,
178+ filePath
179+ ) ;
113180 }
114181 }
115182 throw new Error (
0 commit comments