Skip to content

Commit

Permalink
fix(compiler): make sure language service use updated compiler options (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
cexbrayat authored May 26, 2021
1 parent c568f49 commit 348e30f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/compiler/ts-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/ts-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -405,6 +406,9 @@ export class TsCompiler implements TsCompilerInstance {
shouldIncrementProjectVersion = true
}
}
if (stringify(this._compilerOptions) !== stringify(this._initialCompilerOptions)) {
shouldIncrementProjectVersion = true
}

if (shouldIncrementProjectVersion) this._projectVersion++
}
Expand Down

0 comments on commit 348e30f

Please sign in to comment.