@@ -193,8 +193,18 @@ FS.staticInit();
193193 break;
194194 }
195195
196- current = FS . lookupNode ( current , parts [ i ] ) ;
197196 current_path = PATH . join2 ( current_path , parts [ i ] ) ;
197+ try {
198+ current = FS . lookupNode ( current , parts [ i ] ) ;
199+ } catch ( e ) {
200+ // if noent_okay is true, suppress a ENOENT in the last component
201+ // and return an object with an undefined node. This is needed for
202+ // resolving symlinks in the path when creating a file.
203+ if ( ( e ?. errno === { { { cDefs . ENOENT } } } ) && islast && opts . noent_okay ) {
204+ return { path : current_path } ;
205+ }
206+ throw e ;
207+ }
198208
199209 // jump to the mount's root node if this is a mountpoint
200210 if ( FS . isMountpoint ( current ) && ( ! islast || opts . follow_mount ) ) {
@@ -1036,14 +1046,15 @@ FS.staticInit();
10361046 node = path ;
10371047 } else {
10381048 path = PATH . normalize ( path ) ;
1039- try {
1040- var lookup = FS . lookupPath ( path , {
1041- follow : ! ( flags & { { { cDefs . O_NOFOLLOW } } } )
1042- } ) ;
1043- node = lookup . node ;
1044- } catch ( e ) {
1045- // ignore
1046- }
1049+ // noent_okay makes it so that if the final component of the path
1050+ // doesn't exist, lookupPath returns `node: undefined`. `path` will be
1051+ // updated to point to the target of all symlinks.
1052+ var lookup = FS . lookupPath ( path , {
1053+ follow : ! ( flags & { { { cDefs . O_NOFOLLOW } } } ) ,
1054+ noent_okay : true
1055+ } ) ;
1056+ node = lookup . node ;
1057+ path = lookup . path ;
10471058 }
10481059 // perhaps we need to create the node
10491060 var created = false ;
0 commit comments