Skip to content

Commit 348e30f

Browse files
authoredMay 26, 2021
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
1 parent c568f49 commit 348e30f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎src/compiler/ts-compiler.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ describe('TsCompiler', () => {
329329
beforeEach(() => {
330330
// @ts-expect-error testing purpose
331331
compiler._projectVersion = 1
332+
/**
333+
* This is to ensure that `compilerOptions` value and `_parsedTsConfig.fileNames` are always like
334+
* when compiler instance is created since here we only create compiler instance once for all the tests below.
335+
*/
336+
// @ts-expect-error testing purpose.
337+
compiler._compilerOptions = { ...compiler._initialCompilerOptions }
338+
// @ts-expect-error testing purpose.
339+
compiler._parsedTsConfig.fileNames = []
332340
fileContentCache.clear()
333341
fileVersionCache.clear()
334342
})
@@ -360,6 +368,29 @@ describe('TsCompiler', () => {
360368
expect(compiler._projectVersion).toEqual(2)
361369
})
362370

371+
test('should increase project version if processing file is in compiler file list', () => {
372+
// @ts-expect-error testing purpose
373+
compiler._parsedTsConfig.fileNames.push(fileName)
374+
fileContentCache.set(fileName, fileContent)
375+
fileVersionCache.set(fileName, 1)
376+
// @ts-expect-error testing purpose
377+
compiler._fileContentCache = fileContentCache
378+
// @ts-expect-error testing purpose
379+
compiler._fileVersionCache = fileVersionCache
380+
// @ts-expect-error testing purpose
381+
compiler._compilerOptions = {
382+
// @ts-expect-error testing purpose
383+
...compiler._compilerOptions,
384+
module: ModuleKind.AMD,
385+
}
386+
387+
// @ts-expect-error testing purpose
388+
compiler._updateMemoryCache(fileContent, fileName)
389+
390+
// @ts-expect-error testing purpose
391+
expect(compiler._projectVersion).toEqual(2)
392+
})
393+
363394
test('should increase project version if processing file version is 0', () => {
364395
fileContentCache.set(fileName, fileContent)
365396
fileVersionCache.set(fileName, 0)

‎src/compiler/ts-compiler.ts

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type {
3232
TsJestCompileOptions,
3333
TTypeScript,
3434
} from '../types'
35+
import { stringify } from '../utils/json'
3536
import { rootLogger } from '../utils/logger'
3637
import { Errors, interpolate } from '../utils/messages'
3738

@@ -405,6 +406,9 @@ export class TsCompiler implements TsCompilerInstance {
405406
shouldIncrementProjectVersion = true
406407
}
407408
}
409+
if (stringify(this._compilerOptions) !== stringify(this._initialCompilerOptions)) {
410+
shouldIncrementProjectVersion = true
411+
}
408412

409413
if (shouldIncrementProjectVersion) this._projectVersion++
410414
}

0 commit comments

Comments
 (0)
Please sign in to comment.