Skip to content

Commit

Permalink
Improve warning for multiple loads
Browse files Browse the repository at this point in the history
Ref: #2034
  • Loading branch information
Gerrit0 committed Aug 17, 2022
1 parent ae459f8 commit c8028d1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,10 @@

## v0.20.31 (2021-03-14)

### Features

- Improved warning message if TypeDoc is loaded multiple times.

### Bug Fixes

- readonly tuples were recognized as arrays, closes #1534
Expand Down
5 changes: 3 additions & 2 deletions scripts/accept_visual_regression.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//@ts-check

const { remove, copy } = require("../dist/lib/utils/fs");
const fs = require("fs/promises");
const { copy } = require("../dist/lib/utils/fs");
const { join } = require("path");

const expectedDir = join(__dirname, "../dist/tmp/.reg/expected");
const outputDir = join(__dirname, "../dist/tmp/__screenshots__");

remove(expectedDir)
fs.rmdir(expectedDir, { recursive: true })
.then(() => copy(outputDir, expectedDir))
.catch((err) => {
console.error(err);
Expand Down
6 changes: 4 additions & 2 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
getWatchEntryPoints,
} from "./utils/entry-point";
import { nicePath } from "./utils/paths";
import { hasBeenLoadedMultipleTimes } from "./utils/general";
import { getLoadedPaths, hasBeenLoadedMultipleTimes } from "./utils/general";
import { validateExports } from "./validation/exports";
import { validateDocumentation } from "./validation/documentation";
import { validateLinks } from "./validation/links";
Expand Down Expand Up @@ -152,7 +152,9 @@ export class Application extends ChildableComponent<

if (hasBeenLoadedMultipleTimes()) {
this.logger.warn(
`TypeDoc has been loaded multiple times. This is commonly caused by plugins which have their own installation of TypeDoc. This will likely break things.`
`TypeDoc has been loaded multiple times. This is commonly caused by plugins which have their own installation of TypeDoc. The loaded paths are:\n\t${getLoadedPaths().join(
"\n\t"
)}`
);
}
}
Expand Down
21 changes: 17 additions & 4 deletions src/lib/utils/general.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dirname } from "path";
import * as Util from "util";

/**
Expand Down Expand Up @@ -59,11 +60,23 @@ export function assertNever(x: never): never {
* multiple times, then parts of it will not work as expected.
*/
const loadSymbol = Symbol.for("typedoc_loads");
const getLoads = () => globalThis[loadSymbol as never] || 0;
const pathSymbol = Symbol.for("typedoc_paths");

// @ts-expect-error there's no way to add symbols to globalThis, sadly.
globalThis[loadSymbol] = getLoads() + 1;
interface TypeDocGlobals {
[loadSymbol]?: number;
[pathSymbol]?: string[];
}
const g = globalThis as TypeDocGlobals;

g[loadSymbol] = (g[loadSymbol] || 0) + 1;
g[pathSymbol] ||= [];
// transform /abs/path/to/typedoc/dist/lib/utils/general -> /abs/path/to/typedoc
g[pathSymbol].push(dirname(dirname(dirname(__dirname))));

export function hasBeenLoadedMultipleTimes() {
return getLoads() !== 1;
return g[loadSymbol] !== 1;
}

export function getLoadedPaths() {
return g[pathSymbol] || [];
}

0 comments on commit c8028d1

Please sign in to comment.