microsoft/TypeScript · error · Error

Could not find file: '${fileName}'.

Error message

Could not find file: '${fileName}'.

What it means

Thrown by SyntaxTreeCache.getCurrentSourceFile (services.ts:1410) when the LanguageServiceHost returns a falsy script snapshot for the requested fileName. The language service asks the host for `getScriptSnapshot(fileName)`; if the host does not know the file (no snapshot registered), the cache refuses to parse nothing and throws.

Source

Thrown at src/services/services.ts:1410

    return codefix.getSupportedErrorCodes();
}

class SyntaxTreeCache {
    // For our syntactic only features, we also keep a cache of the syntax tree for the
    // currently edited file.
    private currentFileName: string | undefined;
    private currentFileVersion: string | undefined;
    private currentFileScriptSnapshot: IScriptSnapshot | undefined;
    private currentSourceFile: SourceFile | undefined;

    constructor(private host: LanguageServiceHost) {
    }

    public getCurrentSourceFile(fileName: string): SourceFile {
        const scriptSnapshot = this.host.getScriptSnapshot(fileName);
        if (!scriptSnapshot) {
            // The host does not know about this file.
            throw new Error("Could not find file: '" + fileName + "'.");
        }

        const scriptKind = getScriptKind(fileName, this.host);
        const version = this.host.getScriptVersion(fileName);
        let sourceFile: SourceFile | undefined;

        if (this.currentFileName !== fileName) {
            // This is a new file, just parse it
            const options: CreateSourceFileOptions = {
                languageVersion: ScriptTarget.Latest,
                impliedNodeFormat: getImpliedNodeFormatForFile(
                    toPath(fileName, this.host.getCurrentDirectory(), this.host.getCompilerHost?.()?.getCanonicalFileName || hostGetCanonicalFileName(this.host)),
                    this.host.getCompilerHost?.()?.getModuleResolutionCache?.()?.getPackageJsonInfoCache(),
                    this.host,
                    this.host.getCompilationSettings(),
                ),
                setExternalModuleIndicator: getSetExternalModuleIndicator(this.host.getCompilationSettings()),
                // These files are used to produce syntax-based highlighting, which reads JSDoc, so we must use ParseAll.

View on GitHub (pinned to b465fdbfe1)

Solutions

  1. Ensure the host's getScriptSnapshot(fileName) returns a valid IScriptSnapshot for every file you query — register the file (and its version) before calling any language-service method.
  2. Verify the fileName passed to the language service matches exactly the one the host keys on (normalize slashes and case).
  3. If the file may not exist, check `host.getScriptSnapshot(fileName)` yourself before calling the language service and skip gracefully.
  4. For embedded hosts, pre-populate getScriptSnapshot for project files at host construction.

Example fix

// before — host returns undefined for an unregistered file
class MyHost implements ts.LanguageServiceHost {
  getScriptSnapshot(fileName: string) {
    return this.files.get(fileName)?.snapshot; // undefined when missing
  }
}
ls.getBraceMatchingAtPosition('/unregistered.ts', 0);
// throws: Could not find file: '/unregistered.ts'.

// after — guard before calling the language service
const snap = host.getScriptSnapshot('/unregistered.ts');
if (snap) ls.getBraceMatchingAtPosition('/unregistered.ts', 0);
Defensive patterns

Strategy: type-guard

Validate before calling

// Guard the host lookup before calling the language service.
const snapshot = host.getScriptSnapshot(fileName);
if (!snapshot) {
  throw new Error(`Host has no snapshot for ${fileName}; add the file before querying the language service.`);
}
ls.getBraceMatchingAtPosition(fileName, 0);

Type guard

function hostHasFile(host: ts.LanguageServiceHost, fileName: string): boolean {
  return host.getScriptSnapshot(fileName) !== undefined;
}

Prevention

When it happens

Trigger: A syntactic language-service feature (classification, brace matching, navTree) is invoked for a fileName for which the host's getScriptSnapshot returns undefined. This is the in-process language service (not tsserver); the host is the caller's own implementation of LanguageServiceHost.

Common situations: Embedder's host returns undefined for a file that was never added; querying the language service for a path before opening/adding it; a host that lazily loads snapshots but its loader returned undefined; fileName mismatch (slashes/case) so the host's map lookup misses.

Related errors


AI-assisted analysis of microsoft/TypeScript@b465fdbfe1 (2026-08-12). Data as JSON: /api/errors/b378d9edfee6c932. Report an issue: GitHub.