{"record":{"id":"b378d9edfee6c932","repo":"microsoft/TypeScript","slug":"could-not-find-file-filename","errorCode":null,"errorMessage":"Could not find file: '${fileName}'.","messagePattern":"Could not find file: '(.+?)'\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/services/services.ts","lineNumber":1410,"sourceCode":"    return codefix.getSupportedErrorCodes();\r\n}\r\n\r\nclass SyntaxTreeCache {\r\n    // For our syntactic only features, we also keep a cache of the syntax tree for the\r\n    // currently edited file.\r\n    private currentFileName: string | undefined;\r\n    private currentFileVersion: string | undefined;\r\n    private currentFileScriptSnapshot: IScriptSnapshot | undefined;\r\n    private currentSourceFile: SourceFile | undefined;\r\n\r\n    constructor(private host: LanguageServiceHost) {\r\n    }\r\n\r\n    public getCurrentSourceFile(fileName: string): SourceFile {\r\n        const scriptSnapshot = this.host.getScriptSnapshot(fileName);\r\n        if (!scriptSnapshot) {\r\n            // The host does not know about this file.\r\n            throw new Error(\"Could not find file: '\" + fileName + \"'.\");\r\n        }\r\n\r\n        const scriptKind = getScriptKind(fileName, this.host);\r\n        const version = this.host.getScriptVersion(fileName);\r\n        let sourceFile: SourceFile | undefined;\r\n\r\n        if (this.currentFileName !== fileName) {\r\n            // This is a new file, just parse it\r\n            const options: CreateSourceFileOptions = {\r\n                languageVersion: ScriptTarget.Latest,\r\n                impliedNodeFormat: getImpliedNodeFormatForFile(\r\n                    toPath(fileName, this.host.getCurrentDirectory(), this.host.getCompilerHost?.()?.getCanonicalFileName || hostGetCanonicalFileName(this.host)),\r\n                    this.host.getCompilerHost?.()?.getModuleResolutionCache?.()?.getPackageJsonInfoCache(),\r\n                    this.host,\r\n                    this.host.getCompilationSettings(),\r\n                ),\r\n                setExternalModuleIndicator: getSetExternalModuleIndicator(this.host.getCompilationSettings()),\r\n                // These files are used to produce syntax-based highlighting, which reads JSDoc, so we must use ParseAll.\r","sourceCodeStart":1392,"sourceCodeEnd":1428,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/services/services.ts#L1392-L1428","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","Verify the fileName passed to the language service matches exactly the one the host keys on (normalize slashes and case).","If the file may not exist, check `host.getScriptSnapshot(fileName)` yourself before calling the language service and skip gracefully.","For embedded hosts, pre-populate getScriptSnapshot for project files at host construction."],"exampleFix":"// before — host returns undefined for an unregistered file\nclass MyHost implements ts.LanguageServiceHost {\n  getScriptSnapshot(fileName: string) {\n    return this.files.get(fileName)?.snapshot; // undefined when missing\n  }\n}\nls.getBraceMatchingAtPosition('/unregistered.ts', 0);\n// throws: Could not find file: '/unregistered.ts'.\n\n// after — guard before calling the language service\nconst snap = host.getScriptSnapshot('/unregistered.ts');\nif (snap) ls.getBraceMatchingAtPosition('/unregistered.ts', 0);","handlingStrategy":"type-guard","validationCode":"// Guard the host lookup before calling the language service.\nconst snapshot = host.getScriptSnapshot(fileName);\nif (!snapshot) {\n  throw new Error(`Host has no snapshot for ${fileName}; add the file before querying the language service.`);\n}\nls.getBraceMatchingAtPosition(fileName, 0);","typeGuard":"function hostHasFile(host: ts.LanguageServiceHost, fileName: string): boolean {\n  return host.getScriptSnapshot(fileName) !== undefined;\n}","tryCatchPattern":null,"preventionTips":["Register every file (and its version) with the host before calling any language-service method.","Normalize fileName (slashes, case) to match the host's keying.","For embedded hosts, pre-populate getScriptSnapshot for project files at construction."],"tags":["language-service","host","script-snapshot","embedding"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}