Skip to content

Commit c7f7cb8

Browse files
Copilotjakebailey
andauthored
Fix false TS1006 for included tsconfig JSON imports (#4279)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent cd961a7 commit c7f7cb8

3 files changed

Lines changed: 107 additions & 2 deletions

File tree

internal/compiler/fileloader.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (p *fileLoader) addRootFileTask(fileName string, libFile *LibFile, includeR
206206
if p.opts.Config.ConfigFile != nil {
207207
containingFile = tspath.GetNormalizedAbsolutePath(p.opts.Config.ConfigFile.SourceFile.FileName(), currDir)
208208
}
209-
resolvedFile, diagnostic := p.getSourceFileFromReference(absPath, fileName, containingFile)
209+
resolvedFile, diagnostic := p.getSourceFileFromReference(absPath, fileName, containingFile, includeReason)
210210
rootTask := &parseTask{
211211
normalizedFilePath: resolvedFile,
212212
libFile: libFile,
@@ -388,6 +388,7 @@ func (p *fileLoader) getSourceFileFromReference(
388388
fileName string,
389389
referenceText string,
390390
containingFile string,
391+
includeReason *FileIncludeReason,
391392
) (string, *sourceFileFromReferenceDiagnostic) {
392393
options := p.opts.Config.CompilerOptions()
393394
allowNonTsExtensions := options.AllowNonTsExtensions.IsTrue()
@@ -406,7 +407,7 @@ func (p *fileLoader) getSourceFileFromReference(
406407
return "", &sourceFileFromReferenceDiagnostic{message: diagnostics.File_0_not_found, args: []any{diagnosticFileName}}
407408
}
408409

409-
if tspath.GetCanonicalFileName(containingFile, p.opts.Host.FS().UseCaseSensitiveFileNames()) == canonicalFileName {
410+
if includeReason.isReferencedFile() && tspath.GetCanonicalFileName(containingFile, p.opts.Host.FS().UseCaseSensitiveFileNames()) == canonicalFileName {
410411
return "", &sourceFileFromReferenceDiagnostic{message: diagnostics.A_file_cannot_have_a_reference_to_itself}
411412
}
412413
return fileName, nil
@@ -450,6 +451,7 @@ func (p *fileLoader) resolveTripleslashPathReference(moduleName string, containi
450451
normalizedFileName,
451452
moduleName,
452453
containingFile,
454+
includeReason,
453455
)
454456
if diagnostic != nil {
455457
return nil, &processingDiagnostic{

internal/execute/tsctests/tscbuild_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,31 @@ func TestBuildCommandLine(t *testing.T) {
144144
}
145145
testCases := slices.Concat(
146146
[]*tscInput{
147+
{
148+
subScenario: "included tsconfig json can be imported as json input",
149+
files: FileMap{
150+
"/home/src/workspaces/project/index.ts": `import tsconfig from "./tsconfig.json" with { type: "json" };
151+
declare global {
152+
interface ImportAttributes {
153+
type: "json";
154+
}
155+
}
156+
console.log(tsconfig);`,
157+
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
158+
{
159+
"compilerOptions": {
160+
"module": "preserve",
161+
"moduleResolution": "bundler",
162+
"noEmit": true,
163+
"resolveJsonModule": true,
164+
"strict": true,
165+
"target": "esnext"
166+
},
167+
"include": ["index.ts", "tsconfig.json"]
168+
}`),
169+
},
170+
commandLineArgs: []string{"--build"},
171+
},
147172
{
148173
subScenario: "help",
149174
files: FileMap{},
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
currentDirectory::/home/src/workspaces/project
2+
useCaseSensitiveFileNames::true
3+
Input::
4+
//// [/home/src/workspaces/project/index.ts] *new*
5+
import tsconfig from "./tsconfig.json" with { type: "json" };
6+
declare global {
7+
interface ImportAttributes {
8+
type: "json";
9+
}
10+
}
11+
console.log(tsconfig);
12+
//// [/home/src/workspaces/project/tsconfig.json] *new*
13+
{
14+
"compilerOptions": {
15+
"module": "preserve",
16+
"moduleResolution": "bundler",
17+
"noEmit": true,
18+
"resolveJsonModule": true,
19+
"strict": true,
20+
"target": "esnext"
21+
},
22+
"include": ["index.ts", "tsconfig.json"]
23+
}
24+
25+
tsgo --build
26+
ExitStatus:: Success
27+
Output::
28+
//// [/home/src/tslibs/TS/Lib/lib.esnext.full.d.ts] *Lib*
29+
/// <reference no-default-lib="true"/>
30+
interface Boolean {}
31+
interface Function {}
32+
interface CallableFunction {}
33+
interface NewableFunction {}
34+
interface IArguments {}
35+
interface Number { toExponential: any; }
36+
interface Object {}
37+
interface RegExp {}
38+
interface String { charAt: any; }
39+
interface Array<T> { length: number; [n: number]: T; }
40+
interface ReadonlyArray<T> {}
41+
interface SymbolConstructor {
42+
(desc?: string | number): symbol;
43+
for(name: string): symbol;
44+
readonly toStringTag: symbol;
45+
}
46+
declare var Symbol: SymbolConstructor;
47+
interface Symbol {
48+
readonly [Symbol.toStringTag]: string;
49+
}
50+
declare const console: { log(msg: any): void; };
51+
//// [/home/src/workspaces/project/tsconfig.tsbuildinfo] *new*
52+
{"version":"FakeTSVersion","root":["./index.ts","./tsconfig.json"]}
53+
//// [/home/src/workspaces/project/tsconfig.tsbuildinfo.readable.baseline.txt] *new*
54+
{
55+
"version": "FakeTSVersion",
56+
"root": [
57+
{
58+
"files": [
59+
"./index.ts"
60+
],
61+
"original": "./index.ts"
62+
},
63+
{
64+
"files": [
65+
"./tsconfig.json"
66+
],
67+
"original": "./tsconfig.json"
68+
}
69+
],
70+
"size": 67
71+
}
72+
73+
tsconfig.json::
74+
SemanticDiagnostics::
75+
*refresh* /home/src/tslibs/TS/Lib/lib.esnext.full.d.ts
76+
*refresh* /home/src/workspaces/project/tsconfig.json
77+
*refresh* /home/src/workspaces/project/index.ts
78+
Signatures::

0 commit comments

Comments
 (0)