Skip to content

Commit

Permalink
fix: add missing realpath and initialize files in memory
Browse files Browse the repository at this point in the history
In the situation, when there were already some files built in the real
fs, controlled typescript system returned true for fileExists call on
initial run and false on subsequent calls causing assertion in
TypeScript to fail.

✅ Closes: #630
  • Loading branch information
piotr-oles committed Aug 8, 2021
1 parent 7afc037 commit c37a58c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/typescript-reporter/reporter/ControlledTypeScriptSystem.ts
Expand Up @@ -150,11 +150,18 @@ function createControlledTypeScriptSystem(
}

function getReadFileSystem(path: string) {
if (
!isInitialRun &&
(mode === 'readonly' || mode === 'write-tsbuildinfo') &&
isArtifact(path)
) {
if ((mode === 'readonly' || mode === 'write-tsbuildinfo') && isArtifact(path)) {
if (isInitialRun && !memFileSystem.exists(path) && passiveFileSystem.exists(path)) {
// copy file to memory on initial run
const stats = passiveFileSystem.readStats(path);
if (stats?.isFile()) {
const content = passiveFileSystem.readFile(path);
if (content) {
memFileSystem.writeFile(path, content);
memFileSystem.updateTimes(path, stats.atime, stats.mtime);
}
}
}
return memFileSystem;
}

Expand All @@ -175,6 +182,9 @@ function createControlledTypeScriptSystem(
const controlledSystem: ControlledTypeScriptSystem = {
...typescript.sys,
useCaseSensitiveFileNames: caseSensitive,
realpath(path: string): string {
return getReadFileSystem(path).realPath(path);
},
fileExists(path: string): boolean {
const stats = getReadFileSystem(path).readStats(path);

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/TypeScriptGenerateTrace.spec.ts
Expand Up @@ -73,7 +73,7 @@ describe('TypeScript Generate Trace', () => {

// update sandbox to generate trace
await sandbox.patch(
'tsconfig.json',
'tsconfig.base.json',
' "rootDir": "./packages"',
[' "rootDir": "./packages",', ' "generateTrace": "./traces"'].join('\n')
);
Expand Down

0 comments on commit c37a58c

Please sign in to comment.