Skip to content
Merged
42 changes: 37 additions & 5 deletions packages/contracts/src/custom-network-signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ export interface BuildSignaturesFromContextResult {
baseDirectory: string;
}

/**
* Resolves the on-disk path of this module in both ESM and CJS bundles.
* Falls back to __filename when bundlers strip import.meta.url.
*/
function getCurrentModulePath(): string | undefined {
const moduleUrl = (
import.meta as unknown as { url?: string } | undefined
)?.url;

if (moduleUrl) {
try {
return fileURLToPath(moduleUrl);
} catch (error) {
console.warn('Failed to resolve fileURLToPath from import.meta.url:', error);
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

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

The error message should include guidance on the fallback behavior. Consider updating to: 'Failed to resolve fileURLToPath from import.meta.url, attempting fallback to __filename:' to better inform users that the error is handled gracefully.

Suggested change
console.warn('Failed to resolve fileURLToPath from import.meta.url:', error);
console.warn('Failed to resolve fileURLToPath from import.meta.url, attempting fallback to __filename:', error);

Copilot uses AI. Check for mistakes.
}
}

if (typeof __filename === 'string') {
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

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

The check typeof __filename === 'string' is redundant. In Node.js/Bun environments where __filename exists, it is always a string. The check should be typeof __filename !== 'undefined' to detect its existence, consistent with line 97.

Suggested change
if (typeof __filename === 'string') {
if (typeof __filename !== 'undefined') {

Copilot uses AI. Check for mistakes.
return __filename;
}

return undefined;
}

/**
* Gets the base directory for resolving paths
* @param useScriptDirectory - Whether to use script's directory or current working directory
Expand All @@ -75,9 +99,14 @@ function getBaseDirectory(
return __dirname;
}
// When running as module without callerPath
const moduleDir = dirname(fileURLToPath(import.meta.url));
console.log('Using module directory:', moduleDir);
return moduleDir;
const modulePath = getCurrentModulePath();
if (modulePath) {
const moduleDir = dirname(modulePath);
console.log('Using module directory:', moduleDir);
return moduleDir;
}
console.log('Using current working directory:', process.cwd());
return process.cwd();
}
// Use current working directory
const cwd = process.cwd();
Expand Down Expand Up @@ -308,9 +337,12 @@ module.exports = {
// process.argv[0] is the bun executable
// process.argv[1] is the script being run
const mainScriptPath = path.resolve(process.argv[1] || '');
const currentScriptPath = fileURLToPath(import.meta.url);
const currentModulePath = getCurrentModulePath();
const resolvedModulePath = currentModulePath
? path.resolve(currentModulePath)
: undefined;

if (mainScriptPath === currentScriptPath) {
if (resolvedModulePath && mainScriptPath === resolvedModulePath) {
// This means custom-network-signatures.ts was the script passed to `bun run`
const jsonFilePath = process.argv[2];
const networkName = process.argv[3];
Expand Down
Loading