5151
5252import com .oracle .js .parser .ir .Module .ModuleRequest ;
5353import com .oracle .truffle .api .TruffleFile ;
54+ import com .oracle .truffle .api .TruffleLanguage ;
5455import com .oracle .truffle .api .source .Source ;
5556import com .oracle .truffle .api .strings .TruffleString ;
5657import com .oracle .truffle .js .lang .JavaScriptLanguage ;
@@ -111,24 +112,25 @@ public JSModuleRecord resolveImportedModule(ScriptOrModule referrer, ModuleReque
111112 URI maybeUri = asURI (specifier );
112113
113114 TruffleString maybeCustomPath = realm .getCustomEsmPathMapping (refPath == null ? null : Strings .fromJavaString (refPath ), specifierTS );
115+ TruffleLanguage .Env env = realm .getEnv ();
114116 if (maybeCustomPath != null ) {
115117 canonicalPath = maybeCustomPath .toJavaStringUncached ();
116- moduleFile = realm . getEnv () .getPublicTruffleFile (canonicalPath ). getCanonicalFile ( );
118+ moduleFile = getCanonicalFileIfExists ( env .getPublicTruffleFile (canonicalPath ), env );
117119 } else {
118120 if (refPath == null ) {
119121 if (maybeUri != null ) {
120- moduleFile = realm . getEnv () .getPublicTruffleFile (maybeUri );
122+ moduleFile = env .getPublicTruffleFile (maybeUri );
121123 } else {
122- moduleFile = realm . getEnv () .getPublicTruffleFile (specifier );
124+ moduleFile = env .getPublicTruffleFile (specifier );
123125 }
124126 } else {
125- TruffleFile refFile = realm . getEnv () .getPublicTruffleFile (refPath );
127+ TruffleFile refFile = env .getPublicTruffleFile (refPath );
126128 if (maybeUri != null ) {
127- String uriFile = realm . getEnv () .getPublicTruffleFile (maybeUri ).getCanonicalFile ().getPath ();
129+ String uriFile = env .getPublicTruffleFile (maybeUri ).getCanonicalFile ().getPath ();
128130 moduleFile = refFile .resolveSibling (uriFile );
129131 } else {
130- if (bareSpecifierDirectLookup (specifier )) {
131- moduleFile = realm . getEnv () .getPublicTruffleFile (specifier );
132+ if (! env . isFileIOAllowed () || bareSpecifierDirectLookup (specifier )) {
133+ moduleFile = env .getPublicTruffleFile (specifier );
132134 } else {
133135 moduleFile = refFile .resolveSibling (specifier );
134136 }
@@ -185,27 +187,20 @@ private boolean bareSpecifierDirectLookup(String specifier) {
185187 }
186188
187189 protected JSModuleRecord loadModuleFromUrl (ScriptOrModule referrer , ModuleRequest moduleRequest , TruffleFile maybeModuleFile , String maybeCanonicalPath ) throws IOException {
188- JSModuleRecord existingModule ;
189- TruffleFile moduleFile ;
190+ TruffleFile moduleFile = maybeModuleFile ;
190191 String canonicalPath ;
192+ TruffleLanguage .Env env = realm .getEnv ();
191193 if (maybeCanonicalPath == null ) {
192- if (!maybeModuleFile .exists ()) {
193- // check whether the moduleFile was loaded already (as literal source)
194- // before trying to invoke getCanonicalFile() (which would fail)
195- canonicalPath = maybeModuleFile .getPath ();
196- existingModule = moduleMap .get (canonicalPath );
197- if (existingModule != null ) {
198- return existingModule ;
199- }
200- }
201- moduleFile = maybeModuleFile .getCanonicalFile ();
202- canonicalPath = moduleFile .getPath ();
194+ /*
195+ * We can only canonicalize the path if I/O is allowed and the file exists; otherwise,
196+ * the lookup may still succeed if the module was loaded already (as literal source).
197+ */
198+ canonicalPath = getCanonicalFileIfExists (moduleFile , env ).getPath ();
203199 } else {
204- moduleFile = maybeModuleFile ;
205200 canonicalPath = maybeCanonicalPath ;
206201 }
207202
208- existingModule = moduleMap .get (canonicalPath );
203+ JSModuleRecord existingModule = moduleMap .get (canonicalPath );
209204 if (existingModule != null ) {
210205 return existingModule ;
211206 }
@@ -267,13 +262,20 @@ private String getCanonicalPath(Source source) {
267262 canonicalPath = source .getName ();
268263 } else {
269264 try {
270- if (realm .getEnv ().getFileNameSeparator ().equals ("\\ " ) && path .startsWith ("/" )) {
265+ TruffleLanguage .Env env = realm .getEnv ();
266+ if (env .getFileNameSeparator ().equals ("\\ " ) && path .startsWith ("/" )) {
271267 // on Windows, remove first "/" from /c:/test/dir/ style paths
272268 path = path .substring (1 );
273269 }
274- TruffleFile moduleFile = realm .getEnv ().getPublicTruffleFile (path );
275- if (moduleFile .exists ()) {
276- canonicalPath = moduleFile .getCanonicalFile ().getPath ();
270+ TruffleFile moduleFile = env .getPublicTruffleFile (path );
271+ if (env .isFileIOAllowed () && moduleFile .exists ()) {
272+ try {
273+ canonicalPath = moduleFile .getCanonicalFile ().getPath ();
274+ } catch (NoSuchFileException ex ) {
275+ // The file may have been deleted between exists() and getCanonicalFile().
276+ // We handle this race condition as if the file did not exist.
277+ canonicalPath = path ;
278+ }
277279 } else {
278280 // Source with a non-existing path but with a content.
279281 canonicalPath = path ;
@@ -284,4 +286,16 @@ private String getCanonicalPath(Source source) {
284286 }
285287 return canonicalPath ;
286288 }
289+
290+ private static TruffleFile getCanonicalFileIfExists (TruffleFile file , TruffleLanguage .Env env ) throws IOException {
291+ if (env .isFileIOAllowed () && file .exists ()) {
292+ try {
293+ return file .getCanonicalFile ();
294+ } catch (NoSuchFileException ex ) {
295+ // The file may have been deleted between exists() and getCanonicalFile().
296+ // We handle this race condition as if the file did not exist.
297+ }
298+ }
299+ return file ;
300+ }
287301}
0 commit comments