Skip to content

Commit fd2703d

Browse files
fix(runtime): app path substr considerations (#314)
1 parent 968043e commit fd2703d

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

NativeScript/runtime/ModuleInternal.mm

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,14 @@ throw NativeScriptException(isolate,
638638
stringByDeletingLastPathComponent] UTF8String];
639639

640640
// Shorten the parentDir for GetRequireFunction to avoid V8 parsing issues with long paths
641-
std::string shortParentDir = "/app" + parentDir.substr(RuntimeConfig.ApplicationPath.length());
641+
std::string shortParentDir;
642+
if (parentDir.length() >= RuntimeConfig.ApplicationPath.length() &&
643+
parentDir.compare(0, RuntimeConfig.ApplicationPath.length(), RuntimeConfig.ApplicationPath) == 0) {
644+
shortParentDir = "/app" + parentDir.substr(RuntimeConfig.ApplicationPath.length());
645+
} else {
646+
// Fallback: use the entire path if it doesn't start with ApplicationPath
647+
shortParentDir = parentDir;
648+
}
642649

643650
Local<v8::Function> require = GetRequireFunction(isolate, shortParentDir);
644651
// Use full paths for __filename and __dirname to match module.id
@@ -1566,7 +1573,14 @@ throw NativeScriptException(
15661573
}
15671574

15681575
std::string ModuleInternal::GetCacheFileName(const std::string& path) {
1569-
std::string key = path.substr(RuntimeConfig.ApplicationPath.size() + 1);
1576+
std::string key;
1577+
if (path.length() > RuntimeConfig.ApplicationPath.size() &&
1578+
path.compare(0, RuntimeConfig.ApplicationPath.size(), RuntimeConfig.ApplicationPath) == 0) {
1579+
key = path.substr(RuntimeConfig.ApplicationPath.size() + 1);
1580+
} else {
1581+
// Fallback: use the entire path if it doesn't start with ApplicationPath
1582+
key = path;
1583+
}
15701584
std::replace(key.begin(), key.end(), '/', '-');
15711585

15721586
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

0 commit comments

Comments
 (0)