Skip to content

Commit c8028d1

Browse files
committed
Improve warning for multiple loads
Ref: #2034
1 parent ae459f8 commit c8028d1

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,10 @@
793793

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

796+
### Features
797+
798+
- Improved warning message if TypeDoc is loaded multiple times.
799+
796800
### Bug Fixes
797801

798802
- readonly tuples were recognized as arrays, closes #1534

scripts/accept_visual_regression.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//@ts-check
22

3-
const { remove, copy } = require("../dist/lib/utils/fs");
3+
const fs = require("fs/promises");
4+
const { copy } = require("../dist/lib/utils/fs");
45
const { join } = require("path");
56

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

9-
remove(expectedDir)
10+
fs.rmdir(expectedDir, { recursive: true })
1011
.then(() => copy(outputDir, expectedDir))
1112
.catch((err) => {
1213
console.error(err);

src/lib/application.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
getWatchEntryPoints,
3232
} from "./utils/entry-point";
3333
import { nicePath } from "./utils/paths";
34-
import { hasBeenLoadedMultipleTimes } from "./utils/general";
34+
import { getLoadedPaths, hasBeenLoadedMultipleTimes } from "./utils/general";
3535
import { validateExports } from "./validation/exports";
3636
import { validateDocumentation } from "./validation/documentation";
3737
import { validateLinks } from "./validation/links";
@@ -152,7 +152,9 @@ export class Application extends ChildableComponent<
152152

153153
if (hasBeenLoadedMultipleTimes()) {
154154
this.logger.warn(
155-
`TypeDoc has been loaded multiple times. This is commonly caused by plugins which have their own installation of TypeDoc. This will likely break things.`
155+
`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(
156+
"\n\t"
157+
)}`
156158
);
157159
}
158160
}

src/lib/utils/general.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { dirname } from "path";
12
import * as Util from "util";
23

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

64-
// @ts-expect-error there's no way to add symbols to globalThis, sadly.
65-
globalThis[loadSymbol] = getLoads() + 1;
65+
interface TypeDocGlobals {
66+
[loadSymbol]?: number;
67+
[pathSymbol]?: string[];
68+
}
69+
const g = globalThis as TypeDocGlobals;
70+
71+
g[loadSymbol] = (g[loadSymbol] || 0) + 1;
72+
g[pathSymbol] ||= [];
73+
// transform /abs/path/to/typedoc/dist/lib/utils/general -> /abs/path/to/typedoc
74+
g[pathSymbol].push(dirname(dirname(dirname(__dirname))));
6675

6776
export function hasBeenLoadedMultipleTimes() {
68-
return getLoads() !== 1;
77+
return g[loadSymbol] !== 1;
78+
}
79+
80+
export function getLoadedPaths() {
81+
return g[pathSymbol] || [];
6982
}

0 commit comments

Comments
 (0)