From 53747a8931f5e80323c809b26385f590324e460b Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Mon, 20 Feb 2023 21:46:50 +1100 Subject: [PATCH] Use import paths for errors, and slash them --- code/lib/core-server/src/utils/IndexingError.ts | 6 ++++-- .../core-server/src/utils/StoryIndexGenerator.test.ts | 2 +- code/lib/core-server/src/utils/StoryIndexGenerator.ts | 6 +++++- code/lib/core-server/src/utils/stories-json.test.ts | 10 +++++----- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/code/lib/core-server/src/utils/IndexingError.ts b/code/lib/core-server/src/utils/IndexingError.ts index ce1538dd6f9..4e85c622507 100644 --- a/code/lib/core-server/src/utils/IndexingError.ts +++ b/code/lib/core-server/src/utils/IndexingError.ts @@ -1,3 +1,5 @@ +import slash from 'slash'; + export class IndexingError extends Error { importPaths: string[]; @@ -12,10 +14,10 @@ export class IndexingError extends Error { pathsString() { if (this.importPaths.length === 1) { - return `${this.importPaths[0]}`; + return `${slash(this.importPaths[0])}`; } - return `${this.importPaths}`; + return `${this.importPaths.map(slash).join(',')}`; } toString() { diff --git a/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts b/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts index 737404c0286..6af7c641ce4 100644 --- a/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts +++ b/code/lib/core-server/src/utils/StoryIndexGenerator.test.ts @@ -958,7 +958,7 @@ describe('StoryIndexGenerator', () => { ); await generator.initialize(); await expect(() => generator.getIndex()).rejects.toThrowErrorMatchingInlineSnapshot( - `"Unable to index src/docs2/MetaOf.mdx"` + `"Unable to index ./src/docs2/MetaOf.mdx"` ); }); }); diff --git a/code/lib/core-server/src/utils/StoryIndexGenerator.ts b/code/lib/core-server/src/utils/StoryIndexGenerator.ts index 2aab03f215c..df7e9edb4ff 100644 --- a/code/lib/core-server/src/utils/StoryIndexGenerator.ts +++ b/code/lib/core-server/src/utils/StoryIndexGenerator.ts @@ -167,7 +167,11 @@ export class StoryIndexGenerator { try { entry[absolutePath] = await updater(specifier, absolutePath, entry[absolutePath]); } catch (err) { - const relativePath = path.relative(this.options.workingDir, absolutePath); + const relativePath = `.${path.sep}${path.relative( + this.options.workingDir, + absolutePath + )}`; + entry[absolutePath] = { type: 'error', err: new IndexingError(err.message, [relativePath], err.stack), diff --git a/code/lib/core-server/src/utils/stories-json.test.ts b/code/lib/core-server/src/utils/stories-json.test.ts index 03f4f153ebb..47323ff8ee4 100644 --- a/code/lib/core-server/src/utils/stories-json.test.ts +++ b/code/lib/core-server/src/utils/stories-json.test.ts @@ -655,11 +655,11 @@ describe('useStoriesJson', () => { expect(send).toHaveBeenCalledTimes(1); expect(send.mock.calls[0][0]).toMatchInlineSnapshot(` "Unable to index files: - - src/docs2/MetaOf.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`. - - src/docs2/NoTitle.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`. - - src/docs2/SecondMetaOf.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`. - - src/docs2/Template.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`. - - src/docs2/Title.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`." + - ./src/docs2/MetaOf.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`. + - ./src/docs2/NoTitle.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`. + - ./src/docs2/SecondMetaOf.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`. + - ./src/docs2/Template.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`. + - ./src/docs2/Title.mdx: You cannot use \`.mdx\` files without using \`storyStoreV7\`." `); });