Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions tegg/core/loader/src/LoaderUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class LoaderUtil {
return LoaderUtil.supportExtensions().includes('.ts') ? '.ts' : '.js';
}

static isWindowsPlatform(): boolean {
return process.platform === 'win32';
}

static filePattern(): string[] {
const extensions = LoaderUtil.supportExtensions();
const extensionPattern = extensions
Expand Down Expand Up @@ -79,15 +83,15 @@ export class LoaderUtil {
throw createLoadError(originalFilePath, e);
}
if (exports == null) {
if (process.platform === 'win32') {
if (LoaderUtil.isWindowsPlatform()) {
// convert to file:// url
// 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:'
filePath = pathToFileURL(filePath).toString();
}
try {
exports = await import(filePath);
} catch (e: unknown) {
throw createLoadError(filePath, e);
throw createLoadError(originalFilePath, e);
}
}
const clazzList: EggProtoImplClass[] = [];
Expand Down
22 changes: 21 additions & 1 deletion tegg/core/loader/test/Loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'node:path';
import { PrototypeUtil, SingletonProto } from '@eggjs/core-decorator';
import { EggLoadUnitType } from '@eggjs/metadata';
import type {} from '@eggjs/typings/global';
import { afterEach, describe, it } from 'vitest';
import { afterEach, describe, it, vi } from 'vitest';

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

Expand Down Expand Up @@ -92,6 +92,26 @@ describe('core/loader/test/Loader.test.ts', () => {
},
);
});

it('should keep the caller path when wrapping dynamic import errors on win32', async () => {
const missingFile = path.join(__dirname, './fixtures/modules/module-for-loader/MissingService.ts');
globalThis.__EGG_BUNDLE_MODULE_LOADER__ = () => undefined;

const isWindowsPlatform = vi.spyOn(LoaderUtil, 'isWindowsPlatform').mockReturnValue(true);
try {
await assert.rejects(
async () => {
await LoaderUtil.loadFile(missingFile);
},
(err: Error) => {
assert(err.message.startsWith(`[tegg/loader] load ${missingFile} failed:`));
return true;
},
);
} finally {
isWindowsPlatform.mockRestore();
}
});
});

describe('file has tsc error', () => {
Expand Down
Loading