From 5a9c8d2e715ff362f54f90f5b0f2896cb65b24f9 Mon Sep 17 00:00:00 2001 From: Nihar Date: Tue, 1 Dec 2026 18:03:09 +0530 Subject: [PATCH] fix: report an unreadable config file in check and dictionaries cspell check and cspell dictionaries carried on and exited 0 when --config named a file that could not be read. They now print the configuration error and exit with 1, as lint does. --- packages/cspell/src/app.test.ts | 24 +++++++++++++++++++ packages/cspell/src/application.mts | 6 +++-- packages/cspell/src/commandCheck.ts | 8 +++++-- .../src/dictionaries/listDictionaries.ts | 3 ++- .../cspell/src/util/configFileHelper.test.ts | 14 ++++++++++- packages/cspell/src/util/configFileHelper.ts | 10 ++++++++ 6 files changed, 59 insertions(+), 6 deletions(-) diff --git a/packages/cspell/src/app.test.ts b/packages/cspell/src/app.test.ts index 100262cbc839..cf4332ebb2f7 100644 --- a/packages/cspell/src/app.test.ts +++ b/packages/cspell/src/app.test.ts @@ -406,6 +406,30 @@ describe('Validate cli', () => { expect(parsed[1]).toEqual({ filename: pathRoot('missing-file.txt'), error: 'File not found' }); }); + test('check reports a config file that cannot be read', async () => { + const commander = getCommander(); + const args = argv('check', '--config', pathSamples('missing-config.json'), pathRoot('LICENSE')); + await expect(app.run(commander, args)).rejects.toThrow(app.CheckFailed); + + expect(error).toHaveBeenCalledWith(expect.stringContaining('Configuration Error: Failed to read config file')); + }); + + test('check --json reports a config file that cannot be read', async () => { + const commander = getCommander(); + const args = argv('check', '--json', '--config', pathSamples('missing-config.json'), pathRoot('LICENSE')); + await expect(app.run(commander, args)).rejects.toThrow(app.CheckFailed); + + const parsed = parseCheckJsonOutput(); + expect(parsed).toHaveLength(1); + expect(parsed[0].error).toContain('Configuration Error: Failed to read config file'); + }); + + test('dictionaries reports a config file that cannot be read', async () => { + const commander = getCommander(); + const args = argv('dictionaries', '--config', pathSamples('missing-config.json')); + await expect(app.run(commander, args)).rejects.toThrow(/Configuration Error: Failed to read config file/); + }); + test('check --json --no-exit-code sets exitCode 0 while still emitting JSON', async () => { chalk.level = 1; const commander = getCommander(); diff --git a/packages/cspell/src/application.mts b/packages/cspell/src/application.mts index 87c8ee1cf390..f25c676b59bc 100644 --- a/packages/cspell/src/application.mts +++ b/packages/cspell/src/application.mts @@ -23,7 +23,7 @@ import type { BaseOptions, LegacyOptions, LinterCliOptions, SuggestionOptions, T import { fixLegacy } from './options.js'; import { simpleRepl } from './repl/index.js'; import { finalizeReporter } from './reporters/index.js'; -import { readConfig, toConfigList } from './util/configFileHelper.js'; +import { assertNoConfigErrors, readConfig, toConfigList } from './util/configFileHelper.js'; import { fileInfoToDocument, readFileInfo } from './util/fileHelper.js'; import { readStdin } from './util/stdin.js'; import { getTimeMeasurer } from './util/timer.js'; @@ -98,7 +98,9 @@ export async function checkText(filename: string, options: BaseOptions & LegacyO return checkTextDocument(doc, util.clean({ validateDirectives }), settingsFromCommandLine); } // The named configs replace the search for a config near the document. - const { config } = await readConfig(configFiles, undefined); + const configInfo = await readConfig(configFiles, undefined); + assertNoConfigErrors(configInfo); + const { config } = configInfo; const settings = mergeSettings(settingsFromCommandLine, config); return checkTextDocument(doc, util.clean({ validateDirectives, noConfigSearch: true }), settings); } diff --git a/packages/cspell/src/commandCheck.ts b/packages/cspell/src/commandCheck.ts index 1235486121b1..7dfa7c071eb9 100644 --- a/packages/cspell/src/commandCheck.ts +++ b/packages/cspell/src/commandCheck.ts @@ -7,7 +7,7 @@ import { checkText } from './application.mjs'; import { collect } from './commandHelpers.js'; import { console } from './console.js'; import type { BaseOptions } from './options.js'; -import { CheckFailed } from './util/errors.js'; +import { ApplicationError, CheckFailed } from './util/errors.js'; type CheckCommandOptions = BaseOptions & { json: boolean }; @@ -71,6 +71,10 @@ async function processRequestAnsi(files: string[], options: CheckCommandOptions) } console.log(); } catch (e) { + if (e instanceof ApplicationError && !isErrNoEnt(e)) { + console.error(e.message); + throw new CheckFailed(e.message, e.exitCode); + } const message = isErrNoEnt(e) ? 'File not found' : String(e); console.error(`${message} "${filename}"`); throw new CheckFailed(message, 1); @@ -102,7 +106,7 @@ async function processRequestJSON(files: string[], options: CheckCommandOptions) issueCount += item.isError ? 1 : 0; } } catch (e) { - const message = isErrNoEnt(e) ? 'File not found' : String(e); + const message = isErrNoEnt(e) ? 'File not found' : e instanceof ApplicationError ? e.message : String(e); results.push({ filename, error: message }); console.log(JSON.stringify(results, undefined, 2)); throw new CheckFailed(message, 1); diff --git a/packages/cspell/src/dictionaries/listDictionaries.ts b/packages/cspell/src/dictionaries/listDictionaries.ts index b4b9be7b7b23..a15ff9069b19 100644 --- a/packages/cspell/src/dictionaries/listDictionaries.ts +++ b/packages/cspell/src/dictionaries/listDictionaries.ts @@ -12,7 +12,7 @@ import { } from 'cspell-lib'; import type { DictionariesOptions } from '../options.js'; -import { readConfig } from '../util/configFileHelper.js'; +import { assertNoConfigErrors, readConfig } from '../util/configFileHelper.js'; export interface ListDictionariesResult { /** @@ -165,6 +165,7 @@ function extractSpecialDictionaries(config: CSpellSettingsWithSourceTrace): List export async function listDictionaries(options: DictionariesOptions): Promise { const configFile = await readConfig(options.config, undefined); + assertNoConfigErrors(configFile); const loadDefault = options.defaultConfiguration ?? configFile.config.loadDefaultConfiguration ?? true; const configBase = mergeSettings( diff --git a/packages/cspell/src/util/configFileHelper.test.ts b/packages/cspell/src/util/configFileHelper.test.ts index b547cec7b3b8..9a0601d85be7 100644 --- a/packages/cspell/src/util/configFileHelper.test.ts +++ b/packages/cspell/src/util/configFileHelper.test.ts @@ -7,7 +7,7 @@ import { afterEach, describe, expect, test, vi } from 'vitest'; import { environmentKeys } from '../environment.js'; import { pathPackageRoot } from '../test/test.helper.js'; -import { readConfig } from '../util/configFileHelper.js'; +import { assertNoConfigErrors, readConfig } from '../util/configFileHelper.js'; const root = pathPackageRoot; const samples = path.resolve(root, 'samples'); @@ -69,6 +69,18 @@ describe('Config File Helper', () => { expect(readConfigResult.urls).toEqual([pathToFileURL(rootConfig)]); }); + test('assertNoConfigErrors throws for a config file that cannot be read', async () => { + const result = await readConfig(j(samples, 'nonexistent.json'), undefined); + + expect(() => assertNoConfigErrors(result)).toThrow(/^Configuration Error: Failed to read config file/); + }); + + test('assertNoConfigErrors accepts a config file that can be read', async () => { + const result = await readConfig(j(environmentSamples, 'cspell.yaml'), undefined); + + expect(() => assertNoConfigErrors(result)).not.toThrow(); + }); + test('handles malformed config file gracefully', async () => { const malformedPath = j(samples, 'cspell-bad.json'); process.env[environmentKeys.CSPELL_CONFIG_PATH] = malformedPath; diff --git a/packages/cspell/src/util/configFileHelper.ts b/packages/cspell/src/util/configFileHelper.ts index b4e0f6abac4d..6ed1a1c38005 100644 --- a/packages/cspell/src/util/configFileHelper.ts +++ b/packages/cspell/src/util/configFileHelper.ts @@ -3,6 +3,7 @@ import * as cspell from 'cspell-lib'; import { environmentKeys, getEnvironmentVariable } from '../environment.js'; import type { BaseOptions, CSpellConfigFile } from '../options.js'; +import { ApplicationError } from './errors.js'; import { filenameToUrl } from './fileHelper.js'; export interface ConfigInfo { @@ -63,6 +64,15 @@ async function configFilesToConfigInfo(cfgFiles: CSpellConfigFile[]): Promise cfgFile.url), foundBySearch: false }; } +/** + * Throw when a config file could not be read or imported, as `lint` does. + */ +export function assertNoConfigErrors(configInfo: ConfigInfo): void { + const [first] = cspell.extractImportErrors(configInfo.config); + if (!first) return; + throw new ApplicationError(`Configuration Error: ${first.error.message}`, 1, first.error); +} + export function toConfigList(configFile: BaseOptions['config']): (string | CSpellConfigFile)[] { if (configFile === undefined) return []; return Array.isArray(configFile) ? configFile : [configFile];