diff --git a/packages/compiler-cli/src/ngtsc/file_system/src/cached_file_system.ts b/packages/compiler-cli/src/ngtsc/file_system/src/cached_file_system.ts index 92030c58c7..25e9ff81de 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/src/cached_file_system.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/src/cached_file_system.ts @@ -28,6 +28,11 @@ export class CachedFileSystem implements FileSystem { return this.existsCache.get(path) !; } + invalidateCaches(path: AbsoluteFsPath) { + this.readFileCache.delete(path); + this.existsCache.delete(path); + } + readFile(path: AbsoluteFsPath): string { if (!this.readFileCache.has(path)) { try { diff --git a/packages/compiler-cli/src/ngtsc/file_system/test/cached_file_system_spec.ts b/packages/compiler-cli/src/ngtsc/file_system/test/cached_file_system_spec.ts index 04610ddaa1..1f81b7c1e9 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/test/cached_file_system_spec.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/test/cached_file_system_spec.ts @@ -90,6 +90,39 @@ describe('CachedFileSystem', () => { }); }); + describe('invalidateCaches()', () => { + it('should use the delegate for `readFile()` if the path for the cached file has been invalidated', + () => { + spyOn(delegate, 'lstat').and.returnValue({isSymbolicLink: () => false}); + const spy = spyOn(delegate, 'readFile').and.returnValue('Some contents'); + fs.readFile(abcPath); // Call once to fill the cache + spy.calls.reset(); + + expect(fs.readFile(abcPath)).toBe('Some contents'); + expect(spy).not.toHaveBeenCalled(); + + fs.invalidateCaches(abcPath); + + expect(fs.readFile(abcPath)).toBe('Some contents'); + expect(spy).toHaveBeenCalledWith(abcPath); + }); + + it('should use the delegate `exists()` if the path for the cached file has been invalidated', + () => { + const spy = spyOn(delegate, 'exists').and.returnValue(true); + fs.exists(abcPath); // Call once to fill the cache + spy.calls.reset(); + + expect(fs.exists(abcPath)).toBe(true); + expect(spy).not.toHaveBeenCalled(); + + fs.invalidateCaches(abcPath); + + expect(fs.exists(abcPath)).toBe(true); + expect(spy).toHaveBeenCalledWith(abcPath); + }); + }); + describe('writeFile()', () => { it('should call delegate', () => { const spy = spyOn(delegate, 'writeFile');