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

Report positionless tsconfig option errors on compilerOptions key #58254

Merged
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
43 changes: 35 additions & 8 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ import {
toPath as ts_toPath,
trace,
tracing,
tryCast,
TsConfigSourceFile,
TypeChecker,
typeDirectiveIsEqualTo,
Expand Down Expand Up @@ -1618,6 +1619,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
// Map storing if there is emit blocking diagnostics for given input
const hasEmitBlockingDiagnostics = new Map<string, boolean>();
let _compilerOptionsObjectLiteralSyntax: ObjectLiteralExpression | false | undefined;
let _compilerOptionsPropertySyntax: PropertyAssignment | false | undefined;

let moduleResolutionCache: ModuleResolutionCache | undefined;
let actualResolveModuleNamesWorker: (
Expand Down Expand Up @@ -4400,7 +4402,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}

if (options.checkJs && !getAllowJSCompilerOption(options)) {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "checkJs", "allowJs"));
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "checkJs", "allowJs");
}

if (options.emitDeclarationOnly) {
Expand Down Expand Up @@ -4823,7 +4825,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
});
if (needCompilerDiagnostic) {
programDiagnostics.add(createCompilerDiagnostic(message, ...args));
createCompilerOptionsDiagnostic(message, ...args);
}
}

Expand All @@ -4845,7 +4847,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
}
});
if (needCompilerDiagnostic) {
programDiagnostics.add(createCompilerDiagnostic(message, ...args));
createCompilerOptionsDiagnostic(message, ...args);
}
}

Expand Down Expand Up @@ -4893,25 +4895,50 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
!createOptionDiagnosticInObjectLiteralSyntax(compilerOptionsObjectLiteralSyntax, onKey, option1, option2, message, ...args);

if (needCompilerDiagnostic) {
createCompilerOptionsDiagnostic(message, ...args);
}
}

function createCompilerOptionsDiagnostic(message: DiagnosticMessageChain): void;
function createCompilerOptionsDiagnostic(message: DiagnosticMessage, ...args: DiagnosticArguments): void;
function createCompilerOptionsDiagnostic(message: DiagnosticMessage | DiagnosticMessageChain, ...args: DiagnosticArguments): void;
function createCompilerOptionsDiagnostic(message: DiagnosticMessage | DiagnosticMessageChain, ...args: DiagnosticArguments): void {
const compilerOptionsProperty = getCompilerOptionsPropertySyntax();
if (compilerOptionsProperty) {
// eslint-disable-next-line local/no-in-operator
if ("messageText" in message) {
programDiagnostics.add(createCompilerDiagnosticFromMessageChain(message));
programDiagnostics.add(createDiagnosticForNodeFromMessageChain(options.configFile!, compilerOptionsProperty.name, message));
}
else {
programDiagnostics.add(createCompilerDiagnostic(message, ...args));
programDiagnostics.add(createDiagnosticForNodeInSourceFile(options.configFile!, compilerOptionsProperty.name, message, ...args));
}
}
// eslint-disable-next-line local/no-in-operator
else if ("messageText" in message) {
programDiagnostics.add(createCompilerDiagnosticFromMessageChain(message));
}
else {
programDiagnostics.add(createCompilerDiagnostic(message, ...args));
}
}

function getCompilerOptionsObjectLiteralSyntax() {
if (_compilerOptionsObjectLiteralSyntax === undefined) {
_compilerOptionsObjectLiteralSyntax = forEachPropertyAssignment(
const compilerOptionsProperty = getCompilerOptionsPropertySyntax();
_compilerOptionsObjectLiteralSyntax = compilerOptionsProperty ? tryCast(compilerOptionsProperty.initializer, isObjectLiteralExpression) || false : false;
}
return _compilerOptionsObjectLiteralSyntax || undefined;
}

function getCompilerOptionsPropertySyntax() {
if (_compilerOptionsPropertySyntax === undefined) {
_compilerOptionsPropertySyntax = forEachPropertyAssignment(
getTsConfigObjectLiteralExpression(options.configFile),
"compilerOptions",
prop => isObjectLiteralExpression(prop.initializer) ? prop.initializer : undefined,
identity,
) || false;
}
return _compilerOptionsObjectLiteralSyntax || undefined;
return _compilerOptionsPropertySyntax || undefined;
}

function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, messageChain: DiagnosticMessageChain): boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
tsconfig.json(2,5): error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
test.ts(1,19): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.


!!! error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
==== tsconfig.json (0 errors) ====
==== tsconfig.json (1 errors) ====
{
"compilerOptions": {
~~~~~~~~~~~~~~~~~
!!! error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
Comment on lines 7 to +9
Copy link
Member Author

@andrewbranch andrewbranch Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this test is simulating a combination of tsconfig options and CLI flags. This would have been reported on "moduleResolution": "bundler" if it were in the tsconfig, but it was passed in as a flag instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh

jakebailey marked this conversation as resolved.
Show resolved Hide resolved
"paths": {
"foo/*": ["./dist/*"],
"baz/*.ts": ["./types/*.d.ts"]
Expand Down
91 changes: 65 additions & 26 deletions tests/baselines/reference/tsc/moduleResolution/alternateResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,13 @@ File '/home/src/projects/project/node_modules/foo2/package.json' exists accordin
File '/home/src/projects/project/node_modules/@types/bar2/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~

Found 1 error.

Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -657,10 +660,13 @@ File '/home/src/projects/project/node_modules/foo2/package.json' exists accordin
File '/home/src/projects/project/node_modules/@types/bar2/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -896,10 +902,13 @@ File '/home/src/projects/project/node_modules/foo2/package.json' exists accordin
File '/home/src/projects/project/node_modules/@types/bar2/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~

Found 1 error.

Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -1125,10 +1134,13 @@ File '/home/src/projects/project/node_modules/foo2/package.json' exists accordin
File '/home/src/projects/project/node_modules/@types/bar2/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -1341,10 +1353,13 @@ File '/home/src/projects/project/node_modules/foo2/package.json' exists accordin
File '/home/src/projects/project/node_modules/@types/bar2/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~

Found 1 error.

Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -1526,10 +1541,13 @@ File '/home/src/projects/project/node_modules/foo2/package.json' exists accordin
File '/home/src/projects/project/node_modules/@types/bar2/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -1780,10 +1798,13 @@ File '/home/src/projects/project/node_modules/foo2/package.json' exists accordin
File '/home/src/projects/project/node_modules/@types/bar2/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -2085,10 +2106,13 @@ File '/home/src/projects/project/node_modules/@types/bar/package.json' exists ac
File '/home/src/projects/project/node_modules/foo2/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~

Found 1 error.

Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -2406,10 +2430,13 @@ File '/home/src/projects/project/node_modules/foo/package.json' exists according
File '/home/src/projects/project/node_modules/@types/bar/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -2713,10 +2740,13 @@ File '/home/src/projects/project/node_modules/foo/package.json' exists according
File '/home/src/projects/project/node_modules/@types/bar/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -2952,10 +2982,13 @@ File '/home/src/projects/project/node_modules/foo/package.json' exists according
File '/home/src/projects/project/node_modules/@types/bar/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~

Found 1 error.

Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -3181,10 +3214,13 @@ File '/home/src/projects/project/node_modules/foo/package.json' exists according
File '/home/src/projects/project/node_modules/@types/bar/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down Expand Up @@ -3397,10 +3433,13 @@ File '/home/src/projects/project/node_modules/foo/package.json' exists according
File '/home/src/projects/project/node_modules/@types/bar/package.json' exists according to earlier cached lookups.
File '/lib/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
home/src/projects/project/tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~


Found 1 error.
Found 1 error in home/src/projects/project/tsconfig.json:2

exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
Program root files: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ Resolving real path for '/Users/name/projects/web/node_modules/@types/yargs/inde
File '/a/lib/package.json' does not exist.
File '/a/package.json' does not exist.
File '/package.json' does not exist according to earlier cached lookups.
error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.
tsconfig.json:2:3 - error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.

2 "compilerOptions": {
   ~~~~~~~~~~~~~~~~~

../../../../a/lib/lib.d.ts
Default library for target 'es5'
Expand Down
Loading