-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@angular-devkit/build-angular): automatically purge stale build c…
…ache entries With every build-angular release, previously created cache entries get stale and are no longer used. This causes the cache to keep growing as older files are not purged. With this change we automatically purge entries that have been created with older version of build-angular and can no longer be used with the current installed version. Closes #22323 (cherry picked from commit 6d2087b)
- Loading branch information
1 parent
6876ad3
commit 5b39e0e
Showing
13 changed files
with
129 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/angular_devkit/build_angular/src/utils/package-version.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export const VERSION: string = require('../../package.json').version; |
53 changes: 53 additions & 0 deletions
53
packages/angular_devkit/build_angular/src/utils/purge-cache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { BuilderContext } from '@angular-devkit/architect'; | ||
import { PathLike, existsSync, promises as fsPromises } from 'fs'; | ||
import { join } from 'path'; | ||
import { normalizeCacheOptions } from './normalize-cache'; | ||
|
||
/** Delete stale cache directories used by previous versions of build-angular. */ | ||
export async function purgeStaleBuildCache(context: BuilderContext): Promise<void> { | ||
const projectName = context.target?.project; | ||
if (!projectName) { | ||
return; | ||
} | ||
|
||
const metadata = await context.getProjectMetadata(projectName); | ||
const { basePath, path, enabled } = normalizeCacheOptions(metadata, context.workspaceRoot); | ||
|
||
if (!enabled || !existsSync(basePath)) { | ||
return; | ||
} | ||
|
||
// The below should be removed and replaced with just `rm` when support for Node.Js 12 is removed. | ||
const { rm, rmdir } = fsPromises as typeof fsPromises & { | ||
rm?: ( | ||
path: PathLike, | ||
options?: { | ||
force?: boolean; | ||
maxRetries?: number; | ||
recursive?: boolean; | ||
retryDelay?: number; | ||
}, | ||
) => Promise<void>; | ||
}; | ||
|
||
const entriesToDelete = (await fsPromises.readdir(basePath, { withFileTypes: true })) | ||
.filter((d) => join(basePath, d.name) !== path && d.isDirectory()) | ||
.map((d) => { | ||
const subPath = join(basePath, d.name); | ||
try { | ||
return rm | ||
? rm(subPath, { force: true, recursive: true, maxRetries: 3 }) | ||
: rmdir(subPath, { recursive: true, maxRetries: 3 }); | ||
} catch {} | ||
}); | ||
|
||
await Promise.all(entriesToDelete); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { join } from 'path'; | ||
import { createDir, expectFileNotToExist, expectFileToExist } from '../../utils/fs'; | ||
import { ng } from '../../utils/process'; | ||
import { updateJsonFile } from '../../utils/project'; | ||
|
||
export default async function () { | ||
const cachePath = '.angular/cache'; | ||
const staleCachePath = join(cachePath, 'v1.0.0'); | ||
|
||
// Enable cache for all environments | ||
await updateJsonFile('angular.json', (config) => { | ||
config.cli ??= {}; | ||
config.cli.cache = { | ||
environment: 'all', | ||
enabled: true, | ||
path: cachePath, | ||
}; | ||
}); | ||
|
||
// Create a dummy stale disk cache directory. | ||
await createDir(staleCachePath); | ||
await expectFileToExist(staleCachePath); | ||
|
||
await ng('build'); | ||
await expectFileToExist(cachePath); | ||
await expectFileNotToExist(staleCachePath); | ||
} |