Microsoft/TypeScript
GitHub で見るThe `parseJsonConfigFileContent` function does not resolve relative JSON paths
Open
#60,918 opened on 2025年1月5日
BugDomain: APIHelp Wanted
説明
🔎 Search Terms
json, imports, files, allowJs, resolveJsonModule
🕗 Version & Regression Information
This is replicated in TypeScript version 5.7.2. It has not been tested on earlier versions.
⏯ Playground Link
No response
💻 Code
$ npm install
$ npm test
import path from 'node:path';
import { parseJsonConfigFileContent, readConfigFile, sys } from 'typescript';
function testAbsolute() {
const f = path.join(process.cwd(), 'tsconfig.json');
const d = path.dirname(f);
const j = readConfigFile(f, sys.readFile.bind(sys));
const r = parseJsonConfigFileContent(j.config, sys, d);
return r.fileNames;
}
function testRelative() {
const f = 'tsconfig.json';
const d = path.dirname(f);
const j = readConfigFile(f, sys.readFile.bind(sys));
const r = parseJsonConfigFileContent(j.config, sys, d);
return r.fileNames;
}
// Absolute: [ '/home/projects/vanyauhalin-typescript-parsejsonconfigfilecontent-issue/package.json' ]
// Relative: []
console.log('Absolute:', testAbsolute());
console.log('Relative:', testRelative());
{
"compilerOptions": {
"allowJs": true,
"resolveJsonModule": true
},
"include": ["package.json"]
}
🙁 Actual behavior
Absolute: [ '/home/projects/vanyauhalin-typescript-parsejsonconfigfilecontent-issue/package.json' ]
Relative: []
🙂 Expected behavior
Absolute: [ '/home/projects/vanyauhalin-typescript-parsejsonconfigfilecontent-issue/package.json' ]
Relative: [ './package.json' ]
Additional information about the issue
I tried to find the reason and ended up here. The pattern that is generated for subsequent path validation always starts with a slash. As a result, any relative path in the future will fail this check. An example of the generated pattern is below:
/^\/(?!(node_modules|bower_components|jspm_packages)(\/|$))([^./]([^./]|(\.(?!min\.js$))?)*)?\.json$/