8000 Gracefully handle files that are not from a theme by charlespwd · Pull Request #985 · Shopify/theme-tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Gracefully handle files that are not from a theme #985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025

Conversation

charlespwd
Copy link
Contributor

Make findThemeRootURI return null when it can't find a root

Make everywhere handle null when no root is found

Should fix "preloading" of theme when looking at $HOME/Library/Application Support/Code/User/settings.json

Also, snippets can be found in $HOME/Library/Application Support/Code/User and is a VS Code-owned, non theme thing. So had to modify the findRoot algo to search for assets and snippets folder existence.

Fixes #973

Before you deploy

  • I included a patch bump changeset

8000
@charlespwd charlespwd requested a review from a team as a code owner June 16, 2025 16:11
@charlespwd charlespwd force-pushed the fix/find-root-no-results branch from 1166733 to 6c8ebaf Compare June 16, 2025 16:12
Comment on lines -143 to -147
const path = join(
await findRoot(context.file.uri, context.fileExists),
'config/settings_schema.json',
);
const settingsFile = await context.fs.readFile(path);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toURI does this better.

Comment on lines +70 to +81
const result: FileTree = {};
for (const [relativePath, source] of Object.entries(this.mockTheme)) {
const segments = relativePath.split('/');
let current = result;
for (let i = 0; i < segments.length 8000 - 1; i++) {
const segment = segments[i];
current[segment] ??= {};
current = current[segment] as FileTree;
}
current[segments[segments.length - 1]] = source;
}
return this._fileTree;
return result;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to remove memoization because of a test that mutated this.mockTheme. It's only for tests, perf doesn't matter :)

@@ -43,7 +43,7 @@ export function makeRunChecks(
// then we recheck theme1
const fileExists = makeFileExists(fs);
const rootURIs = await Promise.all(triggerURIs.map((uri) => findRoot(uri, fileExists)));
const deduplicatedRootURIs = new Set(rootURIs);
const deduplicatedRootURIs = new Set<string>(rootURIs.filter((x): x is string => !!x));
await Promise.all([...deduplicatedRootURIs].map(runChecksForRoot));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impact: runChecks won't run when triggered from a file that you can't find a root for.

Comment on lines +28 to +31
if (!rootUri) {
return [];
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No document links when you can't find a root.

@@ -71,7 +72,7 @@ export class LiquidVariableRenameProvider implements BaseRenameProvider {
const rootUri = await this.findThemeRootURI(params.textDocument.uri);
const textDocument = document?.textDocument;

if (!textDocument || !node || !ancestors) return null;
if (!rootUri || !textDocument || !node || !ancestors) return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No variable rename when you can't find a root.

@@ -44,6 +45,7 @@ export class AssetRenameHandler implements BaseRenameHandler {
if (relevantRenames.length !== 1) return;
const rename = relevantRenames[0];
const rootUri = await this.findThemeRootURI(path.dirname(params.files[0].oldUri));
if (!rootUri) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No asset rename when you can't find a root

) {}

async getThemeGraphForURI(uri: string) {
const rootUri = await this.findThemeRootURI(uri);
if (!rootUri) {
return undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No theme graph for files that are not part of a theme

@@ -58,7 +58,7 @@ describe('Module: server', () => {
}
});

fileTree = { 'snippets/code.liquid': fileContents };
fileTree = { '.theme-check.yml': '', 'snippets/code.liquid': fileContents };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Application Support/Code/User/snippets exists and since Application Suport/Code/User/settings.json is a file you're likely to edit, we were inferring Application Support/Code/User to be the root of a theme. That was not very good. So I changed the algo to look for snippets and assets (as well as looking for .theme-chekc.yml).

For the unit tests, it was easiest to simply put a .theme-check.yml

Comment on lines +167 to +169
if (!rootUri) {
return {} as MetafieldDefinitionMap;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no root URI means no .shopify folder gets generated in non-themes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metafields file never got created on my system but i feel like it's a problem outside of the rootUri... can you check yours to see if it's working. It's failing on the exec function in packages/theme-language-server-node/src/metafieldDefinitions.ts line 46.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They load fine for me 🤔

Copy link
Contributor
@aswamy aswamy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor stuff

Comment on lines +167 to +169
if (!rootUri) {
return {} as MetafieldDefinitionMap;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metafields file never got created on my system but i feel like it's a problem outside of the rootUri... can you check yours to see if it's working. It's failing on the exec function in packages/theme-language-server-node/src/metafieldDefinitions.ts line 46.

Make findThemeRootURI return null when it can't find a root

Make everywhere handle null when no root is found

Should fix "preloading" of theme when looking at `$HOME/Library/Application Support/Code/User/settings.json`

Fixes #973
@charlespwd charlespwd force-pushed the fix/find-root-no-results branch from 6c8ebaf to d592591 Compare June 17, 2025 14:14
@charlespwd charlespwd merged commit ce5cb33 into main Jun 17, 2025
7 checks passed
@charlespwd charlespwd deleted the fix/find-root-no-results branch June 17, 2025 14:20
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.

findThemeRoot should throw or return undefined when it can't find one
2 participants
0