Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): make sure typesDir exist before writing to it #5109

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/compiler/sys/in-memory-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,18 @@ export type FsItems = Map<string, FsItem>;
* Options supported by write methods on the in-memory filesystem.
*/
export interface FsWriteOptions {
/**
* only use the in-memory cache and do not write the file to disk
*/
inMemoryOnly?: boolean;
clearFileCache?: boolean;
/**
* flush the write to disk immediately, skipping the in-memory cache
*/
immediateWrite?: boolean;
/**
* specify that the cache should be used
*/
useCache?: boolean;
/**
* An optional tag for the current output target for which this file is being
Expand Down Expand Up @@ -724,7 +733,10 @@ export const createInMemoryFs = (sys: d.CompilerSystem) => {

if (results.changedContent) {
await ensureDir(filePath, false);
await sys.writeFile(filePath, item.fileText);
const { error } = await sys.writeFile(filePath, item.fileText);
if (error) {
throw error;
}
}
}
} else {
Expand Down Expand Up @@ -815,6 +827,15 @@ export const createInMemoryFs = (sys: d.CompilerSystem) => {
* only change the in-memory cache
*/
const ensureDir = async (path: string, inMemoryOnly: boolean) => {
/**
* in case we write to disk immediately, there is no need to split
* all directories into separate calls, we can just use the recursive flag
*/
if (!inMemoryOnly) {
await sys.createDir(dirname(path), { recursive: true });
return;
}

const allDirs: string[] = [];

while (true) {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/sys/tests/in-memory-fs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createTestingSystem } from '../../../testing/testing-sys';
import { normalizePath } from '../../../utils';
import type { FsItem, FsItems } from '../in-memory-fs';
import { createInMemoryFs, getCommitInstructions, InMemoryFileSystem, shouldIgnore } from '../in-memory-fs';
import { FsItem, FsItems } from '../in-memory-fs';

describe(`in-memory-fs, getCommitInstructions`, () => {
let items: FsItems;
Expand Down Expand Up @@ -583,7 +583,7 @@ describe(`in-memory-fs`, () => {
expect(sys.diskReads).toBe(1);

let i = await fs.commit();
expect(sys.diskWrites).toBe(3);
expect(sys.diskWrites).toBe(4);
expect(i.filesWritten).toHaveLength(1);
expect(i.filesWritten[0]).toBe(`/dir/file2.js`);

Expand Down
Loading