Improve formatting of cyclical dependency error reporting
This adds a line break between dependencies, since each line tends to be very long. It's easier to understand the cycle if the actual file names are grouped closer together into a column. This change adds a newline between each dependency in the cycle to better achieve that. It also adds space between the stacktrace below and the context above.
Previously:
Compilation failed Error: Cyclical dependency between files:
src/b.ts -> src/a.ts -> src/b.ts
Now:
Compilation failed Error:
Cyclical dependency between files:
src/b.ts ->
src/a.ts ->
src/b.ts
PiperOrigin-RevId: 291974463
diff --git a/internal/tsc_wrapped/manifest.ts b/internal/tsc_wrapped/manifest.ts
index 4e5b9f6..3642dc7 100644
--- a/internal/tsc_wrapped/manifest.ts
+++ b/internal/tsc_wrapped/manifest.ts
@@ -22,8 +22,8 @@
if (!referencedFileName) continue; // Ambient modules.
if (!result[referencedFileName]) {
if (visiting[referencedFileName]) {
- const path = current + ' -> ' + Object.keys(visiting).join(' -> ');
- throw new Error('Cyclical dependency between files:\n' + path);
+ const path = [current, ...Object.keys(visiting)].join(' ->\n');
+ throw new Error(`\n\nCyclical dependency between files:\n${path}\n`);
}
visiting[referencedFileName] = true;
topologicalSort(result, referencedFileName, modulesManifest, visiting);
diff --git a/internal/tsc_wrapped/tsc_wrapped_test.ts b/internal/tsc_wrapped/tsc_wrapped_test.ts
index 2351cd8..ccb4eee 100644
--- a/internal/tsc_wrapped/tsc_wrapped_test.ts
+++ b/internal/tsc_wrapped/tsc_wrapped_test.ts
@@ -47,7 +47,7 @@
f(res, 'src/f3', 'src$f3', ['src$f1']);
f(res, 'src/f1', 'src$f1', ['src$f2']);
expect(() => constructManifest(res, {relativeOutputPath}))
- .toThrowError(/src\/f2 -> src\/f3 -> src\/f1 -> src\/f2/g);
+ .toThrowError(/src\/f2 ->\nsrc\/f3 ->\nsrc\/f1 ->\nsrc\/f2/g);
});
it('toposorts diamonds', () => {