Skip to content

Commit

Permalink
fix: Make sure relative globRoot is resolved correctly. (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Feb 25, 2021
1 parent 4fb9002 commit 29977c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
15 changes: 15 additions & 0 deletions packages/cspell-lib/samples/cspell.relative-glob-root.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
22 changes: 22 additions & 0 deletions packages/cspell-lib/src/Settings/CSpellSettingsServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -490,6 +508,10 @@ describe('Validate search/load config files', () => {
});
});

function oc<T>(v: Partial<T>): T {
return expect.objectContaining(v);
}

function relSamples(file: string) {
return path.resolve(samplesDir, file);
}
Expand Down
7 changes: 3 additions & 4 deletions packages/cspell-lib/src/Settings/CSpellSettingsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 29977c4

Please sign in to comment.