From 29977c466935948f092527e0300bb0a310394cdc Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Thu, 25 Feb 2021 14:14:12 +0100 Subject: [PATCH] fix: Make sure relative globRoot is resolved correctly. (#1004) --- .../samples/cspell.relative-glob-root.json | 15 +++++++++++++ .../src/Settings/CSpellSettingsServer.test.ts | 22 +++++++++++++++++++ .../src/Settings/CSpellSettingsServer.ts | 7 +++--- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 packages/cspell-lib/samples/cspell.relative-glob-root.json diff --git a/packages/cspell-lib/samples/cspell.relative-glob-root.json b/packages/cspell-lib/samples/cspell.relative-glob-root.json new file mode 100644 index 000000000000..49107b2b6b56 --- /dev/null +++ b/packages/cspell-lib/samples/cspell.relative-glob-root.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/master/cspell.schema.json", + "name": "config with relative glob root", + "version": "0.2", + "ignorePaths": [ + "node_modules" + ], + "globRoot": "../../..", + "words": [ + "aa" + ], + "import": [ + "../cspell.config.json" + ] +} diff --git a/packages/cspell-lib/src/Settings/CSpellSettingsServer.test.ts b/packages/cspell-lib/src/Settings/CSpellSettingsServer.test.ts index 3017b5604063..dc1a764c5096 100644 --- a/packages/cspell-lib/src/Settings/CSpellSettingsServer.test.ts +++ b/packages/cspell-lib/src/Settings/CSpellSettingsServer.test.ts @@ -357,6 +357,24 @@ describe('Validate Glob resolution', () => { expect(settingsV1).toEqual(settingsV); }); + test('globs with relative globRoot', async () => { + const configFilename = path.resolve(samplesDir, 'cspell.relative-glob-root.json'); + const configDir = path.dirname(configFilename); + const config = await loadConfig(configFilename); + + expect(config).toEqual( + oc({ + ignorePaths: expect.arrayContaining([ + { + glob: 'node_modules', + root: path.resolve(configDir, '../../..'), + source: configFilename, + }, + ]), + }) + ); + }); + test('globs from config file (search)', async () => { const config = await searchForConfig(__dirname); expect(config?.ignorePaths).toEqual( @@ -490,6 +508,10 @@ describe('Validate search/load config files', () => { }); }); +function oc(v: Partial): T { + return expect.objectContaining(v); +} + function relSamples(file: string) { return path.resolve(samplesDir, file); } diff --git a/packages/cspell-lib/src/Settings/CSpellSettingsServer.ts b/packages/cspell-lib/src/Settings/CSpellSettingsServer.ts index 249fadce247d..f286215541bb 100644 --- a/packages/cspell-lib/src/Settings/CSpellSettingsServer.ts +++ b/packages/cspell-lib/src/Settings/CSpellSettingsServer.ts @@ -612,14 +612,13 @@ export function extractImportErrors(settings: CSpellSettings): ImportFileRefWith } function resolveGlobRoot(settings: CSpellSettings, pathToSettingsFile: string): string { + const settingsFileDir = path.dirname(pathToSettingsFile); const envGlobRoot = process.env[ENV_CSPELL_GLOB_ROOT]; const defaultGlobRoot = envGlobRoot ?? '${cwd}'; const rawRoot = settings.globRoot ?? - (settings.version === '0.1' || (envGlobRoot && !settings.version) - ? defaultGlobRoot - : path.dirname(pathToSettingsFile)); - const globRoot = rawRoot.replace('${cwd}', process.cwd()); + (settings.version === '0.1' || (envGlobRoot && !settings.version) ? defaultGlobRoot : settingsFileDir); + const globRoot = path.resolve(settingsFileDir, rawRoot.replace('${cwd}', process.cwd())); return globRoot; }