Skip to content

Fix getScriptSnapshot for file with empty content#3358

Open
nanokind wants to merge 2 commits intomicrosoft:v2from
nanokind:fix/empty-script-snapshot
Open

Fix getScriptSnapshot for file with empty content#3358
nanokind wants to merge 2 commits intomicrosoft:v2from
nanokind:fix/empty-script-snapshot

Conversation

@nanokind
Copy link
Copy Markdown

@nanokind nanokind commented Mar 22, 2025

After applying env.updateFile to reset a file's content to empty, then use any env.languageService.* method, it would throw an error message stating "Could not find source file: 'index.ts'"

import {
  createDefaultMapFromCDN,
  createSystem,
  createVirtualTypeScriptEnvironment,
} from '@typescript/vfs';
import ts from 'typescript';

const fsMap = await createDefaultMapFromCDN(
  { target: ts.ScriptTarget.ES2018 },
  '5.7.2',
  false,
  ts
);

const system = createSystem(fsMap);

const compilerOpts = {};
const env = createVirtualTypeScriptEnvironment(system, [], ts, compilerOpts);

env.createFile('/index.ts', 'console.logx()');

env.updateFile('/index.ts', '');

// throws error: Could not find source file: 'index.ts'.
env.languageService.getSemanticDiagnostics('index.ts');

Here is the repro: https://stackblitz.com/edit/vitejs-vite-obbb75xc?file=src%2Findex.ts

This PR fixes this

@nanokind
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant