Skip to content

Commit 824c22c

Browse files
committed
Source of project reference behave as if those files cannot be emitted.
1 parent b1fa2eb commit 824c22c

File tree

9 files changed

+468
-35
lines changed

9 files changed

+468
-35
lines changed

src/compiler/sys.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,33 @@ namespace ts {
472472
}
473473
}
474474

475+
function recursiveCreateDirectory(directoryPath: string, sys: System) {
476+
const basePath = getDirectoryPath(directoryPath);
477+
const shouldCreateParent = basePath !== "" && directoryPath !== basePath && !sys.directoryExists(basePath);
478+
if (shouldCreateParent) {
479+
recursiveCreateDirectory(basePath, sys);
480+
}
481+
if (shouldCreateParent || !sys.directoryExists(directoryPath)) {
482+
sys.createDirectory(directoryPath);
483+
}
484+
}
485+
486+
/**
487+
* patch writefile to create folder before writing the file
488+
*/
489+
/*@internal*/
490+
export function patchWriteFileEnsuringDirectory(sys: System) {
491+
// patch writefile to create folder before writing the file
492+
const originalWriteFile = sys.writeFile;
493+
sys.writeFile = (path, data, writeBom) => {
494+
const directoryPath = getDirectoryPath(normalizeSlashes(path));
495+
if (directoryPath && !sys.directoryExists(directoryPath)) {
496+
recursiveCreateDirectory(directoryPath, sys);
497+
}
498+
originalWriteFile.call(sys, path, data, writeBom);
499+
};
500+
}
501+
475502
/*@internal*/
476503
interface NodeBuffer extends Uint8Array {
477504
write(str: string, offset?: number, length?: number, encoding?: string): number;
@@ -1259,17 +1286,6 @@ namespace ts {
12591286
};
12601287
}
12611288

1262-
function recursiveCreateDirectory(directoryPath: string, sys: System) {
1263-
const basePath = getDirectoryPath(directoryPath);
1264-
const shouldCreateParent = basePath !== "" && directoryPath !== basePath && !sys.directoryExists(basePath);
1265-
if (shouldCreateParent) {
1266-
recursiveCreateDirectory(basePath, sys);
1267-
}
1268-
if (shouldCreateParent || !sys.directoryExists(directoryPath)) {
1269-
sys.createDirectory(directoryPath);
1270-
}
1271-
}
1272-
12731289
let sys: System | undefined;
12741290
if (typeof ChakraHost !== "undefined") {
12751291
sys = getChakraSystem();
@@ -1281,14 +1297,7 @@ namespace ts {
12811297
}
12821298
if (sys) {
12831299
// patch writefile to create folder before writing the file
1284-
const originalWriteFile = sys.writeFile;
1285-
sys.writeFile = (path, data, writeBom) => {
1286-
const directoryPath = getDirectoryPath(normalizeSlashes(path));
1287-
if (directoryPath && !sys!.directoryExists(directoryPath)) {
1288-
recursiveCreateDirectory(directoryPath, sys!);
1289-
}
1290-
originalWriteFile.call(sys, path, data, writeBom);
1291-
};
1300+
patchWriteFileEnsuringDirectory(sys);
12921301
}
12931302
return sys!;
12941303
})();

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ interface Array<T> {}`
6666
params.newLine,
6767
params.useWindowsStylePaths,
6868
params.environmentVariables);
69+
// Just like sys, patch the host to use writeFile
70+
patchWriteFileEnsuringDirectory(host);
6971
return host;
7072
}
7173

@@ -990,6 +992,19 @@ interface Array<T> {}`
990992
}
991993
}
992994

995+
export type TestServerHostTrackingWrittenFiles = TestServerHost & { writtenFiles: Map<true>; };
996+
997+
export function changeToHostTrackingWrittenFiles(inputHost: TestServerHost) {
998+
const host = inputHost as TestServerHostTrackingWrittenFiles;
999+
const originalWriteFile = host.writeFile;
1000+
host.writtenFiles = createMap<true>();
1001+
host.writeFile = (fileName, content) => {
1002+
originalWriteFile.call(host, fileName, content);
1003+
const path = host.toFullPath(fileName);
1004+
host.writtenFiles.set(path, true);
1005+
};
1006+
return host;
1007+
}
9931008
export const tsbuildProjectsLocation = "/user/username/projects";
9941009
export function getTsBuildProjectFilePath(project: string, file: string) {
9951010
return `${tsbuildProjectsLocation}/${project}/${file}`;

src/server/project.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,11 @@ namespace ts.server {
537537
return this.projectService.getSourceFileLike(fileName, this);
538538
}
539539

540-
private shouldEmitFile(scriptInfo: ScriptInfo) {
541-
return scriptInfo && !scriptInfo.isDynamicOrHasMixedContent();
540+
/*@internal*/
541+
shouldEmitFile(scriptInfo: ScriptInfo | undefined) {
542+
return scriptInfo &&
543+
!scriptInfo.isDynamicOrHasMixedContent() &&
544+
!this.program!.isSourceOfProjectReferenceRedirect(scriptInfo.path);
542545
}
543546

544547
getCompileOnSaveAffectedFileList(scriptInfo: ScriptInfo): string[] {
@@ -548,7 +551,7 @@ namespace ts.server {
548551
updateProjectIfDirty(this);
549552
this.builderState = BuilderState.create(this.program!, this.projectService.toCanonicalFileName, this.builderState);
550553
return mapDefined(BuilderState.getFilesAffectedBy(this.builderState, this.program!, scriptInfo.path, this.cancellationToken, data => this.projectService.host.createHash!(data)), // TODO: GH#18217
551-
sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)!) ? sourceFile.fileName : undefined);
554+
sourceFile => this.shouldEmitFile(this.projectService.getScriptInfoForPath(sourceFile.path)) ? sourceFile.fileName : undefined);
552555
}
553556

554557
/**

src/server/session.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,9 @@ namespace ts.server {
10301030

10311031
private getEmitOutput(args: protocol.FileRequestArgs): EmitOutput {
10321032
const { file, project } = this.getFileAndProject(args);
1033-
return project.getLanguageService().getEmitOutput(file);
1033+
return project.shouldEmitFile(project.getScriptInfo(file)) ?
1034+
project.getLanguageService().getEmitOutput(file) :
1035+
{ emitSkipped: true, outputFiles: [] };
10341036
}
10351037

10361038
private mapDefinitionInfo(definitions: ReadonlyArray<DefinitionInfo>, project: Project): ReadonlyArray<protocol.FileSpanWithContext> {

src/testRunner/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
"unittests/tsserver/occurences.ts",
138138
"unittests/tsserver/openFile.ts",
139139
"unittests/tsserver/projectErrors.ts",
140+
"unittests/tsserver/projectReferenceCompileOnSave.ts",
140141
"unittests/tsserver/projectReferenceErrors.ts",
141142
"unittests/tsserver/projectReferences.ts",
142143
"unittests/tsserver/projects.ts",

src/testRunner/unittests/tsbuildWatchMode.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ namespace ts.tscWatch {
22
import projectsLocation = TestFSWithWatch.tsbuildProjectsLocation;
33
import getFilePathInProject = TestFSWithWatch.getTsBuildProjectFilePath;
44
import getFileFromProject = TestFSWithWatch.getTsBuildProjectFile;
5-
type TsBuildWatchSystem = WatchedSystem & { writtenFiles: Map<true>; };
5+
type TsBuildWatchSystem = TestFSWithWatch.TestServerHostTrackingWrittenFiles;
66

77
function createTsBuildWatchSystem(fileOrFolderList: ReadonlyArray<TestFSWithWatch.FileOrFolderOrSymLink>, params?: TestFSWithWatch.TestServerHostCreationParameters) {
8-
const host = createWatchedSystem(fileOrFolderList, params) as TsBuildWatchSystem;
9-
const originalWriteFile = host.writeFile;
10-
host.writtenFiles = createMap<true>();
11-
host.writeFile = (fileName, content) => {
12-
originalWriteFile.call(host, fileName, content);
13-
const path = host.toFullPath(fileName);
14-
host.writtenFiles.set(path, true);
15-
};
16-
return host;
8+
return TestFSWithWatch.changeToHostTrackingWrittenFiles(
9+
createWatchedSystem(fileOrFolderList, params)
10+
);
1711
}
1812

1913
export function createSolutionBuilder(system: WatchedSystem, rootNames: ReadonlyArray<string>, defaultOptions?: BuildOptions) {

src/testRunner/unittests/tsserver/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ namespace ts.projectSystem {
498498
return protocolToLocation(str)(start);
499499
}
500500

501-
function protocolToLocation(text: string): (pos: number) => protocol.Location {
501+
export function protocolToLocation(text: string): (pos: number) => protocol.Location {
502502
const lineStarts = computeLineStarts(text);
503503
return pos => {
504504
const x = computeLineAndCharacterOfPosition(lineStarts, pos);

0 commit comments

Comments
 (0)