@@ -105,6 +105,48 @@ describe("isPathOutsideWorkspace", () => {
105105 }
106106 } )
107107
108+ it ( "falls back to the lexical path when no ancestor resolves up to the root (#169)" , ( ) => {
109+ const inside = path . join ( workspaceDir , "a" , "b" , "new-file.ts" )
110+
111+ // Force realpath to report ENOENT for every segment, so the walk-up reaches the
112+ // filesystem root without resolving anything and falls back to the lexical path.
113+ // Both the target and the workspace folder resolve lexically, so containment holds.
114+ const spy = vi . spyOn ( fs . realpathSync , "native" ) . mockImplementation ( ( ) => {
115+ const err : NodeJS . ErrnoException = new Error ( "no entry" )
116+ err . code = "ENOENT"
117+ throw err
118+ } )
119+
120+ try {
121+ expect ( isPathOutsideWorkspace ( inside ) ) . toBe ( false )
122+ } finally {
123+ spy . mockRestore ( )
124+ }
125+ } )
126+
127+ it ( "treats a path as outside when the workspace folder itself cannot be resolved (#169)" , ( ) => {
128+ const inside = path . join ( workspaceDir , "file.ts" )
129+ fs . writeFileSync ( inside , "x" )
130+
131+ // The target resolves fine, but realpath on the workspace folder throws EACCES.
132+ // A folder that can't be resolved can't prove containment, so we fail closed.
133+ const realNative = fs . realpathSync . native
134+ const spy = vi . spyOn ( fs . realpathSync , "native" ) . mockImplementation ( ( ( p : string ) => {
135+ if ( p === workspaceDir ) {
136+ const err : NodeJS . ErrnoException = new Error ( "permission denied" )
137+ err . code = "EACCES"
138+ throw err
139+ }
140+ return realNative ( p )
141+ } ) as typeof fs . realpathSync . native )
142+
143+ try {
144+ expect ( isPathOutsideWorkspace ( inside ) ) . toBe ( true )
145+ } finally {
146+ spy . mockRestore ( )
147+ }
148+ } )
149+
108150 it ( "returns true when there are no workspace folders" , ( ) => {
109151 mockWorkspace . folders = [ ]
110152 expect ( isPathOutsideWorkspace ( path . join ( workspaceDir , "file.ts" ) ) ) . toBe ( true )
0 commit comments