Skip to content

Commit 20f6e9d

Browse files
authored
Fix ConfigurationError regression for custom syntaxes (#9245)
1 parent 21a57e8 commit 20f6e9d

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stylelint": patch
3+
---
4+
5+
Fixed: `ConfigurationError` regression for custom syntaxes

lib/__tests__/standalone-syntax.test.mjs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { writeFile } from 'node:fs/promises';
1+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
2+
import { join } from 'node:path';
3+
import { tmpdir } from 'node:os';
24

35
import replaceBackslashes from '../testUtils/replaceBackslashes.mjs';
46
import standalone from '../standalone.mjs';
@@ -137,7 +139,7 @@ it('rejects on unknown custom syntax option', async () => {
137139
config: config(),
138140
}),
139141
).rejects.toThrow(
140-
'Could not find "unknown-module". Do you need to install the package or use the "configBasedir" option?',
142+
'Cannot resolve custom syntax module "unknown-module". Check that module "unknown-module" is available and spelled correctly.',
141143
);
142144
});
143145

@@ -178,6 +180,26 @@ describe('customSyntax set in the config', () => {
178180
expect(results[0].warnings[0]).toMatchObject({ line: 2, column: 3, rule: 'block-no-empty' });
179181
});
180182

183+
it('standalone with bare-package custom syntax not reachable from configBasedir or cwd', async () => {
184+
const isolatedDir = await mkdtemp(join(tmpdir(), 'stylelint-isolated-'));
185+
186+
try {
187+
const { results } = await standalone({
188+
config: config({ customSyntax: 'postcss-scss' }),
189+
configBasedir: isolatedDir,
190+
cwd: isolatedDir,
191+
code: '$foo: bar; // foo;\nb {}',
192+
formatter: stringFormatter,
193+
});
194+
195+
expect(results).toHaveLength(1);
196+
expect(results[0].warnings).toHaveLength(1);
197+
expect(results[0].warnings[0]).toMatchObject({ line: 2, column: 3, rule: 'block-no-empty' });
198+
} finally {
199+
await rm(isolatedDir, { recursive: true, force: true });
200+
}
201+
});
202+
181203
it('standalone with custom syntax as npm package', async () => {
182204
const { results } = await standalone({
183205
config: config({ customSyntax: await import('postcss-scss') }),
@@ -234,7 +256,7 @@ describe('customSyntax set in the config', () => {
234256
config: config({ customSyntax: 'unknown-module' }),
235257
}),
236258
).rejects.toThrow(
237-
'Could not find "unknown-module". Do you need to install the package or use the "configBasedir" option?',
259+
'Cannot resolve custom syntax module "unknown-module". Check that module "unknown-module" is available and spelled correctly.',
238260
);
239261
});
240262

lib/augmentConfig.mjs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ function absolutizeGlob(glob, basedir) {
3535
return normalizePath(result);
3636
}
3737

38+
/**
39+
* @param {string} lookup
40+
* @returns {boolean}
41+
*/
42+
function isPathLike(lookup) {
43+
return lookup.startsWith('./') || lookup.startsWith('../') || isAbsolute(lookup);
44+
}
45+
3846
/**
3947
* - Merges config and stylelint options
4048
* - Makes all paths absolute
@@ -187,7 +195,7 @@ function absolutizePaths(config, configDir, cwd) {
187195
config.processors = config.processors.map(toAbsolutePath);
188196
}
189197

190-
if (isString(config.customSyntax)) {
198+
if (isString(config.customSyntax) && isPathLike(config.customSyntax)) {
191199
config.customSyntax = getModulePath(configDir, config.customSyntax, cwd);
192200
}
193201

@@ -200,9 +208,10 @@ function absolutizePaths(config, configDir, cwd) {
200208
if (isObject(entry)) {
201209
return {
202210
files: [entry.files].flat().map((glob) => absolutizeGlob(glob, configDir)),
203-
customSyntax: isString(entry.customSyntax)
204-
? getModulePath(configDir, entry.customSyntax, cwd)
205-
: entry.customSyntax,
211+
customSyntax:
212+
isString(entry.customSyntax) && isPathLike(entry.customSyntax)
213+
? getModulePath(configDir, entry.customSyntax, cwd)
214+
: entry.customSyntax,
206215
};
207216
}
208217

0 commit comments

Comments
 (0)