Skip to content

Fixes issues with reload because of output emit #39030

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
Jun 16, 2020
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
35 changes: 34 additions & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2943,7 +2943,13 @@ namespace ts {
* @param extraFileExtensions optionaly file extra file extension information from host
*/
/* @internal */
export function getFileNamesFromConfigSpecs(spec: ConfigFileSpecs, basePath: string, options: CompilerOptions, host: ParseConfigHost, extraFileExtensions: readonly FileExtensionInfo[] = []): ExpandResult {
export function getFileNamesFromConfigSpecs(
spec: ConfigFileSpecs,
basePath: string,
options: CompilerOptions,
host: ParseConfigHost,
extraFileExtensions: readonly FileExtensionInfo[] = emptyArray
): ExpandResult {
basePath = normalizePath(basePath);

const keyMapper = createGetCanonicalFileName(host.useCaseSensitiveFileNames);
Expand Down Expand Up @@ -3030,6 +3036,33 @@ namespace ts {
};
}

/* @internal */
export function isExcludedFile(
pathToCheck: string,
spec: ConfigFileSpecs,
basePath: string,
useCaseSensitiveFileNames: boolean,
currentDirectory: string
): boolean {
const { filesSpecs, validatedIncludeSpecs, validatedExcludeSpecs } = spec;
if (!length(validatedIncludeSpecs) || !length(validatedExcludeSpecs)) return false;

basePath = normalizePath(basePath);

const keyMapper = createGetCanonicalFileName(useCaseSensitiveFileNames);
if (filesSpecs) {
for (const fileName of filesSpecs) {
if (keyMapper(getNormalizedAbsolutePath(fileName, basePath)) === pathToCheck) return false;
}
}

const excludePattern = getRegularExpressionForWildcard(validatedExcludeSpecs, combinePaths(normalizePath(currentDirectory), basePath), "exclude");
const excludeRegex = excludePattern && getRegexFromPattern(excludePattern, useCaseSensitiveFileNames);
if (!excludeRegex) return false;
if (excludeRegex.test(pathToCheck)) return true;
return !hasExtension(pathToCheck) && excludeRegex.test(ensureTrailingDirectorySeparator(pathToCheck));
}

function validateSpecs(specs: readonly string[], errors: Push<Diagnostic>, allowTrailingRecursion: boolean, jsonSourceFile: TsConfigSourceFile | undefined, specKey: string): readonly string[] {
return specs.filter(spec => {
const diag = specToDiagnostic(spec, allowTrailingRecursion);
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2049,17 +2049,20 @@ namespace ts {
let oldIndex = 0;
const newLen = newItems.length;
const oldLen = oldItems.length;
let hasChanges = false;
while (newIndex < newLen && oldIndex < oldLen) {
const newItem = newItems[newIndex];
const oldItem = oldItems[oldIndex];
const compareResult = comparer(newItem, oldItem);
if (compareResult === Comparison.LessThan) {
inserted(newItem);
newIndex++;
hasChanges = true;
}
else if (compareResult === Comparison.GreaterThan) {
deleted(oldItem);
oldIndex++;
hasChanges = true;
}
else {
unchanged(oldItem, newItem);
Expand All @@ -2069,10 +2072,13 @@ namespace ts {
}
while (newIndex < newLen) {
inserted(newItems[newIndex++]);
hasChanges = true;
}
while (oldIndex < oldLen) {
deleted(oldItems[oldIndex++]);
hasChanges = true;
}
return hasChanges;
}

export function fill<T>(length: number, cb: (index: number) => T): T[] {
Expand Down
78 changes: 48 additions & 30 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ namespace ts {

const cache = createMap<HostDirectoryWatcher>();
const callbackCache = createMultiMap<{ dirName: string; callback: DirectoryWatcherCallback; }>();
const cacheToUpdateChildWatches = createMap<{ dirName: string; options: WatchOptions | undefined; }>();
const cacheToUpdateChildWatches = createMap<{ dirName: string; options: WatchOptions | undefined; fileNames: string[]; }>();
let timerToUpdateChildWatches: any;

const filePathComparer = getStringComparer(!host.useCaseSensitiveFileNames);
Expand Down Expand Up @@ -538,9 +538,12 @@ namespace ts {
};
}

function invokeCallbacks(dirPath: Path, fileNameOrInvokeMap: string | Map<true>) {
type InvokeMap = Map<string[] | true>;
function invokeCallbacks(dirPath: Path, fileName: string): void;
function invokeCallbacks(dirPath: Path, invokeMap: InvokeMap, fileNames: string[] | undefined): void;
function invokeCallbacks(dirPath: Path, fileNameOrInvokeMap: string | InvokeMap, fileNames?: string[]) {
let fileName: string | undefined;
let invokeMap: Map<true> | undefined;
let invokeMap: InvokeMap | undefined;
if (isString(fileNameOrInvokeMap)) {
fileName = fileNameOrInvokeMap;
}
Expand All @@ -549,10 +552,21 @@ namespace ts {
}
// Call the actual callback
callbackCache.forEach((callbacks, rootDirName) => {
if (invokeMap && invokeMap.has(rootDirName)) return;
if (invokeMap && invokeMap.get(rootDirName) === true) return;
if (rootDirName === dirPath || (startsWith(dirPath, rootDirName) && dirPath[rootDirName.length] === directorySeparator)) {
if (invokeMap) {
invokeMap.set(rootDirName, true);
if (fileNames) {
const existing = invokeMap.get(rootDirName);
if (existing) {
(existing as string[]).push(...fileNames);
}
else {
invokeMap.set(rootDirName, fileNames.slice());
}
}
else {
invokeMap.set(rootDirName, true);
}
}
else {
callbacks.forEach(({ callback }) => callback(fileName!));
Expand All @@ -566,7 +580,7 @@ namespace ts {
const parentWatcher = cache.get(dirPath);
if (parentWatcher && host.directoryExists(dirName)) {
// Schedule the update and postpone invoke for callbacks
scheduleUpdateChildWatches(dirName, dirPath, options);
scheduleUpdateChildWatches(dirName, dirPath, fileName, options);
return;
}

Expand All @@ -575,9 +589,13 @@ namespace ts {
removeChildWatches(parentWatcher);
}

function scheduleUpdateChildWatches(dirName: string, dirPath: Path, options: WatchOptions | undefined) {
if (!cacheToUpdateChildWatches.has(dirPath)) {
cacheToUpdateChildWatches.set(dirPath, { dirName, options });
function scheduleUpdateChildWatches(dirName: string, dirPath: Path, fileName: string, options: WatchOptions | undefined) {
const existing = cacheToUpdateChildWatches.get(dirPath);
if (existing) {
existing.fileNames.push(fileName);
}
else {
cacheToUpdateChildWatches.set(dirPath, { dirName, options, fileNames: [fileName] });
}
if (timerToUpdateChildWatches) {
host.clearTimeout(timerToUpdateChildWatches);
Expand All @@ -590,22 +608,30 @@ namespace ts {
timerToUpdateChildWatches = undefined;
sysLog(`sysLog:: onTimerToUpdateChildWatches:: ${cacheToUpdateChildWatches.size}`);
const start = timestamp();
const invokeMap = createMap<true>();
const invokeMap = createMap<string[]>();

while (!timerToUpdateChildWatches && cacheToUpdateChildWatches.size) {
const { value: [dirPath, { dirName, options }], done } = cacheToUpdateChildWatches.entries().next();
const { value: [dirPath, { dirName, options, fileNames }], done } = cacheToUpdateChildWatches.entries().next();
Debug.assert(!done);
cacheToUpdateChildWatches.delete(dirPath);
// Because the child refresh is fresh, we would need to invalidate whole root directory being watched
// to ensure that all the changes are reflected at this time
invokeCallbacks(dirPath as Path, invokeMap);
updateChildWatches(dirName, dirPath as Path, options);
const hasChanges = updateChildWatches(dirName, dirPath as Path, options);
invokeCallbacks(dirPath as Path, invokeMap, hasChanges ? undefined : fileNames);
}

sysLog(`sysLog:: invokingWatchers:: ${timestamp() - start}ms:: ${cacheToUpdateChildWatches.size}`);
callbackCache.forEach((callbacks, rootDirName) => {
if (invokeMap.has(rootDirName)) {
callbacks.forEach(({ callback, dirName }) => callback(dirName));
const existing = invokeMap.get(rootDirName);
if (existing) {
callbacks.forEach(({ callback, dirName }) => {
if (isArray(existing)) {
existing.forEach(callback);
}
else {
callback(dirName);
}
});
}
});

Expand All @@ -623,34 +649,26 @@ namespace ts {
}
}

function updateChildWatches(dirName: string, dirPath: Path, options: WatchOptions | undefined) {
function updateChildWatches(parentDir: string, parentDirPath: Path, options: WatchOptions | undefined) {
// Iterate through existing children and update the watches if needed
const parentWatcher = cache.get(dirPath);
if (parentWatcher) {
parentWatcher.childWatches = watchChildDirectories(dirName, parentWatcher.childWatches, options);
}
}

/**
* Watch the directories in the parentDir
*/
function watchChildDirectories(parentDir: string, existingChildWatches: ChildWatches, options: WatchOptions | undefined): ChildWatches {
const parentWatcher = cache.get(parentDirPath);
if (!parentWatcher) return false;
let newChildWatches: ChildDirectoryWatcher[] | undefined;
enumerateInsertsAndDeletes<string, ChildDirectoryWatcher>(
const hasChanges = enumerateInsertsAndDeletes<string, ChildDirectoryWatcher>(
host.directoryExists(parentDir) ? mapDefined(host.getAccessibleSortedChildDirectories(parentDir), child => {
const childFullName = getNormalizedAbsolutePath(child, parentDir);
// Filter our the symbolic link directories since those arent included in recursive watch
// which is same behaviour when recursive: true is passed to fs.watch
return !isIgnoredPath(childFullName) && filePathComparer(childFullName, normalizePath(host.realpath(childFullName))) === Comparison.EqualTo ? childFullName : undefined;
}) : emptyArray,
existingChildWatches,
parentWatcher.childWatches,
(child, childWatcher) => filePathComparer(child, childWatcher.dirName),
createAndAddChildDirectoryWatcher,
closeFileWatcher,
addChildDirectoryWatcher
);

return newChildWatches || emptyArray;
parentWatcher.childWatches = newChildWatches || emptyArray;
return hasChanges;

/**
* Create new childDirectoryWatcher and add it to the new ChildDirectoryWatcher list
Expand Down
69 changes: 19 additions & 50 deletions src/compiler/tsbuildPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,9 @@ namespace ts {
getConfigFileParsingDiagnostics(config),
config.projectReferences
);
if (state.watch) {
state.builderPrograms.set(projectPath, program);
}
step++;
}

Expand Down Expand Up @@ -982,7 +985,7 @@ namespace ts {
if (emitResult.emittedFiles && state.writeFileName) {
emitResult.emittedFiles.forEach(name => listEmittedFile(state, config, name));
}
afterProgramDone(state, projectPath, program, config);
afterProgramDone(state, program, config);
step = BuildStep.QueueReferencingProjects;
return emitResult;
}
Expand Down Expand Up @@ -1023,7 +1026,7 @@ namespace ts {
newestDeclarationFileContentChangedTime,
oldestOutputFileName
});
afterProgramDone(state, projectPath, program, config);
afterProgramDone(state, program, config);
step = BuildStep.QueueReferencingProjects;
buildResult = resultFlags;
return emitDiagnostics;
Expand Down Expand Up @@ -1269,7 +1272,6 @@ namespace ts {

function afterProgramDone<T extends BuilderProgram>(
state: SolutionBuilderState<T>,
proj: ResolvedConfigFilePath,
program: T | undefined,
config: ParsedCommandLine
) {
Expand All @@ -1278,10 +1280,7 @@ namespace ts {
if (state.host.afterProgramEmitAndDiagnostics) {
state.host.afterProgramEmitAndDiagnostics(program);
}
if (state.watch) {
program.releaseProgram();
state.builderPrograms.set(proj, program);
}
program.releaseProgram();
}
else if (state.host.afterEmitBundle) {
state.host.afterEmitBundle(config);
Expand All @@ -1304,7 +1303,7 @@ namespace ts {
// List files if any other build error using program (emit errors already report files)
state.projectStatus.set(resolvedPath, { type: UpToDateStatusType.Unbuildable, reason: `${errorType} errors` });
if (canEmitBuildInfo) return { buildResult, step: BuildStep.EmitBuildInfo };
afterProgramDone(state, resolvedPath, program, config);
afterProgramDone(state, program, config);
return { buildResult, step: BuildStep.QueueReferencingProjects };
}

Expand Down Expand Up @@ -1809,38 +1808,6 @@ namespace ts {
));
}

function isSameFile(state: SolutionBuilderState, file1: string, file2: string) {
return comparePaths(file1, file2, state.currentDirectory, !state.host.useCaseSensitiveFileNames()) === Comparison.EqualTo;
}

function isOutputFile(state: SolutionBuilderState, fileName: string, configFile: ParsedCommandLine) {
if (configFile.options.noEmit) return false;

// ts or tsx files are not output
if (!fileExtensionIs(fileName, Extension.Dts) &&
(fileExtensionIs(fileName, Extension.Ts) || fileExtensionIs(fileName, Extension.Tsx))) {
return false;
}

// If options have --outFile or --out, check if its that
const out = outFile(configFile.options);
if (out && (isSameFile(state, fileName, out) || isSameFile(state, fileName, removeFileExtension(out) + Extension.Dts))) {
return true;
}

// If declarationDir is specified, return if its a file in that directory
if (configFile.options.declarationDir && containsPath(configFile.options.declarationDir, fileName, state.currentDirectory, !state.host.useCaseSensitiveFileNames())) {
return true;
}

// If --outDir, check if file is in that directory
if (configFile.options.outDir && containsPath(configFile.options.outDir, fileName, state.currentDirectory, !state.host.useCaseSensitiveFileNames())) {
return true;
}

return !forEach(configFile.fileNames, inputFile => isSameFile(state, fileName, inputFile));
}

function watchWildCardDirectories(state: SolutionBuilderState, resolved: ResolvedConfigFileName, resolvedPath: ResolvedConfigFilePath, parsed: ParsedCommandLine) {
if (!state.watch) return;
updateWatchingWildcardDirectories(
Expand All @@ -1850,16 +1817,18 @@ namespace ts {
state.hostWithWatch,
dir,
fileOrDirectory => {
const fileOrDirectoryPath = toPath(state, fileOrDirectory);
if (fileOrDirectoryPath !== toPath(state, dir) && hasExtension(fileOrDirectoryPath) && !isSupportedSourceFileName(fileOrDirectory, parsed.options)) {
state.writeLog(`Project: ${resolved} Detected file add/remove of non supported extension: ${fileOrDirectory}`);
return;
}

if (isOutputFile(state, fileOrDirectory, parsed)) {
state.writeLog(`${fileOrDirectory} is output file`);
return;
}
if (isIgnoredFileFromWildCardWatching({
watchedDirPath: toPath(state, dir),
fileOrDirectory,
fileOrDirectoryPath: toPath(state, fileOrDirectory),
configFileName: resolved,
configFileSpecs: parsed.configFileSpecs!,
currentDirectory: state.currentDirectory,
options: parsed.options,
program: state.builderPrograms.get(resolvedPath),
useCaseSensitiveFileNames: state.parseConfigFileHost.useCaseSensitiveFileNames,
writeLog: s => state.writeLog(s)
})) return;

invalidateProjectAndScheduleBuilds(state, resolvedPath, ConfigFileProgramReloadLevel.Partial);
},
Expand Down
23 changes: 13 additions & 10 deletions src/compiler/watchPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,23 +734,26 @@ namespace ts {
fileOrDirectory => {
Debug.assert(!!configFileName);

let fileOrDirectoryPath: Path | undefined = toPath(fileOrDirectory);
const fileOrDirectoryPath = toPath(fileOrDirectory);

// Since the file existence changed, update the sourceFiles cache
if (cachedDirectoryStructureHost) {
cachedDirectoryStructureHost.addOrDeleteFileOrDirectory(fileOrDirectory, fileOrDirectoryPath);
}
nextSourceFileVersion(fileOrDirectoryPath);

fileOrDirectoryPath = removeIgnoredPath(fileOrDirectoryPath);
if (!fileOrDirectoryPath) return;

// If the the added or created file or directory is not supported file name, ignore the file
// But when watched directory is added/removed, we need to reload the file list
if (fileOrDirectoryPath !== directory && hasExtension(fileOrDirectoryPath) && !isSupportedSourceFileName(fileOrDirectory, compilerOptions)) {
writeLog(`Project: ${configFileName} Detected file add/remove of non supported extension: ${fileOrDirectory}`);
return;
}
if (isIgnoredFileFromWildCardWatching({
watchedDirPath: toPath(directory),
fileOrDirectory,
fileOrDirectoryPath,
configFileName,
configFileSpecs,
options: compilerOptions,
program: getCurrentBuilderProgram(),
currentDirectory,
useCaseSensitiveFileNames,
writeLog
})) return;

// Reload is pending, do the reload
if (reloadLevel !== ConfigFileProgramReloadLevel.Full) {
Expand Down
Loading