Skip to content

Commit 9b7f05f

Browse files
committed
langium-cli: improved error reporting in case of langium config loading failures
1 parent 9e0a475 commit 9b7f05f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/langium-cli/src/package.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import fs from 'fs-extra';
88
import type { IParserConfig } from 'langium';
9+
import { EOL } from 'os';
910
import * as path from 'path';
1011
import type { GenerateOptions } from './generate.js';
1112
import { log } from './generator/util.js';
@@ -92,15 +93,24 @@ export async function loadConfig(options: GenerateOptions): Promise<LangiumConfi
9293
filePath = path.normalize(defaultFile);
9394
}
9495
const relativePath = path.dirname(filePath);
96+
if (!path.isAbsolute(filePath)) {
97+
filePath = path.resolve(process.cwd(), filePath);
98+
}
9599
log('log', options, `Reading config from ${chalk.white.bold(filePath)}`);
96100
try {
97101
const obj = await fs.readJson(filePath, { encoding: 'utf-8' });
98-
const config: LangiumConfig = path.basename(filePath) === 'package.json' ? obj.langium : obj;
102+
const config: LangiumConfig | undefined = path.basename(filePath) === 'package.json' ? obj.langium : obj;
103+
if (!config) {
104+
throw new Error('Langium config is missing.');
105+
}
99106
config[RelativePath] = relativePath;
100107
config.importExtension ??= '.js';
101108
return config;
102109
} catch (err) {
103-
log('error', options, chalk.red('Failed to read config file.'), err);
110+
const suffix = options.file
111+
? path.basename(filePath) === 'package.json' ? `an object named 'langium' in ${filePath}` : filePath
112+
: `${path.resolve(path.dirname(filePath), 'langium-config.json')} or an object named 'langium' in ${path.resolve(path.dirname(filePath), 'package.json')}`;
113+
log('error', options, chalk.red(`Failed to read Langium config: Could not find ${suffix}.${EOL}`), err);
104114
process.exit(1);
105115
}
106116
}

0 commit comments

Comments
 (0)