Skip non-theme directories when preloading files (for the LSP)#1152
Open
elia wants to merge 1 commit intoShopify:mainfrom
Open
Skip non-theme directories when preloading files (for the LSP)#1152elia wants to merge 1 commit intoShopify:mainfrom
elia wants to merge 1 commit intoShopify:mainfrom
Conversation
Restrict preload() to Shopify theme directories (assets, blocks, config, layout, locales, sections, snippets, templates). Previously all .json files under the root were loaded, including unrelated data files. A single 175 MB JSON file parsed into an AST can consume 1-2 GB of heap, easily exhausting even an 8 GB limit on large repos. Co-Authored-By: Claude <noreply@anthropic.com>
graygilmore
approved these changes
Mar 16, 2026
Contributor
graygilmore
left a comment
There was a problem hiding this comment.
This makes sense. I'm going to just check with the team that we don't want to make any exceptions for files in the root directory.
aswamy
reviewed
Mar 16, 2026
Contributor
aswamy
left a comment
There was a problem hiding this comment.
Thank you for your contribution @elia !
I would suggest one thing here. The current approach does work, but can still be costly if there are deeply nested files (like modules in your theme directory).
If we move this change to the recursiveReadDirectory function itself, we could prevent unnecessary reads of directories at the top-level of the recursive call.
e.g.
// context-utils.js
export async function recursiveReadDirectory(
fs: AbstractFileSystem,
uri: string,
filter: (fileTuple: FileTuple) => boolean,
directoryFilter?: (fileTuple: FileTuple) => boolean,
): Promise<UriString[]> {
const allFiles = await fs.readDirectory(uri);
const files = allFiles.filter(
(ft) =>
!isIgnored(ft) &&
(isDirectory(ft) ? (directoryFilter ? directoryFilter(ft) : true) : filter(ft)),
);
const results = await Promise.all(
files.map((ft) => {
if (isDirectory(ft)) {
// Only apply directoryFilter at the root level — once inside a theme
// directory, recurse freely.
return recursiveReadDirectory(fs, ft[0], filter);
} else {
return Promise.resolve([ft[0]]);
}
}),
);
return results.flat();
}
// DocumentManager.ts
const THEME_DIRS =
/\/(assets|blocks|config|layout|locales|sections|snippets|templates)$/;
const filesToLoad = await recursiveReadDirectory(
this.fs,
rootUri,
([uri]) => /\.(liquid|json)$/.test(uri) && !this.sourceCodes.has(uri),
([uri]) => THEME_DIRS.test(uri),
);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What are you adding in this PR?
Fixes #1151.
When preloading files at startup, the language server was recursively loading all
.jsonfiles under the theme root — including unrelated data files (e.g.data/orders.json). A single large JSON file parsed into an AST can consume 1–2 GB of heap, causing the server to OOM on repos that happen to have adata/directory alongside the theme.This change restricts preloading to Shopify theme directories only:
assets,blocks,config,layout,locales,sections,snippets,templates.What's next? Any followup issues?
None.
What did you learn?
Nothing surprising in retrospect, but worth noting:
recursiveReadDirectorystarts from the workspace root, not the theme root — so any large files anywhere in the repo are fair game without an explicit list.Before you deploy
changeset