Skip to content

Commit 444f6eb

Browse files
committed
fix(tegg): preserve load error caller path
1 parent 0ebb28e commit 444f6eb

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

tegg/core/loader/src/LoaderUtil.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export class LoaderUtil {
4343
return LoaderUtil.supportExtensions().includes('.ts') ? '.ts' : '.js';
4444
}
4545

46+
static isWindowsPlatform(): boolean {
47+
return process.platform === 'win32';
48+
}
49+
4650
static filePattern(): string[] {
4751
const extensions = LoaderUtil.supportExtensions();
4852
const extensionPattern = extensions
@@ -79,15 +83,15 @@ export class LoaderUtil {
7983
throw createLoadError(originalFilePath, e);
8084
}
8185
if (exports == null) {
82-
if (process.platform === 'win32') {
86+
if (LoaderUtil.isWindowsPlatform()) {
8387
// convert to file:// url
8488
// avoid windows path issue: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:'
8589
filePath = pathToFileURL(filePath).toString();
8690
}
8791
try {
8892
exports = await import(filePath);
8993
} catch (e: unknown) {
90-
throw createLoadError(filePath, e);
94+
throw createLoadError(originalFilePath, e);
9195
}
9296
}
9397
const clazzList: EggProtoImplClass[] = [];

tegg/core/loader/test/Loader.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'node:path';
44
import { PrototypeUtil, SingletonProto } from '@eggjs/core-decorator';
55
import { EggLoadUnitType } from '@eggjs/metadata';
66
import type {} from '@eggjs/typings/global';
7-
import { afterEach, describe, it } from 'vitest';
7+
import { afterEach, describe, it, vi } from 'vitest';
88

99
import { LoaderFactory, LoaderUtil } from '../src/index.ts';
1010

@@ -92,6 +92,26 @@ describe('core/loader/test/Loader.test.ts', () => {
9292
},
9393
);
9494
});
95+
96+
it('should keep the caller path when wrapping dynamic import errors on win32', async () => {
97+
const missingFile = path.join(__dirname, './fixtures/modules/module-for-loader/MissingService.ts');
98+
globalThis.__EGG_BUNDLE_MODULE_LOADER__ = () => undefined;
99+
100+
const isWindowsPlatform = vi.spyOn(LoaderUtil, 'isWindowsPlatform').mockReturnValue(true);
101+
try {
102+
await assert.rejects(
103+
async () => {
104+
await LoaderUtil.loadFile(missingFile);
105+
},
106+
(err: Error) => {
107+
assert(err.message.startsWith(`[tegg/loader] load ${missingFile} failed:`));
108+
return true;
109+
},
110+
);
111+
} finally {
112+
isWindowsPlatform.mockRestore();
113+
}
114+
});
95115
});
96116

97117
describe('file has tsc error', () => {

0 commit comments

Comments
 (0)