Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-tools-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme-language-server-common': patch
---

Make automatic Shopify metafield fetching opt-in so opening a theme workspace does not unexpectedly start CLI authentication.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ export const CHECK_ON_OPEN = 'themeCheck.checkOnOpen' as const;
export const CHECK_ON_SAVE = 'themeCheck.checkOnSave' as const;
export const CHECK_ON_CHANGE = 'themeCheck.checkOnChange' as const;
export const PRELOAD_ON_BOOT = 'themeCheck.preloadOnBoot' as const;
export const FETCH_METAFIELD_DEFINITIONS = 'themeCheck.fetchMetafieldDefinitions' as const;
export const ConfigurationKeys = [
CHECK_ON_OPEN,
CHECK_ON_SAVE,
CHECK_ON_CHANGE,
PRELOAD_ON_BOOT,
FETCH_METAFIELD_DEFINITIONS,
] as const;

export class Configuration {
[CHECK_ON_OPEN]: boolean = true;
[CHECK_ON_SAVE]: boolean = true;
[CHECK_ON_CHANGE]: boolean = true;
[PRELOAD_ON_BOOT]: boolean = true;
[FETCH_METAFIELD_DEFINITIONS]: boolean = false;

constructor(
private connection: Connection,
Expand All @@ -37,6 +40,10 @@ export class Configuration {
this[CHECK_ON_SAVE] = this.capabilities.initializationOption(CHECK_ON_SAVE, true);
this[CHECK_ON_CHANGE] = this.capabilities.initializationOption(CHECK_ON_CHANGE, true);
this[PRELOAD_ON_BOOT] = this.capabilities.initializationOption(PRELOAD_ON_BOOT, true);
this[FETCH_METAFIELD_DEFINITIONS] = this.capabilities.initializationOption(
FETCH_METAFIELD_DEFINITIONS,
false,
);
}

async shouldCheckOnOpen() {
Expand All @@ -59,6 +66,11 @@ export class Configuration {
return this[PRELOAD_ON_BOOT];
}

async shouldFetchMetafieldDefinitions() {
await this.fetchConfiguration();
return this[FETCH_METAFIELD_DEFINITIONS];
}

clearCache() {
this.fetchConfiguration.clearCache();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
} from 'vscode-languageserver';
import { MockConnection, mockConnection } from '../test/MockConnection';
import { Dependencies } from '../types';
import { CHECK_ON_CHANGE, CHECK_ON_OPEN, CHECK_ON_SAVE } from './Configuration';
import {
CHECK_ON_CHANGE,
CHECK_ON_OPEN,
CHECK_ON_SAVE,
FETCH_METAFIELD_DEFINITIONS,
} from './Configuration';
import { startServer } from './startServer';

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
Expand All @@ -23,13 +28,15 @@ describe('Module: server', () => {
let checkOnChange: boolean | null = null;
let checkOnSave: boolean | null = null;
let checkOnOpen: boolean | null = null;
let fetchMetafieldDefinitionsSetting: boolean | null = null;
let connection: MockConnection;
let dependencies: ReturnType<typeof getDependencies>;
let fileTree: MockTheme;
let logger: any;
let fetchMetafieldDefinitionsForURI: ReturnType<typeof vi.fn>;

beforeEach(() => {
checkOnChange = checkOnSave = checkOnOpen = null;
checkOnChange = checkOnSave = checkOnOpen = fetchMetafieldDefinitionsSetting = null;

// Initialize all ze mocks...
connection = mockConnection(mockRoot);
Expand All @@ -45,12 +52,16 @@ describe('Module: server', () => {
return checkOnOpen;
case CHECK_ON_SAVE:
return checkOnSave;
case FETCH_METAFIELD_DEFINITIONS:
return fetchMetafieldDefinitionsSetting;
default:
return null;
}
});
} else if (method === 'client/registerCapability') {
return null;
} else if (method === 'workspace/workspaceFolders') {
return [{ uri: mockRoot, name: 'theme' }];
} else {
throw new Error(
`Does not know how to mock response to '${method}' requests. Check your test.`,
Expand All @@ -60,7 +71,9 @@ describe('Module: server', () => {

fileTree = { '.theme-check.yml': '', 'snippets/code.liquid': fileContents };
logger = vi.fn();
fetchMetafieldDefinitionsForURI = vi.fn();
dependencies = getDependencies(logger, fileTree);
dependencies.fetchMetafieldDefinitionsForURI = fetchMetafieldDefinitionsForURI;

// Start the server
startServer(connection, dependencies);
Expand All @@ -79,6 +92,31 @@ describe('Module: server', () => {
expect(logger).toHaveBeenCalledWith("[SERVER] Let's roll!");
});

it('does not fetch metafield definitions by default', async () => {
connection.setup({
workspace: {
configuration: true,
workspaceFolders: { supported: true },
},
});
await flushAsync();

expect(fetchMetafieldDefinitionsForURI).not.toHaveBeenCalled();
});

it('fetches metafield definitions when enabled', async () => {
fetchMetafieldDefinitionsSetting = true;
connection.setup({
workspace: {
configuration: true,
workspaceFolders: { supported: true },
},
});
await flushAsync();

expect(fetchMetafieldDefinitionsForURI).toHaveBeenCalledWith(mockRoot);
});

it('should debounce calls to runChecks', async () => {
connection.setup();
await flushAsync();
Expand Down Expand Up @@ -392,6 +430,7 @@ describe('Module: server', () => {
jsonValidationSet: {
schemas: async () => [],
},
fetchMetafieldDefinitionsForURI,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ export function startServer(
);

const fetchMetafieldDefinitionsForWorkspaceFolders = async (folders: WorkspaceFolder[]) => {
if (!fetchMetafieldDefinitionsForURI) return;
if (
!fetchMetafieldDefinitionsForURI ||
!(await configuration.shouldFetchMetafieldDefinitions())
) {
return;
}

for (let folder of folders) {
const mode = await getModeForURI(folder.uri);
Expand Down
1 change: 1 addition & 0 deletions packages/vscode-extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This VS Code extensions comes with batteries included.
- `"themeCheck.checkOnChange": boolean`, (default: `true`) makes it so theme check runs on file change.
- `"themeCheck.checkOnSave": boolean`, (default: `true`) makes it so theme check runs on file save.
- `"themeCheck.preloadOnBoot": boolean`, (default: `true`) makes it so all files are preloaded on extension activation.
- `"themeCheck.fetchMetafieldDefinitions": boolean`, (default: `false`) fetches metafield definitions from Shopify when a theme workspace is opened. This may prompt you to log in to Shopify.

## License

Expand Down
7 changes: 7 additions & 0 deletions packages/vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@
],
"description": "When true, theme check preloads all the files from your theme for fast rename handling and theme graph generation.",
"default": true
},
"themeCheck.fetchMetafieldDefinitions": {
"type": [
"boolean"
],
"description": "When true, fetches metafield definitions from Shopify when a theme workspace is opened. This may prompt you to log in to Shopify.",
"default": false
}
}
},
Expand Down
Loading