Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The parseJsonConfigFileContent function does not resolve relative JSON paths #60918

Open
vanyauhalin opened this issue Jan 5, 2025 · 0 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@vanyauhalin
Copy link

vanyauhalin commented Jan 5, 2025

πŸ”Ž 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$/
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Jan 6, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants