Skip to content
Merged
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/cyclic-extends.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme-check-node': patch
---

Report configs that extend each other instead of hanging while resolving them.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ SomeCheck:
expect(config.context).to.equal('app');
});

it('rejects configs that extend each other', async () => {
const filePath = await createMockYamlFile('extends: ./base.yml');
await createMockConfigFile(tempDir, 'extends: ./.theme-check.yml', 'base.yml');

await expect(resolveConfig(filePath)).rejects.toThrow(
`Cyclic extends in theme-check config: ${filePath} -> ${path.join(tempDir, 'base.yml')} -> ${filePath}`,
);
});

async function createMockYamlFile(content: string): Promise<string> {
return createMockConfigFile(tempDir, content);
}
Expand Down
13 changes: 11 additions & 2 deletions packages/theme-check-node/src/config/resolve/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,27 @@ const modernConfigsPath = () => {
export async function resolveConfig(
configPath: AbsolutePath | ModernIdentifier,
isRootConfig: boolean = false,
/** the configs that extend this one, from the root config down */
extendedBy: AbsolutePath[] = [],
): Promise<ConfigDescription> {
if (isModernIdentifier(configPath)) {
const modernConfigPath = path.join(
modernConfigsPath(),
configPath.replace(/^theme-check:/, '') + '.yml',
);
return resolveConfig(modernConfigPath);
return resolveConfig(modernConfigPath, false, extendedBy);
}

const chain = [...extendedBy, configPath];
if (extendedBy.includes(configPath)) {
throw new Error(`Cyclic extends in theme-check config: ${chain.join(' -> ')}`);
}

// TODO: Add support for more file formats.
const current = await readYamlConfigDescription(configPath, isRootConfig);
const baseConfigs = await Promise.all(current.extends.map((extend) => resolveConfig(extend)));
const baseConfigs = await Promise.all(
current.extends.map((extend) => resolveConfig(extend, false, chain)),
);
return mergeFragments(baseConfigs, current);
}

Expand Down
Loading