@@ -15,6 +15,7 @@ import { createJsonBuildManifest, emitFilesToDisk } from '../../tools/esbuild/ut
1515import { colors as ansiColors } from '../../utils/color' ;
1616import { deleteOutputDir } from '../../utils/delete-output-dir' ;
1717import { bazelEsbuildPluginPath , useJSONBuildLogs } from '../../utils/environment-options' ;
18+ import { isSubDirectory } from '../../utils/path' ;
1819import { purgeStaleBuildCache } from '../../utils/purge-cache' ;
1920import { assertCompatibleAngularVersion } from '../../utils/version' ;
2021import { runEsBuildBuildAction } from './build-action' ;
@@ -197,45 +198,57 @@ export async function* buildApplication(
197198
198199 // Writes the output files to disk and ensures the containing directories are present
199200 const directoryExists = new Set < string > ( ) ;
200- await emitFilesToDisk ( Object . entries ( result . files ) , async ( [ filePath , file ] ) => {
201- if (
202- outputOptions . ignoreServer &&
203- ( file . type === BuildOutputFileType . ServerApplication ||
204- file . type === BuildOutputFileType . ServerRoot )
205- ) {
206- return ;
207- }
201+ try {
202+ await emitFilesToDisk ( Object . entries ( result . files ) , async ( [ filePath , file ] ) => {
203+ if (
204+ outputOptions . ignoreServer &&
205+ ( file . type === BuildOutputFileType . ServerApplication ||
206+ file . type === BuildOutputFileType . ServerRoot )
207+ ) {
208+ return ;
209+ }
208210
209- const fullFilePath = generateFullPath ( filePath , file . type , outputOptions ) ;
211+ const fullFilePath = generateFullPath ( filePath , file . type , outputOptions ) ;
210212
211- // Ensure output subdirectories exist
212- const fileBasePath = path . dirname ( fullFilePath ) ;
213- if ( fileBasePath && ! directoryExists . has ( fileBasePath ) ) {
214- await fs . mkdir ( fileBasePath , { recursive : true } ) ;
215- directoryExists . add ( fileBasePath ) ;
216- }
213+ // Ensure output subdirectories exist
214+ const fileBasePath = path . dirname ( fullFilePath ) ;
215+ if ( fileBasePath && ! directoryExists . has ( fileBasePath ) ) {
216+ await fs . mkdir ( fileBasePath , { recursive : true } ) ;
217+ directoryExists . add ( fileBasePath ) ;
218+ }
217219
218- if ( file . origin === 'memory' ) {
219- // Write file contents
220- await fs . writeFile ( fullFilePath , file . contents ) ;
221- } else {
222- // Copy file contents
223- await fs . cp ( file . inputPath , fullFilePath , {
224- mode : fs . constants . COPYFILE_FICLONE ,
225- preserveTimestamps : true ,
226- } ) ;
227- }
228- } ) ;
220+ if ( file . origin === 'memory' ) {
221+ // Write file contents
222+ await fs . writeFile ( fullFilePath , file . contents ) ;
223+ } else {
224+ // Copy file contents
225+ await fs . cp ( file . inputPath , fullFilePath , {
226+ mode : fs . constants . COPYFILE_FICLONE ,
227+ preserveTimestamps : true ,
228+ } ) ;
229+ }
230+ } ) ;
231+ } catch ( error ) {
232+ context . logger . error ( error instanceof Error ? error . message : String ( error ) ) ;
233+ yield { success : false } ;
234+ continue ;
235+ }
229236
230237 // Delete any removed files if incremental
231238 if ( result . kind === ResultKind . Incremental && result . removed ?. length ) {
232- await Promise . all (
233- result . removed . map ( ( file ) => {
234- const fullFilePath = generateFullPath ( file . path , file . type , outputOptions ) ;
239+ try {
240+ await Promise . all (
241+ result . removed . map ( ( file ) => {
242+ const fullFilePath = generateFullPath ( file . path , file . type , outputOptions ) ;
235243
236- return fs . rm ( fullFilePath , { force : true , maxRetries : 3 } ) ;
237- } ) ,
238- ) ;
244+ return fs . rm ( fullFilePath , { force : true , maxRetries : 3 } ) ;
245+ } ) ,
246+ ) ;
247+ } catch ( error ) {
248+ context . logger . error ( error instanceof Error ? error . message : String ( error ) ) ;
249+ yield { success : false } ;
250+ continue ;
251+ }
239252 }
240253
241254 yield { success : true } ;
@@ -268,6 +281,12 @@ function generateFullPath(
268281 // NOTE: 'base' is a fully resolved path at this point
269282 const fullFilePath = path . join ( outputOptions . base , typeDirectory , filePath ) ;
270283
284+ if ( ! isSubDirectory ( outputOptions . base , fullFilePath ) ) {
285+ throw new Error (
286+ `The output file path "${ fullFilePath } " is outside of the configured output path "${ outputOptions . base } ".` ,
287+ ) ;
288+ }
289+
271290 return fullFilePath ;
272291}
273292
0 commit comments