From 348e30f426803efc51ecba26ab42619938fcb5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Exbrayat?= Date: Wed, 26 May 2021 23:41:11 +0200 Subject: [PATCH] fix(compiler): make sure language service use updated compiler options (#2628) When a file initially exists in compiler file set when `TsCompiler` is created, later in `getCompiledOutput` the value of `compilerOptions` which makes `LanguageService` is not aware of because `_updateMemoryCache` checks and sees that the file is the same so `projectVersion` value is not increased which makes `LanguageService` uses the outdated `_compilerOptions`. This commit adds a comparison via JSON stringify between `_compilerOptions` and `_initialCompilerOptions`. If there is a difference, increase `projectVersion` for `LanguageService`. Closes #2629 --- src/compiler/ts-compiler.spec.ts | 31 +++++++++++++++++++++++++++++++ src/compiler/ts-compiler.ts | 4 ++++ 2 files changed, 35 insertions(+) diff --git a/src/compiler/ts-compiler.spec.ts b/src/compiler/ts-compiler.spec.ts index 56395a69f7..540750aec6 100644 --- a/src/compiler/ts-compiler.spec.ts +++ b/src/compiler/ts-compiler.spec.ts @@ -329,6 +329,14 @@ describe('TsCompiler', () => { beforeEach(() => { // @ts-expect-error testing purpose compiler._projectVersion = 1 + /** + * This is to ensure that `compilerOptions` value and `_parsedTsConfig.fileNames` are always like + * when compiler instance is created since here we only create compiler instance once for all the tests below. + */ + // @ts-expect-error testing purpose. + compiler._compilerOptions = { ...compiler._initialCompilerOptions } + // @ts-expect-error testing purpose. + compiler._parsedTsConfig.fileNames = [] fileContentCache.clear() fileVersionCache.clear() }) @@ -360,6 +368,29 @@ describe('TsCompiler', () => { expect(compiler._projectVersion).toEqual(2) }) + test('should increase project version if processing file is in compiler file list', () => { + // @ts-expect-error testing purpose + compiler._parsedTsConfig.fileNames.push(fileName) + fileContentCache.set(fileName, fileContent) + fileVersionCache.set(fileName, 1) + // @ts-expect-error testing purpose + compiler._fileContentCache = fileContentCache + // @ts-expect-error testing purpose + compiler._fileVersionCache = fileVersionCache + // @ts-expect-error testing purpose + compiler._compilerOptions = { + // @ts-expect-error testing purpose + ...compiler._compilerOptions, + module: ModuleKind.AMD, + } + + // @ts-expect-error testing purpose + compiler._updateMemoryCache(fileContent, fileName) + + // @ts-expect-error testing purpose + expect(compiler._projectVersion).toEqual(2) + }) + test('should increase project version if processing file version is 0', () => { fileContentCache.set(fileName, fileContent) fileVersionCache.set(fileName, 0) diff --git a/src/compiler/ts-compiler.ts b/src/compiler/ts-compiler.ts index 0543708566..7eafdb1b86 100644 --- a/src/compiler/ts-compiler.ts +++ b/src/compiler/ts-compiler.ts @@ -32,6 +32,7 @@ import type { TsJestCompileOptions, TTypeScript, } from '../types' +import { stringify } from '../utils/json' import { rootLogger } from '../utils/logger' import { Errors, interpolate } from '../utils/messages' @@ -405,6 +406,9 @@ export class TsCompiler implements TsCompilerInstance { shouldIncrementProjectVersion = true } } + if (stringify(this._compilerOptions) !== stringify(this._initialCompilerOptions)) { + shouldIncrementProjectVersion = true + } if (shouldIncrementProjectVersion) this._projectVersion++ }