Microsoft/TypeScript

The `parseJsonConfigFileContent` function does not resolve relative JSON paths

開放

#60,918 建立於 2025年1月5日

 (0 則留言) (0 個反應) (0 位負責人)TypeScript (13,395 個分叉)batch import
BugDomain: APIHelp Wanted

倉庫指標

星標
 (108,860 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

🔎 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

Open in StackBlitz

$ 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$/

貢獻者指南