{"record":{"id":"cf53c13cf2963fba","repo":"microsoft/TypeScript","slug":"cannot-shadow-a-mutable-file-system","errorCode":null,"errorMessage":"Cannot shadow a mutable file system.","messagePattern":"Cannot shadow a mutable file system\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/harness/vfsUtil.ts","lineNumber":162,"sourceCode":"        if (this.isReadonly) return;\r\n        const fs = new FileSystem(this.ignoreCase, { time: this._time });\r\n        fs._lazy = this._lazy;\r\n        fs._cwd = this._cwd;\r\n        fs._time = this._time;\r\n        fs._shadowRoot = this._shadowRoot;\r\n        fs._dirStack = this._dirStack;\r\n        fs.makeReadonly();\r\n        this._lazy = {};\r\n        this._shadowRoot = fs;\r\n    }\r\n\r\n    /**\r\n     * Gets a shadow copy of this file system. Changes to the shadow copy do not affect the\r\n     * original, allowing multiple copies of the same core file system without multiple copies\r\n     * of the same data.\r\n     */\r\n    public shadow(ignoreCase: boolean = this.ignoreCase): FileSystem {\r\n        if (!this.isReadonly) throw new Error(\"Cannot shadow a mutable file system.\");\r\n        if (ignoreCase && !this.ignoreCase) throw new Error(\"Cannot create a case-insensitive file system from a case-sensitive one.\");\r\n        const fs = new FileSystem(ignoreCase, { time: this._time });\r\n        fs._shadowRoot = this;\r\n        fs._cwd = this._cwd;\r\n        return fs;\r\n    }\r\n\r\n    /**\r\n     * Gets or sets the timestamp (in milliseconds) used for file status, returning the previous timestamp.\r\n     *\r\n     * @link http://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html\r\n     */\r\n    public time(value?: number): number {\r\n        if (value !== undefined) {\r\n            if (this.isReadonly) throw createIOError(\"EPERM\");\r\n            this._time = value;\r\n        }\r\n        else if (!this.isReadonly) {\r","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/harness/vfsUtil.ts#L144-L180","documentation":"Thrown by FileSystem.shadow (vfsUtil.ts:162) when asked to shadow a file system that is not yet read-only. Shadowing shares data lazily between the original and the copy; allowing it on a mutable FS would let the original keep mutating state the shadow has already inherited. Callers must freeze the source first via makeReadonly().","triggerScenarios":"Test code calls `fs.shadow()` on a freshly-built virtual FS that has not yet been frozen. The constructor leaves isReadonly false; shadowing requires the source to be immutable so the copy-on-write invariants hold.","commonSituations":"Building a shared base VFS once and forking shadows per test without freezing; refactoring setup code that previously called makeReadonly; copy-pasting shadow usage from a readonly fixture into a mutable one.","solutions":["Call `fs.makeReadonly()` immediately before `fs.shadow()`.","Prefer `fs.snapshot()` if you actually want to fork the current mutable state — snapshot internally freezes a clone and re-points _shadowRoot.","Freeze the base VFS once in beforeAll/module init, then shadow per test.","Audit setup helpers to guarantee makeReadonly is called before any shadow."],"exampleFix":"// before\nconst base = new VirtualFileSystem(/* ... */);\npopulate(base);\nconst fork = base.shadow(); // throws\n\n// after — freeze first\nconst base = new VirtualFileSystem(/* ... */);\npopulate(base);\nbase.makeReadonly();\nconst fork = base.shadow();","handlingStrategy":"validation","validationCode":"// Freeze before shadowing.\nif (!fs.isReadonly) fs.makeReadonly();\nconst fork = fs.shadow();","typeGuard":"function isReadOnlyFs(fs: FileSystem): boolean {\n  return fs.isReadonly === true;\n}","tryCatchPattern":null,"preventionTips":["Always call makeReadonly before shadow.","Build and freeze shared base VFSes once in beforeAll/module init.","Prefer snapshot() when you want to fork mutable state."],"tags":["test-harness","vfs","readonly","filesystem"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}