Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use url.pathToFileURL to correctly resolve functions paths on windows #3412

Merged
merged 4 commits into from
Apr 18, 2024
Merged
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
9 changes: 6 additions & 3 deletions packages/toolpad-studio/src/server/functionsRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import * as path from 'path';
import * as url from 'url';
import * as fs from 'fs/promises';
import { TOOLPAD_DATA_PROVIDER_MARKER, ToolpadDataProvider } from '@toolpad/studio-runtime/server';
import * as z from 'zod';
import { fromZodError } from 'zod-validation-error';
import * as crypto from 'crypto';

async function loadExports(filePath: string): Promise<Map<string, unknown>> {
const fullPath = path.resolve(filePath);
const content = await fs.readFile(fullPath, 'utf-8');
// Need a valid file URL on Windows for the dynamic import()
const importFileUrl = url.pathToFileURL(path.resolve(filePath));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const importFileUrl = url.pathToFileURL(path.resolve(filePath));
// Correct cross-platform resolution of the import URL
// See: https://nodejs.org/api/url.html#urlpathtofileurlpath
const importFileUrl = url.pathToFileURL(path.resolve(filePath));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case vscode doesn't suffice? 😄

Screenshot 2024-04-18 at 09 27 30

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 I thought it deserves special placement because of the bug. Perhaps we can link to the issue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I've added a comment

const content = await fs.readFile(importFileUrl, 'utf-8');
const hash = crypto.createHash('md5').update(content).digest('hex');
const exports = await import(`${fullPath}?${hash}`);
importFileUrl.searchParams.set('hash', hash);
const exports = await import(importFileUrl.href);
return new Map(Object.entries(exports));
}

Expand Down
Loading