Skip to content

Commit c7345dd

Browse files
authored
fix: resolve config from global path (#7234)
1 parent 8c66f6b commit c7345dd

File tree

7 files changed

+216
-1
lines changed

7 files changed

+216
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
3+
// Version of the setting file. Always 0.1
4+
"version": "0.2",
5+
// language - current active spelling language
6+
"language": "en"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
3+
// Version of the setting file. Always 0.1
4+
"version": "0.2",
5+
// language - current active spelling language
6+
"language": "en"
7+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
% https://www.overleaf.com
2+
\documentclass[a4paper]{article}
3+
%% Language and font encodings
4+
\usepackage[english]{babel}
5+
\usepackage[utf8x]{inputenc}
6+
\usepackage[T1]{fontenc}
7+
8+
%% Sets page size and margins
9+
\usepackage[a4paper,
10+
top=3cm,
11+
bottom=2cm,
12+
left=3cm,
13+
right=3cm,
14+
marginparwidth=1.75cm]{geometry}
15+
16+
%% Useful packages
17+
\usepackage{amsmath}
18+
\usepackage{graphicx}
19+
\usepackage[colorinlistoftodos]{todonotes}
20+
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
21+
22+
\newcommand{\MyNewCommand}[1]{Parameter: #1}
23+
24+
\title{Your Paper}
25+
\author{You}
26+
27+
\begin{document}
28+
\maketitle
29+
30+
\begin{abstract}
31+
Your abstract.
32+
\end{abstract}
33+
34+
\section{Introduction}
35+
36+
Your introduction goes here! Some examples of commonly used commands and features are listed below, to help you get started. If you have a question, please use the help menu (``?'') on the top bar to search for help or ask us a question.
37+
38+
\section{Some examples to get started}
39+
40+
\subsection{How to add Comments}
41+
42+
Comments can be added to your project by clicking on the comment icon in the toolbar above. % * <[email protected]> 2014-09-03T09:54:16.211Z:
43+
%
44+
% Here's an example comment!
45+
%
46+
To reply to a comment, simply click the reply button in the lower right corner of the comment, and you can close them when you're done.
47+
48+
\subsection{How to include Figures}
49+
50+
First you have to upload the image file from your computer using the upload link the project menu. Then use the includegraphics command to include it in your document. Use the figure environment and the caption command to add a number and a caption to your figure. See the code for Figure \ref{fig:frog} in this section for an example.
51+
52+
\begin{figure}
53+
\centering
54+
\includegraphics[width=0.3\textwidth]{frog.jpg}
55+
\caption{\label{fig:frog}This frog was uploaded via the project menu.}
56+
\end{figure}
57+
58+
\subsection{How to add Tables}
59+
60+
Use the table and tabular commands for basic tables --- see Table~\ref{tab:widgets}, for example.
61+
62+
\begin{table}
63+
\centering
64+
\begin{tabular}{l|r}
65+
Item & Quantity \\\hline
66+
Widgets & 42 \\
67+
Gadgets & 13
68+
\end{tabular}
69+
\caption{\label{tab:widgets}An example table.}
70+
\end{table}
71+
72+
\subsection{How to write Mathematics}
73+
74+
\LaTeX{} is great at typesetting mathematics. Let $X_1, X_2, \ldots, X_n$ be a sequence of independent and identically distributed random variables with $\text{E}[X_i] = \mu$ and $\text{Var}[X_i] = \sigma^2 < \infty$, and let
75+
\[S_n = \frac{X_1 + X_2 + \cdots + X_n}{n}
76+
= \frac{1}{n}\sum_{i}^{n} X_i\]
77+
denote their mean. Then as $n$ approaches infinity, the random variables $\sqrt{n}(S_n - \mu)$ converge in distribution to a normal $\mathcal{N}(0, \sigma^2)$.
78+
79+
80+
\subsection{How to create Sections and Subsections}
81+
82+
Use section and subsections to organize your document. Simply use the section and subsection buttons in the toolbar to create them, and we'll handle all the formatting and numbering automatically.
83+
84+
\subsection{How to add Lists}
85+
86+
You can make lists with automatic numbering \dots
87+
88+
\begin{enumerate}
89+
\item Like this,
90+
\item and like this.
91+
\end{enumerate}
92+
\dots or bullet points \dots
93+
\begin{itemize}
94+
\item Like this,
95+
\item and like this.
96+
\end{itemize}
97+
98+
\subsection{How to add Citations and a References List}
99+
100+
You can upload a \verb|.bib| file containing your BibTeX entries, created with JabRef; or import your \href{https://www.overleaf.com/blog/184}{Mendeley}, CiteULike or Zotero library as a \verb|.bib| file. You can then cite entries from it, like this: \cite{greenwade93}. Just remember to specify a bibliography style, as well as the filename of the \verb|.bib|.
101+
102+
You can find a \href{https://www.overleaf.com/help/97-how-to-include-a-bibliography-using-bibtex}{video tutorial here} to learn more about BibTeX.
103+
104+
We hope you find Overleaf useful, and please let us know if you have any feedback using the help menu above --- or use the contact form at \url{https://www.overleaf.com/contact}!
105+
106+
\bibliographystyle{alpha}
107+
\bibliography{sample}
108+
109+
\end{document}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This text is fulll of errrorrrs.
2+
3+
It hass someissues on everyline.
4+
5+
But it should still be okkk to reead.

packages/cspell/src/app/environment.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ export interface CSpellEnvironmentVariables {
1717
*/
1818
CSPELL_ENABLE_DICTIONARY_LOG_FIELDS?: string;
1919
CSPELL_GLOB_ROOT?: string;
20+
/**
21+
* Path to a specific CSpell config file.
22+
*/
23+
CSPELL_CONFIG_PATH?: string;
24+
/**
25+
* Path to the default CSpell config file, used if no other config is found.
26+
*/
27+
CSPELL_DEFAULT_CONFIG_PATH?: string;
2028
}
2129

2230
export type EnvironmentKeys = keyof CSpellEnvironmentVariables;
@@ -30,6 +38,8 @@ export const environmentKeys: EnvironmentKeyNames = {
3038
CSPELL_ENABLE_DICTIONARY_LOG_FILE: 'CSPELL_ENABLE_DICTIONARY_LOG_FILE',
3139
CSPELL_ENABLE_DICTIONARY_LOG_FIELDS: 'CSPELL_ENABLE_DICTIONARY_LOG_FIELDS',
3240
CSPELL_GLOB_ROOT: 'CSPELL_GLOB_ROOT',
41+
CSPELL_CONFIG_PATH: 'CSPELL_CONFIG_PATH',
42+
CSPELL_DEFAULT_CONFIG_PATH: 'CSPELL_DEFAULT_CONFIG_PATH',
3343
};
3444

3545
export function getEnvironmentVariables(): CSpellEnvironmentVariables {

packages/cspell/src/app/lint/lint.test.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import * as path from 'node:path';
22

3-
import { describe, expect, test, vi } from 'vitest';
3+
import { afterEach, describe, expect, test, vi } from 'vitest';
44

55
import { CheckFailed } from '../app.mjs';
66
import { environmentKeys } from '../environment.js';
77
import { pathPackageRoot } from '../test/test.helper.js';
8+
import { readConfig } from '../util/fileHelper.js';
89
import { InMemoryReporter } from '../util/InMemoryReporter.js';
910
import { runLint } from './lint.js';
1011
import { LintRequest } from './LintRequest.js';
1112

1213
const root = pathPackageRoot;
1314
const samples = path.resolve(root, 'samples');
1415
const latexSamples = path.resolve(samples, 'latex');
16+
const environmentSamples = path.resolve(samples, 'environment_config');
1517
const failFastSamples = path.resolve(samples, 'fail-fast');
1618
const hiddenSamples = path.resolve(samples, 'hidden-test');
1719
const fixtures = path.resolve(root, 'fixtures');
@@ -24,6 +26,11 @@ const oc = <T>(obj: T) => expect.objectContaining(obj);
2426
const j = path.join;
2527

2628
describe('Linter Validation Tests', () => {
29+
afterEach(() => {
30+
delete process.env[environmentKeys.CSPELL_CONFIG_PATH];
31+
delete process.env[environmentKeys.CSPELL_DEFAULT_CONFIG_PATH];
32+
});
33+
2734
test('globs on the command line override globs in the config.', async () => {
2835
const options = { root: latexSamples };
2936
const reporter = new InMemoryReporter();
@@ -33,6 +40,72 @@ describe('Linter Validation Tests', () => {
3340
expect(rWithFiles.files).toBe(1);
3441
});
3542

43+
test('uses CSPELL_CONFIG_PATH as fallback when config not provided', async () => {
44+
const options = { root: environmentSamples };
45+
const reporter = new InMemoryReporter();
46+
const customConfig = j(environmentSamples, 'cspell.custom.json');
47+
48+
process.env[environmentKeys.CSPELL_CONFIG_PATH] = customConfig;
49+
50+
const result = await runLint(new LintRequest(['**/*.txt'], options, reporter));
51+
expect(result.errors).toBe(0);
52+
expect(result.files).toBe(1);
53+
expect(result.issues).toBe(7);
54+
});
55+
56+
test('falls back to CSPELL_DEFAULT_CONFIG_PATH when no config is set', async () => {
57+
const options = { root: environmentSamples };
58+
const reporter = new InMemoryReporter();
59+
const defaultConfig = j(environmentSamples, 'cspell.default.json');
60+
61+
process.env[environmentKeys.CSPELL_DEFAULT_CONFIG_PATH] = defaultConfig;
62+
63+
const result = await runLint(new LintRequest(['**/*.tex'], options, reporter));
64+
expect(result.errors).toBe(0);
65+
expect(result.files).toBeGreaterThan(0);
66+
expect(result.issues).toBe(2);
67+
});
68+
69+
test('throws an error when the specified config file does not exist', async () => {
70+
const rootConfig = j(environmentSamples, 'nonexistent.json');
71+
72+
process.env[environmentKeys.CSPELL_CONFIG_PATH] = rootConfig
73+
process.env[environmentKeys.CSPELL_DEFAULT_CONFIG_PATH] = rootConfig
74+
75+
const readConfigResult = await readConfig(undefined, environmentSamples);
76+
77+
expect(readConfigResult.source).toBe(rootConfig);
78+
79+
const resultConfig = readConfigResult.config as any;
80+
expect(resultConfig.__importRef.error).toBeTruthy();
81+
expect(resultConfig.__importRef.error).toBeInstanceOf(Error);
82+
expect(resultConfig.__importRef.error.message).toContain('Failed to read config file');
83+
84+
const options = { root: environmentSamples };
85+
const reporter = new InMemoryReporter();
86+
87+
const result = await runLint(new LintRequest(['**/*.txt'], options, reporter));
88+
expect(result.files).toBe(0);
89+
expect(result.errors).toBe(1);
90+
expect(result.issues).toBe(0);
91+
});
92+
93+
test('handles malformed config file gracefully', async () => {
94+
const malformedPath = j(samples, 'cspell-bad.json');
95+
process.env[environmentKeys.CSPELL_CONFIG_PATH] = malformedPath;
96+
process.env[environmentKeys.CSPELL_DEFAULT_CONFIG_PATH] = malformedPath;
97+
98+
const result = await readConfig(undefined, samples);
99+
const resultConfig = result.config;
100+
101+
expect(result.source).toBe(malformedPath);
102+
expect(resultConfig).toHaveProperty('id', 'Invalid JSON');
103+
expect(resultConfig).toHaveProperty('name', 'Trailing comma');
104+
expect(resultConfig).toHaveProperty('__importRef');
105+
});
106+
107+
108+
36109
const optionsRootCSpellJson = { root, config: j(root, 'cspell.json') };
37110

38111
// cspell:ignore Tufte checkedd

packages/cspell/src/app/util/fileHelper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { CSpellUserSettings, Document, Issue } from 'cspell-lib';
1010
import * as cspell from 'cspell-lib';
1111
import { fileToDocument, isBinaryFile as isUriBinaryFile } from 'cspell-lib';
1212

13+
import { environmentKeys, getEnvironmentVariable } from '../environment.js';
1314
import { CSpellConfigFile } from '../options.js';
1415
import { asyncAwait, asyncFlatten, asyncMap, asyncPipe, mergeAsyncIterables } from './async.js';
1516
import { FileUrlPrefix, STDIN, STDINProtocol, STDINUrlPrefix, UTF8 } from './constants.js';
@@ -36,6 +37,9 @@ export async function readConfig(
3637
configFile: string | CSpellConfigFile | undefined,
3738
root: string | undefined,
3839
): Promise<ConfigInfo> {
40+
configFile ??= getEnvironmentVariable(environmentKeys.CSPELL_CONFIG_PATH);
41+
configFile ??= getEnvironmentVariable(environmentKeys.CSPELL_DEFAULT_CONFIG_PATH);
42+
3943
if (configFile) {
4044
const cfgFile = typeof configFile === 'string' ? await readConfigHandleError(configFile) : configFile;
4145
const config = await cspell.resolveConfigFileImports(cfgFile);

0 commit comments

Comments
 (0)