Microsoft/TypeScript

The `parseJsonConfigFileContent` function does not resolve relative JSON paths

Open

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

在 GitHub 查看
 (0 留言) (0 反應) (0 負責人)TypeScript (48,455 star) (6,726 fork)batch import
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

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

貢獻者指南