Skip to content

Commit

Permalink
fix: ensure SvelteKit file is patched when first open (#2160)
Browse files Browse the repository at this point in the history
#2053

If a file is patched the first time, typescript might have cached the unpatched version. It won't update even if we return the patched version in getScriptSnapshot. This should only happen to files the client opens after the first compilation of the proxy language service. In the first compilation, there won't be any files in the cache. And once there are any updates TypeScript will reparse the file with the patched snapshot.
  • Loading branch information
jasonlyu123 authored Sep 19, 2023
1 parent 93f6415 commit 54f921a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/typescript-plugin/src/language-service/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ export const kitExports: Record<
}
};

const FORCE_UPDATE_VERSION = 'FORCE_UPDATE_VERSION';

export function isKitRouteExportAllowedIn(
basename: string,
kitExport: (typeof kitExports)[keyof typeof kitExports]
Expand Down Expand Up @@ -607,10 +609,11 @@ function getProxiedLanguageService(info: ts.server.PluginCreateInfo, ts: _ts, lo
}

getKitScriptSnapshotIfUpToDate(fileName: string) {
const scriptVersion = this.getScriptVersion(fileName);
if (
!this.files[fileName] ||
this.getScriptVersion(fileName) !==
originalLanguageServiceHost.getScriptVersion(fileName)
(scriptVersion !== originalLanguageServiceHost.getScriptVersion(fileName) &&
scriptVersion !== FORCE_UPDATE_VERSION)
) {
return undefined;
}
Expand All @@ -635,8 +638,16 @@ function getProxiedLanguageService(info: ts.server.PluginCreateInfo, ts: _ts, lo
const { text, addedCode } = result;
const snap = ts.ScriptSnapshot.fromString(text);
snap.getChangeRange = (_) => undefined;

// If this is a new file, typescript might have cached the unpatched version
// It won't update even if we return the patched version in getScriptSnapshot, so we force an update
// This should only happen to files that are opened by the client after the first compilation of the proxy language service
// and won't happen if there are any updates to the file afterwards
this.files[fileName] = {
version: originalLanguageServiceHost.getScriptVersion(fileName),
version:
this.files[fileName] === undefined
? FORCE_UPDATE_VERSION
: originalLanguageServiceHost.getScriptVersion(fileName),
file: snap,
addedCode
};
Expand Down

0 comments on commit 54f921a

Please sign in to comment.