Skip to content

Fix empty files diagnostics reporting #26865

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

Merged
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
23 changes: 16 additions & 7 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1843,8 +1843,21 @@ namespace ts {
if (hasProperty(raw, "files") && !isNullOrUndefined(raw.files)) {
if (isArray(raw.files)) {
filesSpecs = <ReadonlyArray<string>>raw.files;
if (filesSpecs.length === 0) {
createCompilerDiagnosticOnlyIfJson(Diagnostics.The_files_list_in_config_file_0_is_empty, configFileName || "tsconfig.json");
const hasReferences = hasProperty(raw, "references") && !isNullOrUndefined(raw.references);
const hasZeroOrNoReferences = !hasReferences || raw.references.length === 0;
if (filesSpecs.length === 0 && hasZeroOrNoReferences) {
if (sourceFile) {
const fileName = configFileName || "tsconfig.json";
const diagnosticMessage = Diagnostics.The_files_list_in_config_file_0_is_empty;
const nodeValue = firstDefined(getTsConfigPropArray(sourceFile, "files"), property => property.initializer);
const error = nodeValue
? createDiagnosticForNodeInSourceFile(sourceFile, nodeValue, diagnosticMessage, fileName)
: createCompilerDiagnostic(diagnosticMessage, fileName);
errors.push(error);
}
else {
createCompilerDiagnosticOnlyIfJson(Diagnostics.The_files_list_in_config_file_0_is_empty, configFileName || "tsconfig.json");
}
}
}
else {
Expand Down Expand Up @@ -2067,11 +2080,6 @@ namespace ts {
createDiagnosticForNodeInSourceFile(sourceFile, valueNode, message, arg0)
);
return;
case "files":
if ((<ReadonlyArray<string>>value).length === 0) {
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, valueNode, Diagnostics.The_files_list_in_config_file_0_is_empty, configFileName || "tsconfig.json"));
}
return;
}
},
onSetUnknownOptionKeyValueInRoot(key: string, keyNode: PropertyName, _value: CompilerOptionsValue, _valueNode: Expression) {
Expand All @@ -2081,6 +2089,7 @@ namespace ts {
}
};
const json = convertToObjectWorker(sourceFile, errors, /*returnValue*/ true, getTsconfigRootOptionsMap(), optionsIterator);

if (!typeAcquisition) {
if (typingOptionstypeAcquisition) {
typeAcquisition = (typingOptionstypeAcquisition.enableAutoDiscovery !== undefined) ?
Expand Down
16 changes: 13 additions & 3 deletions src/compiler/tsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,6 @@ namespace ts {
context.projectStatus.setValue(proj, { type: UpToDateStatusType.Unbuildable, reason: "Config file errors" });
return resultFlags;
}

if (configFile.fileNames.length === 0) {
// Nothing to build - must be a solution file, basically
return BuildResultFlags.None;
Expand All @@ -956,7 +955,8 @@ namespace ts {
projectReferences: configFile.projectReferences,
host,
rootNames: configFile.fileNames,
options: configFile.options
options: configFile.options,
configFileParsingDiagnostics: configFile.errors,
};
const program = createProgram(programOptions);

Expand Down Expand Up @@ -1149,19 +1149,22 @@ namespace ts {

const queue = graph.buildQueue;
reportBuildQueue(graph);

let anyFailed = false;
for (const next of queue) {
const proj = configFileCache.parseConfigFile(next);
if (proj === undefined) {
anyFailed = true;
break;
}

// report errors early when using continue or break statements
const errors = proj.errors;
const status = getUpToDateStatus(proj);
verboseReportProjectStatus(next, status);

const projName = proj.options.configFilePath!;
if (status.type === UpToDateStatusType.UpToDate && !context.options.force) {
reportErrors(errors);
// Up to date, skip
if (defaultOptions.dry) {
// In a dry build, inform the user of this fact
Expand All @@ -1171,17 +1174,20 @@ namespace ts {
}

if (status.type === UpToDateStatusType.UpToDateWithUpstreamTypes && !context.options.force) {
reportErrors(errors);
// Fake build
updateOutputTimestamps(proj);
continue;
}

if (status.type === UpToDateStatusType.UpstreamBlocked) {
reportErrors(errors);
if (context.options.verbose) reportStatus(Diagnostics.Skipping_build_of_project_0_because_its_dependency_1_has_errors, projName, status.upstreamProjectName);
continue;
}

if (status.type === UpToDateStatusType.ContainerOnly) {
reportErrors(errors);
// Do nothing
continue;
}
Expand All @@ -1193,6 +1199,10 @@ namespace ts {
return anyFailed ? ExitStatus.DiagnosticsPresent_OutputsSkipped : ExitStatus.Success;
}

function reportErrors(errors: Diagnostic[]) {
errors.forEach((err) => host.reportDiagnostic(err));
}

/**
* Report the build ordering inferred from the current project graph if we're in verbose mode
*/
Expand Down
42 changes: 42 additions & 0 deletions src/testRunner/unittests/tsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,48 @@ namespace ts {
});
}

export namespace EmptyFiles {
const projFs = loadProjectFromDisk("tests/projects/empty-files");

const allExpectedOutputs = [
"/src/core/index.js",
"/src/core/index.d.ts",
"/src/core/index.d.ts.map",
];

describe("tsbuild - empty files option in tsconfig", () => {
it("has empty files diagnostic when files is empty and no references are provided", () => {
const fs = projFs.shadow();
const host = new fakes.SolutionBuilderHost(fs);
const builder = createSolutionBuilder(host, ["/src/no-references"], { dry: false, force: false, verbose: false });

host.clearDiagnostics();
builder.buildAllProjects();
host.assertDiagnosticMessages(Diagnostics.The_files_list_in_config_file_0_is_empty);

// Check for outputs to not be written.
for (const output of allExpectedOutputs) {
assert(!fs.existsSync(output), `Expect file ${output} to not exist`);
}
});

it("does not have empty files diagnostic when files is empty and references are provided", () => {
const fs = projFs.shadow();
const host = new fakes.SolutionBuilderHost(fs);
const builder = createSolutionBuilder(host, ["/src/with-references"], { dry: false, force: false, verbose: false });

host.clearDiagnostics();
builder.buildAllProjects();
host.assertDiagnosticMessages(/*empty*/);

// Check for outputs to be written.
for (const output of allExpectedOutputs) {
assert(fs.existsSync(output), `Expect file ${output} to exist`);
}
});
});
}

describe("tsbuild - graph-ordering", () => {
let host: fakes.SolutionBuilderHost | undefined;
const deps: [string, string][] = [
Expand Down
37 changes: 37 additions & 0 deletions src/testRunner/unittests/tsconfigParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ namespace ts {
}
}

function assertParseFileDiagnosticsExclusion(jsonText: string, configFileName: string, basePath: string, allFileList: string[], expectedExcludedDiagnosticCode: number) {
{
const parsed = getParsedCommandJson(jsonText, configFileName, basePath, allFileList);
assert.isTrue(parsed.errors.length >= 0);
assert.isTrue(parsed.errors.findIndex(e => e.code === expectedExcludedDiagnosticCode) === -1, `Expected error code ${expectedExcludedDiagnosticCode} to not be in ${JSON.stringify(parsed.errors)}`);
}
{
const parsed = getParsedCommandJsonNode(jsonText, configFileName, basePath, allFileList);
assert.isTrue(parsed.errors.length >= 0);
assert.isTrue(parsed.errors.findIndex(e => e.code === expectedExcludedDiagnosticCode) === -1, `Expected error code ${expectedExcludedDiagnosticCode} to not be in ${JSON.stringify(parsed.errors)}`);
}
}

it("returns empty config for file with only whitespaces", () => {
assertParseResult("", { config : {} });
assertParseResult(" ", { config : {} });
Expand Down Expand Up @@ -280,6 +293,30 @@ namespace ts {
Diagnostics.The_files_list_in_config_file_0_is_empty.code);
});

it("generates errors for empty files list when no references are provided", () => {
const content = `{
"files": [],
"references": []
}`;
assertParseFileDiagnostics(content,
"/apath/tsconfig.json",
"tests/cases/unittests",
["/apath/a.ts"],
Diagnostics.The_files_list_in_config_file_0_is_empty.code);
});

it("does not generate errors for empty files list when one or more references are provided", () => {
const content = `{
"files": [],
"references": [{ "path": "/apath" }]
}`;
assertParseFileDiagnosticsExclusion(content,
"/apath/tsconfig.json",
"tests/cases/unittests",
["/apath/a.ts"],
Diagnostics.The_files_list_in_config_file_0_is_empty.code);
});

it("generates errors for directory with no .ts files", () => {
const content = `{
}`;
Expand Down
1 change: 1 addition & 0 deletions tests/projects/empty-files/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function multiply(a: number, b: number) { return a * b; }
7 changes: 7 additions & 0 deletions tests/projects/empty-files/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true
}
}
9 changes: 9 additions & 0 deletions tests/projects/empty-files/no-references/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"references": [],
"files": [],
"compilerOptions": {
"composite": true,
"declaration": true,
"forceConsistentCasingInFileNames": true
}
}
11 changes: 11 additions & 0 deletions tests/projects/empty-files/with-references/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"references": [
{ "path": "../core" },
],
"files": [],
"compilerOptions": {
"composite": true,
"declaration": true,
"forceConsistentCasingInFileNames": true
}
}