Skip to content

Commit 436833a

Browse files
authored
Allow allowImportingTsExtensions to be set in any module resolution mode (#52230)
1 parent 8094007 commit 436833a

File tree

8 files changed

+219
-8
lines changed

8 files changed

+219
-8
lines changed

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
10911091
{
10921092
name: "allowImportingTsExtensions",
10931093
type: "boolean",
1094-
affectsModuleResolution: true,
1094+
affectsSemanticDiagnostics: true,
10951095
category: Diagnostics.Modules,
10961096
description: Diagnostics.Allow_imports_to_include_TypeScript_file_extensions_Requires_moduleResolution_bundler_and_either_noEmit_or_emitDeclarationOnly_to_be_set,
10971097
defaultValueDescription: false,

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4233,7 +4233,7 @@
42334233
"category": "Error",
42344234
"code": 5095
42354235
},
4236-
"Option 'allowImportingTsExtensions' can only be used when 'moduleResolution' is set to 'bundler' and either 'noEmit' or 'emitDeclarationOnly' is set.": {
4236+
"Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.": {
42374237
"category": "Error",
42384238
"code": 5096
42394239
},

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,9 +3033,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
30333033
// Program errors validate that `noEmit` or `emitDeclarationOnly` is also set,
30343034
// so this function doesn't check them to avoid propagating errors.
30353035
export function shouldAllowImportingTsExtension(compilerOptions: CompilerOptions, fromFileName?: string) {
3036-
return getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler && (
3037-
!!compilerOptions.allowImportingTsExtensions ||
3038-
fromFileName && isDeclarationFileName(fromFileName));
3036+
return !!compilerOptions.allowImportingTsExtensions || fromFileName && isDeclarationFileName(fromFileName);
30393037
}
30403038

30413039
/**

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4213,7 +4213,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
42134213
}
42144214

42154215
if (options.allowImportingTsExtensions && !(options.noEmit || options.emitDeclarationOnly)) {
4216-
createOptionValueDiagnostic("allowImportingTsExtensions", Diagnostics.Option_allowImportingTsExtensions_can_only_be_used_when_moduleResolution_is_set_to_bundler_and_either_noEmit_or_emitDeclarationOnly_is_set);
4216+
createOptionValueDiagnostic("allowImportingTsExtensions", Diagnostics.Option_allowImportingTsExtensions_can_only_be_used_when_either_noEmit_or_emitDeclarationOnly_is_set);
42174217
}
42184218

42194219
const moduleResolution = getEmitModuleResolutionKind(options);

src/testRunner/unittests/tscWatch/programUpdates.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,4 +1957,42 @@ import { x } from "../b";`),
19571957
},
19581958
]
19591959
});
1960+
1961+
verifyTscWatch({
1962+
scenario,
1963+
subScenario: "when changing `allowImportingTsExtensions` of config file",
1964+
commandLineArgs: ["-w", "-p", ".", "--extendedDiagnostics"],
1965+
sys: () => {
1966+
const module1: File = {
1967+
path: `/user/username/projects/myproject/a.ts`,
1968+
content: ``
1969+
};
1970+
const module2: File = {
1971+
path: `/user/username/projects/myproject/b.ts`,
1972+
content: `import "./a.ts";`
1973+
};
1974+
const config: File = {
1975+
path: `/user/username/projects/myproject/tsconfig.json`,
1976+
content: JSON.stringify({
1977+
compilerOptions: {
1978+
noEmit: true,
1979+
allowImportingTsExtensions: false
1980+
}
1981+
}),
1982+
};
1983+
return createWatchedSystem([module1, module2, config, libFile], { currentDirectory: "/user/username/projects/myproject" });
1984+
},
1985+
edits: [
1986+
{
1987+
caption: "Change allowImportingTsExtensions to true",
1988+
edit: sys => sys.writeFile(`/user/username/projects/myproject/tsconfig.json`, JSON.stringify({
1989+
compilerOptions: {
1990+
noEmit: true,
1991+
allowImportingTsExtensions: true
1992+
}
1993+
})),
1994+
timeouts: sys => sys.checkTimeoutQueueLengthAndRun(1),
1995+
},
1996+
]
1997+
});
19601998
});

tests/baselines/reference/bundlerImportTsExtensions(allowimportingtsextensions=true,noemit=false).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error TS5056: Cannot write file 'out/b.js' because it would be overwritten by multiple input files.
22
error TS5056: Cannot write file 'out/c.js' because it would be overwritten by multiple input files.
3-
error TS5096: Option 'allowImportingTsExtensions' can only be used when 'moduleResolution' is set to 'bundler' and either 'noEmit' or 'emitDeclarationOnly' is set.
3+
error TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.
44
error TS6054: File '/project/e.txt' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx', '.cts', '.d.cts', '.cjs', '.mts', '.d.mts', '.mjs'.
55
The file is in the program because:
66
Root file specified for compilation
@@ -11,7 +11,7 @@ error TS6054: File '/project/e.txt' has an unsupported extension. The only suppo
1111

1212
!!! error TS5056: Cannot write file 'out/b.js' because it would be overwritten by multiple input files.
1313
!!! error TS5056: Cannot write file 'out/c.js' because it would be overwritten by multiple input files.
14-
!!! error TS5096: Option 'allowImportingTsExtensions' can only be used when 'moduleResolution' is set to 'bundler' and either 'noEmit' or 'emitDeclarationOnly' is set.
14+
!!! error TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.
1515
!!! error TS6054: File '/project/e.txt' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx', '.cts', '.d.cts', '.cjs', '.mts', '.d.mts', '.mjs'.
1616
!!! error TS6054: The file is in the program because:
1717
!!! error TS6054: Root file specified for compilation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
Input::
2+
//// [/user/username/projects/myproject/a.ts]
3+
4+
5+
//// [/user/username/projects/myproject/b.ts]
6+
import "./a.ts";
7+
8+
//// [/user/username/projects/myproject/tsconfig.json]
9+
{"compilerOptions":{"noEmit":true,"allowImportingTsExtensions":false}}
10+
11+
//// [/a/lib/lib.d.ts]
12+
/// <reference no-default-lib="true"/>
13+
interface Boolean {}
14+
interface Function {}
15+
interface CallableFunction {}
16+
interface NewableFunction {}
17+
interface IArguments {}
18+
interface Number { toExponential: any; }
19+
interface Object {}
20+
interface RegExp {}
21+
interface String { charAt: any; }
22+
interface Array<T> { length: number; [n: number]: T; }
23+
24+
25+
/a/lib/tsc.js -w -p . --extendedDiagnostics
26+
Output::
27+
[12:00:23 AM] Starting compilation in watch mode...
28+
29+
Current directory: /user/username/projects/myproject CaseSensitiveFileNames: false
30+
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 undefined Config file
31+
Synchronizing program
32+
CreatingProgramWith::
33+
roots: ["/user/username/projects/myproject/a.ts","/user/username/projects/myproject/b.ts"]
34+
options: {"noEmit":true,"allowImportingTsExtensions":false,"watch":true,"project":"/user/username/projects/myproject","extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
35+
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/a.ts 250 undefined Source file
36+
FileWatcher:: Added:: WatchInfo: /user/username/projects/myproject/b.ts 250 undefined Source file
37+
FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 250 undefined Source file
38+
DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types 1 undefined Type roots
39+
Elapsed:: *ms DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject/node_modules/@types 1 undefined Type roots
40+
b.ts:1:8 - error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
41+
42+
1 import "./a.ts";
43+
   ~~~~~~~~
44+
45+
[12:00:24 AM] Found 1 error. Watching for file changes.
46+
47+
DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory
48+
Elapsed:: *ms DirectoryWatcher:: Added:: WatchInfo: /user/username/projects/myproject 1 undefined Wild card directory
49+
50+
51+
Program root files: ["/user/username/projects/myproject/a.ts","/user/username/projects/myproject/b.ts"]
52+
Program options: {"noEmit":true,"allowImportingTsExtensions":false,"watch":true,"project":"/user/username/projects/myproject","extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
53+
Program structureReused: Not
54+
Program files::
55+
/a/lib/lib.d.ts
56+
/user/username/projects/myproject/a.ts
57+
/user/username/projects/myproject/b.ts
58+
59+
Semantic diagnostics in builder refreshed for::
60+
/a/lib/lib.d.ts
61+
/user/username/projects/myproject/a.ts
62+
/user/username/projects/myproject/b.ts
63+
64+
Shape signatures in builder refreshed for::
65+
/a/lib/lib.d.ts (used version)
66+
/user/username/projects/myproject/a.ts (used version)
67+
/user/username/projects/myproject/b.ts (used version)
68+
69+
PolledWatches::
70+
/user/username/projects/myproject/node_modules/@types:
71+
{"pollingInterval":500}
72+
73+
FsWatches::
74+
/user/username/projects/myproject/tsconfig.json:
75+
{}
76+
/user/username/projects/myproject/a.ts:
77+
{}
78+
/user/username/projects/myproject/b.ts:
79+
{}
80+
/a/lib/lib.d.ts:
81+
{}
82+
83+
FsWatchesRecursive::
84+
/user/username/projects/myproject:
85+
{}
86+
87+
exitCode:: ExitStatus.undefined
88+
89+
90+
Change:: Change allowImportingTsExtensions to true
91+
92+
Input::
93+
//// [/user/username/projects/myproject/tsconfig.json]
94+
{"compilerOptions":{"noEmit":true,"allowImportingTsExtensions":true}}
95+
96+
97+
Output::
98+
FileWatcher:: Triggered with /user/username/projects/myproject/tsconfig.json 1:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 undefined Config file
99+
Scheduling update
100+
Elapsed:: *ms FileWatcher:: Triggered with /user/username/projects/myproject/tsconfig.json 1:: WatchInfo: /user/username/projects/myproject/tsconfig.json 2000 undefined Config file
101+
Reloading config file: /user/username/projects/myproject/tsconfig.json
102+
Synchronizing program
103+
[12:00:28 AM] File change detected. Starting incremental compilation...
104+
105+
CreatingProgramWith::
106+
roots: ["/user/username/projects/myproject/a.ts","/user/username/projects/myproject/b.ts"]
107+
options: {"noEmit":true,"allowImportingTsExtensions":true,"watch":true,"project":"/user/username/projects/myproject","extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
108+
[12:00:29 AM] Found 0 errors. Watching for file changes.
109+
110+
111+
112+
Program root files: ["/user/username/projects/myproject/a.ts","/user/username/projects/myproject/b.ts"]
113+
Program options: {"noEmit":true,"allowImportingTsExtensions":true,"watch":true,"project":"/user/username/projects/myproject","extendedDiagnostics":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
114+
Program structureReused: Completely
115+
Program files::
116+
/a/lib/lib.d.ts
117+
/user/username/projects/myproject/a.ts
118+
/user/username/projects/myproject/b.ts
119+
120+
Semantic diagnostics in builder refreshed for::
121+
/a/lib/lib.d.ts
122+
/user/username/projects/myproject/a.ts
123+
/user/username/projects/myproject/b.ts
124+
125+
No shapes updated in the builder::
126+
127+
PolledWatches::
128+
/user/username/projects/myproject/node_modules/@types:
129+
{"pollingInterval":500}
130+
131+
FsWatches::
132+
/user/username/projects/myproject/tsconfig.json:
133+
{}
134+
/user/username/projects/myproject/a.ts:
135+
{}
136+
/user/username/projects/myproject/b.ts:
137+
{}
138+
/a/lib/lib.d.ts:
139+
{}
140+
141+
FsWatchesRecursive::
142+
/user/username/projects/myproject:
143+
{}
144+
145+
exitCode:: ExitStatus.undefined
146+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @allowImportingTsExtensions: true
2+
// @noEmit: true
3+
// @moduleResolution: classic,node10,node16,nodenext,bundler
4+
// @jsx: preserve
5+
// @noTypesAndSymbols: true
6+
7+
// @Filename: /ts.ts
8+
export {};
9+
10+
// @Filename: /tsx.tsx
11+
export {};
12+
13+
// @Filename: /dts.d.ts
14+
export {};
15+
16+
// @Filename: /b.ts
17+
import {} from "./ts.js";
18+
import {} from "./ts.ts";
19+
import type {} from "./ts.d.ts";
20+
21+
import {} from "./tsx.js";
22+
import {} from "./tsx.jsx";
23+
import {} from "./tsx.ts";
24+
import {} from "./tsx.tsx";
25+
import type {} from "./tsx.d.ts";
26+
27+
import {} from "./dts.js";
28+
import {} from "./dts.ts";
29+
import type {} from "./dts.d.ts";

0 commit comments

Comments
 (0)