{"record":{"id":"c19b23ec97942f27","repo":"microsoft/TypeScript","slug":"file-index-index-in-openfile-was-out-of-range","errorCode":null,"errorMessage":"File index (${index}) in openFile was out of range. There are only ${this.testData.files.length} files in this test.","messagePattern":"File index \\((.+?)\\) in openFile was out of range\\. There are only (.+?) files in this test\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/harness/fourslashImpl.ts","lineNumber":4427,"sourceCode":"\r\n        return text.substring(startPos, endPos);\r\n    }\r\n\r\n    // Get the text of the entire line the caret is currently at\r\n    private getCurrentLineContent() {\r\n        return this.getLineContent(\r\n            this.languageServiceAdapterHost.positionToLineAndCharacter(\r\n                this.activeFile.fileName,\r\n                this.currentCaretPosition,\r\n            ).line,\r\n        );\r\n    }\r\n\r\n    private findFile(indexOrName: string | number): FourSlashFile {\r\n        if (typeof indexOrName === \"number\") {\r\n            const index = indexOrName;\r\n            if (index >= this.testData.files.length) {\r\n                throw new Error(`File index (${index}) in openFile was out of range. There are only ${this.testData.files.length} files in this test.`);\r\n            }\r\n            else {\r\n                return this.testData.files[index];\r\n            }\r\n        }\r\n        else if (ts.isString(indexOrName)) {\r\n            const { file, availableNames } = this.tryFindFileWorker(indexOrName);\r\n            if (!file) {\r\n                throw new Error(`No test file named \"${indexOrName}\" exists. Available file names are: ${availableNames.join(\", \")}`);\r\n            }\r\n            return file;\r\n        }\r\n        else {\r\n            return ts.Debug.assertNever(indexOrName);\r\n        }\r\n    }\r\n\r\n    private tryFindFileWorker(name: string): { readonly file: FourSlashFile | undefined; readonly availableNames: readonly string[]; } {\r","sourceCodeStart":4409,"sourceCodeEnd":4445,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/harness/fourslashImpl.ts#L4409-L4445","documentation":"Thrown by findFile when openFile (or goTo.file) is called with a numeric index greater than or equal to the number of files declared in the test. The message reports both the bad index and the total file count so the caller can see how far off the value is.","triggerScenarios":"Calling goTo.file(2) or openFile(2) in a fourslash test that only declares 1 or 2 files. The numeric branch of findFile checks index >= this.testData.files.length and throws.","commonSituations":"A test is edited to remove a file (deleting an @FileName block) but the index references are not updated, or an author assumes 1-based indexing (it is 0-based). Also occurs after merging test files.","solutions":["Check the message for the total file count and ensure your index is less than that count (0-based).","If you need a later file, add it to the test using @FileName or the multi-file fourslash syntax.","Prefer referencing files by name (goTo.file(\"/path.ts\")) instead of index to avoid fragility when files are added or removed."],"exampleFix":"// before — test has 2 files but code references index 2\n// const file = 2; goTo.file(file);\n// after — use a valid 0-based index or name\n// goTo.file(1);\ngoTo.file(\"/lib/module.ts\");","handlingStrategy":"validation","validationCode":"function safeFileIndex(state: FourSlashTestState, index: number) {\n    if (index < 0 || index >= state.testData.files.length) {\n        throw new Error(`Index ${index} invalid; test has ${state.testData.files.length} files`);\n    }\n    return state.testData.files[index];\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer file names over numeric indices for stability.","After adding/removing a file in a multi-file test, audit all numeric openFile/goTo.file references."],"tags":["fourslash","file-management","index-out-of-range"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}