resolveAbsoluteModulePath method
Resolves a module path to an absolute path.
This method takes a module path (which may be relative) and resolves it to an absolute path using various strategies:
- If the path is already absolute, it's returned as is
- If a current script path is available, it tries to resolve relative to that
- It tries to resolve relative to the current working directory
- If not in product mode, it tries to resolve relative to the Dart script path
- As a last resort, it converts the path to a simple absolute path
modulePath - The module path to resolve
Returns the resolved absolute path
Implementation
String resolveAbsoluteModulePath(String modulePath) {
final normalizedModulePath = path.normalize(modulePath);
// If the path is already absolute, return it as is
if (path.isAbsolute(normalizedModulePath)) {
Logger.debugLazy(
() => "Module path is already absolute: $normalizedModulePath",
category: 'FileManager',
);
return normalizedModulePath;
}
// Try different strategies to resolve the path
try {
// First try relative to the current script path if available
if (_interpreter?.currentScriptPath != null) {
final scriptDir = path.dirname(_interpreter!.currentScriptPath!);
final resolvedPath = path.normalize(
path.join(scriptDir, normalizedModulePath),
);
Logger.debugLazy(
() =>
"Resolved module path relative to current script: $resolvedPath",
category: 'FileManager',
);
return resolvedPath;
}
// Then try relative to the current working directory
try {
final currentDir = fs.getCurrentDirectory();
if (currentDir == null) {
throw Exception("Current directory is null");
}
final resolvedPath = path.normalize(
path.join(currentDir, normalizedModulePath),
);
Logger.debugLazy(
() =>
"Resolved module path relative to current directory: $resolvedPath",
category: 'FileManager',
);
return resolvedPath;
} catch (e) {
// If that fails, try relative to the Dart script path
try {
// Only use Platform.script if not in product mode
if (!isProductMode) {
final dartScriptPath = platform.scriptPath;
if (dartScriptPath == null) {
throw Exception("Dart script path is null");
}
final dartScriptDir = path.dirname(dartScriptPath);
final resolvedPath = path.normalize(
path.join(dartScriptDir, normalizedModulePath),
);
Logger.debugLazy(
() =>
"Resolved module path relative to Dart script: $resolvedPath",
category: 'FileManager',
);
return resolvedPath;
} else {
Logger.debugLazy(
() =>
"Running as compiled executable, skipping Platform.script path resolution",
category: 'FileManager',
);
// In product mode, skip Platform.script and use simple absolute path
final resolvedPath = path.normalize(
path.absolute(normalizedModulePath),
);
Logger.debugLazy(
() => "Using simple absolute path in product mode: $resolvedPath",
category: 'FileManager',
);
return resolvedPath;
}
} catch (e2) {
// Fallback to simple absolute path
final resolvedPath = path.normalize(
path.absolute(normalizedModulePath),
);
Logger.debugLazy(
() =>
"Error resolving module path, using simple absolute path: $resolvedPath",
category: 'FileManager',
);
return resolvedPath;
}
}
} catch (e) {
// Fallback to simple absolute path
final resolvedPath = path.normalize(path.absolute(normalizedModulePath));
Logger.debugLazy(
() =>
"Error resolving module path: $e, using simple absolute path: $resolvedPath",
category: 'FileManager',
);
return resolvedPath;
}
}