microsoft / microsoft/TypeScript

Expose a function for constructing a new System based on a custom FS implementation

Open
#44,379 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Suggestion

Currently TypeScript constructs a System and sets it on the sys property on the TypeScript module. Many parts of the Compiler APIs accept a custom implementation for System, which is useful for example for testing purposes when a virtual file system is being used.

But it is not easily mockable, as the System interface is quite extensive. Internally, TypeScript constructs this by invoking an anonymous internal function, like so:

ts.sys = (function () {
 // ...
})();

🔍 Search Terms

createSystem, Mock System, Virtual Filesystem

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I would like to propose exposing a function, createSystem, that takes an Partial of the following structure:

interface CreateSystemOptions {
    // An FS implementation
    fs: typeof import("fs");

    // These are the only two properties you use from the os module
    os: Pick<typeof import("os"), "platform"|"EOL">;
    
    // You default to using process.cwd() here, but the current working directory could in theory be something else
    cwd: string;
}

export function createSystem (options: Partial<CreateSystemOptions>): System {
    // ...
}

This function should also be used internally by TypeScript, and constructs a new System. This would allow for easier mocking.

📃 Motivating Example

Imagine building a Program programmatically that relies on an incremental CompilerHost. With the createSystem function, you would very easily be able to swap out the fs implementation and pass it a virtual file system based on the test(s) you're running:

import * as ts from "typescript";
import virtualFs from "memfs";
import realFs from "fs";

function createCompilerHost (fs: typeof import("fs") = realFs): ts.CompilerHost {
    return ts.createIncrementalCompilerHost({}, ts.createSystem({fs}));
}

const program = ts.createProgram({
    host: createCompilerHost(virtualFs),
    rootNames: ["/foo/bar/baz.ts"],
    options: {}
})

💻 Use Cases

Use cases:

  • Testing
  • Tooling around TypeScript's Compiler APIs
  • Virtual file systems, such as is the case with some bundlers running in watch mode

Workarounds:

  • Manually reimplementing most of the System interface, and manually maintaining it across TypeScript versions.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the internal anonymous function that constructs ts.sys and reviewing how createIncrementalCompilerHost consumes a System. Define the public createSystem entry point around the proposed options and ensure TypeScript uses it internally. Done means callers can provide a virtual FS without manually reimplementing most of System.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.