Skip to content

fix: tsconfig extends supporting same extensions (inc .jsonc) for relative and package imports (bug #43121) #62092

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ import {
NewLineKind,
Node,
NodeArray,
nodeNextJsonConfigResolver,
nodeNextTsconfigResolver,
normalizePath,
normalizeSlashes,
NumericLiteral,
Expand Down Expand Up @@ -3630,7 +3630,7 @@ function getExtendsConfigPath(
return extendedConfigPath;
}
// If the path isn't a rooted or relative path, resolve like a module
const resolved = nodeNextJsonConfigResolver(extendedConfig, combinePaths(basePath, "tsconfig.json"), host);
const resolved = nodeNextTsconfigResolver(extendedConfig, combinePaths(basePath, "tsconfig.json"), host);
if (resolved.resolvedModule) {
return resolved.resolvedModule.resolvedFileName;
}
Expand Down
17 changes: 10 additions & 7 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ const enum Extensions {
Json = 1 << 3, // '.json'

ImplementationFiles = TypeScript | JavaScript,
Any = (TypeScript | JavaScript | Declaration | Json | 1 << 4),
}

function formatExtensions(extensions: Extensions) {
Expand All @@ -193,6 +194,7 @@ function formatExtensions(extensions: Extensions) {
if (extensions & Extensions.JavaScript) result.push("JavaScript");
if (extensions & Extensions.Declaration) result.push("Declaration");
if (extensions & Extensions.Json) result.push("JSON");
if (extensions === Extensions.Any) result.push("Any");
return result.join(", ");
}

Expand Down Expand Up @@ -1772,7 +1774,7 @@ export function nodeModuleNameResolver(moduleName: string, containingFile: strin
export function nodeModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference, conditions?: string[], isConfigLookup?: boolean): ResolvedModuleWithFailedLookupLocations {
let extensions;
if (isConfigLookup) {
extensions = Extensions.Json;
extensions = Extensions.Any;
}
else if (compilerOptions.noDtsResolution) {
extensions = Extensions.ImplementationFiles;
Expand All @@ -1788,8 +1790,8 @@ export function nodeModuleNameResolver(moduleName: string, containingFile: strin
}

/** @internal */
export function nodeNextJsonConfigResolver(moduleName: string, containingFile: string, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations {
return nodeModuleNameResolverWorker(NodeResolutionFeatures.NodeNextDefault, moduleName, getDirectoryPath(containingFile), { moduleResolution: ModuleResolutionKind.NodeNext }, host, /*cache*/ undefined, Extensions.Json, /*isConfigLookup*/ true, /*redirectedReference*/ undefined, /*conditions*/ undefined);
export function nodeNextTsconfigResolver(moduleName: string, containingFile: string, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations {
return nodeModuleNameResolverWorker(NodeResolutionFeatures.NodeNextDefault, moduleName, getDirectoryPath(containingFile), { moduleResolution: ModuleResolutionKind.NodeNext }, host, /*cache*/ undefined, Extensions.Any, /*isConfigLookup*/ true, /*redirectedReference*/ undefined, /*conditions*/ undefined);
}

function nodeModuleNameResolverWorker(
Expand Down Expand Up @@ -2111,9 +2113,9 @@ function loadFileNameFromPackageJsonField(extensions: Extensions, candidate: str
return result !== undefined ? { path: candidate, ext, resolvedUsingTsExtension: packageJsonValue ? !endsWith(packageJsonValue, ext) : undefined } : undefined;
}

if (state.isConfigLookup && extensions === Extensions.Json && fileExtensionIs(candidate, Extension.Json)) {
if (state.isConfigLookup && (extensions === Extensions.Any || extensionsToExtensionsArray(extensions).some(ext => fileExtensionIs(candidate, ext)))) {
const result = tryFile(candidate, onlyRecordFailures, state);
return result !== undefined ? { path: candidate, ext: Extension.Json, resolvedUsingTsExtension: undefined } : undefined;
return result !== undefined ? { path: candidate, ext: getAnyExtensionFromPath(candidate), resolvedUsingTsExtension: undefined } : undefined;
}

return loadModuleFromFileNoImplicitExtensions(extensions, candidate, onlyRecordFailures, state);
Expand Down Expand Up @@ -2168,6 +2170,7 @@ function tryAddingExtensions(candidate: string, extensions: Extensions, original
|| undefined;
default:
return extensions & Extensions.Declaration && !isDeclarationFileName(candidate + originalExtension) && tryExtension(`.d${originalExtension}.ts`)
|| extensions === Extensions.Any && tryExtension(originalExtension)
|| undefined;
}

Expand Down Expand Up @@ -2603,7 +2606,7 @@ function loadModuleFromSelfNameReference(extensions: Extensions, moduleName: str
return loadModuleFromExports(scope, extensions, subpath, state, cache, redirectedReference);
}
const priorityExtensions = extensions & (Extensions.TypeScript | Extensions.Declaration);
const secondaryExtensions = extensions & ~(Extensions.TypeScript | Extensions.Declaration);
const secondaryExtensions = extensions === Extensions.Any ? Extensions.Any : extensions & ~(Extensions.TypeScript | Extensions.Declaration);
return loadModuleFromExports(scope, priorityExtensions, subpath, state, cache, redirectedReference)
|| loadModuleFromExports(scope, secondaryExtensions, subpath, state, cache, redirectedReference);
}
Expand Down Expand Up @@ -3002,7 +3005,7 @@ function loadModuleFromNearestNodeModulesDirectoryWorker(extensions: Extensions,
// 2. For each ancestor node_modules directory, try to find:
// i. JS files in the implementation package
const priorityExtensions = extensions & (Extensions.TypeScript | Extensions.Declaration);
const secondaryExtensions = extensions & ~(Extensions.TypeScript | Extensions.Declaration);
const secondaryExtensions = extensions === Extensions.Any ? Extensions.Any : extensions & ~(Extensions.TypeScript | Extensions.Declaration);
// (1)
if (priorityExtensions) {
traceIfEnabled(state, Diagnostics.Searching_all_ancestor_node_modules_directories_for_preferred_extensions_Colon_0, formatExtensions(priorityExtensions));
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/tsconfigExtendsJsoncPackage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
===================================================================
JsFile: index.js
mapUrl: data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBYSxRQUFBLENBQUMsR0FBRyxFQUFFLENBQUMifQ==
sourceRoot:
sources: index.ts
===================================================================
-------------------------------------------------------------------
emittedFile:/index.js
sourceFile:index.ts
-------------------------------------------------------------------
>>>"use strict";
>>>Object.defineProperty(exports, "__esModule", { value: true });
>>>exports.x = void 0;
>>>exports.x = 42;
1 >
2 >^^^^^^^^
3 > ^
4 > ^^^
5 > ^^
6 > ^
7 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >export const
2 >
3 > x
4 > =
5 > 42
6 > ;
1 >Emitted(4, 1) Source(1, 14) + SourceIndex(0)
2 >Emitted(4, 9) Source(1, 14) + SourceIndex(0)
3 >Emitted(4, 10) Source(1, 15) + SourceIndex(0)
4 >Emitted(4, 13) Source(1, 18) + SourceIndex(0)
5 >Emitted(4, 15) Source(1, 20) + SourceIndex(0)
6 >Emitted(4, 16) Source(1, 21) + SourceIndex(0)
---
>>>//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBYSxRQUFBLENBQUMsR0FBRyxFQUFFLENBQUMifQ==
6 changes: 6 additions & 0 deletions tests/baselines/reference/tsconfigExtendsJsoncPackage.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [tests/cases/compiler/tsconfigExtendsJsoncPackage.ts] ////

=== /index.ts ===
export const x = 42;
>x : Symbol(x, Decl(index.ts, 0, 12))

9 changes: 9 additions & 0 deletions tests/baselines/reference/tsconfigExtendsJsoncPackage.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [tests/cases/compiler/tsconfigExtendsJsoncPackage.ts] ////

=== /index.ts ===
export const x = 42;
>x : 42
> : ^^
>42 : 42
> : ^^

19 changes: 19 additions & 0 deletions tests/baselines/reference/tsconfigExtendsJsoncRelative.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
===================================================================
JsFile: index.js
mapUrl: data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBYSxRQUFBLENBQUMsR0FBRyxFQUFFLENBQUMifQ==
sourceRoot:
sources: index.ts
===================================================================
-------------------------------------------------------------------
emittedFile:/index.js
sourceFile:index.ts
-------------------------------------------------------------------
>>>"use strict";
>>>Object.defineProperty(exports, "__esModule", { value: true });
>>>exports.x = void 0;
>>>exports.x = 42;
1 >
2 >^^^^^^^^
3 > ^
4 > ^^^
5 > ^^
6 > ^
7 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >export const
2 >
3 > x
4 > =
5 > 42
6 > ;
1 >Emitted(4, 1) Source(1, 14) + SourceIndex(0)
2 >Emitted(4, 9) Source(1, 14) + SourceIndex(0)
3 >Emitted(4, 10) Source(1, 15) + SourceIndex(0)
4 >Emitted(4, 13) Source(1, 18) + SourceIndex(0)
5 >Emitted(4, 15) Source(1, 20) + SourceIndex(0)
6 >Emitted(4, 16) Source(1, 21) + SourceIndex(0)
---
>>>//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBYSxRQUFBLENBQUMsR0FBRyxFQUFFLENBQUMifQ==
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//// [tests/cases/compiler/tsconfigExtendsJsoncRelative.ts] ////

=== /index.ts ===
export const x = 42;
>x : Symbol(x, Decl(index.ts, 0, 12))

9 changes: 9 additions & 0 deletions tests/baselines/reference/tsconfigExtendsJsoncRelative.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [tests/cases/compiler/tsconfigExtendsJsoncRelative.ts] ////

=== /index.ts ===
export const x = 42;
>x : 42
> : ^^
>42 : 42
> : ^^

23 changes: 23 additions & 0 deletions tests/cases/compiler/tsconfigExtendsJsoncPackage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Test case related to GitHub issue #43121: tsconfig should support extends with suffix `.jsonc`
// This test case is to ensure an `extends` pointing to a package with a jsonc file works

// @filename: /node_modules/test-config/base.jsonc
{
"compilerOptions": {
"inlineSourceMap": true
}
}

// @filename: /node_modules/test-config/package.json
{
"name": "test-config",
"version": "1.0.0"
}

// @filename: /tsconfig.json
{
"extends": "test-config/base.jsonc"
}

// @filename: /index.ts
export const x = 42;
17 changes: 17 additions & 0 deletions tests/cases/compiler/tsconfigExtendsJsoncRelative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Test case related to GitHub issue #43121: tsconfig should support extends with suffix `.jsonc`
// Relative extends paths were passing before the issue: this test is to prevent regressions

// @filename: /configs/base.jsonc
{
"compilerOptions": {
"inlineSourceMap": true
}
}

// @filename: /tsconfig.json
{
"extends": "./configs/base.jsonc"
}

// @filename: /index.ts
export const x = 42;
Loading