Skip to content

Avoid compile on save when there is no emit impact #32688

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 4 commits into from
Aug 7, 2019
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
28 changes: 20 additions & 8 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ namespace ts.server {
return false;
}


function dtsChangeCanAffectEmit(compilationSettings: CompilerOptions) {
return getEmitDeclarations(compilationSettings) || !!compilationSettings.emitDecoratorMetadata;
}

function formatDiag(fileName: NormalizedPath, project: Project, diag: Diagnostic): protocol.Diagnostic {
const scriptInfo = project.getScriptInfoForNormalizedPath(fileName)!; // TODO: GH#18217
return {
Expand Down Expand Up @@ -1604,15 +1609,22 @@ namespace ts.server {
path => this.projectService.getScriptInfoForPath(path)!,
projects,
(project, info) => {
let result: protocol.CompileOnSaveAffectedFileListSingleProject | undefined;
if (project.compileOnSaveEnabled && project.languageServiceEnabled && !project.isOrphan() && !project.getCompilationSettings().noEmit) {
result = {
projectFileName: project.getProjectName(),
fileNames: project.getCompileOnSaveAffectedFileList(info),
projectUsesOutFile: !!project.getCompilationSettings().outFile || !!project.getCompilationSettings().out
};
if (!project.compileOnSaveEnabled || !project.languageServiceEnabled || project.isOrphan()) {
return undefined;
}

const compilationSettings = project.getCompilationSettings();

if (!!compilationSettings.noEmit || fileExtensionIs(info.fileName, Extension.Dts) && !dtsChangeCanAffectEmit(compilationSettings)) {
// avoid triggering emit when a change is made in a .d.ts when declaration emit and decorator metadata emit are disabled
return undefined;
}
return result;

return {
projectFileName: project.getProjectName(),
fileNames: project.getCompileOnSaveAffectedFileList(info),
projectUsesOutFile: !!compilationSettings.outFile || !!compilationSettings.out
};
}
);
}
Expand Down
106 changes: 106 additions & 0 deletions src/testRunner/unittests/tsserver/compileOnSave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,112 @@ namespace ts.projectSystem {
});
});

describe("for changes in declaration files", () => {
function testDTS(dtsFileContents: string, tsFileContents: string, opts: CompilerOptions, expectDTSEmit: boolean) {
const dtsFile = {
path: "/a/runtime/a.d.ts",
content: dtsFileContents
};
const f2 = {
path: "/a/b.ts",
content: tsFileContents
};
const config = {
path: "/a/tsconfig.json",
content: JSON.stringify({
compilerOptions: opts,
compileOnSave: true
})
};
const host = createServerHost([dtsFile, f2, config]);
const session = projectSystem.createSession(host);
session.executeCommand(<protocol.OpenRequest>{
seq: 1,
type: "request",
command: "open",
arguments: { file: dtsFile.path }
});
const projectService = session.getProjectService();
checkNumberOfProjects(projectService, { configuredProjects: 1 });
const project = projectService.configuredProjects.get(config.path)!;
checkProjectRootFiles(project, [dtsFile.path, f2.path]);
session.executeCommand(<protocol.OpenRequest>{
seq: 2,
type: "request",
command: "open",
arguments: { file: f2.path }
});
checkNumberOfProjects(session.getProjectService(), { configuredProjects: 1 });
const { response } = session.executeCommand(<protocol.CompileOnSaveAffectedFileListRequest>{
seq: 3,
type: "request",
command: "compileOnSaveAffectedFileList",
arguments: { file: dtsFile.path }
});
if (expectDTSEmit) {
assert.equal((response as protocol.CompileOnSaveAffectedFileListSingleProject[]).length, 1, "expected output from 1 project");
assert.equal((response as protocol.CompileOnSaveAffectedFileListSingleProject[])[0].fileNames.length, 2, "expected to affect 2 files");
}
else {
assert.equal((response as protocol.CompileOnSaveAffectedFileListSingleProject[]).length, 0, "expected no output");
}


const { response: response2 } = session.executeCommand(<protocol.CompileOnSaveAffectedFileListRequest>{
seq: 4,
type: "request",
command: "compileOnSaveAffectedFileList",
arguments: { file: f2.path }
});
assert.equal((response2 as protocol.CompileOnSaveAffectedFileListSingleProject[]).length, 1, "expected output from 1 project");
}

it("should return empty array if change is made in a global declaration file", () => {
testDTS(
/*dtsFileContents*/ "declare const x: string;",
/*tsFileContents*/ "var y = 1;",
/*opts*/ {},
/*expectDTSEmit*/ false
);
});

it("should return empty array if change is made in a module declaration file", () => {
testDTS(
/*dtsFileContents*/ "export const x: string;",
/*tsFileContents*/ "import { x } from './runtime/a;",
/*opts*/ {},
/*expectDTSEmit*/ false
);
});

it("should return results if change is made in a global declaration file with declaration emit", () => {
testDTS(
/*dtsFileContents*/ "declare const x: string;",
/*tsFileContents*/ "var y = 1;",
/*opts*/ { declaration: true },
/*expectDTSEmit*/ true
);
});

it("should return results if change is made in a global declaration file with composite enabled", () => {
testDTS(
/*dtsFileContents*/ "declare const x: string;",
/*tsFileContents*/ "var y = 1;",
/*opts*/ { composite: true },
/*expectDTSEmit*/ true
);
});

it("should return results if change is made in a global declaration file with decorator emit enabled", () => {
testDTS(
/*dtsFileContents*/ "declare const x: string;",
/*tsFileContents*/ "var y = 1;",
/*opts*/ { experimentalDecorators: true, emitDecoratorMetadata: true },
/*expectDTSEmit*/ true
);
});
});

describe("tsserverProjectSystem emit with outFile or out setting", () => {
function test(opts: CompilerOptions, expectedUsesOutFile: boolean) {
const f1 = {
Expand Down