oclif / core

Node.js Open CLI Framework. Built by Salesforce.

Home Page:https://oclif.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

loadTSConfig assume valid JSON

moltar opened this issue · comments

Describe the bug

The loadTSConfig function assumes tsconfig.json is JSON, however, tsconfig.json officially is a JSONC file, which allows for comments.

From: https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html#tsconfig

The TSConfig is a jsonc file ...

async function loadTSConfig(root: string): Promise<TSConfig | undefined> {
try {
if (TS_CONFIGS[root]) return TS_CONFIGS[root]
const tsconfigPath = join(root, 'tsconfig.json')
const tsconfig = await readJson<TSConfig>(tsconfigPath)
if (!tsconfig || Object.keys(tsconfig.compilerOptions).length === 0) return
TS_CONFIGS[root] = tsconfig
if (tsconfig.extends) {
const {parse} = await import('tsconfck')
const result = await parse(tsconfigPath)
const tsNodeOpts = Object.fromEntries(
(result.extended ?? []).flatMap((e) => Object.entries(e.tsconfig['ts-node'] ?? {})).reverse(),
)
TS_CONFIGS[root] = {...result.tsconfig, 'ts-node': tsNodeOpts}
}
return TS_CONFIGS[root]
} catch (error) {
if (error instanceof SyntaxError) {
debug(`Could not parse tsconfig.json. Skipping ts-node registration for ${root}.`)
memoizedWarn(`Could not parse tsconfig.json for ${root}. Falling back to compiled source.`)
}
}
}

Fails with:

Warning: Could not parse tsconfig.json for .... Falling back to compiled source.

To Reproduce

  • Add a comment to tsconfig.json
  • Run a command

Expected behavior

Allow comments.

Screenshots

N/A