@@ -57,40 +57,17 @@ static String getApplicationFilesPath()
5757 @ RuntimeCallable
5858 private static String resolvePath (String path , String baseDir )
5959 {
60- // This method is called my the NativeScriptRuntime.cpp RequireCallback method.
6160 // The baseDir is the directory path of the calling module.
6261 checkForExternalPath = true ;
6362 File file = resolvePathHelper (path , baseDir );
6463
65- if (file != null && file .exists ())
66- {
67- File projectRootDir = new File (RootPackageDir );
68- if (checkForExternalPath && isFileExternal (file , projectRootDir ))
69- {
70- if (logger .isEnabled ())
71- {
72- logger .write ("Module " + path + " is on external path" );
73- }
74- return "EXTERNAL_FILE_ERROR" ;
75- }
76- else
77- {
78- return file .getAbsolutePath ();
79- }
80- }
81-
82- // empty path will be handled by the NativeScriptRuntime.cpp and a JS error will be thrown
83- if (logger .isEnabled ())
84- {
85- logger .write ("Module " + path + " not found. required from directory " + baseDir );
86- }
87- return "" ;
64+ return file .getAbsolutePath ();
8865 }
8966
9067 private static boolean isFileExternal (File source , File target )
9168 {
9269 File currentParentDir = source .getParentFile ();
93-
70+
9471 while (currentParentDir != null )
9572 {
9673 if (currentParentDir .equals (target ))
@@ -108,6 +85,7 @@ private static File resolvePathHelper(String path, String baseDir)
10885 {
10986 File directory = null ;
11087 File file = null ;
88+ String possibleException = null ;
11189
11290 if (baseDir == null || baseDir .isEmpty ())
11391 {
@@ -119,19 +97,37 @@ private static File resolvePathHelper(String path, String baseDir)
11997 // absolute path
12098 directory = new File (path );
12199 file = getFileWithExtension (path );
100+ if (!file .exists ()) {
101+ possibleException = "Failed to find module with absolute path: \" " + path + "\" ." ;
102+ }
122103 }
123104 else if (path .startsWith ("./" ) || path .startsWith ("../" ) || path .startsWith ("~/" ))
124105 {
125106 // same or up directory
126107 String resolvedPath = FileSystem .resolveRelativePath (ApplicationFilesPath , path , baseDir );
127108 directory = new File (resolvedPath );
128109 file = getFileWithExtension (directory .getAbsolutePath ());
110+
111+ if (!file .exists ()) {
112+ if (path .startsWith ("~/" )) {
113+ possibleException = "Failed to find module: \" " + path + "\" , relative to: " + ModulesFilesPath ;
114+ }
115+ else {
116+ String paretnFolderPath = file .getParentFile ().getAbsolutePath ();
117+ if (ApplicationFilesPath .length () <= paretnFolderPath .length ()) {
118+ possibleException = "Failed to find module: \" " + path + "\" , relative to: " + paretnFolderPath .substring (ApplicationFilesPath .length ()) + "/" ;
119+ }
120+ }
121+ }
129122 }
130123 else
131124 {
132125 // search for tns_module
133126 directory = new File (ApplicationFilesPath + NativeScriptModulesFilesPath , path );
134127 file = getFileWithExtension (directory .getPath ());
128+ if (!file .exists ()) {
129+ possibleException = "Failed to find module: \" " + path + "\" , relative to: " + NativeScriptModulesFilesPath ;
130+ }
135131 }
136132
137133 if (!file .exists () && directory .exists () && directory .isDirectory ())
@@ -161,14 +157,14 @@ else if (path.startsWith("./") || path.startsWith("../") || path.startsWith("~/"
161157 }
162158 catch (IOException e )
163159 {
164- // json read failed
165- file = null ;
160+ throw new NativeScriptException (e .getMessage ());
166161 }
167162 catch (JSONException e )
168163 {
169- file = null ;
164+ throw new NativeScriptException ( e . getMessage ()) ;
170165 }
171166 }
167+
172168 if (!found )
173169 {
174170 // search for index.js
@@ -177,19 +173,29 @@ else if (path.startsWith("./") || path.startsWith("../") || path.startsWith("~/"
177173
178174 // TODO: search for <folderName>.js ?
179175
180- if (file != null ) {
181- // cache the main file for later use
182- folderAsModuleCache .put (folderPath , file .getAbsolutePath ());
183- }
176+ // cache the main file for later use
177+ folderAsModuleCache .put (folderPath , file .getAbsolutePath ());
184178 }
185179 else {
186180 // do not check whether this path is external for the application - it is already checked
187181 checkForExternalPath = false ;
188182 file = new File (cachedPath );
189183 }
190184 }
191-
192- return file ;
185+
186+ File projectRootDir = new File (ApplicationFilesPath );
187+ if (checkForExternalPath && isFileExternal (file , projectRootDir ))
188+ {
189+ throw new NativeScriptException ("Module " + path + " is an external path. You can only load modules inside the application!" );
190+ }
191+
192+ if (file .exists ())
193+ {
194+ return file ;
195+ }
196+ else {
197+ throw new NativeScriptException (possibleException );
198+ }
193199 }
194200
195201 private static File getFileWithExtension (String path )
0 commit comments